Skip to content

Commit 5202876

Browse files
committed
Fix existing tests
1 parent f0e36a2 commit 5202876

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

tests/test_routes.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import app
22
import unittest
3+
from fastapi.testclient import TestClient
34
import json
45
from unittest import mock
56
from app import services
7+
from app.main import APP
68

79
from .test_jhu import mocked_requests_get, mocked_strptime_isoformat, DATETIME_STRING
810

@@ -20,6 +22,7 @@ class FlaskRoutesTest(unittest.TestCase):
2022

2123
def setUp(self):
2224
self.client = FlaskRoutesTest.app.test_client()
25+
self.asgi_client = TestClient(APP)
2326
self.date = DATETIME_STRING
2427

2528
def read_file_v1(self, state):
@@ -29,15 +32,11 @@ def read_file_v1(self, state):
2932
return expected_json_output
3033

3134
def test_root_api(self, mock_request_get, mock_datetime):
32-
"""Validate redirections of /"""
33-
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
34-
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
35-
return_data = self.client.get("/")
35+
"""Validate that / returns content and is not a redirect."""
36+
response = self.asgi_client.get("/")
3637

37-
assert return_data.status_code == 302
38-
39-
assert dict(return_data.headers)["Location"] == \
40-
"https://github.com/ExpDev07/coronavirus-tracker-api"
38+
assert response.status_code == 200
39+
assert not response.is_redirect
4140

4241
def test_v1_confirmed(self, mock_request_get, mock_datetime):
4342
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
@@ -79,8 +78,7 @@ def test_v2_latest(self, mock_request_get, mock_datetime):
7978
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
8079
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
8180
state = "latest"
82-
return_data = self.client.get("/v2/{}".format(state)).data.decode()
83-
return_data = json.loads(return_data)
81+
return_data = self.asgi_client.get(f"/v2/{state}").json()
8482

8583
check_dict = {
8684
'latest': {
@@ -96,21 +94,21 @@ def test_v2_locations(self, mock_request_get, mock_datetime):
9694
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
9795
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
9896
state = "locations"
99-
return_data = self.client.get("/v2/{}".format(state)).data.decode()
97+
return_data = self.asgi_client.get("/v2/{}".format(state)).json()
10098

10199
filepath = "tests/expected_output/v2_{state}.json".format(state=state)
102100
with open(filepath, "r") as file:
103101
expected_json_output = file.read()
104102

105-
#assert return_data == expected_json_output
103+
# assert return_data == json.loads(expected_json_output)
106104

107105
def test_v2_locations_id(self, mock_request_get, mock_datetime):
108106
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
109107
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
110108

111109
state = "locations"
112110
test_id = 1
113-
return_data = self.client.get("/v2/{}/{}".format(state, test_id)).data.decode()
111+
return_data = self.asgi_client.get("/v2/{}/{}".format(state, test_id)).json()
114112

115113
filepath = "tests/expected_output/v2_{state}_id_{test_id}.json".format(state=state, test_id=test_id)
116114
with open(filepath, "r") as file:

0 commit comments

Comments
 (0)