From 43b9b864af176198f67b49e348d488f531599863 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Fri, 6 Aug 2021 09:29:12 +0200 Subject: [PATCH] Improve the testing of ConcentrationModel.last_state_change --- cara/models.py | 2 +- cara/tests/models/test_concentration_model.py | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/cara/models.py b/cara/models.py index 2f7dcbd7..80f993d5 100644 --- a/cara/models.py +++ b/cara/models.py @@ -775,7 +775,7 @@ class ConcentrationModel: def last_state_change(self, time: float) -> float: """ - Find the most recent state change. + Find the most recent/previous state change. """ for change_time in self.state_change_times()[::-1]: diff --git a/cara/tests/models/test_concentration_model.py b/cara/tests/models/test_concentration_model.py index a036d350..9470dd56 100644 --- a/cara/tests/models/test_concentration_model.py +++ b/cara/tests/models/test_concentration_model.py @@ -68,6 +68,32 @@ def simple_conc_model(): ) +@pytest.mark.parametrize( + "time, expected_last_state_change", [ + [0., 0], + [1., 0], + [1.05, 1.], + [1.1, 1.], + [1.11, 1.1], + [1.999, 1.1], + [1.9991, 1.999], + [2., 1.999], + [2.1, 2], + [3., 2], + ] +) +def test_last_state_change_time( + simple_conc_model: models.ConcentrationModel, + time, + expected_last_state_change, +): + assert simple_conc_model.last_state_change(float(time)) == expected_last_state_change + + +def test_last_state_change_time_out_of_range(simple_conc_model: models.ConcentrationModel): + assert simple_conc_model.last_state_change(3.1) == 3.0 + + @pytest.mark.parametrize( "time, expected_next_state_change", [ [0, 0],