From 6cbfbbc151759b3a03a458f6183fe663d6204594 Mon Sep 17 00:00:00 2001 From: Franz Philip Brandmueller Date: Mon, 4 Sep 2023 23:08:44 +0200 Subject: [PATCH] refactor: add tests / small change --- myeia/api.py | 8 +++++--- tests/test_myeia.py | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/myeia/api.py b/myeia/api.py index d55e6e3..1765754 100644 --- a/myeia/api.py +++ b/myeia/api.py @@ -94,7 +94,7 @@ def get_series( >>> eia.get_series("NG.RNGC1.W") """ api_endpoint = f"seriesid/{series_id}?api_key={self.token}" - + url = f"{self.base_url}{api_endpoint}" base_df = get_json_response(url, headers=self.header) @@ -110,7 +110,6 @@ def get_series( start_date, end_date = get_date_range(start_date, end_date) mask = (df.index >= start_date) & (df.index <= end_date) # To avoid slicing errors (FutureWarning: slicing on non-monotonic DatetimeIndexes with non-existing keys) return df.loc[mask] - def get_series_via_route( self, @@ -145,7 +144,7 @@ def get_series_via_route( start_date, end_date = get_date_range(start_date, end_date) list_of_series = [series] if isinstance(series, str) else series - + data = [] sort_args = "&sort[0][column]=period&sort[0][direction]=desc" @@ -155,6 +154,9 @@ def get_series_via_route( url = f"{self.base_url}{api_endpoint}{facet_args}{sort_args}" base_df = get_json_response(url, headers=self.header) + if base_df.empty: + raise ValueError(f"Error getting data for series: {series}. Please check your request.") + if not facet: facet = "series" diff --git a/tests/test_myeia.py b/tests/test_myeia.py index 86f658a..46735dd 100644 --- a/tests/test_myeia.py +++ b/tests/test_myeia.py @@ -26,9 +26,13 @@ def test_get_series(series_id, start_date, end_date): [ ("natural-gas/pri/fut", "RNGC1", "daily", "series", None, None), ("natural-gas/pri/fut", ["RNGC1", "RNGC2"], "daily", "series", None, None), - ("natural-gas/pri/fut", ["RNGC1", "RNGC2"], "daily", "series", "2021-01-01", "2021-01-31"), + ("natural-gas/pri/fut", ["RNGC1", "RNGC2"], "daily", "series", "2021-01-01", "2021-11-31"), ("petroleum/stoc/wstk", "WCESTUS1", "weekly", "series", None, None), ("petroleum/move/pipe", "MD0MP_R10-R20_1", "monthly", "series", None, None), + ("petroleum/crd/crpdn", "MCRFPP51", "monthly", "series", None, None), + ("petroleum/crd/crpdn", "MCRFPP52", "monthly", "series", None, None), + ("petroleum/crd/crpdn", ["MCRFPP51", "MCRFPP52"], "monthly", "series", None, None), + ("petroleum/crd/crpdn", ["MCRFPP51", "MCRFPP52"], "monthly", "series", "2021-01-01", "2021-11-31"), ], ) def test_get_series_via_route(route, series, frequency, facet, start_date, end_date):