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