forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_routes.py
More file actions
118 lines (92 loc) · 4.81 KB
/
test_routes.py
File metadata and controls
118 lines (92 loc) · 4.81 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
import app
import unittest
import json
from unittest import mock
from app import services
from .test_jhu import mocked_requests_get, mocked_strptime_isoformat, DATETIME_STRING
@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
"""
#load app context only once.
app = app.create_app()
def setUp(self):
self.client = FlaskRoutesTest.app.test_client()
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 redirections of /"""
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
return_data = self.client.get("/")
assert return_data.status_code == 302
assert dict(return_data.headers)["Location"] == \
"https://github.com/ExpDev07/coronavirus-tracker-api"
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.client.get("/{}".format(state)).data.decode()
assert return_data == 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.client.get("/{}".format(state)).data.decode()
assert return_data == 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.client.get("/{}".format(state)).data.decode()
assert return_data == 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.client.get("/{}".format(state)).data.decode()
#print(return_data)
assert return_data == 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.client.get("/v2/{}".format(state)).data.decode()
return_data = json.loads(return_data)
check_dict = {'latest': {'confirmed': 1940,
'deaths': 1940,
'recovered': 1940}}
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.client.get("/v2/{}".format(state)).data.decode()
filepath = "tests/expected_output/v2_{state}.json".format(state=state)
with open(filepath, "r") as file:
expected_json_output = file.read()
assert return_data == 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.client.get("/v2/{}/{}".format(state, test_id)).data.decode()
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