|
| 1 | +import unittest |
| 2 | +from unittest import mock |
| 3 | +import requests |
| 4 | +from datetime import datetime |
| 5 | +from datetime import timedelta |
| 6 | + |
| 7 | +from webservice.redirect import RemoteSDAPCache |
| 8 | +from webservice.redirect import CollectionNotFound |
| 9 | +from webservice.redirect.RemoteSDAPCache import RemoteSDAPList |
| 10 | + |
| 11 | +class MockResponse: |
| 12 | + def __init__(self, json_data, status_code): |
| 13 | + self.json_data = json_data |
| 14 | + self.status_code = status_code |
| 15 | + |
| 16 | + def json(self): |
| 17 | + return self.json_data |
| 18 | + |
| 19 | +LIST_CONTENT = [ |
| 20 | + { |
| 21 | + "shortName": "PM25", |
| 22 | + "title": "PM25", |
| 23 | + "tileCount": 21515, |
| 24 | + "start": 1514818800.0, |
| 25 | + "end": 1640991600.0, |
| 26 | + "iso_start": "2018-01-01T15:00:00+0000", |
| 27 | + "iso_end": "2021-12-31T23:00:00+0000" |
| 28 | + } |
| 29 | + ] |
| 30 | + |
| 31 | +LIST_CONTENT_FORMER = [LIST_CONTENT[0].copy()] |
| 32 | +LIST_CONTENT_FORMER[0]['start'] = 0 |
| 33 | + |
| 34 | +def mocked_requests_get(*asgs, **kwargs): |
| 35 | + return MockResponse(LIST_CONTENT, 200) |
| 36 | + |
| 37 | + |
| 38 | +def mocked_requests_get_timeout(*asgs, **kwargs): |
| 39 | + raise requests.exceptions.ConnectTimeout() |
| 40 | + |
| 41 | + |
| 42 | +def mocked_requests_get_not_found(*asgs, **kwargs): |
| 43 | + return MockResponse({}, 404) |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +class MyTestCase(unittest.TestCase): |
| 48 | + |
| 49 | + @mock.patch('requests.get', side_effect=mocked_requests_get) |
| 50 | + def test_get(self, mock_get): |
| 51 | + remote_sdap_cache = RemoteSDAPCache() |
| 52 | + |
| 53 | + collection = remote_sdap_cache.get('https://aq-sdap.stcenter.net/nexus/', 'PM25') |
| 54 | + self.assertEqual(collection["start"], 1514818800.0) |
| 55 | + |
| 56 | + @mock.patch('requests.get', side_effect=mocked_requests_get_timeout) |
| 57 | + def test_get_timeout(self, mock_get): |
| 58 | + remote_sdap_cache = RemoteSDAPCache() |
| 59 | + with self.assertRaises(CollectionNotFound): |
| 60 | + remote_sdap_cache.get('https://aq-sdap.stcenter.net/nexus/', 'PM25') |
| 61 | + |
| 62 | + |
| 63 | + @mock.patch('requests.get', side_effect=mocked_requests_get_not_found) |
| 64 | + def test_get_not_found(self, mock_get): |
| 65 | + remote_sdap_cache = RemoteSDAPCache() |
| 66 | + with self.assertRaises(CollectionNotFound): |
| 67 | + remote_sdap_cache.get('https://aq-sdap.stcenter.net/nexus/', 'PM25') |
| 68 | + |
| 69 | + @mock.patch('requests.get', side_effect=mocked_requests_get) |
| 70 | + def test_get_expired(self, mock_get): |
| 71 | + remote_sdap_cache = RemoteSDAPCache() |
| 72 | + |
| 73 | + remote_sdap_cache.sdap_lists['https://aq-sdap.stcenter.net/nexus/'] = RemoteSDAPList( |
| 74 | + list=LIST_CONTENT_FORMER, |
| 75 | + outdated_at=datetime.now() - timedelta(seconds=3600*25) |
| 76 | + ) |
| 77 | + |
| 78 | + collection = remote_sdap_cache.get('https://aq-sdap.stcenter.net/nexus/', 'PM25') |
| 79 | + |
| 80 | + # check requests.get is called once |
| 81 | + self.assertEqual(mock_get.call_count, 1) |
| 82 | + self.assertEqual(collection["start"], 1514818800.0) |
| 83 | + |
| 84 | + @mock.patch('requests.get', side_effect=mocked_requests_get) |
| 85 | + def test_get_cached_valid(self, mock_get): |
| 86 | + remote_sdap_cache = RemoteSDAPCache() |
| 87 | + |
| 88 | + remote_sdap_cache.sdap_lists['https://aq-sdap.stcenter.net/nexus'] = RemoteSDAPList( |
| 89 | + list=LIST_CONTENT_FORMER, |
| 90 | + outdated_at=datetime.now() - timedelta(seconds=3600 * 23) |
| 91 | + ) |
| 92 | + |
| 93 | + collection = remote_sdap_cache.get('https://aq-sdap.stcenter.net/nexus/', 'PM25') |
| 94 | + |
| 95 | + # check requests.get is called once |
| 96 | + self.assertEqual(mock_get.call_count, 0) |
| 97 | + self.assertEqual(collection["start"], 0) |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + unittest.main() |
0 commit comments