Skip to content

Commit

Permalink
Test error conditions for ExtensibleRate
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Sep 7, 2022
1 parent 9122029 commit db89907
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
36 changes: 35 additions & 1 deletion test/python/test_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,19 +1543,53 @@ def test_from_dict(self):
def test_roundtrip(self):
pytest.skip("ExtensibleRate does not yet support roundtrip conversion")

def test_set_parameters_error(self):
with pytest.raises(KeyError):
# Instantiate with no A factor
ct.ReactionRate.from_dict({"type": "user-rate-1"})

def test_eval_error(self):
# Instantiate with non-numeric A factor to cause an exception during evaluation
R = ct.ReactionRate.from_dict({"type": "user-rate-1", "A": "xyz"})
with pytest.raises(TypeError):
R(500)


class TestExtensible2(utilities.CanteraTest):
def test_load_module(self):
_input_template = """
extensions:
- type: python
name: {module}
phases:
- name: gas
thermo: ideal-gas
species: [{{h2o2.yaml/species: all}}]
kinetics: gas
state: {{T: 300.0, P: 1 atm}}
"""

@classmethod
def setUpClass(cls):
here = str(Path(__file__).parent)
if here not in sys.path:
sys.path.append(here)

def test_load_module(self):
gas = ct.Solution("extensible-reactions.yaml", transport_model=None)

for T in np.linspace(300, 3000, 10):
gas.TP = T, None
assert gas.forward_rate_constants[0] == pytest.approx(3.14 * T**2)

def test_missing_module(self):
with pytest.raises(ct.CanteraError, match="No module named 'fake_ext'"):
ct.Solution(yaml=self._input_template.format(module="fake_ext"))

def test_invalid_module(self):
with pytest.raises(ct.CanteraError, match="SyntaxError"):
ct.Solution(yaml=self._input_template.format(module="user_ext_invalid"))


class InterfaceReactionTests(ReactionTests):
# test suite for surface reaction expressions
Expand Down
11 changes: 11 additions & 0 deletions test/python/user_ext_invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import cantera as ct

this is a syntax error

@ct.extension(name="square-rate")
class SquareRate(ct.ExtensibleRate):
def after_set_parameters(self, node, units):
self.A = node["A"]

def replace_eval(self, T):
return self.A * T**2

0 comments on commit db89907

Please sign in to comment.