Start fixing tests. This involves further blocking portal changes.

This will need PR 225 of labthings-fastapi to be merged to run.
This commit is contained in:
Julian Stirling 2025-12-14 19:21:06 +00:00
parent 9ef417971f
commit cfbb7cf7f9
5 changed files with 33 additions and 35 deletions

View file

@ -6,6 +6,8 @@ This doesn't check the behaviour of the JPEG shaprness monitor.
import numpy as np
import pytest
from labthings_fastapi.testing import create_thing_without_server
from openflexure_microscope_server.things.autofocus import (
AutofocusThing,
NoFocusFoundError,
@ -77,7 +79,7 @@ def test_looping_autofocus(start_z, max_loc, centre, attempts_expected, passes,
sharpness_monitor.move_data.side_effect = return_sharpness
autofocus_thing = AutofocusThing()
autofocus_thing = create_thing_without_server(AutofocusThing)
if passes:
autofocus_thing.looping_autofocus(
stage=stage,

View file

@ -1,7 +1,6 @@
"""Use the Simulation camera to test base camera functionality."""
import tempfile
from contextlib import contextmanager
import numpy as np
import pytest
@ -10,29 +9,30 @@ from PIL import Image
import labthings_fastapi as lt
from openflexure_microscope_server.things.camera.simulation import SimulatedCamera
@contextmanager
def camera_server(camera: SimulatedCamera) -> lt.ThingClient:
@pytest.fixture
def camera_server() -> lt.ThingClient:
"""Add the camera to a ThingServer and start a TestClient application.
The test client application is needed for the camera to have a blocking portal.
The test client will be needed for the camera to run async frame generation code.
"""
with tempfile.TemporaryDirectory() as tmpdir:
server = lt.ThingServer(settings_folder=tmpdir)
server.add_thing(camera, "camera")
with TestClient(server.app):
yield server
conf = {
"camera": "openflexure_microscope_server.things.camera.simulation:SimulatedCamera",
"stage": "openflexure_microscope_server.things.stage.dummy:DummyStage",
}
server = lt.ThingServer(things=conf, settings_folder=tmpdir)
yield server
def test_handle_broken_frame():
def test_handle_broken_frame(camera_server):
"""Monkey patch the the mjpeg steam so 1 in 5 frames are broken, then test operation.
This simulates the very occasional broken frames that can occur when grabbing
directly from the MJPEG stream.
"""
camera = SimulatedCamera()
camera = camera_server.things["camera"]
# Money patch the mjpeg_stream grab_frame to break 1 in 5 frames.
frame_number = 0
@ -50,7 +50,8 @@ def test_handle_broken_frame():
return frame
camera.mjpeg_stream.grab_frame = flaky_grabber
with camera_server(camera):
with TestClient(camera_server.app):
# Check that this does cause broken frames.
# The noqa is because we don't know exactly when the error is thrown so we
# can't have a single simple statement in the pytest raises.
@ -68,10 +69,10 @@ def test_handle_broken_frame():
assert isinstance(array, np.ndarray)
def test_simulation_cam_calibration():
def test_simulation_cam_calibration(camera_server):
"""Test that the simulated camera can be calibrated and reports calibration correctly."""
camera = SimulatedCamera()
with camera_server(camera):
camera = camera_server.things["camera"]
with TestClient(camera_server.app):
assert camera.calibration_required
camera.full_auto_calibrate()
assert not camera.calibration_required