Fix unit tests for stage_measrue as an OFMThing

This commit is contained in:
Julian Stirling 2026-05-14 16:49:29 +01:00
parent 5c128c1467
commit 714435f2d4
3 changed files with 25 additions and 8 deletions

View file

@ -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."""

View file

@ -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:

View file

@ -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