Skip to content

Commit

Permalink
simplify error check in unit tests (#263)
Browse files Browse the repository at this point in the history
* fix grammar

* use match feature of pytest.raises to check error message
  • Loading branch information
CunliangGeng authored Jul 6, 2024
1 parent 1d5f50c commit 9897381
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/nplinker/strain/strain_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def remove(self, strain: Strain):
else:
self._strain_dict_name[name] = new_strain_list
else:
raise ValueError(f"Strain {strain} not found in strain collection.")
raise ValueError(f"Strain {strain} not found in the strain collection.")

def filter(self, strain_set: set[Strain]):
"""Remove all strains that are not in strain_set from the strain collection.
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/genomics/test_antismash_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ def test_default(self, tmp_path):
def test_error_nonempty_path(self, tmp_path):
nonempty_path = tmp_path / "extracted" / "antismash" / f"{self.antismash_id}" / "subdir"
nonempty_path.mkdir(parents=True)
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Nonempty directory"):
download_and_extract_antismash_data(self.antismash_id, tmp_path, tmp_path / "extracted")
assert "Nonempty directory" in e.value.args[0]

# test a non-existent ID, which can be either a fake ID, non-existent in NCBI
# or a valid NCBI genome ID but it does not have BGC data in antismash database
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/genomics/test_antismash_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,5 @@ def test_parse_bgc_genbank():

def test_parse_bgc_genbank_error():
gbk_file = str(DATA_DIR / "fake_antismash.region001.gbk")
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Not found product prediction in antiSMASH Genbank file"):
parse_bgc_genbank(gbk_file)
assert "Not found product prediction in antiSMASH Genbank file" in e.value.args[0]
8 changes: 4 additions & 4 deletions tests/unit/genomics/test_mibig_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def test_version(self, tmp_path):
assert metadata.is_file()

def test_error_same_path(self, tmp_path):
with pytest.raises(ValueError) as e:
with pytest.raises(
ValueError, match="Identical path of download directory and extract directory"
):
mibig.download_and_extract_mibig_metadata(tmp_path, tmp_path)
assert e.value.args[0] == "Identical path of download directory and extract directory"

def test_error_nonempty_path(self, tmp_path):
nonempty_path = tmp_path / "metadata" / "subdir"
nonempty_path.mkdir(parents=True)

with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Nonempty directory"):
mibig.download_and_extract_mibig_metadata(tmp_path, nonempty_path.parent)
assert "Nonempty directory" in e.value.args[0]
4 changes: 1 addition & 3 deletions tests/unit/genomics/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ def test_add_strain_to_bgc_error(bgcs):
strains.add(strain1)
strains.add(strain2)

with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Multiple strain objects found .*"):
add_strain_to_bgc(strains, bgcs)

assert "Multiple strain objects found for BGC id 'BGC_01'" in e.value.args[0]


def test_add_bgc_to_gcf(bgcs):
"""Test add_bgc_to_gcf function."""
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/metabolomics/test_gnps_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ def setup_with_fixture(gnps_website_is_down):


def test_unknown_workflow(tmpdir):
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Unknown workflow type for GNPS task .*"):
GNPSDownloader("0ad6535e34d449788f297e712f43068a", tmpdir)
assert "Unknown workflow type for GNPS task" in str(e.value)


@pytest.mark.parametrize(
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/metabolomics/test_gnps_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@


def test_unknown_workflow(gnps_zip_files, tmpdir):
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Unknown workflow type for GNPS archive .*"):
GNPSExtractor(gnps_zip_files[GNPSFormat.Unknown], tmpdir)
assert "Unknown workflow type for GNPS archive" in str(e.value)


@pytest.mark.parametrize("workflow", [GNPSFormat.FBMN, GNPSFormat.SNETS, GNPSFormat.SNETSV2])
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/strain/test_strain_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_add_different_id_different_alias(strain: Strain, collection: StrainColl

def test_remove(strain: Strain):
sc = StrainCollection()
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Strain .* not found in the strain collection"):
sc.remove(strain)
assert strain not in sc

Expand All @@ -151,7 +151,7 @@ def test_remove_same_id_different_alias(collection: StrainCollection):

def test_remove_different_id(collection: StrainCollection):
strain = Strain("strain_2")
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Strain .* not found in the strain collection"):
collection.remove(strain)
assert len(collection) == 1
assert strain not in collection
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_has_name(collection: StrainCollection):
def test_lookup(collection: StrainCollection, strain: Strain):
for name in strain.names:
assert collection.lookup(name) == [strain]
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Strain .* not found in the strain collection"):
collection.lookup("strain_not_exist")


Expand Down

0 comments on commit 9897381

Please sign in to comment.