Exemplo n.º 1
0
    def __init__(
        self,
        key: str = None,
        matcher: CountryNameMatcher = None
    ):
        self._key: str = key
        self.data: pd.DataFrame = None
        self.last_updated: datetime = None
        self.expire_time = timedelta(minutes=10)

        self._matcher = matcher
        if self._matcher is None:
            self._matcher = CountryNameMatcher()
Exemplo n.º 2
0
async def test_github_data_preparer():
    matcher = CountryNameMatcher()
    data: str = await mock_load_github()

    data = GithibDataPreparer.prepare(data, matcher)
    assert type(data) == pd.DataFrame

    columns = data.to_dict()
    check_type(None, columns, Dict[datetime, Dict[str, int]])

    exceptions = {'kosovo'}
    assert set(data.index) - matcher.keys() == exceptions
    assert 'all' in data.index
    assert len(data) > 200
Exemplo n.º 3
0
async def test_region_handling():
    matcher = CountryNameMatcher()
    data: str = await mock_load_github()

    data = GithibDataPreparer._csv_to_dataframe(data)
    data = GithibDataPreparer._colnames_to_datetime(data)
    data = GithibDataPreparer._country_and_region_names_to_keys(data, matcher)

    unhandled_regions = data.filter(['country', 'region']).dropna()
    unhandled_regions = unhandled_regions.loc[~unhandled_regions.region.
                                              isin(matcher.keys())]
    exceptions = {'australia', 'canada', 'china'}
    unhandled_regions = unhandled_regions.loc[~unhandled_regions.country.
                                              isin(exceptions)]
    assert len(unhandled_regions) == 0, str(unhandled_regions)
Exemplo n.º 4
0
def test_graph(data):
    for _ in range(5):
        key = random.choice(data.index)
        name = CountryNameMatcher().key_to_name(key)
        country = data.loc[key]

        fig = GithubGraph.draw(country, name)

        assert type(fig) == matplotlib.figure.Figure
        ax = fig.gca()
        assert len(ax.lines) == 1
        line = ax.lines[0]

        dates = list(country.index)
        values = list(country)

        assert ax.get_title() == name
        assert _date(ax.get_xlim()[0]) == _date(dates[0])
        assert _date(ax.get_xlim()[1]) > _date(dates[-1])
        assert ax.get_ylim()[0] == 0
        assert ax.get_ylim()[1] > values[-1]
        assert all([
            _date(xdata) == _date(date)
            for xdata, date in zip(line.get_xdata(), dates)
        ])
        assert all(
            [ydata == value for ydata, value in zip(line.get_ydata(), values)])
Exemplo n.º 5
0
class RapidapiSource(Source):
    def __init__(
        self,
        key: str = None,
        matcher: CountryNameMatcher = None
    ):
        self._key: str = key
        self.data: pd.DataFrame = None
        self.last_updated: datetime = None
        self.expire_time = timedelta(minutes=10)

        self._matcher = matcher
        if self._matcher is None:
            self._matcher = CountryNameMatcher()

    def single_country(self, name='all') -> CountryInfo:
        key = self._matcher.name_to_key(name)
        country: list = self.countries_by_keys(key)
        if len(country) == 0:
            raise CountryNotFound(f'No such country: {country}')
        country: dict = country[0]
        return country

    def countries_by_keys(self, *keys: List[str]) -> List[CountryInfo]:
        selected_countries = self.data[self.data.key.isin(keys)]
        country_dicts: list = selected_countries.to_dict(orient='records')
        return country_dicts

    def countries_by_range(self, start=1, end=10) -> List[CountryInfo]:
        range_data = self.data.loc[start:end]
        range_data = range_data.to_dict(orient='records')
        return range_data

    async def load_data(self) -> str:
        if self._key is None:
            raise NoRapidapiKey

        async with httpx.AsyncClient() as client:
            response = await client.get(
                "https://covid-193.p.rapidapi.com/statistics",
                headers={
                    'x-rapidapi-host': "covid-193.p.rapidapi.com",
                    'x-rapidapi-key': self._key,
                }
            )
        return response.text

    def prepare_data(self, data: str) -> pd.DataFrame:
        data = RapidapiDataPreparer.prepare(data, self._matcher)
        return data
Exemplo n.º 6
0
async def test_data_is_new():
    data = await mock_load_github()
    data = GithibDataPreparer.prepare(data, CountryNameMatcher())
    github = GithubSource()
    assert github._new_data(data)

    github.data = data.copy()
    assert not github._new_data(data)

    new_date = datetime(year=3_000, month=1, day=1)
    data[new_date] = [0] * len(data)
    assert github._new_data(data)

    new_date = datetime(year=1_000, month=1, day=1)
    data[new_date] = [0] * len(data)
    assert not github._new_data(data)
Exemplo n.º 7
0
def test_save_graph(data, tmpdir):
    for _ in range(5):
        key = random.choice(data.index)
        name = CountryNameMatcher().key_to_name(key)
        country = data.loc[key]
        fig = GithubGraph.draw(country, name)

        with patch.object(matplotlib.figure.Figure, 'savefig', Mock()):
            png_path = GithubGraph.save(fig, f'{key}_appendix')
            fig.savefig.assert_called_with(png_path, dpi=fig.dpi)
            assert png_path == (Path.cwd() / GithubGraph.directory /
                                f'{key}_appendix.png')

        with tmpdir.as_cwd():
            dir_path = Path.cwd() / GithubGraph.directory
            dir_path.mkdir(exist_ok=True)

            png_path = GithubGraph.save(fig, 'whatever')
            assert png_path.is_file()
Exemplo n.º 8
0
async def data():
    data = await mock_load_github()
    matcher = CountryNameMatcher()
    data = GithibDataPreparer.prepare(data, matcher)
    return data