Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adapt testsuite to /v2. See #65
  • Loading branch information
gribok committed Mar 18, 2020
commit 841d0c29d1c367ee3221d3249126d7bc86ef68db
2 changes: 1 addition & 1 deletion app/routes/v2/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ def location(id):
'location': {
**jhu.get(id).serialize(), **{ 'timelines': timelines }
}
})
})
1 change: 1 addition & 0 deletions tests/expected_output/v1_all.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/expected_output/v2_locations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"locations":[{"coordinates":{"latitude":"15","longitude":"101"},"country":"Thailand","country_code":"TH","id":0,"latest":{"confirmed":114,"deaths":114,"recovered":114},"province":""},{"coordinates":{"latitude":"36","longitude":"138"},"country":"Japan","country_code":"JP","id":1,"latest":{"confirmed":839,"deaths":839,"recovered":839},"province":""},{"coordinates":{"latitude":"1.2833","longitude":"103.8333"},"country":"Singapore","country_code":"SG","id":2,"latest":{"confirmed":226,"deaths":226,"recovered":226},"province":""},{"coordinates":{"latitude":"28.1667","longitude":"84.25"},"country":"Nepal","country_code":"NP","id":3,"latest":{"confirmed":1,"deaths":1,"recovered":1},"province":""},{"coordinates":{"latitude":"2.5","longitude":"112.5"},"country":"Malaysia","country_code":"MY","id":4,"latest":{"confirmed":428,"deaths":428,"recovered":428},"province":""},{"coordinates":{"latitude":"49.2827","longitude":"-123.1207"},"country":"Canada","country_code":"CA","id":5,"latest":{"confirmed":73,"deaths":73,"recovered":73},"province":"British Columbia"},{"coordinates":{"latitude":"-33.8688","longitude":"151.2093"},"country":"Australia","country_code":"AU","id":6,"latest":{"confirmed":134,"deaths":134,"recovered":134},"province":"New South Wales"},{"coordinates":{"latitude":"-37.8136","longitude":"144.9631"},"country":"Australia","country_code":"AU","id":7,"latest":{"confirmed":57,"deaths":57,"recovered":57},"province":"Victoria"},{"coordinates":{"latitude":"-28.0167","longitude":"153.4"},"country":"Australia","country_code":"AU","id":8,"latest":{"confirmed":61,"deaths":61,"recovered":61},"province":"Queensland"},{"coordinates":{"latitude":"11.55","longitude":"104.9167"},"country":"Cambodia","country_code":"KH","id":9,"latest":{"confirmed":7,"deaths":7,"recovered":7},"province":""}]}
1 change: 1 addition & 0 deletions tests/expected_output/v2_locations_id_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"location":{"coordinates":{"latitude":"36","longitude":"138"},"country":"Japan","country_code":"JP","id":1,"latest":{"confirmed":839,"deaths":839,"recovered":839},"province":"","timelines":{"confirmed":{"latest":839,"timeline":{"2020-01-22T00:00:00Z":2,"2020-01-23T00:00:00Z":1,"2020-01-24T00:00:00Z":2,"2020-01-25T00:00:00Z":2,"2020-01-26T00:00:00Z":4,"2020-01-27T00:00:00Z":4,"2020-01-28T00:00:00Z":7,"2020-01-29T00:00:00Z":7,"2020-01-30T00:00:00Z":11,"2020-01-31T00:00:00Z":15,"2020-02-01T00:00:00Z":20,"2020-02-02T00:00:00Z":20,"2020-02-03T00:00:00Z":20,"2020-02-04T00:00:00Z":22,"2020-02-05T00:00:00Z":22,"2020-02-06T00:00:00Z":45,"2020-02-07T00:00:00Z":25,"2020-02-08T00:00:00Z":25,"2020-02-09T00:00:00Z":26,"2020-02-10T00:00:00Z":26,"2020-02-11T00:00:00Z":26,"2020-02-12T00:00:00Z":28,"2020-02-13T00:00:00Z":28,"2020-02-14T00:00:00Z":29,"2020-02-15T00:00:00Z":43,"2020-02-16T00:00:00Z":59,"2020-02-17T00:00:00Z":66,"2020-02-18T00:00:00Z":74,"2020-02-19T00:00:00Z":84,"2020-02-20T00:00:00Z":94,"2020-02-21T00:00:00Z":105,"2020-02-22T00:00:00Z":122,"2020-02-23T00:00:00Z":147,"2020-02-24T00:00:00Z":159,"2020-02-25T00:00:00Z":170,"2020-02-26T00:00:00Z":189,"2020-02-27T00:00:00Z":214,"2020-02-28T00:00:00Z":228,"2020-02-29T00:00:00Z":241,"2020-03-01T00:00:00Z":256,"2020-03-02T00:00:00Z":274,"2020-03-03T00:00:00Z":293,"2020-03-04T00:00:00Z":331,"2020-03-05T00:00:00Z":360,"2020-03-06T00:00:00Z":420,"2020-03-07T00:00:00Z":461,"2020-03-08T00:00:00Z":502,"2020-03-09T00:00:00Z":511,"2020-03-10T00:00:00Z":581,"2020-03-11T00:00:00Z":639,"2020-03-12T00:00:00Z":639,"2020-03-13T00:00:00Z":701,"2020-03-14T00:00:00Z":773,"2020-03-15T00:00:00Z":839}},"deaths":{"latest":839,"timeline":{"2020-01-22T00:00:00Z":2,"2020-01-23T00:00:00Z":1,"2020-01-24T00:00:00Z":2,"2020-01-25T00:00:00Z":2,"2020-01-26T00:00:00Z":4,"2020-01-27T00:00:00Z":4,"2020-01-28T00:00:00Z":7,"2020-01-29T00:00:00Z":7,"2020-01-30T00:00:00Z":11,"2020-01-31T00:00:00Z":15,"2020-02-01T00:00:00Z":20,"2020-02-02T00:00:00Z":20,"2020-02-03T00:00:00Z":20,"2020-02-04T00:00:00Z":22,"2020-02-05T00:00:00Z":22,"2020-02-06T00:00:00Z":45,"2020-02-07T00:00:00Z":25,"2020-02-08T00:00:00Z":25,"2020-02-09T00:00:00Z":26,"2020-02-10T00:00:00Z":26,"2020-02-11T00:00:00Z":26,"2020-02-12T00:00:00Z":28,"2020-02-13T00:00:00Z":28,"2020-02-14T00:00:00Z":29,"2020-02-15T00:00:00Z":43,"2020-02-16T00:00:00Z":59,"2020-02-17T00:00:00Z":66,"2020-02-18T00:00:00Z":74,"2020-02-19T00:00:00Z":84,"2020-02-20T00:00:00Z":94,"2020-02-21T00:00:00Z":105,"2020-02-22T00:00:00Z":122,"2020-02-23T00:00:00Z":147,"2020-02-24T00:00:00Z":159,"2020-02-25T00:00:00Z":170,"2020-02-26T00:00:00Z":189,"2020-02-27T00:00:00Z":214,"2020-02-28T00:00:00Z":228,"2020-02-29T00:00:00Z":241,"2020-03-01T00:00:00Z":256,"2020-03-02T00:00:00Z":274,"2020-03-03T00:00:00Z":293,"2020-03-04T00:00:00Z":331,"2020-03-05T00:00:00Z":360,"2020-03-06T00:00:00Z":420,"2020-03-07T00:00:00Z":461,"2020-03-08T00:00:00Z":502,"2020-03-09T00:00:00Z":511,"2020-03-10T00:00:00Z":581,"2020-03-11T00:00:00Z":639,"2020-03-12T00:00:00Z":639,"2020-03-13T00:00:00Z":701,"2020-03-14T00:00:00Z":773,"2020-03-15T00:00:00Z":839}},"recovered":{"latest":839,"timeline":{"2020-01-22T00:00:00Z":2,"2020-01-23T00:00:00Z":1,"2020-01-24T00:00:00Z":2,"2020-01-25T00:00:00Z":2,"2020-01-26T00:00:00Z":4,"2020-01-27T00:00:00Z":4,"2020-01-28T00:00:00Z":7,"2020-01-29T00:00:00Z":7,"2020-01-30T00:00:00Z":11,"2020-01-31T00:00:00Z":15,"2020-02-01T00:00:00Z":20,"2020-02-02T00:00:00Z":20,"2020-02-03T00:00:00Z":20,"2020-02-04T00:00:00Z":22,"2020-02-05T00:00:00Z":22,"2020-02-06T00:00:00Z":45,"2020-02-07T00:00:00Z":25,"2020-02-08T00:00:00Z":25,"2020-02-09T00:00:00Z":26,"2020-02-10T00:00:00Z":26,"2020-02-11T00:00:00Z":26,"2020-02-12T00:00:00Z":28,"2020-02-13T00:00:00Z":28,"2020-02-14T00:00:00Z":29,"2020-02-15T00:00:00Z":43,"2020-02-16T00:00:00Z":59,"2020-02-17T00:00:00Z":66,"2020-02-18T00:00:00Z":74,"2020-02-19T00:00:00Z":84,"2020-02-20T00:00:00Z":94,"2020-02-21T00:00:00Z":105,"2020-02-22T00:00:00Z":122,"2020-02-23T00:00:00Z":147,"2020-02-24T00:00:00Z":159,"2020-02-25T00:00:00Z":170,"2020-02-26T00:00:00Z":189,"2020-02-27T00:00:00Z":214,"2020-02-28T00:00:00Z":228,"2020-02-29T00:00:00Z":241,"2020-03-01T00:00:00Z":256,"2020-03-02T00:00:00Z":274,"2020-03-03T00:00:00Z":293,"2020-03-04T00:00:00Z":331,"2020-03-05T00:00:00Z":360,"2020-03-06T00:00:00Z":420,"2020-03-07T00:00:00Z":461,"2020-03-08T00:00:00Z":502,"2020-03-09T00:00:00Z":511,"2020-03-10T00:00:00Z":581,"2020-03-11T00:00:00Z":639,"2020-03-12T00:00:00Z":639,"2020-03-13T00:00:00Z":701,"2020-03-14T00:00:00Z":773,"2020-03-15T00:00:00Z":839}}}}}
19 changes: 19 additions & 0 deletions tests/test_coordinates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from unittest import mock
import pytest

