@@ -24,7 +24,15 @@ def read_file(self, state):
2424 """
2525 Mock HTTP GET-method and return text from file
2626 """
27- filepath = "tests/example_data/time_series_19-covid-{}.csv" .format (state )
27+ state = state .lower ()
28+
29+ # Determine filepath.
30+ filepath = "tests/example_data/{}.csv" .format (state )
31+
32+ if state == 'recovered' :
33+ filepath = 'tests/example_data/time_series_19-covid-Recovered.csv'
34+
35+ # Return fake response.
2836 print ("Try to read {}" .format (filepath ))
2937 with open (filepath , "r" ) as file :
3038 return file .read ()
@@ -58,76 +66,6 @@ def isoformat(self):
5866
5967 return DateTimeStrpTime (date , strformat )
6068
61- @pytest .mark .parametrize ("category, capitalize_category" , [
62- ("deaths" , "Deaths" ),
63- ("recovered" , "Recovered" ),
64- ("confirmed" , "Confirmed" )])
65- @mock .patch ('app.services.location.jhu.requests.get' , side_effect = mocked_requests_get )
66- def test_validate_category (mock_request_get , category , capitalize_category ):
67- base_url = 'https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-%s.csv'
68- request = app .services .location .jhu .requests .get (base_url % category )
69-
70- assert request .state == capitalize_category
71-
72- @pytest .mark .parametrize ("category, datetime_str, latest_value, country_name, \
73- country_code, province, latest_country_value, \
74- coordinate_lat, coordinate_long" ,
75- [("deaths" , DATETIME_STRING , 1940 , "Thailand" , "TH" , "" ,
76- 114 , "15" , "101" ),
77- ("recovered" , DATETIME_STRING , 1940 , "Thailand" , "TH" , "" ,
78- 114 , "15" , "101" ),
79- ("confirmed" , DATETIME_STRING , 1940 , "Thailand" , "TH" , "" ,
80- 114 , "15" , "101" )])
81- @mock .patch ('app.services.location.jhu.datetime' )
82- @mock .patch ('app.services.location.jhu.requests.get' , side_effect = mocked_requests_get )
83- def test_get_category (mock_request_get , mock_datetime , category , datetime_str ,
84- latest_value , country_name , country_code , province , latest_country_value ,
85- coordinate_lat , coordinate_long ):
86- #mock app.services.location.jhu.datetime.utcnow().isoformat()
87- mock_datetime .utcnow .return_value .isoformat .return_value = datetime_str
88- output = jhu .get_category (category )
89-
90- #simple schema validation
91- assert output ["source" ] == "https://github.com/ExpDev07/coronavirus-tracker-api"
92-
93- assert isinstance (output ["latest" ], int )
94- assert output ["latest" ] == latest_value #based on example data
95-
96- #check for valid datestring
97- assert date .is_date (output ["last_updated" ]) is True
98- #ensure date formating
99- assert output ["last_updated" ] == datetime_str + "Z" #based on example data
100-
101- #validate location schema
102- location_entry = output ["locations" ][0 ]
103-
104- assert isinstance (location_entry ["country" ], str )
105- assert location_entry ["country" ] == country_name #based on example data
106-
107- assert isinstance (location_entry ["country_code" ], str )
108- assert len (location_entry ["country_code" ]) == 2
109- assert location_entry ["country_code" ] == country_code #based on example data
110-
111- assert isinstance (location_entry ["province" ], str )
112- assert location_entry ["province" ] == province #based on example data
113-
114- assert isinstance (location_entry ["latest" ], int )
115- assert location_entry ["latest" ] == latest_country_value #based on example data
116-
117- #validate coordinates in location
118- coordinates = location_entry ["coordinates" ]
119-
120- assert isinstance (coordinates ["lat" ], str )
121- assert coordinates ["lat" ] == coordinate_lat
122-
123- assert isinstance (coordinates ["long" ], str )
124- assert coordinates ["long" ] == coordinate_long
125-
126- #validate history in location
127- history = location_entry ["history" ]
128- assert date .is_date (list (history .keys ())[0 ]) is True
129- assert isinstance (list (history .values ())[0 ], int )
130-
13169@mock .patch ('app.services.location.jhu.datetime' )
13270@mock .patch ('app.services.location.jhu.requests.get' , side_effect = mocked_requests_get )
13371def test_get_locations (mock_request_get , mock_datetime ):
0 commit comments