Skip to content

Commit bdcd082

Browse files
authored
Merge pull request #29 from advanced-computing/irina2
run the test.yml and address the issues
2 parents c87620f + 16a092a commit bdcd082

File tree

7 files changed

+32
-13
lines changed

7 files changed

+32
-13
lines changed

Homepage.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
"&offset=0&length=5000"
3131
)
3232

33+
3334
@st.cache_data(ttl=60 * 60) # cache 1 hour
3435
def fetch_supply_json(url: str) -> dict:
3536
r = requests.get(url, timeout=30)
3637
r.raise_for_status()
3738
return r.json()
3839

40+
3941
try:
4042
payload = fetch_supply_json(SUPPLY_URL)
4143
except Exception as e:

pages/2_WTI_Price.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
"&offset=0&length=5000"
3030
)
3131

32+
3233
@st.cache_data(ttl=60 * 60)
3334
def fetch_wti_json(url: str) -> dict:
3435
r = requests.get(url, timeout=30)
3536
r.raise_for_status()
3637
return r.json()
3738

39+
3840
try:
3941
payload = fetch_wti_json(URL)
4042
except Exception as e:
@@ -61,9 +63,8 @@ def fetch_wti_json(url: str) -> dict:
6163
st.stop()
6264

6365
# Aggregate weekly (safe even if already weekly)
64-
weekly_wti = (
65-
sum_by_week(df, date_col="week", value_col="value")
66-
.rename(columns={"value": "wti_price"})
66+
weekly_wti = sum_by_week(df, date_col="week", value_col="value").rename(
67+
columns={"value": "wti_price"}
6768
)
6869

6970
# Latest price

project.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
"st.caption(\n",
205205
" \"Note: 'Product supplied' is often used as a proxy for consumption. \"\n",
206206
" \"This visualization is descriptive (not causal).\"\n",
207-
")\n"
207+
")"
208208
]
209209
}
210210
],

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ streamlit==1.54.0
1414
numpy
1515
requests
1616
altair==4.2.2
17+
pytest
18+
pytest-cov

tests/eia_part3.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def sum_by_week(df: pd.DataFrame, date_col: str, value_col: str) -> pd.DataFrame
6060
)
6161
return out
6262

63+
6364
def validate_required_columns(df: pd.DataFrame, required_cols: list[str]) -> None:
6465
"""Raise ValueError if any required column is missing."""
6566
missing = [c for c in required_cols if c not in df.columns]

tests/test_eia_part3.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pandas as pd
22
import pytest
3-
4-
from tests.eia_part3 import (
3+
from eia_part3 import (
54
add_week_ending_friday_column,
65
build_df_from_eia_data,
76
coerce_numeric_and_dropna,
@@ -11,6 +10,15 @@
1110
validate_required_columns,
1211
)
1312

13+
# Constants used in tests to satisfy Ruff PLR2004 (no "magic numbers" in comparisons)
14+
EXPECTED_FIRST_VALUE = 100
15+
EXPECTED_FILTERED_VALUE = 2
16+
EXPECTED_LATEST_VALUE = 500.0
17+
EXPECTED_WEEK_COUNT = 2
18+
EXPECTED_WEEK1_SUM = 17
19+
EXPECTED_WEEK2_SUM = 3
20+
EXPECTED_SINGLE_VALUE = 10
21+
1422

1523
def test_build_df_from_eia_data_parses_and_drops_bad_rows():
1624
# includes: valid row, invalid date, invalid value
@@ -26,7 +34,7 @@ def test_build_df_from_eia_data_parses_and_drops_bad_rows():
2634
assert pd.api.types.is_datetime64_any_dtype(df["week"])
2735
assert pd.api.types.is_numeric_dtype(df["value"])
2836
assert df["week"].iloc[0] == pd.to_datetime("2012-01-06")
29-
assert df["value"].iloc[0] == 100
37+
assert df["value"].iloc[0] == EXPECTED_FIRST_VALUE
3038

3139

3240
def test_filter_since_keeps_2012_and_after():
@@ -39,7 +47,7 @@ def test_filter_since_keeps_2012_and_after():
3947

4048
assert len(df2) == 1
4149
assert df2["week"].iloc[0] == pd.to_datetime("2012-01-06")
42-
assert df2["value"].iloc[0] == 2
50+
assert df2["value"].iloc[0] == EXPECTED_FILTERED_VALUE
4351

4452

4553
def test_latest_value_returns_value_of_most_recent_date_even_if_unsorted():
@@ -52,7 +60,7 @@ def test_latest_value_returns_value_of_most_recent_date_even_if_unsorted():
5260
df = build_df_from_eia_data(data)
5361
v = latest_value(df, date_col="week", value_col="value")
5462

55-
assert v == 500.0
63+
assert v == EXPECTED_LATEST_VALUE
5664

5765

5866
def test_latest_value_raises_on_empty_df():
@@ -70,9 +78,14 @@ def test_sum_by_week_sums_duplicates():
7078
out = sum_by_week(df, date_col="week", value_col="value")
7179

7280
assert list(out.columns) == ["week", "value"]
73-
assert len(out) == 2
74-
assert out.loc[out["week"] == pd.to_datetime("2012-01-06"), "value"].iloc[0] == 17
75-
assert out.loc[out["week"] == pd.to_datetime("2012-01-13"), "value"].iloc[0] == 3
81+
assert len(out) == EXPECTED_WEEK_COUNT
82+
assert (
83+
out.loc[out["week"] == pd.to_datetime("2012-01-06"), "value"].iloc[0] == EXPECTED_WEEK1_SUM
84+
)
85+
assert (
86+
out.loc[out["week"] == pd.to_datetime("2012-01-13"), "value"].iloc[0] == EXPECTED_WEEK2_SUM
87+
)
88+
7689

7790
def test_validate_required_columns_passes_when_present():
7891
df = pd.DataFrame({"week": [pd.to_datetime("2012-01-06")], "value": [1]})
@@ -99,4 +112,4 @@ def test_coerce_numeric_and_dropna_drops_invalid_values():
99112
out = coerce_numeric_and_dropna(df, value_col="value")
100113

101114
assert len(out) == 1
102-
assert out["value"].iloc[0] == 10
115+
assert out["value"].iloc[0] == EXPECTED_SINGLE_VALUE

0 commit comments

Comments
 (0)