Update picamera tests to use new test environment

This commit is contained in:
Julian Stirling 2025-12-17 19:18:57 +00:00
parent b70d36c629
commit 17b1f0e72a
4 changed files with 50 additions and 78 deletions

View file

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

View file

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

View file

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

View file

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