|
1
|
|
|
"""Test verbose logging.""" |
|
2
|
|
|
|
|
3
|
1 |
|
import os |
|
4
|
1 |
|
import json |
|
5
|
1 |
|
import time |
|
6
|
1 |
|
import sqlite3 |
|
7
|
1 |
|
import tempfile |
|
8
|
|
|
|
|
9
|
1 |
|
from httmock import urlmatch, HTTMock, with_httmock, all_requests |
|
10
|
|
|
|
|
11
|
1 |
|
from ppp_datamodel.communication import Request, TraceItem, Response |
|
12
|
1 |
|
from ppp_datamodel import Resource, Missing |
|
13
|
1 |
|
from ppp_libmodule.tests import PPPTestCase |
|
14
|
|
|
|
|
15
|
1 |
|
from ppp_core import app |
|
16
|
|
|
|
|
17
|
|
|
config1 = """ |
|
18
|
|
|
{ |
|
19
|
|
|
"debug": true, |
|
20
|
|
|
"modules": [ |
|
21
|
|
|
{ |
|
22
|
|
|
"name": "my_module", |
|
23
|
|
|
"url": "http://test/my_module/", |
|
24
|
|
|
"coefficient": 1 |
|
25
|
|
|
} |
|
26
|
|
|
], |
|
27
|
|
|
"log": { |
|
28
|
|
|
"verbose_database_url": "sqlite:///%s" |
|
29
|
|
|
} |
|
30
|
|
|
}""" |
|
31
|
|
|
|
|
32
|
|
|
@urlmatch(netloc='test', path='/my_module/') |
|
33
|
|
|
def my_module_mock(url, request): |
|
34
|
|
|
r = Request.from_json(request.body) |
|
35
|
|
|
if r.tree == Resource('one'): |
|
36
|
|
|
c = '"measures": {"accuracy": 1, "relevance": 1}, "tree": {"type": "resource", "value": "two"}' |
|
37
|
|
|
return {'status_code': 200, |
|
38
|
|
|
'content': '[{"language": "en", %s, ' |
|
39
|
|
|
'"trace": [{"module": "module1", %s}]}]' % |
|
40
|
|
|
(c, c)} |
|
41
|
|
|
else: |
|
42
|
|
|
return {'status_code': 200, |
|
43
|
|
|
'content': '[]'} |
|
44
|
|
|
|
|
45
|
|
|
class TestVerboseLog(PPPTestCase(app)): |
|
46
|
|
|
config_var = 'PPP_CORE_CONFIG' |
|
47
|
|
|
|
|
48
|
|
|
def setUp(self): |
|
49
|
|
|
self.fd = tempfile.NamedTemporaryFile('w+') |
|
50
|
|
|
self.config = config1 % self.fd.name |
|
51
|
|
|
super().setUp() |
|
52
|
|
|
|
|
53
|
|
|
def tearDown(self): |
|
54
|
|
|
super().tearDown() |
|
55
|
|
|
self.fd.close() |
|
56
|
|
|
|
|
57
|
|
|
def testVerboseLog(self): |
|
58
|
|
|
q = Request('1', 'en', Resource('one'), {}, []) |
|
59
|
|
|
with HTTMock(my_module_mock): |
|
60
|
|
|
answers = self.request(q) |
|
61
|
|
|
|
|
62
|
|
|
time.sleep(0.1) |
|
63
|
|
|
|
|
64
|
|
|
conn = sqlite3.connect(self.fd.name) |
|
65
|
|
|
with conn: |
|
66
|
|
|
r = conn.execute('SELECT request_handling_start_time, request_handling_end_time, request_answers_json FROM requests;').fetchall() |
|
67
|
|
|
fields = ('start', 'end', 'answers') |
|
68
|
|
|
zipper = lambda x:{'start': x[0], 'end': x[1], |
|
69
|
|
|
'answers': json.loads(x[2])} |
|
70
|
|
|
r = list(map(zipper, r)) |
|
71
|
|
|
|
|
72
|
|
|
self.assertEqual(len(r), 1, r) |
|
73
|
|
|
self.assertAlmostEqual(r[0]['start'], time.time(), delta=1.) |
|
74
|
|
|
self.assertAlmostEqual(r[0]['end'], time.time(), delta=1.) |
|
75
|
|
|
self.assertEqual(len(r[0]['answers']), 1, r[0]['answers']) |
|
76
|
|
|
self.assertEqual(set(r[0]['answers'][0]), {'language', 'tree', 'measures', 'trace'}) |
|
77
|
|
|
|