from app import coordinates

@pytest.mark.parametrize("latitude, longitude",
[("1", "2"),
(100, "2"),
(-3, 0),
(-10, -10000000)])
def test_coordinates_class(latitude, longitude):
coord_obj = coordinates.Coordinates(latitude=latitude,
longitude=longitude)

#validate serialize
check_obj = {'latitude' : latitude,
'longitude': longitude}

assert coord_obj.serialize() == check_obj
71 changes: 0 additions & 71 deletions tests/test_data.py

This file was deleted.

145 changes: 145 additions & 0 deletions tests/test_jhu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
from unittest import mock
import datetime
import pytest

import app
from app.utils import date
from app import location
from app.services.location import jhu

DATETIME_STRING = "2020-03-17T10:23:22.505550"

def mocked_requests_get(*args, **kwargs):
class FakeRequestsGetResponse:
"""
Returns instance of `FakeRequestsGetResponse`
when calling `app.services.location.jhu.requests.get()`
"""
def __init__(self, url, filename, state):
self.url = url
self.filename = filename
self.state = state
self.text = self.read_file(self.state)


def read_file(self, state):
"""
Mock HTTP GET-method and return text from file
"""
filepath = "tests/example_data/time_series_19-covid-{}.csv".format(state)
print("Try to read {}".format(filepath))
with open(filepath, "r") as file:
return file.read()

