From 2f3c60d79568797624b38ca9a8028f1c8207fec5 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Tue, 4 Jun 2024 20:53:51 +0200 Subject: [PATCH] added type check --- festim/generic_simulation.py | 4 ++++ test/simulation/test_initialise.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/festim/generic_simulation.py b/festim/generic_simulation.py index 6723efae3..f8e672bc2 100644 --- a/festim/generic_simulation.py +++ b/festim/generic_simulation.py @@ -177,6 +177,10 @@ def T(self, value): self._T = value elif isinstance(value, (int, float, sp.Expr)): self._T = festim.Temperature(value) + else: + raise TypeError( + "accepted types for T attribute are int, float, sympy.Expr or festim.Temperature" + ) def attribute_source_terms(self): """Assigns the source terms (in self.sources) to the correct field diff --git a/test/simulation/test_initialise.py b/test/simulation/test_initialise.py index c5a3d43d3..ee3e4849c 100644 --- a/test/simulation/test_initialise.py +++ b/test/simulation/test_initialise.py @@ -205,3 +205,18 @@ def test_error_is_raised_when_no_temp(): with pytest.raises(AttributeError, match="Temperature is not defined"): my_model.initialise() + + +@pytest.mark.parametrize("value", ["coucou", [0, 0]]) +def test_wrong_type_temperature(value): + """ + Creates a Simulation object and checks that a TypeError is raised + when the T attribute is given a value of the wrong type + """ + my_model = F.Simulation() + + with pytest.raises( + TypeError, + match="accepted types for T attribute are int, float, sympy.Expr or festim.Temperature", + ): + my_model.T = value