Total Complexity | 5 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | # -*- coding: utf-8 -*- |
||
6 | class MockerResourceWithNetworkClient: |
||
7 | |||
8 | resource_module = None |
||
9 | |||
10 | mock_fluent_request_class = None |
||
11 | |||
12 | @classmethod |
||
13 | def setup_class(cls): |
||
14 | |||
15 | # there are several similar modules-resources. |
||
16 | # all we need to test them -- mock NetworkClient, to prevent |
||
17 | # network access. |
||
18 | # Let's do it with mock.patch.object |
||
19 | # |
||
20 | # Mocked client will be accessible in tests as `cls.client` |
||
21 | |||
22 | # mock FluentRequest |
||
23 | cls._patcher_fluent_request_class = mock.patch( |
||
24 | 'route4me.sdk._net.FluentRequest' |
||
25 | ) |
||
26 | cls.mock_fluent_request_class = cls._patcher_fluent_request_class.start() |
||
27 | |||
28 | @classmethod |
||
29 | def teardown_class(cls): |
||
30 | """Teardown test environment for the entire class |
||
31 | """ |
||
32 | cls._patcher_fluent_request_class.stop() |
||
33 | |||
34 | def set_response(self, status_code=200, data=None, **qw): |
||
35 | |||
36 | x = mock.MagicMock() |
||
37 | x.status_code = status_code |
||
38 | x.json.return_value = data |
||
39 | |||
40 | for k in qw: |
||
41 | x.k = qw[k] |
||
42 | |||
43 | self.mock_fluent_request_class.return_value.send.return_value = x |
||
44 | |||
45 | def last_request(self): |
||
46 | return self.mock_fluent_request_class.return_value |
||
47 |