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