1
|
1 |
|
import json |
2
|
1 |
|
from httmock import urlmatch, HTTMock, with_httmock, all_requests |
3
|
|
|
|
4
|
1 |
|
from ppp_datamodel import Resource, Missing, Sentence |
5
|
1 |
|
from ppp_datamodel.communication import Request, TraceItem, Response |
6
|
1 |
|
from ppp_libmodule.tests import PPPTestCase |
7
|
1 |
|
from ppp_core import app, router |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
config1 = """ |
11
|
|
|
{ |
12
|
|
|
"debug": true, |
13
|
|
|
"modules": [ |
14
|
|
|
{ |
15
|
|
|
"name": "my_module1", |
16
|
|
|
"url": "http://test/my_module1/", |
17
|
|
|
"coefficient": 1 |
18
|
|
|
}, |
19
|
|
|
{ |
20
|
|
|
"name": "my_module2", |
21
|
|
|
"url": "python:tests.test_python:Module2", |
22
|
|
|
"coefficient": 1 |
23
|
|
|
}, |
24
|
|
|
{ |
25
|
|
|
"name": "my_module3", |
26
|
|
|
"url": "python:tests.test_python:Module3", |
27
|
|
|
"coefficient": 1 |
28
|
|
|
} |
29
|
|
|
] |
30
|
|
|
}""" |
31
|
|
|
|
32
|
|
|
|
33
|
1 |
|
@urlmatch(netloc='test', path='/my_module1/') |
34
|
|
|
def my_module1_mock(url, request): |
35
|
1 |
|
c = '"measures": {"accuracy": 1, "relevance": 1}, "tree": {"type": "resource", "value": "one"}' |
36
|
1 |
|
return {'status_code': 200, |
37
|
|
|
'content': '[{"language": "en", %s, ' |
38
|
|
|
'"trace": [{"module": "module1", %s}]}]' % |
39
|
|
|
(c, c)} |
40
|
1 |
|
class Module2: |
41
|
1 |
|
def __init__(self, request): |
42
|
1 |
|
self.request = request |
43
|
|
|
|
44
|
1 |
|
def answer(self): |
45
|
1 |
|
return [Response('en', self.request.tree, {}, [])] |
46
|
|
|
|
47
|
1 |
|
class Module3: |
48
|
1 |
|
def __init__(self, request): |
49
|
1 |
|
self.request = request |
50
|
|
|
|
51
|
1 |
|
def answer(self): |
52
|
1 |
|
assert False |
53
|
|
|
|
54
|
|
|
|
55
|
1 |
|
class FakeLogger: |
56
|
1 |
|
def __init__(self): |
57
|
1 |
|
self._errors = [] |
58
|
1 |
|
def error(self, s): |
59
|
1 |
|
self._errors.append(s) |
60
|
1 |
|
def info(self, s): |
61
|
1 |
|
pass |
62
|
|
|
|
63
|
1 |
|
class TestPython(PPPTestCase(app)): |
64
|
1 |
|
config_var = 'PPP_CORE_CONFIG' |
65
|
1 |
|
config = config1 |
66
|
1 |
|
def testPython(self): |
67
|
1 |
|
with HTTMock(my_module1_mock): |
68
|
1 |
|
q = Request('1', 'en', Sentence('foo'), {}, []) |
69
|
1 |
|
(router.logger, logger) = (FakeLogger(), router.logger) |
70
|
1 |
|
try: |
71
|
1 |
|
answers = self.request(q) |
72
|
|
|
finally: |
73
|
1 |
|
(fakelogger, router.logger) = (router.logger, logger) |
74
|
1 |
|
self.assertEqual(len(answers), 3, answers) |
75
|
1 |
|
self.assertEqual(len(list(filter(lambda x:x.tree.value == 'foo', answers))), 1) |
76
|
1 |
|
self.assertEqual(len(list(filter(lambda x:x.tree.value == 'one', answers))), 2) |
77
|
|
|
self.assertEqual(len(fakelogger._errors), 4, fakelogger._errors) |
78
|
|
|
|