diff --git a/bionetgen/modelapi/model.py b/bionetgen/modelapi/model.py index 0ef3e666..4e5c8c51 100644 --- a/bionetgen/modelapi/model.py +++ b/bionetgen/modelapi/model.py @@ -1,4 +1,4 @@ -import copy, tempfile, shutil +import copy, tempfile, shutil, os from bionetgen.main import BioNetGen from bionetgen.core.exc import BNGModelError @@ -489,7 +489,7 @@ def setup_simulator(self, sim_type="libRR"): # with windows try: tmp_folder = tempfile.mkdtemp() - sbml_name = f"{self.model_name}_sbml.xml" + sbml_name = os.path.join(tmp_folder, f"{self.model_name}_sbml.xml") # write the sbml with open(sbml_name, "w+") as f: if not ( @@ -510,7 +510,10 @@ def setup_simulator(self, sim_type="libRR"): selections = ["time"] + [obs for obs in self.observables] self.simulator.simulator.timeCourseSelections = selections finally: - shutil.rmtree(tmp_folder) + try: + shutil.rmtree(tmp_folder) + except Exception: + pass self.actions = curr_actions elif sim_type == "cpy": # get the simulator diff --git a/tests/test_csimulator.py b/tests/test_csimulator.py index ebc5ee10..ba674cbf 100644 --- a/tests/test_csimulator.py +++ b/tests/test_csimulator.py @@ -60,11 +60,12 @@ def __init__(self): csim.model = MockModel() - with unittest.mock.patch( - "os.path.abspath", side_effect=lambda x: x - ), unittest.mock.patch( - "bionetgen.simulator.csimulator.CSimWrapper" - ) as mock_wrapper: + with ( + unittest.mock.patch("os.path.abspath", side_effect=lambda x: x), + unittest.mock.patch( + "bionetgen.simulator.csimulator.CSimWrapper" + ) as mock_wrapper, + ): csim.simulator = "dummy_lib_file" mock_wrapper.assert_called_once() args, kwargs = mock_wrapper.call_args @@ -131,3 +132,52 @@ def __init__(self): mock_wrapper.simulate.assert_called_once_with(1, 5, 4) assert res == ("timepoints", "obs_all", "spcs_all") + + +def test_simulator_setter_success(): + # Bypass init + sim = CSimulator.__new__(CSimulator) + sim.model = unittest.mock.Mock() + + # Setup mock parameters and species + param_mock = unittest.mock.Mock() + param_mock.expr = "1.5" + + param_invalid = unittest.mock.Mock() + param_invalid.expr = "not_a_float" + + sim.model.parameters = { + "param1": param_mock, + "_ignored": unittest.mock.Mock(), + "param2": param_invalid, + } + sim.model.species = {"spec1": unittest.mock.Mock(), "spec2": unittest.mock.Mock()} + + with unittest.mock.patch( + "bionetgen.simulator.csimulator.CSimWrapper" + ) as mock_wrapper: + sim.simulator = "dummy_lib" + + # Check that CSimWrapper is instantiated correctly + mock_wrapper.assert_called_once() + args, kwargs = mock_wrapper.call_args + assert "dummy_lib" in args[0] + assert kwargs["num_params"] == 1 # only param1 is valid and not ignored + assert kwargs["num_spec_init"] == 2 # 2 species + + # Check property getter + assert sim.simulator == mock_wrapper.return_value + + +def test_simulator_setter_compile_error(): + sim = CSimulator.__new__(CSimulator) + sim.model = unittest.mock.Mock() + sim.model.parameters = {} + sim.model.species = {} + + with unittest.mock.patch( + "bionetgen.simulator.csimulator.CSimWrapper", + side_effect=Exception("Wrapper failed"), + ): + with pytest.raises(BNGCompileError): + sim.simulator = "dummy_lib"