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
158 lines (124 loc) · 5.86 KB
/
test_routes.py
File metadata and controls
158 lines (124 loc) · 5.86 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
import json
import unittest
from pprint import pformat as pf
from unittest import mock
import pytest
from fastapi.testclient import TestClient
# import app
# from app import services
from app.main import APP
from .test_jhu import DATETIME_STRING, mocked_requests_get, mocked_strptime_isoformat
@mock.patch("app.services.location.jhu.datetime")
@mock.patch("app.services.location.jhu.requests.get", side_effect=mocked_requests_get)
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
def test_root_api(self, mock_request_get, mock_datetime):
"""Validate that / returns a 200 and is not a redirect."""
response = self.asgi_client.get("/")
assert response.status_code == 200
assert not response.is_redirect
def test_v1_confirmed(self, mock_request_get, mock_datetime):
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
state = "confirmed"
expected_json_output = self.read_file_v1(state=state)
return_data = self.asgi_client.get("/{}".format(state)).json()
assert return_data == json.loads(expected_json_output)
def test_v1_deaths(self, mock_request_get, mock_datetime):
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
state = "deaths"
expected_json_output = self.read_file_v1(state=state)
return_data = self.asgi_client.get("/{}".format(state)).json()
assert return_data == json.loads(expected_json_output)
def test_v1_recovered(self, mock_request_get, mock_datetime):
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
state = "recovered"
expected_json_output = self.read_file_v1(state=state)
return_data = self.asgi_client.get("/{}".format(state)).json()
assert return_data == json.loads(expected_json_output)
def test_v1_all(self, mock_request_get, mock_datetime):
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
state = "all"
expected_json_output = self.read_file_v1(state=state)
return_data = self.asgi_client.get("/{}".format(state)).json()
assert return_data == json.loads(expected_json_output)
def test_v2_latest(self, mock_request_get, mock_datetime):
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
state = "latest"
return_data = self.asgi_client.get(f"/v2/{state}").json()
check_dict = {"latest": {"confirmed": 1940, "deaths": 1940, "recovered": 0}}
assert return_data == check_dict
def test_v2_locations(self, mock_request_get, mock_datetime):
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
state = "locations"
return_data = self.asgi_client.get("/v2/{}".format(state)).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)
def test_v2_locations_id(self, mock_request_get, mock_datetime):
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
state = "locations"
test_id = 1
return_data = self.asgi_client.get("/v2/{}/{}".format(state, test_id)).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 == expected_json_output
def tearDown(self):
pass
@pytest.mark.parametrize(
"query_params,expected_status",
[
({"source": "csbs"}, 200),
({"source": "jhu"}, 200),
({"timelines": True}, 200),
({"timelines": "true"}, 200),
({"timelines": 1}, 200),
({"source": "jhu", "timelines": True}, 200),
({"source": "csbs", "country_code": "US"}, 200),
({"source": "jhu", "country_code": "US"}, 404),
],
)
def test_locations_status_code(api_client, query_params, expected_status):
response = api_client.get("/v2/locations", params=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.parametrize(
"query_params",
[
{"source": "csbs"},
{"source": "jhu"},
{"timelines": True},
{"timelines": "true"},
{"timelines": 1},
{"source": "jhu", "timelines": True},
],
)
def test_latest(api_client, query_params):
response = api_client.get("/v2/latest", params=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"]