forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_routes.py
More file actions
188 lines (146 loc) · 6.78 KB
/
test_routes.py
File metadata and controls
188 lines (146 loc) · 6.78 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import json
import unittest
from pprint import pformat as pf
from unittest import mock
import pytest
from async_asgi_testclient import TestClient
from app.main import APP
from .conftest import mocked_strptime_isoformat
from .test_jhu import DATETIME_STRING
@pytest.mark.usefixtures("mock_client_session_class")
@pytest.mark.asyncio
class FlaskRoutesTest(unittest.TestCase):
"""
Need to mock some objects to control testing data locally
Routes are hard to test regarding singleton app.
Store all integration testcases in one class to ensure app context
"""
def setUp(self):
self.asgi_client = TestClient(APP)
self.date = DATETIME_STRING
def read_file_v1(self, state):
filepath = "tests/expected_output/v1_{state}.json".format(state=state)
with open(filepath, "r") as file:
expected_json_output = file.read()
return expected_json_output
async def test_root_api(self):
"""Validate that / returns a 200 and is not a redirect."""
response = await self.asgi_client.get("/")
assert response.status_code == 200
assert not response.is_redirect
async def test_v1_confirmed(self):
state = "confirmed"
expected_json_output = self.read_file_v1(state=state)
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
response = await self.asgi_client.get("/{}".format(state))
return_data = response.json()
assert return_data == json.loads(expected_json_output)
async def test_v1_deaths(self):
state = "deaths"
expected_json_output = self.read_file_v1(state=state)
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
response = await self.asgi_client.get("/{}".format(state))
return_data = response.json()
assert return_data == json.loads(expected_json_output)
async def test_v1_recovered(self):
state = "recovered"
expected_json_output = self.read_file_v1(state=state)
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
response = await self.asgi_client.get("/{}".format(state))
return_data = response.json()
assert return_data == json.loads(expected_json_output)
async def test_v1_all(self):
state = "all"
expected_json_output = self.read_file_v1(state=state)
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
response = await self.asgi_client.get("/{}".format(state))
return_data = response.json()
assert return_data == json.loads(expected_json_output)
async def test_v2_latest(self):
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
state = "latest"
response = await self.asgi_client.get(f"/v2/{state}")
return_data = response.json()
check_dict = {"latest": {"confirmed": 1940, "deaths": 1940, "recovered": 0}}
assert return_data == check_dict
async def test_v2_locations(self):
state = "locations"
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
response = await self.asgi_client.get("/v2/{}".format(state))
return_data = response.json()
filepath = "tests/expected_output/v2_{state}.json".format(state=state)
with open(filepath, "r") as file:
expected_json_output = file.read()
assert return_data == json.loads(expected_json_output)
async def test_v2_locations_id(self):
state = "locations"
test_id = 1
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
response = await self.asgi_client.get("/v2/{}/{}".format(state, test_id))
return_data = response.json()
filepath = "tests/expected_output/v2_{state}_id_{test_id}.json".format(
state=state, test_id=test_id
)
with open(filepath, "r") as file:
expected_json_output = file.read()
assert return_data == json.loads(expected_json_output)
@pytest.mark.asyncio
@pytest.mark.parametrize(
"query_params,expected_status",
[
({"source": "csbs"}, 200),
({"source": "jhu"}, 200),
({"source": "nyt"}, 200),
({"timelines": True}, 200),
({"timelines": "true"}, 200),
({"timelines": 1}, 200),
({"source": "jhu", "timelines": True}, 200),
({"source": "nyt", "timelines": True}, 200),
({"source": "csbs", "country_code": "US"}, 200),
({"source": "nyt", "country_code": "US"}, 200),
({"source": "jhu", "country_code": "US"}, 404),
],
)
async def test_locations_status_code(
async_api_client, query_params, expected_status, mock_client_session
):
response = await async_api_client.get("/v2/locations", query_string=query_params)
print(f"GET {response.url}\n{response}")
print(f"\tjson:\n{pf(response.json())[:1000]}\n\t...")
assert response.status_code == expected_status
@pytest.mark.asyncio
@pytest.mark.parametrize(
"query_params",
[
{"source": "csbs"},
{"source": "jhu"},
{"source": "nyt"},
{"timelines": True},
{"timelines": "true"},
{"timelines": 1},
{"source": "jhu", "timelines": True},
{"source": "nyt", "timelines": True},
],
)
async def test_latest(async_api_client, query_params, mock_client_session):
response = await async_api_client.get("/v2/latest", query_string=query_params)
print(f"GET {response.url}\n{response}")
response_json = response.json()
print(f"\tjson:\n{pf(response_json)}")
assert response.status_code == 200
assert response_json["latest"]["confirmed"]
assert response_json["latest"]["deaths"]