From 17b1f0e72a3f0a429b312aff7b43e5117771eb79 Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Wed, 17 Dec 2025 19:18:57 +0000 Subject: [PATCH] Update picamera tests to use new test environment --- .../picamera2/cam_test_utils.py | 37 +++++++++++++ .../picamera2/cam_test_utils/__init__.py | 53 ------------------- hardware-specific-tests/picamera2/conftest.py | 30 +++-------- .../picamera2/test_calibration.py | 8 +-- 4 files changed, 50 insertions(+), 78 deletions(-) create mode 100644 hardware-specific-tests/picamera2/cam_test_utils.py delete mode 100644 hardware-specific-tests/picamera2/cam_test_utils/__init__.py diff --git a/hardware-specific-tests/picamera2/cam_test_utils.py b/hardware-specific-tests/picamera2/cam_test_utils.py new file mode 100644 index 00000000..25e5f020 --- /dev/null +++ b/hardware-specific-tests/picamera2/cam_test_utils.py @@ -0,0 +1,37 @@ +"""Utilities to help with testing the camera.""" + +from contextlib import contextmanager +from typing import Optional + +from openflexure_microscope_server.things.camera.picamera import StreamingPiCamera2 + +from ..tests.utilities.lt_test_utils import LabThingsTestEnv + + +@contextmanager +def camera_test_env(settings_folder: Optional[str] = None): + """Yield a test environment with a server that contains just the camera. + + This is a context manager, not a pytest fixture, as it needs to be created + multiple times in some tests. + + :param settings_folder: The settings folder for the camera, if none is supplied, new + temporary directory will be used as the settings folder. + """ + thing_conf = {"camera": StreamingPiCamera2} + with LabThingsTestEnv(things=thing_conf, settings_folder=settings_folder) as env: + yield env + + +@contextmanager +def camera_test_client(settings_folder: Optional[str] = None): + """Yield a camera ThingClient on a camera server. + + This is a context manager, not a pytest fixture, as it needs to be created + multiple times in some tests. + + :param settings_folder: The settings folder for the camera, if none is supplied, new + temporary directory will be used as the settings folder. + """ + with camera_test_env(settings_folder=settings_folder) as env: + return env.get_thing_client("camera") diff --git a/hardware-specific-tests/picamera2/cam_test_utils/__init__.py b/hardware-specific-tests/picamera2/cam_test_utils/__init__.py deleted file mode 100644 index 3893d583..00000000 --- a/hardware-specific-tests/picamera2/cam_test_utils/__init__.py +++ /dev/null @@ -1,53 +0,0 @@ -"""Utilities to help with testing the camera.""" - -import tempfile -from contextlib import contextmanager -from typing import Optional - -from fastapi.testclient import TestClient - -import labthings_fastapi as lt - - -@contextmanager -def camera_test_client_and_server(settings_folder: Optional[str] = None): - """Yield a camera ThingClient and the associated camera server. - - This is a context manager, not a pytest fixture, as it needs to be created - multiple times in some tests. - - :param cam: The camera Thing to be used. If not supplied a new one will be created. - :param settings_folder: The settings folder for the camera, if none is supplied, new - temporary directory will be used as the settings folder. - """ - # Create a temp dir, if the setting folder is set it isn't really needed - # but doesn't add much overhead. - with tempfile.TemporaryDirectory() as tmpdir: - if settings_folder is None: - settings_folder = tmpdir - thing_conf = { - "camera": "openflexure_microscope_server.things.camera.picamera:StreamingPiCamera2", - } - server = lt.ThingServer(things=thing_conf, settings_folder=settings_folder) - - with TestClient(server.app) as test_client: - client = lt.ThingClient.from_url("/camera/", client=test_client) - yield client, server - del server - - -@contextmanager -def camera_test_client(settings_folder: Optional[str] = None): - """Yield a camera ThingClient on a camera server. - - This is a context manager, not a pytest fixture, as it needs to be created - multiple times in some tests. - - :param cam: The camera Thing to be used. If not supplied a new one will be created. - :param settings_folder: The settings folder for the camera, if none is supplied, new - temporary directory will be used as the settings folder. - """ - with camera_test_client_and_server( - settings_folder=settings_folder - ) as client_and_server: - yield client_and_server[0] diff --git a/hardware-specific-tests/picamera2/conftest.py b/hardware-specific-tests/picamera2/conftest.py index 68ec8dc1..63b3500f 100644 --- a/hardware-specific-tests/picamera2/conftest.py +++ b/hardware-specific-tests/picamera2/conftest.py @@ -8,28 +8,14 @@ from .cam_test_utils import camera_test_client_and_server @pytest.fixture -def picamera_client_and_server() -> lt.ThingClient: - """Initialise a test picamera_client and server for the StreamingPiCamera2 Thing. - - This fixture: - - * Sets up a ThingServer, - * Registers a StreamingPiCamera2 instance at the "camera" endpoint - * Yields a ThingClient and the server for interacting with it during tests. - * The picamera thing can be found at server.things["camera"] - """ - with camera_test_client_and_server() as client_and_server: - yield client_and_server +def picamera_test_env() -> lt.ThingClient: + """Initialise a test environment with only a StreamingPiCamera2 Thing.""" + with camera_test_client_and_server() as env: + yield env @pytest.fixture -def picamera_client(picamera_client_and_server) -> lt.ThingClient: - """Initialise a test picamera_client for the StreamingPiCamera2 Thing. - - This fixture: - - * Sets up a ThingServer, - * Registers a StreamingPiCamera2 instance at the "camera" endpoint - * return a ThingClient for interacting with it during tests. - """ - return picamera_client_and_server[0] +def picamera_client() -> lt.ThingClient: + """Initialise a test picamera_client (in a LabThings test env).""" + with camera_test_client_and_server() as env: + return env.get_thing_client["camera"] diff --git a/hardware-specific-tests/picamera2/test_calibration.py b/hardware-specific-tests/picamera2/test_calibration.py index 1e239344..f02e363a 100644 --- a/hardware-specific-tests/picamera2/test_calibration.py +++ b/hardware-specific-tests/picamera2/test_calibration.py @@ -3,13 +3,15 @@ import tempfile from copy import deepcopy +from openflexure_microscope_server.things.camera.picamera import StreamingPiCamera2 + from .cam_test_utils import camera_test_client -def test_calibration(picamera_client_and_server): +def test_calibration(picamera_test_env): """Check that full auto calibrate completes and set the expected values.""" - picamera_client, server = picamera_client_and_server - picamera_thing = server.things["camera"] + picamera_client, server = picamera_test_env.get_thing_client["camera"] + picamera_thing = picamera_test_env.get_thing_by_type(StreamingPiCamera2) # Check the calibration_required property used by the calibration wizard assert picamera_thing.calibration_required # Save copy of default tuning file for end of test