From bcff06c3fdfb64cc4366aeb395de05c56e906e4e Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Wed, 10 Jul 2024 09:42:36 +0200 Subject: [PATCH] Test swig exception-handling Related to #2478. --- python/tests/test_swig_interface.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/python/tests/test_swig_interface.py b/python/tests/test_swig_interface.py index 2dfb46e0a7..6a5cd92d11 100644 --- a/python/tests/test_swig_interface.py +++ b/python/tests/test_swig_interface.py @@ -534,3 +534,28 @@ def test_rdataview(sbml_example_presimulation_module): # field names are included by dir() assert "x" in dir(rdata) + + +def test_python_exceptions(sbml_example_presimulation_module): + """Test that C++ exceptions are correctly caught and re-raised in Python.""" + + # only amici-base extension + solver = amici.CVodeSolver() + with pytest.raises( + RuntimeError, match="maxsteps must be a positive number" + ): + solver.setMaxSteps(-1) + + # only model extension + model = sbml_example_presimulation_module.get_model() + with pytest.raises(RuntimeError, match="Steadystate mask has wrong size"): + model.set_steadystate_mask([1] * model.nx_solver * 2) + + edata = amici.ExpData(1, 1, 1, [1]) + # too short sx0 + edata.sx0 = (1, 2) + with pytest.raises( + RuntimeError, + match=r"Number of initial conditions sensitivities \(36\) in model does not match ExpData \(2\).", + ): + amici.runAmiciSimulation(model, solver, edata)