Skip to content

Commit

Permalink
add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Apr 2, 2024
1 parent 8a42c38 commit 6cc8dae
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions tests/data/test_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def test_init(self):
assert species.adsorbed is True
assert isclose(species.energy, -1.0)

def test_eq(self):
species_0 = Species("CO2", -1.0, True, -2.0)
assert species_0 != "Invalid type"

def test_str(self):
species_0 = Species("CO2", -1.0, True, -2.0)
assert str(species_0) == "*CO2(-1.0, -2.0)"
Expand All @@ -26,20 +30,33 @@ def test_str(self):
assert str(species_1) == "H2O_g(-2.0, -3.0)"

def test_invalid_adsorbed_type(self):
with pytest.raises(TypeError):
with pytest.raises(TypeError, match="Adsorbed should be boolean"):
Species("test_species", -1, adsorbed="True")

def test_invalid_energy_type(self):
with pytest.raises(TypeError):
with pytest.raises(TypeError, match="Energy should be float"):
Species("test_species", "hi", adsorbed="True")

def test_invalid_correction_dtype(self):
with pytest.raises(TypeError, match="Correction should be float"):
Species("test_species", -1, adsorbed=True, correction="wrong_type")

def test_from_str(self):
species_0 = Species.from_str("*CO2(-1.0, -2.0)")
assert species_0 == Species("CO2", -1.0, True, -2.0)

species_1 = Species.from_str("H2O_g(-2.0, -3.0)")
assert species_1 == Species("H2O_g", -2.0, False, -3.0)

def test_from_str_invalid(self):
with pytest.raises(TypeError, match="Expect type str"):
Species.from_str([1, 2]) # expect a str

with pytest.raises(
ValueError, match="Invalid format for energy and correction"
):
Species.from_str("H2O_g(-2.0, -3.0, invalid)")

def test_from_dict(self):
species_0 = Species.from_dict(
{
Expand All @@ -59,3 +76,16 @@ def test_from_dict(self):
}
)
assert species_1 == Species("H2O", -2, False, 0.0)

def test_from_dict_invalid(self):
with pytest.raises(TypeError, match="Expect a dict"):
Species.from_dict("invalid type")

with pytest.raises(ValueError, match="Missing required arg"):
Species.from_dict(
{
"name": "H2O",
"energy": -2,
# "adsorbed": False,
}
)

0 comments on commit 6cc8dae

Please sign in to comment.