Skip to content

Commit

Permalink
friendlier error messages when species/genetic maps aren't found
Browse files Browse the repository at this point in the history
  • Loading branch information
vsbuffalo authored and petrelharp committed Oct 14, 2021
1 parent fa3b76a commit 31ef60c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
25 changes: 20 additions & 5 deletions stdpopsim/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def get_species(id):
if id not in registered_species:
# TODO we should probably have a custom exception here and standardise
# on using these for all the catalog search functions.
raise ValueError(f"Species '{id}' not in catalog")
avail_sps_str = ", ".join(registered_species)
raise ValueError(f"Species '{id}' not in catalog ({avail_sps_str})")
return registered_species[id]


Expand Down Expand Up @@ -220,7 +221,11 @@ def get_demographic_model(self, id):
for model in self.demographic_models:
if model.id == id:
return model
raise ValueError(f"DemographicModel '{self.id}/{id}' not in catalog")
available_models = [dm.id for dm in self.demographic_models]
avail_models_str = ", ".join(available_models)
raise ValueError(
f"DemographicModel '{self.id}/{id}' not in catalog ({avail_models_str})"
)

def add_demographic_model(self, model):
if model.id in [m.id for m in self.demographic_models]:
Expand All @@ -244,7 +249,9 @@ def get_dfe(self, id):
for dfe in self.dfes:
if dfe.id == id:
return dfe
raise ValueError(f"DFE '{self.id}/{id}' not in catalog")
available_dfes = [d.id for d in self.dfes]
avail_dfes_str = ", ".join(available_dfes)
raise ValueError(f"DFE '{self.id}/{id}' not in catalog ({avail_dfes_str})")

def add_dfe(self, dfe):
if dfe.id in [d.id for d in self.dfes]:
Expand Down Expand Up @@ -273,7 +280,11 @@ def get_genetic_map(self, id):
for gm in self.genetic_maps:
if gm.id == id:
return gm
raise ValueError(f"Genetic map '{self.id}/{id}' not in catalog")
available_maps = [gm.id for gm in self.genetic_maps]
avail_maps_str = ", ".join(available_maps)
raise ValueError(
f"Genetic map '{self.id}/{id}' not in catalog ({avail_maps_str})"
)

def add_annotations(self, annotations):
if annotations.id in [an.id for an in self.annotations]:
Expand All @@ -297,4 +308,8 @@ def get_annotations(self, id):
for an in self.annotations:
if an.id == id:
return an
raise ValueError(f"Annotations '{self.id}/{id}' not in catalog")
available_anno = [anno.id for anno in self.annotations]
avail_anno_str = ", ".join(available_anno)
raise ValueError(
f"Annotations '{self.id}/{id}' not in catalog ({avail_anno_str})"
)
14 changes: 11 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,22 +769,30 @@ class TestSearchWrappers:
def test_bad_species(self):
with mock.patch("stdpopsim.cli.exit", autospec=True) as mocked_exit:
cli.get_species_wrapper("XXX")
mocked_exit.assert_called_once_with("Species 'XXX' not in catalog")
avail_sps_str = ", ".join(stdpopsim.species.registered_species)
mocked_exit.assert_called_once_with(
f"Species 'XXX' not in catalog ({avail_sps_str})"
)

def test_bad_model(self):
species = stdpopsim.get_species("HomSap")
with mock.patch("stdpopsim.cli.exit", autospec=True) as mocked_exit:
cli.get_model_wrapper(species, "XXX")
available_models = [dm.id for dm in species.demographic_models]
avail_models_str = ", ".join(available_models)
mocked_exit.assert_called_once_with(
"DemographicModel 'HomSap/XXX' not in catalog"
f"DemographicModel 'HomSap/XXX' not in catalog ({avail_models_str})"
)

def test_bad_genetic_map(self):
species = stdpopsim.get_species("HomSap")
with mock.patch("stdpopsim.cli.exit", autospec=True) as mocked_exit:
cli.get_genetic_map_wrapper(species, "XXX")
available_maps = [gm.id for gm in species.genetic_maps]
avail_maps_str = ", ".join(available_maps)

mocked_exit.assert_called_once_with(
"Genetic map 'HomSap/XXX' not in catalog"
f"Genetic map 'HomSap/XXX' not in catalog ({avail_maps_str})"
)


Expand Down

0 comments on commit 31ef60c

Please sign in to comment.