Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
diehlbw committed Oct 1, 2024
1 parent f206a06 commit 7472ac3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/seismometer/configuration/config_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def generate_dictionary_from_parquet(
for events section, by default "Type".
"""
df = pd.read_parquet(inpath)
if df.empty:
raise ValueError("No data loaded; check the input file")

datadict = None
match section:
Expand All @@ -36,9 +38,6 @@ def generate_dictionary_from_parquet(
case _:
raise ValueError(f"Section {section} not recognized; only supports predictions and events")

if not datadict:
raise ValueError("No items generated; check the input file")

write_yaml(datadict.model_dump(), outpath)


Expand All @@ -53,8 +52,12 @@ def _generate_prediction_dictionary(df: pd.DataFrame) -> PredictionDictionary:

def _generate_event_dictionary(df, column="Type") -> EventDictionary:
"""Generates entry for each unique value in the specified column"""
items = [
DictionaryItem(name=c, dtype="string", definition=f"Placeholder description for {c}")
for c in df[column].unique()
]
items = (
[
DictionaryItem(name=c, dtype="string", definition=f"Placeholder description for {c}")
for c in df[column].unique()
]
if column in df
else []
)
return EventDictionary(events=items)
8 changes: 8 additions & 0 deletions tests/configuration/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,11 @@ def test_generate_dict_invalid_section(self, mock_read):
undertest.generate_dictionary_from_parquet("TESTIN", "out.yml", section=section_type)

assert section_type in str(err.value)


# This is intentionally outside the class to NOT use the shared pd patch
@pytest.mark.parametrize("section_type", ["events", "predictions"])
@patch.object(undertest.pd, "read_parquet", return_value=pd.DataFrame())
def test_generate_dict_no_data_raises_error(mock_read, section_type, tmp_as_current):
with pytest.raises(ValueError, match="No data loaded"):
undertest.generate_dictionary_from_parquet("TESTIN", "out.yml", section=section_type)

0 comments on commit 7472ac3

Please sign in to comment.