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)
def get_data_country_by_category_by_province(category, country_code): """ Get all the data of a country by category. There is three category (confirmed | death | recovered) """ data = request.get_data_time_series(category) # The normalized locations. locations = [] for item in data: if country_code.upper() == countrycodes.country_code( item['Country/Region']): # Filter out all the dates. history = dict( filter(lambda element: date_util.is_date(element[0]), item.items())) # Sorted date history history = sorted_history_date(formated_date(history)) # Country for this location. country = item['Country/Region'] # Latest data insert value. latest = list(history.values())[-1] # Normalize the item and append to locations. locations.append({ # General info. 'country': country, 'country_code': countrycodes.country_code(country), 'province': item['Province/State'], # History. 'history': history, # Latest statistic. 'total': int(latest or 0), }) # Check if the country without province exist data_country_all_province = [] # By country and province for country in locations: if country['country_code'] == country_code.upper( ) and country['province'] == '': return { 'data': country['history'], 'country': country['country'], 'country_code': country['country_code'], 'province': country['province'], 'last_updated': '' } # Otherwise regrouped all provinces of the country for country in locations: if country['country_code'] == country_code.upper(): data_country_all_province.append(country['history']) return { 'data': data_country_by_province(data_country_all_province), 'country': country['country'], 'country_code': country['country_code'], 'province': '', 'last_updated': '' }
def get_data(category): """ Retrieves the data for the provided type. The data is cached for 1 hour. """ # Adhere to category naming standard. category = category.lower().capitalize(); # Request the data request = requests.get(base_url % category) text = request.text # Parse the CSV. data = list(csv.DictReader(text.splitlines())) # The normalized locations. locations = [] for item in data: # Filter out all the dates. history = dict(filter(lambda element: date_util.is_date(element[0]), item.items())) # Country for this location. country = item['Country/Region'] # Latest data insert value. latest = list(history.values())[-1]; # Normalize the item and append to locations. locations.append({ # General info. 'country': country, 'country_code': countrycodes.country_code(country), 'province': item['Province/State'], # Coordinates. 'coordinates': { 'lat': item['Lat'], 'long': item['Long'], }, # History. 'history': history, # Latest statistic. 'latest': int(latest or 0), }) # Latest total. latest = sum(map(lambda location: location['latest'], locations)) # Return the final data. return { 'locations': locations, 'latest': latest, 'last_updated': datetime.utcnow().isoformat() + 'Z', 'source': 'https://github.com/ExpDev07/coronavirus-tracker-api', }
def get_data(category): """ Retrieves the data for the provided type. """ # Adhere to category naming standard. category = category.lower().capitalize(); # Request the data request = requests.get(base_url % category) text = request.text # Parse the CSV. data = list(csv.DictReader(text.splitlines())) # The normalized locations. locations = [] for item in data: # Filter out all the dates. history = dict(filter(lambda element: date_util.is_date(element[0]), item.items())) # Normalize the item and append to locations. locations.append({ # General info. 'country': item['Country/Region'], 'province': item['Province/State'], # Coordinates. 'coordinates': { 'lat': item['Lat'], 'long': item['Long'], }, # History. 'history': history, # Latest statistic. 'latest': int(list(history.values())[-1]), }) # Latest total. latest = sum(map(lambda location: location['latest'], locations)) # Return the final data. return { 'locations': locations, 'latest': latest }
def get_data(category): """ Retrieves the data for the provided type. The data is cached for 1 hour. """ category = category.lower() # URL to request data from. url = base_url + "time_series_covid19_%s_global.csv" % category request = requests.get(url) text = request.text # Parse the CSV. data = list(csv.DictReader(text.splitlines())) # The normalized locations. locations = [] for item in data: # Filter out all the dates. history = dict( filter(lambda element: date_util.is_date(element[0]), item.items())) # Sorted date history history = sorted_history_date(formated_date(history)) # Country for this location. country = item['Country/Region'] # Latest data insert value. latest = list(history.values())[-1] # Normalize the item and append to locations. locations.append({ 'country': country, 'country_code': countrycodes.country_code(country), 'province': item['Province/State'], # History. 'history': history, # Latest statistic. 'total': int(latest or 0), }) # Latest total. total = sum(map(lambda location: location['total'], locations)) # Return the final data. return { 'locations': locations, 'total': total, 'last_updated': datetime.utcnow().isoformat() + 'Z' }
def get_all_data_by_category(category): """ Get all the data of all the countries by category. There is three category (confirmed | death | recovered) """ data = request.get_data_time_series(category) # The normalized locations. locations = [] for item in data: # Filter out all the dates. history = dict( filter(lambda element: date_util.is_date(element[0]), item.items())) # Sorted date history history = sorted_history_date(formated_date(history)) # Country for this location. country = item['Country/Region'] # Latest data insert value. latest = list(history.values())[-1] # Normalize the item and append to locations. locations.append({ # General info. 'country': country, 'country_code': countrycodes.country_code(country), 'province': item['Province/State'], # History. 'history': history, # Latest statistic. 'total': int(latest or 0), }) # Latest total. total = sum(map(lambda location: location['total'], locations)) # Return the final data. return { 'locations': locations, 'total': total, 'last_updated': datetime.utcnow().isoformat() + 'Z' }
def test_is_date(str_date, fuzzy_bool, expected_value): """ Testdata from https://stackoverflow.com/a/25341965/7120095 """ assert date.is_date(str_date, fuzzy=fuzzy_bool) is expected_value
def get_data(category): """ Retrieves the data for the provided type. The data is cached for 1 hour. """ # Adhere to category naming standard. category = category.lower().capitalize() print(">>>>>>", category) print("Base_url: >>> ", base_url % category) # Request the data request = requests.get(base_url % category) text = request.text # Parse the CSV. data = list(csv.DictReader(text.splitlines())) # The normalized locations. locations = {} latest_count = 0 # latest count for item in data: # Filter out all the dates. history = dict( filter(lambda element: date_util.is_date(element[0]), item.items())) # Country for this location. country = item['Country/Region'] # Latest data insert value. latest = list(history.values())[-1] # Normalize the item and append to locations. if (country not in locations): locations[country] = [] locations[country].append({ # General info. 'country': country, 'country_code': countrycodes.country_code(country), 'province': item['Province/State'], # Coordinates. 'coordinates': { 'lat': item['Lat'], 'long': item['Long'], }, # History. 'history': history, # Latest statistic. 'latest': int(latest or 0), }) latest_count += int(latest or 0) # Return the final data. return { 'locations': locations, 'latest': latest_count, 'last_updated': datetime.utcnow().isoformat() + 'Z' }