Skip to content

Commit

Permalink
refactor: add tests / small change
Browse files Browse the repository at this point in the history
  • Loading branch information
philsv committed Sep 4, 2023
1 parent 6fa7086 commit 6cbfbbc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 5 additions & 3 deletions myeia/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down Expand Up @@ -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"

Expand All @@ -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"

Expand Down
6 changes: 5 additions & 1 deletion tests/test_myeia.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 6cbfbbc

Please sign in to comment.