async def get_state_data(self): promises = await asyncio.gather( self.DATA_SERVICE.get_data(self.ENDPOINT), self.LOCATION_SERVICE.get_state_data(), ) results_by_county, last_updated = promises[0] state_data = promises[1] # Aggregate results on a per state basis state_results = {} for result in results_by_county: key = tuple(result.id.split("@")[:2]) if key not in state_results: properties_for_state = (state_data[key] if key in state_data else LocationProperties()) state_results[key] = JhuLocation( id=Functions.to_location_id(key), uid=properties_for_state.uid, iso2=properties_for_state.iso2, iso3=properties_for_state.iso3, code3=properties_for_state.code3, fips=properties_for_state.fips, county=properties_for_state.admin2, state=result.state, country=result.country, latitude=properties_for_state.coordinates.latitude, longitude=properties_for_state.coordinates.longitude, last_updated=last_updated, timelines={ "confirmed": {}, "deaths": {} }, latest=None, ) jhu_location = state_results[key] for confirmed_date, count in result.timelines[ "confirmed"].category.items(): value = jhu_location.timelines["confirmed"].get( confirmed_date, 0) jhu_location.timelines["confirmed"][ confirmed_date] = value + count for deaths_date, count in result.timelines[ "deaths"].category.items(): value = jhu_location.timelines["deaths"].get(deaths_date, 0) jhu_location.timelines["deaths"][deaths_date] = value + count # Remap dicts to Category for _, state in state_results.items(): state.timelines["confirmed"] = Category( state.timelines["confirmed"]) state.timelines["deaths"] = Category(state.timelines["deaths"]) state.latest = Statistics( state.timelines["confirmed"].latest, state.timelines["deaths"].latest).to_dict() return state_results.values(), last_updated
async def get_county_data(self): csv_data = "" logger.info("Fetching CSV data for counties...") async with webclient.WEBCLIENT.get( f"{self.ENDPOINT}/COUNTY_INFO.csv") as response: csv_data = await response.text() parsed_data = list(csv.DictReader(csv_data.splitlines())) county_map = {} for county_data in parsed_data: county_map[self._county_data_id(county_data)] = LocationProperties( county_data["UID"], county_data["iso2"], county_data["iso3"], county_data["code3"], county_data["FIPS"], county_data["Admin2"], county_data["State"], county_data["Country"], Coordinates(county_data["Latitude"], county_data["Longitude"]), county_data["Formal_Name"], int(county_data["Population"] or 0), ) return county_map
def test__given_valid_location__set_properties__success(): # Arrange location = Location(**TestBase.VALID_LOCATION) location_properties = LocationProperties(uid="123") location.set_properties(location_properties) # Act & Assert assert location.properties.uid == "123"
async def get_country_data(self): coordinates = Coordinates("37.0902", "-95.7129") population = COUNTRY_POPULATION["US"] return { ("US", ): LocationProperties( uid="840", iso2="US", iso3="USA", code3="840", fips="", admin2="", state="", country="US", coordinates=coordinates, combined_key="United States", population=population, ) }
def test__build_coordinates__success(): actual_coordinates = LocationProperties.build_coordinates(40, 73) expected_coordinates = Coordinates(40, 73) assert actual_coordinates.latitude == expected_coordinates.latitude assert actual_coordinates.longitude == expected_coordinates.longitude
"""Location Properties Test Location Properties unit test. """ import pytest from backend.models.classes.coordinates import Coordinates from backend.models.classes.location_properties import LocationProperties VALID_LOCATION_PROPERTIES = LocationProperties( uid="uid", iso2="iso2", iso3="iso3", code3="code3", fips="fips", admin2="admin2", state="state", country="country", coordinates=Coordinates(40, 73), combined_key="combined_key", population=6000, ) def test__vaLid_location_properties__to_dict__success(): expected_dict = { "uid": "uid", "iso2": "iso2", "iso3": "iso3", "code3": "code3", "fips": "fips",