Skip to content

Commit 4dddf0f

Browse files
Jingjing TangJingjing Tang
authored andcommitted
updated code for adding a test case where some necessary cols are missing
1 parent 3a2adcb commit 4dddf0f

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

nchs_mortality/delphi_nchs_mortality/pull.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ def pull_nchs_mortality_data(token: str, map_df: pd.DataFrame, test_mode: str):
5656
"end_week is not always the same as start_week, check the raw file"
5757
)
5858

59-
df = df.astype(TYPE_DICT)
59+
try:
60+
df = df.astype(TYPE_DICT)
61+
except KeyError:
62+
raise ValueError("Expected column(s) missed, The dataset "
63+
"schema may have changed. Please investigate and "
64+
"amend the code.")
65+
6066
df = df[df["state"] != "United States"]
6167
df.loc[df["state"] == "New York City", "state"] = "New York"
6268

@@ -85,10 +91,6 @@ def pull_nchs_mortality_data(token: str, map_df: pd.DataFrame, test_mode: str):
8591

8692
# Add population info
8793
KEEP_COLUMNS.extend(["timestamp", "geo_id", "population"])
88-
try:
89-
df = df.merge(map_df, on="state")[KEEP_COLUMNS]
90-
except KeyError:
91-
raise ValueError("Expected column(s) missed, The dataset "
92-
"schema may have changed. Please investigate and "
93-
"amend the code.")
94+
df = df.merge(map_df, on="state")[KEEP_COLUMNS]
95+
9496
return df

nchs_mortality/tests/test_pull.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ def test_bad_file_with_inconsistent_time_col(self):
3131
with pytest.raises(ValueError):
3232
df = pull_nchs_mortality_data(token, map_df,
3333
"bad_data_with_inconsistent_time_col.csv")
34+
35+
def test_bad_file_with_inconsistent_time_col(self):
36+
with pytest.raises(ValueError):
37+
df = pull_nchs_mortality_data(token, map_df,
38+
"bad_data_with_missing_cols.csv")

0 commit comments

Comments
 (0)