diff --git a/src/openflexure_microscope_server/things/__init__.py b/src/openflexure_microscope_server/things/__init__.py index c497209f..255e0379 100644 --- a/src/openflexure_microscope_server/things/__init__.py +++ b/src/openflexure_microscope_server/things/__init__.py @@ -5,6 +5,7 @@ with other Things and including them in the LabThings-FastAPI config file. """ import os +from types import TracebackType from typing import Optional, Self import labthings_fastapi as lt @@ -35,6 +36,19 @@ class OFMThing(lt.Thing): ) return self + def __exit__( + self, + _exc_type: type[BaseException], + _exc_value: Optional[BaseException], + _traceback: Optional[TracebackType], + ) -> None: + """Close the OFMThing. + + This is needed for the context manager protocol to work. Currently it doesn't + do anything. + """ + pass + @property def data_dir(self) -> str: """The data directory for this thing.""" diff --git a/src/openflexure_microscope_server/things/stage_measure.py b/src/openflexure_microscope_server/things/stage_measure.py index c9422072..cedfaa0f 100644 --- a/src/openflexure_microscope_server/things/stage_measure.py +++ b/src/openflexure_microscope_server/things/stage_measure.py @@ -202,11 +202,7 @@ class RangeofMotionThing(OFMThing): timestamp = datetime.now().strftime("%Y_%m_%d_%H_%M") datafile_path = os.path.join(self.data_dir, f"rom_data_{timestamp}.json") - with open( - datafile_path, - "w", - encoding="utf-8", - ) as f: + with open(datafile_path, "w", encoding="utf-8") as f: json.dump(data, f, indent=4) finally: diff --git a/tests/unit_tests/test_stage_measure.py b/tests/unit_tests/test_stage_measure.py index fad2df21..9204878f 100644 --- a/tests/unit_tests/test_stage_measure.py +++ b/tests/unit_tests/test_stage_measure.py @@ -1,6 +1,7 @@ """File contains unit tests for stage_measure.""" import logging +import tempfile from copy import copy import numpy as np @@ -148,11 +149,14 @@ def test_error_if_no_stream_res_set_when_requesting_img_coords(): @pytest.fixture -def rom_thing(example_rom_data, csm_matrix) -> stage_measure.RangeofMotionThing: +def rom_thing(example_rom_data, csm_matrix, mocker) -> stage_measure.RangeofMotionThing: """Yield a RangeofMotionThing already populated with some example rom_data.""" rom_thing = create_thing_without_server( stage_measure.RangeofMotionThing, mock_all_slots=True ) + type(rom_thing._thing_server_interface).application_config = mocker.PropertyMock( + return_value={"data_folder": tempfile.gettempdir()} + ) rom_thing._stream_resolution = [800, 600] rom_thing._rom_data = example_rom_data @@ -586,7 +590,7 @@ def test_perform_rom_actions_locked(rom_thing): rom_thing.perform_recentre() -def test_perform_rom_test_(rom_thing, mocker): +def test_perform_rom_test(rom_thing, mocker): """Check that perform Rom Test runs the expected high level algorithm.""" def check_and_modify_rom_data(axis: str, direction: int, **_kwargs): @@ -605,7 +609,10 @@ def test_perform_rom_test_(rom_thing, mocker): mocker.patch.object( rom_thing._cam, "grab_as_array", return_value=np.zeros([123, 456, 3]) ) - final_dict = rom_thing.perform_rom_test() + + # Enter the ROM thing to get the data directory. + with rom_thing as running_rom_thing: + final_dict = running_rom_thing.perform_rom_test() # This should take less than 1 sec assert final_dict["Time"] <= 1