|
6 | 6 | import app |
7 | 7 | from app import location |
8 | 8 | from app.services.location import jhu |
9 | | -from app.utils import date |
| 9 | +from tests.fixtures import mock_client_session |
| 10 | +from tests.fixtures import mocked_session_get |
| 11 | +from tests.fixtures import mocked_strptime_isoformat |
10 | 12 |
|
11 | 13 | DATETIME_STRING = "2020-03-17T10:23:22.505550" |
12 | 14 |
|
13 | 15 |
|
14 | | -def mocked_requests_get(*args, **kwargs): |
15 | | - class FakeRequestsGetResponse: |
16 | | - """ |
17 | | - Returns instance of `FakeRequestsGetResponse` |
18 | | - when calling `app.services.location.jhu.requests.get()` |
19 | | - """ |
20 | | - |
21 | | - def __init__(self, url, filename, state): |
22 | | - self.url = url |
23 | | - self.filename = filename |
24 | | - self.state = state |
25 | | - self.text = self.read_file(self.state) |
26 | | - |
27 | | - def read_file(self, state): |
28 | | - """ |
29 | | - Mock HTTP GET-method and return text from file |
30 | | - """ |
31 | | - state = state.lower() |
32 | | - |
33 | | - # Determine filepath. |
34 | | - filepath = "tests/example_data/{}.csv".format(state) |
35 | | - |
36 | | - # Return fake response. |
37 | | - print("Try to read {}".format(filepath)) |
38 | | - with open(filepath, "r") as file: |
39 | | - return file.read() |
40 | | - |
41 | | - # get url from `request.get` |
42 | | - url = args[0] |
43 | | - |
44 | | - # get filename from url |
45 | | - filename = url.split("/")[-1] |
46 | | - |
47 | | - # clean up for id token (e.g. Deaths) |
48 | | - state = filename.split("-")[-1].replace(".csv", "").lower().capitalize() |
49 | | - |
50 | | - return FakeRequestsGetResponse(url, filename, state) |
51 | | - |
52 | | - |
53 | | -def mocked_strptime_isoformat(*args, **kwargs): |
54 | | - class DateTimeStrpTime: |
55 | | - """ |
56 | | - Returns instance of `DateTimeStrpTime` |
57 | | - when calling `app.services.location.jhu.datetime.trptime(date, '%m/%d/%y').isoformat()` |
58 | | - """ |
59 | | - |
60 | | - def __init__(self, date, strformat): |
61 | | - self.date = date |
62 | | - self.strformat = strformat |
63 | | - |
64 | | - def isoformat(self): |
65 | | - return datetime.datetime.strptime(self.date, self.strformat).isoformat() |
66 | | - |
67 | | - date = args[0] |
68 | | - strformat = args[1] |
69 | | - |
70 | | - return DateTimeStrpTime(date, strformat) |
71 | | - |
72 | | - |
| 16 | +@pytest.mark.asyncio |
73 | 17 | @mock.patch("app.services.location.jhu.datetime") |
74 | | -@mock.patch("app.services.location.jhu.requests.get", side_effect=mocked_requests_get) |
75 | | -def test_get_locations(mock_request_get, mock_datetime): |
76 | | - # mock app.services.location.jhu.datetime.utcnow().isoformat() |
| 18 | +async def test_get_locations(mock_datetime): |
77 | 19 | mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING |
78 | 20 | mock_datetime.strptime.side_effect = mocked_strptime_isoformat |
79 | 21 |
|
80 | | - output = jhu.get_locations() |
81 | | - assert isinstance(output, list) |
82 | | - assert isinstance(output[0], location.Location) |
| 22 | + async with mock_client_session() as mocked_client_session: |
| 23 | + mocked_client_session.get = mocked_session_get |
| 24 | + output = await jhu.get_locations() |
| 25 | + |
| 26 | + assert isinstance(output, list) |
| 27 | + assert isinstance(output[0], location.Location) |
83 | 28 |
|
84 | | - # `jhu.get_locations()` creates id based on confirmed list |
85 | | - location_confirmed = jhu.get_category("confirmed") |
86 | | - assert len(output) == len(location_confirmed["locations"]) |
| 29 | + # `jhu.get_locations()` creates id based on confirmed list |
| 30 | + location_confirmed = await jhu.get_category("confirmed") |
| 31 | + assert len(output) == len(location_confirmed["locations"]) |
0 commit comments