Skip to content

Commit

Permalink
add unit tests for ReactionStep
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Apr 2, 2024
1 parent 6cc8dae commit 656141a
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/data/test_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ def test_init(self):
# Test __str__
assert str(reactionstep) == "1.0*CO2 + 1.0H+ + 1.0e- -> 1.0*COOH"

def test_neg_stoi_number(self):
reactants = {
Species("A", -1, True, 0.5): -1, # negative
Species("H2O_g", -4, False, 2.0): 2,
}
products = {
Species("B", -2, True, 1.0): 2,
}

with pytest.warns(
UserWarning, match="Negative stoichiometric number found"
):
ReactionStep(reactants=reactants, products=products)

def test_eq(self):
react_step = "*A + 2H2O_g -> 2*B"

Expand All @@ -54,6 +68,17 @@ def test_eq(self):
reactants=reactants, products=products
)

assert (
ReactionStep(reactants=reactants, products=products)
!= "*A + 2H2O_g -> 2*B"
)

# test inverse reaction
with pytest.warns(match="Found a reverse reaction step"):
assert ReactionStep(
reactants=reactants, products=products
) != ReactionStep(reactants=products, products=reactants)

def test_sepa_stoi_number(self):
spec_string_0 = " *CO2(-6, 3) "
assert ReactionStep._sepa_stoi_number(spec_string_0) == (
Expand Down Expand Up @@ -96,6 +121,19 @@ def test_from_str(self):
reactants=reactants_1, products=products_1
)

def test_from_str_invalid(self):
with pytest.raises(TypeError, match="Expect a string"):
ReactionStep.from_str(
["*A", "2H2O_g", "2*B"], # should be str
energy_dict,
)

with pytest.raises(ValueError, match="Invalid ReactionStep str"):
ReactionStep.from_str(
"*A -> 2H2O_g -> 2*B", # expect two parts
energy_dict,
)

def test_invalid_reactants(self):
with pytest.raises(TypeError):
# Pass an invalid type for species
Expand Down Expand Up @@ -149,3 +187,7 @@ def test_from_str(self):
assert reaction[1] == ReactionStep.from_str(
"*B -> *C + H2_g", energy_dict
)

def test_from_str_invalid(self):
with pytest.raises(TypeError, match="Expect a str"):
Reaction.from_str(["*A", "2H2O_g", "2*B"], energy_dict)

0 comments on commit 656141a

Please sign in to comment.