#get url from `request.get`
url = args[0]
#get filename from url
filename = url.split("/")[-1]
#clean up for id token (e.g. Deaths)
state = filename.split("-")[-1].replace(".csv", "").lower().capitalize()

return FakeRequestsGetResponse(url, filename, state)

def mocked_strptime_isoformat(*args, **kwargs):
class DateTimeStrpTime:
"""
Returns instance of `DateTimeStrpTime`
when calling `app.services.location.jhu.datetime.trptime(date, '%m/%d/%y').isoformat()`
"""
def __init__(self, date, strformat):
self.date = date
self.strformat = strformat

def isoformat(self):
return datetime.datetime.strptime(self.date, self.strformat).isoformat()

date = args[0]
strformat = args[1]

return DateTimeStrpTime(date, strformat)

@pytest.mark.parametrize("category, capitalize_category",
[("deaths", "Deaths"),
("recovered", "Recovered"),
("confirmed", "Confirmed")])
@mock.patch('app.services.location.jhu.requests.get', side_effect=mocked_requests_get)
def test_validate_category(mock_request_get, category, capitalize_category):
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'

request = app.services.location.jhu.requests.get(base_url % category)
assert request.state == capitalize_category

@pytest.mark.parametrize("category, datetime_str, latest_value, country_name, \
country_code, province, latest_country_value, \
coordinate_lat, coordinate_long",
[("deaths", DATETIME_STRING, 1940, "Thailand", "TH", "",
114, "15", "101"),
("recovered", DATETIME_STRING, 1940, "Thailand", "TH", "",
114, "15", "101"),
("confirmed", DATETIME_STRING, 1940, "Thailand", "TH", "",
114, "15", "101")])
@mock.patch('app.services.location.jhu.datetime')
@mock.patch('app.services.location.jhu.requests.get', side_effect=mocked_requests_get)
def test_get_category(mock_request_get, mock_datetime, category, datetime_str,
latest_value, country_name, country_code, province, latest_country_value,
coordinate_lat, coordinate_long):
#mock app.services.location.jhu.datetime.utcnow().isoformat()
mock_datetime.utcnow.return_value.isoformat.return_value = datetime_str
output = jhu.get_category(category)

