testQueriesMultipleModuleWithFail()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2
Metric Value
cc 2
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 9.6666
1
"""Test the core's capabilities to calling a third-party module."""
2
3 1
import json
4 1
from httmock import urlmatch, HTTMock, with_httmock, all_requests
5
6 1
from ppp_datamodel import Missing
7 1
from ppp_datamodel.communication import Request, TraceItem, Response
8 1
from ppp_libmodule.tests import PPPTestCase
9 1
from ppp_core import app
10
11 1
one_module_config = """
12
{
13
    "debug": true,
14
    "modules": [
15
        {
16
            "name": "my_module",
17
            "url": "http://test/my_module/",
18
            "coefficient": 1
19
        }
20
    ],
21
    "recursion": {
22
        "max_passes": 1
23
    }
24
}"""
25
26 1
three_modules_config = """
27
{
28
    "debug": true,
29
    "modules": [
30
        {
31
            "name": "my_module",
32
            "url": "http://test/my_module/",
33
            "coefficient": 1
34
        },
35
        {
36
            "name": "my_module2",
37
            "url": "http://test/my_module2/",
38
            "coefficient": 1
39
        },
40
        {
41
            "name": "my_module3",
42
            "url": "http://test/my_module3/",
43
            "coefficient": 1
44
        }
45
    ],
46
    "recursion": {
47
        "max_passes": 1
48
    }
49
}"""
50
51 1
one_valid_module_config = """
52
{
53
    "debug": true,
54
    "modules": [
55
        {
56
            "name": "my_module",
57
            "url": "http://test/my_module/",
58
            "coefficient": 1
59
        },
60
        {
61
            "name": "my_module4",
62
            "url": "http://test/my_module4/",
63
            "coefficient": 1
64
        }
65
    ],
66
    "recursion": {
67
        "max_passes": 1
68
    }
69
}"""
70
71 1
@urlmatch(netloc='test', path='/my_module/')
72
def my_module_mock(url, request):
73 1
    c = '"measures": {"relevance": 0.5, "accuracy": 0.5}, "tree": {"type": "missing"}'
74 1
    return {'status_code': 200,
75
            'content': '[{"language": "en", %s, '
76
                         '"trace": [{"module": "module1", %s}]}]' %
77
                         (c, c)}
78
79 1
@urlmatch(netloc='test', path='/my_module2/')
80
def my_module2_mock(url, request):
81 1
    body = Request.from_json(request.body)
82 1
    m = {'relevance': 0.3, 'accuracy': 1}
83 1
    response = Response('en', body.tree, m,
84
                        [TraceItem('module2', body.tree, m)])
85 1
    return {'status_code': 200,
86
            'content': '[%s]' % response.as_json()}
87
88 1
@urlmatch(netloc='test', path='/my_module3/')
89
def my_module3_mock(url, request):
90 1
    c = '"measures": {"relevance": 0.55, "accuracy": 0.5}, "tree": {"type": "missing"}'
91 1
    return {'status_code': 200,
92
            'content': '[{"language": "en", %s, '
93
                         '"trace": [{"module": "module3", %s}]}]' % (c, c)}
94
95 1
@urlmatch(netloc='test', path='/my_module4/')
96
def my_module4_mock(url, request):
97 1
    c = '"measures": {"relevance": 0.5, "accuracy": 1.5}, "tree": {"type": "missing"}'
98 1
    return {'status_code': 200,
99
            'content': '[{"language": "en", %s, '
100
                         '"trace": [{"module": "module4", %s}]}]' % (c, c)}
101
102 1
class CallModuleTest(PPPTestCase(app)):
103 1
    config_var = 'PPP_CORE_CONFIG'
104 1
    config = ''
105 1
    def testQueriesModule(self):
106 1
        self.config_file.write(one_module_config)
107 1
        self.config_file.seek(0)
108 1
        q = {'id': '1', 'language': 'en', 'tree': {'type': 'triple',
109
             'subject': {'type': 'resource', 'value': 'foo'},
110
             'predicate': {'type': 'resource', 'value': 'bar'},
111
             'object': {'type': 'resource', 'value': 'baz'}},
112
             'measures': {}, 'trace': []}
113 1
        m = {'relevance': 0.5, 'accuracy': 0.5}
114 1
        with HTTMock(my_module_mock):
115 1
            self.assertResponse(q, [
116
                Response('en', Missing(), m,
117
                         [TraceItem('module1', Missing(), m)])])
118 1
    def testQueriesMultipleModule(self):
119 1
        self.config_file.write(three_modules_config)
120 1
        self.config_file.seek(0)
121 1
        q = Request('1', 'en', Missing(), {}, [])
122 1
        m1 = {'relevance': 0.5, 'accuracy': 0.5}
123 1
        m2 = {'relevance': 0.3, 'accuracy': 1}
124 1
        m3 = {'relevance': 0.55, 'accuracy': 0.5}
125 1
        with HTTMock(my_module_mock, my_module2_mock, my_module3_mock):
126 1
            self.assertResponse(q, [
127
                Response('en', Missing(), m3,
128
                         [TraceItem('module3', Missing(), m3)]),
129
                Response('en', Missing(), m1,
130
                         [TraceItem('module1', Missing(), m1)]),
131
                Response('en', Missing(), m2,
132
                         [TraceItem('module2', Missing(), m2)]),
133
                ])
134 1
    def testQueriesMultipleModuleWithFail(self):
135 1
        self.config_file.write(one_valid_module_config)
136 1
        self.config_file.seek(0)
137 1
        q = Request('1', 'en', Missing(), {}, [])
138 1
        m = {'relevance': 0.5, 'accuracy': 0.5}
139 1
        with HTTMock(my_module_mock, my_module4_mock):
140 1
            self.assertResponse(q, [
141
                Response('en', Missing(), m,
142
                         [TraceItem('module1', Missing(), m)])])
143