From 5e28a96d8512ab85918cc8514b34adcd78ff5491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Sams=C3=B3?= Date: Thu, 13 Jul 2023 15:36:59 +0200 Subject: [PATCH] fix failing test --- tests/pytest_pysd/pytest_pysd.py | 104 ++++++++++++++++--------------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/tests/pytest_pysd/pytest_pysd.py b/tests/pytest_pysd/pytest_pysd.py index 09cad057..3dfd434c 100644 --- a/tests/pytest_pysd/pytest_pysd.py +++ b/tests/pytest_pysd/pytest_pysd.py @@ -28,6 +28,8 @@ test_model_constant_pipe = more_tests.joinpath( "constant_pipeline/test_constant_pipeline.mdl") +test_model_stepper = more_tests.joinpath( + "stepper_cache/stepper_cache.mdl") class TestPySD(): @@ -1484,57 +1486,6 @@ def test_change_constant_pipe(self, model): assert\ (out2["constant3"] == (5*new_var.values-1)*new_var.values).all() - @pytest.mark.parametrize("model_path", [test_model]) - def test_stepper_workflow(self, model): - - output = ModelOutput() - - model.set_stepper(output, step_vars=["room_temperature"], - final_time=5) - - assert model.cache_type["room_temperature"] == "step" - - result_temperatures = np.arange(70, 111) - - # run 40 steps - for _ in range(len(result_temperatures) - 1): - model.step(1, {"room_temperature": model["room_temperature"] + 1}) - - result_df = output.collect(model) - - np.array_equal(result_df["Room Temperature"].values, result_temperatures) - assert np.floor(result_df.loc[5, "Teacup Temperature"]) == 144 - - def test_stepper_cache_assignment(self, _root): - - test_model_stepper = _root.joinpath( - "more-tests/stepper_cache/stepper_cache.mdl") - - model = pysd.read_vensim(test_model_stepper) - - assert model.cache_type["foo"] == "run" - assert model.cache_type["bar"] == "run" - assert model.cache_type["foobar"] == "run" - - output = ModelOutput() - - model.set_stepper(output, params={"foo": 0}, time_step=0.2, - saveper=0.2, final_time=5, step_vars=["foo"]) - - assert model.cache_type["foo"] == "step" - assert model.cache_type["bar"] == "step" - assert model.cache_type["foobar"] == "step" - - steps = int((model["final_time"] - model["initial_time"]) / - model["time_step"]) - - for i in range(1,steps + 1): - model.step(1, {"foo": i}) - - res = output.collect(model) - - assert np.floor(res.loc[5, "foobar"]) == 100 - class TestExportImport(): @@ -1630,3 +1581,54 @@ def test_run_export_import(self, tmp_path, model, return_ts, final_t): assert_frames_close(stocks1, stocks.loc[return_ts[1]]) if return_ts[2]: assert_frames_close(stocks2, stocks.loc[return_ts[2]]) + +class TestStepper(): + + @pytest.mark.parametrize("model_path", [test_model_stepper]) + def test_stepper_cache_assignment(self, model): + + assert model.cache_type["foo"] == "run" + assert model.cache_type["bar"] == "run" + assert model.cache_type["foobar"] == "run" + + output = ModelOutput() + + model.set_stepper(output, params={"foo": 0}, time_step=0.2, + saveper=0.2, final_time=5, step_vars=["foo"]) + + assert model.cache_type["foo"] == "step" + assert model.cache_type["bar"] == "step" + assert model.cache_type["foobar"] == "step" + + steps = int((model["final_time"] - model["initial_time"]) / + model["time_step"]) + + for i in range(1,steps + 1): + model.step(1, {"foo": i}) + + res = output.collect(model) + + assert np.floor(res.loc[5, "foobar"]) == 100 + + @pytest.mark.parametrize("model_path", [test_model]) + def test_stepper_workflow(self, model): + + assert model.cache_type["room_temperature"] == "run" + + output = ModelOutput() + + model.set_stepper(output, step_vars=["room_temperature"], + final_time=5) + + assert model.cache_type["room_temperature"] == "step" + + result_temperatures = np.arange(70, 111) + + # run 40 steps + for _ in range(len(result_temperatures) - 1): + model.step(1, {"room_temperature": model["room_temperature"] + 1}) + + result_df = output.collect(model) + + np.array_equal(result_df["Room Temperature"].values, result_temperatures) + assert np.floor(result_df.loc[5, "Teacup Temperature"]) == 144