#simple schema validation
assert output["source"] == "https://github.com/ExpDev07/coronavirus-tracker-api"

assert isinstance(output["latest"], int)
assert output["latest"] == latest_value #based on example data

#check for valid datestring
assert date.is_date(output["last_updated"]) is True
#ensure date formating
assert output["last_updated"] == datetime_str + "Z" #based on example data

#validate location schema
location_entry = output["locations"][0]

assert isinstance(location_entry["country"], str)
assert location_entry["country"] == country_name #based on example data

assert isinstance(location_entry["country_code"], str)
assert len(location_entry["country_code"]) == 2
assert location_entry["country_code"] == country_code #based on example data

assert isinstance(location_entry["province"], str)
assert location_entry["province"] == province #based on example data

assert isinstance(location_entry["latest"], int)
assert location_entry["latest"] == latest_country_value #based on example data

#validate coordinates in location
coordinates = location_entry["coordinates"]

assert isinstance(coordinates["lat"], str)
assert coordinates["lat"] == coordinate_lat

assert isinstance(coordinates["long"], str)
assert coordinates["long"] == coordinate_long

#validate history in location
history = location_entry["history"]
assert date.is_date(list(history.keys())[0]) is True
assert isinstance(list(history.values())[0], int)

@mock.patch('app.services.location.jhu.datetime')
@mock.patch('app.services.location.jhu.requests.get', side_effect=mocked_requests_get)
def test_get_locations(mock_request_get, mock_datetime):
#mock app.services.location.jhu.datetime.utcnow().isoformat()
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
mock_datetime.strptime.side_effect = mocked_strptime_isoformat

output = jhu.get_locations()
assert isinstance(output, list)
assert isinstance(output[0], location.Location)

#`jhu.get_locations()` creates id based on confirmed list
location_confirmed = jhu.get_category("confirmed")
assert len(output) == len(location_confirmed["locations"])
44 changes: 44 additions & 0 deletions tests/test_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from unittest import mock
import pytest

from app import (location, coordinates, timeline)

def mocked_timeline(*args, **kwargs):
class TestTimeline:
def __init__(self, latest):
self.latest = latest

return TestTimeline(args[0])

@pytest.mark.parametrize("test_id, country, country_code, province, latitude, longitude, \
confirmed_latest, deaths_latest, recovered_latest",
[(0, "Thailand", "TH", "", 15, 100, 1000, 1111, 22222),
(1, "Deutschland", "DE", "", 15, 100, 1000, 1111, 22222),
(2, "Cruise Ship", "XX", "", 15, 100, 1000, 1111, 22222)])
@mock.patch('app.timeline.Timeline', side_effect=mocked_timeline)
def test_location_class(mocked_timeline, test_id, country, country_code, province, latitude,
longitude, confirmed_latest, deaths_latest, recovered_latest):

# id, country, province, coordinates, confirmed, deaths, recovered
coordinate = coordinates.Coordinates(latitude=latitude, longitude=longitude)
confirmed = timeline.Timeline(confirmed_latest)
deaths = timeline.Timeline(deaths_latest)
recovered = timeline.Timeline(recovered_latest)

location_obj = location.Location(test_id, country, province, coordinate,
confirmed, deaths, recovered)

assert location_obj.country_code == country_code

#validate serialize
check_dict = {'id': test_id,
'country': country,
'province': province,
'country_code': country_code,
'coordinates': {'latitude': latitude,
'longitude': longitude},
'latest': {'confirmed': confirmed_latest,
'deaths': deaths_latest,
'recovered': recovered_latest}}

assert location_obj.serialize() == check_dict
Loading