Skip to content

Commit b8eb7f1

Browse files
authored
Merge pull request #156 from ExpDev07/update-jhu-urls
JHU updates its directory structure and drops tracking of recoveries
2 parents acae882 + c1a5c52 commit b8eb7f1

File tree

5 files changed

+31
-75
lines changed

5 files changed

+31
-75
lines changed

app/services/location/jhu.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get(self, id):
2727
"""
2828
Base URL for fetching category.
2929
"""
30-
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';
30+
base_url = 'https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/csse_covid_19_data/csse_covid_19_time_series/';
3131

3232
@cached(cache=TTLCache(maxsize=1024, ttl=3600))
3333
def get_category(category):
@@ -39,10 +39,20 @@ def get_category(category):
3939
"""
4040

4141
# Adhere to category naming standard.
42-
category = category.lower().capitalize();
42+
category = category.lower();
43+
44+
# URL to request data from.
45+
url = base_url + 'time_series_covid19_%s_global.csv' % category
46+
47+
# Different URL is needed for recoveries.
48+
# Read about deprecation here: https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data/csse_covid_19_time_series.
49+
if category == 'recovered':
50+
url = base_url + 'time_series_19-covid-Recovered.csv'
51+
52+
print (url)
4353

4454
# Request the data
45-
request = requests.get(base_url % category)
55+
request = requests.get(url)
4656
text = request.text
4757

4858
# Parse the CSV.

app/timeline.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ def latest(self):
2121
"""
2222
Gets the latest available history value.
2323
"""
24-
return list(self.timeline.values())[-1] or 0
24+
# Get values in a list.
25+
values = list(self.timeline.values())
26+
27+
# Last item is the latest.
28+
if len(values):
29+
return values[-1] or 0
30+
31+
# Fallback value of 0.
32+
return 0
2533

2634
def serialize(self):
2735
"""

tests/example_data/time_series_19-covid-Confirmed.csv renamed to tests/example_data/time_series_covid19_confirmed_global.csv

File renamed without changes.
File renamed without changes.

tests/test_jhu.py

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
13371
def test_get_locations(mock_request_get, mock_datetime):

0 commit comments

Comments
 (0)