44from unittest import mock
55
66import pytest
7- from fastapi . testclient import TestClient
7+ from async_asgi_testclient import TestClient
88
9- # import app
10- # from app import services
9+ from .fixtures import mock_client_session
10+ from .fixtures import mocked_session_get
11+ from .fixtures import mocked_strptime_isoformat
12+ from .test_jhu import DATETIME_STRING
1113from app .main import APP
1214
13- from .test_jhu import DATETIME_STRING , mocked_requests_get , mocked_strptime_isoformat
14-
1515
16+ @pytest .mark .asyncio
1617@mock .patch ("app.services.location.jhu.datetime" )
17- @mock .patch ("app.services.location.jhu.requests.get" , side_effect = mocked_requests_get )
1818class FlaskRoutesTest (unittest .TestCase ):
1919 """
2020 Need to mock some objects to control testing data locally
@@ -32,89 +32,112 @@ def read_file_v1(self, state):
3232 expected_json_output = file .read ()
3333 return expected_json_output
3434
35- def test_root_api (self , mock_request_get , mock_datetime ):
35+ async def test_root_api (self , mock_datetime ):
3636 """Validate that / returns a 200 and is not a redirect."""
37- response = self .asgi_client .get ("/" )
37+ async with mock_client_session () as mocked_client_session :
38+ mocked_client_session .get = mocked_session_get
39+ response = await self .asgi_client .get ("/" )
3840
3941 assert response .status_code == 200
4042 assert not response .is_redirect
4143
42- def test_v1_confirmed (self , mock_request_get , mock_datetime ):
44+ async def test_v1_confirmed (self , mock_datetime ):
4345 mock_datetime .utcnow .return_value .isoformat .return_value = self .date
4446 mock_datetime .strptime .side_effect = mocked_strptime_isoformat
4547 state = "confirmed"
4648 expected_json_output = self .read_file_v1 (state = state )
47- return_data = self .asgi_client .get ("/{}" .format (state )).json ()
49+ async with mock_client_session () as mocked_client_session :
50+ mocked_client_session .get = mocked_session_get
51+ response = await self .asgi_client .get ("/{}" .format (state ))
52+ return_data = response .json ()
4853
4954 assert return_data == json .loads (expected_json_output )
5055
51- def test_v1_deaths (self , mock_request_get , mock_datetime ):
56+ async def test_v1_deaths (self , mock_datetime ):
5257 mock_datetime .utcnow .return_value .isoformat .return_value = self .date
5358 mock_datetime .strptime .side_effect = mocked_strptime_isoformat
5459 state = "deaths"
5560 expected_json_output = self .read_file_v1 (state = state )
56- return_data = self .asgi_client .get ("/{}" .format (state )).json ()
61+ async with mock_client_session () as mocked_client_session :
62+ mocked_client_session .get = mocked_session_get
63+ response = await self .asgi_client .get ("/{}" .format (state ))
64+ return_data = response .json ()
5765
5866 assert return_data == json .loads (expected_json_output )
5967
60- def test_v1_recovered (self , mock_request_get , mock_datetime ):
68+ async def test_v1_recovered (self , mock_datetime ):
6169 mock_datetime .utcnow .return_value .isoformat .return_value = self .date
6270 mock_datetime .strptime .side_effect = mocked_strptime_isoformat
6371 state = "recovered"
6472 expected_json_output = self .read_file_v1 (state = state )
65- return_data = self .asgi_client .get ("/{}" .format (state )).json ()
73+ async with mock_client_session () as mocked_client_session :
74+ mocked_client_session .get = mocked_session_get
75+ response = await self .asgi_client .get ("/{}" .format (state ))
76+ return_data = response .json ()
6677
6778 assert return_data == json .loads (expected_json_output )
6879
69- def test_v1_all (self , mock_request_get , mock_datetime ):
80+ async def test_v1_all (self , mock_datetime ):
7081 mock_datetime .utcnow .return_value .isoformat .return_value = self .date
7182 mock_datetime .strptime .side_effect = mocked_strptime_isoformat
7283 state = "all"
7384 expected_json_output = self .read_file_v1 (state = state )
74- return_data = self .asgi_client .get ("/{}" .format (state )).json ()
85+ async with mock_client_session () as mocked_client_session :
86+ mocked_client_session .get = mocked_session_get
87+ response = await self .asgi_client .get ("/{}" .format (state ))
88+ return_data = response .json ()
7589
7690 assert return_data == json .loads (expected_json_output )
7791
78- def test_v2_latest (self , mock_request_get , mock_datetime ):
92+ async def test_v2_latest (self , mock_datetime ):
7993 mock_datetime .utcnow .return_value .isoformat .return_value = DATETIME_STRING
8094 mock_datetime .strptime .side_effect = mocked_strptime_isoformat
8195 state = "latest"
82- return_data = self .asgi_client .get (f"/v2/{ state } " ).json ()
96+ async with mock_client_session () as mocked_client_session :
97+ mocked_client_session .get = mocked_session_get
98+ response = await self .asgi_client .get (f"/v2/{ state } " )
99+ return_data = response .json ()
83100
84101 check_dict = {"latest" : {"confirmed" : 1940 , "deaths" : 1940 , "recovered" : 0 }}
85102
86103 assert return_data == check_dict
87104
88- def test_v2_locations (self , mock_request_get , mock_datetime ):
105+ async def test_v2_locations (self , mock_datetime ):
89106 mock_datetime .utcnow .return_value .isoformat .return_value = DATETIME_STRING
90107 mock_datetime .strptime .side_effect = mocked_strptime_isoformat
91108 state = "locations"
92- return_data = self .asgi_client .get ("/v2/{}" .format (state )).json ()
109+ async with mock_client_session () as mocked_client_session :
110+ mocked_client_session .get = mocked_session_get
111+ response = await self .asgi_client .get ("/v2/{}" .format (state ))
112+ return_data = response .json ()
93113
94114 filepath = "tests/expected_output/v2_{state}.json" .format (state = state )
95115 with open (filepath , "r" ) as file :
96116 expected_json_output = file .read ()
97117
118+ # TODO: Why is this failing?
98119 # assert return_data == json.loads(expected_json_output)
99120
100- def test_v2_locations_id (self , mock_request_get , mock_datetime ):
121+ async def test_v2_locations_id (self , mock_datetime ):
101122 mock_datetime .utcnow .return_value .isoformat .return_value = DATETIME_STRING
102123 mock_datetime .strptime .side_effect = mocked_strptime_isoformat
103124
104125 state = "locations"
105126 test_id = 1
106- return_data = self .asgi_client .get ("/v2/{}/{}" .format (state , test_id )).json ()
127+ async with mock_client_session () as mocked_client_session :
128+ mocked_client_session .get = mocked_session_get
129+ response = await self .asgi_client .get ("/v2/{}/{}" .format (state , test_id ))
130+ return_data = response .json ()
107131
108132 filepath = "tests/expected_output/v2_{state}_id_{test_id}.json" .format (state = state , test_id = test_id )
109133 with open (filepath , "r" ) as file :
110134 expected_json_output = file .read ()
111135
136+ # TODO: Why is this failing?
112137 # assert return_data == expected_json_output
113138
114- def tearDown (self ):
115- pass
116-
117139
140+ @pytest .mark .asyncio
118141@pytest .mark .parametrize (
119142 "query_params,expected_status" ,
120143 [
@@ -128,13 +151,18 @@ def tearDown(self):
128151 ({"source" : "jhu" , "country_code" : "US" }, 404 ),
129152 ],
130153)
131- def test_locations_status_code (api_client , query_params , expected_status ):
132- response = api_client .get ("/v2/locations" , params = query_params )
154+ async def test_locations_status_code (query_params , expected_status ):
155+ api_client = TestClient (APP )
156+ async with mock_client_session () as mocked_client_session :
157+ mocked_client_session .get = mocked_session_get
158+ response = await api_client .get ("/v2/locations" , query_string = query_params )
159+
133160 print (f"GET { response .url } \n { response } " )
134161 print (f"\t json:\n { pf (response .json ())[:1000 ]} \n \t ..." )
135162 assert response .status_code == expected_status
136163
137164
165+ @pytest .mark .asyncio
138166@pytest .mark .parametrize (
139167 "query_params" ,
140168 [
@@ -146,8 +174,12 @@ def test_locations_status_code(api_client, query_params, expected_status):
146174 {"source" : "jhu" , "timelines" : True },
147175 ],
148176)
149- def test_latest (api_client , query_params ):
150- response = api_client .get ("/v2/latest" , params = query_params )
177+ async def test_latest (query_params ):
178+ api_client = TestClient (APP )
179+ async with mock_client_session () as mocked_client_session :
180+ mocked_client_session .get = mocked_session_get
181+ response = await api_client .get ("/v2/latest" , query_string = query_params )
182+
151183 print (f"GET { response .url } \n { response } " )
152184
153185 response_json = response .json ()
0 commit comments