From 728aa12d077a1300e26aac88c66893d2f9e8c6a4 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Mon, 2 Dec 2024 23:26:32 +0000 Subject: [PATCH] Add more unit tests I'm now testing camera metadata and stage functionality. This is only using the simulated HW - but it's a start. It would be good to add tests for the camera-stage mapping Thing, though that may require speeding up the stage. --- tests/test_dummy_server.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tests/test_dummy_server.py b/tests/test_dummy_server.py index db7fd89e..697fabd0 100644 --- a/tests/test_dummy_server.py +++ b/tests/test_dummy_server.py @@ -1,9 +1,11 @@ +import json import tempfile from fastapi import Depends, FastAPI from fastapi.testclient import TestClient - from labthings_fastapi.client import ThingClient +from PIL import Image +import piexif from openflexure_microscope_server.server import ThingServer from openflexure_microscope_server.things.camera.simulation import SimulatedCamera @@ -20,3 +22,33 @@ def test_autofocus(): with TestClient(server.app) as client: autofocus = ThingClient.from_url("/autofocus/", client) _ = autofocus.fast_autofocus() + +def test_grab_jpeg(): + with TestClient(server.app) as client: + camera = ThingClient.from_url("/camera/", client) + blob = camera.grab_jpeg() + _image = Image.open(blob.open()) + +def test_capture_jpeg_metadata(): + with TestClient(server.app) as client: + camera = ThingClient.from_url("/camera/", client) + blob = camera.capture_jpeg() + image = Image.open(blob.open()) + exif_dict = piexif.load(image.info["exif"]) + encoded_metadata = exif_dict["Exif"][piexif.ExifIFD.UserComment] + metadata = json.loads(encoded_metadata) + assert "position" in metadata["/stage/"] + +def test_stage(): + with TestClient(server.app) as client: + stage = ThingClient.from_url("/stage/", client) + start = stage.position + move = {"x": 1, "y": 2, "z": 3} + stage.move_relative(**move) + pos = stage.position + for s, m, p in zip(start.values(), move.values(), pos.values()): + assert s + m == p + stage.move_relative(**{k: -v for k, v in move.items()}) + pos = stage.position + for s, p in zip(start.values(), pos.values()): + assert s == p