From d73eb5b703a9319bd0505e168db49b5a0d09ca35 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 25 Aug 2025 14:44:44 +0200 Subject: [PATCH] Add `Problem.assert_valid` One-liner to check that a given `Problem` is valid. --- petab/v2/core.py | 21 +++++++++++++++++++++ tests/v2/test_core.py | 10 ++++++++++ 2 files changed, 31 insertions(+) diff --git a/petab/v2/core.py b/petab/v2/core.py index 8207c573..ed1568a6 100644 --- a/petab/v2/core.py +++ b/petab/v2/core.py @@ -1901,6 +1901,27 @@ def validate( return validation_results + def assert_valid(self, **kwargs) -> None: + """Assert that the PEtab problem is valid. + + :param kwargs: Additional arguments passed to :meth:`Problem.validate`. + + :raises AssertionError: If the PEtab problem is not valid. + """ + from ..v2.lint import ValidationIssueSeverity + + validation_results = self.validate(**kwargs) + errors = [ + r + for r in validation_results + if r.level >= ValidationIssueSeverity.ERROR + ] + if errors: + raise AssertionError( + "PEtab problem is not valid:\n" + + "\n".join(e.message for e in errors) + ) + def add_condition( self, id_: str, name: str = None, **kwargs: Number | str | sp.Expr ): diff --git a/tests/v2/test_core.py b/tests/v2/test_core.py index 7a93ecf1..cc2842eb 100644 --- a/tests/v2/test_core.py +++ b/tests/v2/test_core.py @@ -58,6 +58,16 @@ def test_condition_table_round_trip(): assert conditions == conditions2 +def test_assert_valid(): + problem = petab1to2(example_dir_fujita / "Fujita.yaml") + problem.assert_valid() + problem.observable_tables[0] = ObservableTable() + with pytest.raises( + AssertionError, match="not defined in the observable table" + ): + problem.assert_valid() + + def test_experiment_add_periods(): """Test operators for Experiment""" exp = Experiment(id="exp1")