Further improve picamera tests to reuse fixtures and context managers.

This commit is contained in:
Julian Stirling 2025-10-28 21:35:20 +00:00
parent 995a464ba2
commit 2010e8eeb2
4 changed files with 51 additions and 67 deletions

View file

@ -1,47 +1,16 @@
"""Test data collection from the Raspberry Picamera."""
import tempfile
from copy import deepcopy
from fastapi.testclient import TestClient
import pytest
import labthings_fastapi as lt
from openflexure_microscope_server.things.camera.picamera import (
StreamingPiCamera2,
MissingCalibrationError,
)
@pytest.fixture
def picamera_thing() -> StreamingPiCamera2:
"""Return a StreamingPiCamera2 Thing.
This is the Thing that the fixture client uses. It can be used to probe the
Thing directly to check actions had the expected response.
"""
return StreamingPiCamera2()
@pytest.fixture
def client(picamera_thing) -> lt.ThingClient:
"""Initialise a test client for the StreamingPiCamera2 Thing.
This fixture:
* Sets up a ThingServer,
* Registers a StreamingPiCamera2 instance at the "/camera/" endpoint
* Provides a ThingClient for interacting with it during tests.
"""
temp_folder = tempfile.TemporaryDirectory()
server = lt.ThingServer(settings_folder=temp_folder.name)
server.add_thing(picamera_thing, "/camera/")
with TestClient(server.app) as test_client:
client = lt.ThingClient.from_url("/camera/", client=test_client)
yield client
def test_get_tuning_algo(picamera_thing):
"""Test that get_tuning algorithm retrieves tuning algorithms."""
# Missing algorithm raises an error.
@ -73,7 +42,7 @@ def test_get_tuning_algo(picamera_thing):
assert picamera_thing.get_tuning_algo("rpi.geq", raise_if_missing=False) is None
def test_calibration(picamera_thing, client):
def test_calibration(picamera_thing, picamera_client):
"""Check that full auto calibrate completes and set the expected values."""
# Check the calibration_required property used by the calibration wizard
assert picamera_thing.calibration_required
@ -88,7 +57,7 @@ def test_calibration(picamera_thing, client):
assert not picamera_thing.background_detector_status.ready
# Run full auto calibrate
client.full_auto_calibrate()
picamera_client.full_auto_calibrate()
# After calibration it should report that calibration is no longer required
assert not picamera_thing.calibration_required