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 |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
config1 = """ |
11
|
|
|
{ |
12
|
|
|
"debug": true, |
13
|
|
|
"modules": [ |
14
|
|
|
{ |
15
|
|
|
"name": "my_module", |
16
|
|
|
"url": "http://test/my_module1/", |
17
|
|
|
"coefficient": 1, |
18
|
|
|
"filters": { |
19
|
|
|
"whitelist": ["sentence", "triple"] |
20
|
|
|
} |
21
|
|
|
}, |
22
|
|
|
{ |
23
|
|
|
"name": "my_module2", |
24
|
|
|
"url": "http://test/my_module2/", |
25
|
|
|
"coefficient": 1 |
26
|
|
|
} |
27
|
|
|
] |
28
|
|
|
}""" |
29
|
|
|
|
30
|
|
|
|
31
|
1 |
|
config2 = """ |
32
|
|
|
{ |
33
|
|
|
"debug": true, |
34
|
|
|
"modules": [ |
35
|
|
|
{ |
36
|
|
|
"name": "my_module", |
37
|
|
|
"url": "http://test/my_module1/", |
38
|
|
|
"coefficient": 1 |
39
|
|
|
}, |
40
|
|
|
{ |
41
|
|
|
"name": "my_module2", |
42
|
|
|
"url": "http://test/my_module2/", |
43
|
|
|
"coefficient": 1, |
44
|
|
|
"filters": { |
45
|
|
|
"blacklist": ["triple", "missing", "resource"] |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
] |
49
|
|
|
}""" |
50
|
|
|
|
51
|
1 |
|
@urlmatch(netloc='test', path='/my_module1/') |
52
|
|
|
def my_module1_mock(url, request): |
53
|
1 |
|
c = '"measures": {"accuracy": 1, "relevance": 1}, "tree": {"type": "resource", "value": "one"}' |
54
|
1 |
|
return {'status_code': 200, |
55
|
|
|
'content': '[{"language": "en", %s, ' |
56
|
|
|
'"trace": [{"module": "module1", %s}]}]' % |
57
|
|
|
(c, c)} |
58
|
1 |
|
@urlmatch(netloc='test', path='/my_module2/') |
59
|
|
|
def my_module2_mock(url, request): |
60
|
1 |
|
c = '"measures": {"accuracy": 1, "relevance": 1}, "tree": {"type": "resource", "value": "two"}' |
61
|
1 |
|
return {'status_code': 200, |
62
|
|
|
'content': '[{"language": "en", %s, ' |
63
|
|
|
'"trace": [{"module": "module1", %s}]}]' % |
64
|
|
|
(c, c)} |
65
|
|
|
|
66
|
|
|
|
67
|
1 |
|
class TestWhitelist(PPPTestCase(app)): |
|
|
|
|
68
|
1 |
|
config_var = 'PPP_CORE_CONFIG' |
69
|
1 |
|
config = config1 |
70
|
1 |
|
def testWhitelist(self): |
71
|
1 |
|
with HTTMock(my_module1_mock, my_module2_mock): |
72
|
1 |
|
q = Request('1', 'en', Sentence('foo'), {}, []) |
73
|
1 |
|
answers = self.request(q) |
74
|
1 |
|
self.assertEqual(len(answers), 2, answers) |
75
|
1 |
|
q = Request('1', 'en', Missing(), {}, []) |
76
|
1 |
|
answers = self.request(q) |
77
|
1 |
|
self.assertEqual(len(answers), 1, answers) |
78
|
1 |
|
self.assertEqual(answers[0].tree.value, 'two') |
79
|
|
|
|
80
|
|
|
|
81
|
1 |
|
class TestBlacklist(PPPTestCase(app)): |
|
|
|
|
82
|
1 |
|
config_var = 'PPP_CORE_CONFIG' |
83
|
1 |
|
config = config2 |
84
|
1 |
|
def testBlacklist(self): |
85
|
1 |
|
with HTTMock(my_module1_mock, my_module2_mock): |
86
|
1 |
|
q = Request('1', 'en', Sentence('foo'), {}, []) |
87
|
1 |
|
answers = self.request(q) |
88
|
1 |
|
self.assertEqual(len(answers), 2, answers) |
89
|
1 |
|
q = Request('1', 'en', Missing(), {}, []) |
90
|
1 |
|
answers = self.request(q) |
91
|
1 |
|
self.assertEqual(len(answers), 1, answers) |
92
|
|
|
self.assertEqual(answers[0].tree.value, 'one') |
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.