Update the picamera tests for labthings 0.0.12 syntax

This commit is contained in:
Julian Stirling 2025-12-15 18:28:28 +00:00
parent baf0ff9a11
commit 7a26b262a9
3 changed files with 48 additions and 32 deletions

View file

@ -8,13 +8,36 @@ from fastapi.testclient import TestClient
import labthings_fastapi as lt
from openflexure_microscope_server.things.camera.picamera import StreamingPiCamera2
@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(
cam: Optional[StreamingPiCamera2] = None, settings_folder: Optional[str] = None
):
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
@ -24,18 +47,7 @@ def camera_test_client(
:param settings_folder: The settings folder for the camera, if none is supplied, new
temporary directory will be used as the settings folder.
"""
if cam is None:
cam = StreamingPiCamera2()
# 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
server = lt.ThingServer(settings_folder=settings_folder)
server.add_thing(cam, "camera")
with TestClient(server.app) as test_client:
client = lt.ThingClient.from_url("camera", client=test_client)
yield client
del server
del cam
with camera_test_client_and_server(
settings_folder=settings_folder
) as client_and_server:
yield client_and_server[0]