| Total Complexity | 4 |
| Total Lines | 41 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | """Module to help to create tests.""" |
||
| 2 | |||
| 3 | |||
| 4 | def get_mocked_requests(_): |
||
| 5 | """Mock requests.get.""" |
||
| 6 | return MockResponse( |
||
| 7 | { |
||
| 8 | }, |
||
| 9 | 200, |
||
| 10 | ) |
||
| 11 | |||
| 12 | |||
| 13 | class MockResponse: |
||
| 14 | """ |
||
| 15 | Mock a requests response object. |
||
| 16 | |||
| 17 | Just define a function and add the patch decorator to the test. |
||
| 18 | Example: |
||
| 19 | def mocked_requests_get(*args, **kwargs): |
||
| 20 | return MockResponse({}, 200) |
||
| 21 | @patch('requests.get', side_effect=mocked_requests_get) |
||
| 22 | |||
| 23 | """ |
||
| 24 | |||
| 25 | def __init__(self, json_data, status_code): |
||
| 26 | """Create mock response object with parameters. |
||
| 27 | |||
| 28 | Args: |
||
| 29 | json_data: JSON response content |
||
| 30 | status_code: HTTP status code. |
||
| 31 | """ |
||
| 32 | self.json_data = json_data |
||
| 33 | self.status_code = status_code |
||
| 34 | |||
| 35 | def json(self): |
||
| 36 | """Return the response json data.""" |
||
| 37 | return self.json_data |
||
| 38 | |||
| 39 | def __str__(self): |
||
| 40 | return self.__class__.__name__ |
||
| 41 |