Add runtime checkable protocol for checking gallery compatibility.
This commit is contained in:
parent
93d7d75b0d
commit
4583151345
3 changed files with 181 additions and 17 deletions
|
|
@ -1,5 +1,81 @@
|
|||
"""Tests that captures have the expected metadata."""
|
||||
|
||||
import logging
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from labthings_fastapi.testing import create_thing_without_server
|
||||
|
||||
from openflexure_microscope_server.things import OFMThing
|
||||
from openflexure_microscope_server.things.gallery import (
|
||||
GalleryCompatibleThing,
|
||||
GalleryThing,
|
||||
)
|
||||
|
||||
from ..shared_utils.lt_test_utils import LabThingsTestEnv
|
||||
|
||||
|
||||
class MockGalleryData(BaseModel):
|
||||
"""Mock gallery data."""
|
||||
|
||||
mock: str
|
||||
foobar: int
|
||||
|
||||
|
||||
class MinimalGalleryClass:
|
||||
"""A class that provides data to the gallery needs but is NOT a thing.
|
||||
|
||||
This should fail the protocol check as it is not an OFMThing
|
||||
"""
|
||||
|
||||
gallery_data_name: str = "Mock"
|
||||
|
||||
gallery_data_schema: type[BaseModel] = MockGalleryData
|
||||
|
||||
def get_gallery_data(self) -> list[BaseModel]:
|
||||
"""Return a list of Mock Gallery Data."""
|
||||
return [MockGalleryData(mock="mock", foobar="foobar")]
|
||||
|
||||
|
||||
class MinimalGallerySource(OFMThing, MinimalGalleryClass):
|
||||
"""Minimal example of a Thing that can show in the gallery.
|
||||
|
||||
Mixes MinimalGalleryClass into OFMThing to be recognised by the protocol.
|
||||
"""
|
||||
|
||||
show_in_gallery = True
|
||||
|
||||
|
||||
class BadGallerySource(OFMThing):
|
||||
"""An OFMThing that almost defines enough to show in the gallery.
|
||||
|
||||
Shouldn't math the protocol as ``gallery_data_name`` is missing.
|
||||
"""
|
||||
|
||||
show_in_gallery = True
|
||||
|
||||
gallery_data_schema: type[BaseModel] = MockGalleryData
|
||||
|
||||
def get_gallery_data(self) -> list[BaseModel]:
|
||||
"""Return a list of Mock Gallery Data."""
|
||||
return [MockGalleryData(mock="mock", foobar="foobar")]
|
||||
|
||||
|
||||
def test_gallery_compatible_protocol():
|
||||
"""Check the gallery compatible protocol detects compatible classes only."""
|
||||
# Minimal class has the gallery specific methods but is not an OFMThing.
|
||||
assert not isinstance(MinimalGalleryClass(), GalleryCompatibleThing)
|
||||
|
||||
# BadGallerySource is an OFM Thing but is missing one of the required properties.
|
||||
assert not isinstance(
|
||||
create_thing_without_server(BadGallerySource), GalleryCompatibleThing
|
||||
)
|
||||
|
||||
# MinimalGallerySource should match!
|
||||
assert isinstance(
|
||||
create_thing_without_server(MinimalGallerySource), GalleryCompatibleThing
|
||||
)
|
||||
|
||||
|
||||
def test_gallery_thing_finds_all_providers(simulation_test_env):
|
||||
"""Check the gallery identifies the correct Things that will provide data."""
|
||||
|
|
@ -15,3 +91,32 @@ def test_gallery_thing_finds_all_providers(simulation_test_env):
|
|||
assert snake_workflow not in gallery.gallery_providing_things.values()
|
||||
assert camera not in gallery.gallery_providing_things.values()
|
||||
assert smart_scan in gallery.gallery_providing_things.values()
|
||||
|
||||
assert not isinstance(snake_workflow, GalleryCompatibleThing)
|
||||
assert isinstance(smart_scan, GalleryCompatibleThing)
|
||||
|
||||
|
||||
def test_gallery_logs_for_bad_providers(caplog):
|
||||
"""Test that an error is logged if a gallery source doesn't match the protocol."""
|
||||
# Create a config with the minimal and the bad thing
|
||||
things = {
|
||||
"minimal_thing": MinimalGallerySource,
|
||||
"bad_thing": BadGallerySource,
|
||||
"gallery": GalleryThing,
|
||||
}
|
||||
with caplog.at_level(logging.INFO):
|
||||
# Start the server
|
||||
with LabThingsTestEnv(
|
||||
things=things, application_config={"data_folder": "./openflexure/data/"}
|
||||
) as env:
|
||||
gallery = env.get_thing_by_name("gallery")
|
||||
# Minimal thing is listed as a gallery provider
|
||||
assert "minimal_thing" in gallery.gallery_providing_things
|
||||
# Bad thing isn't as the protocol doesn't match
|
||||
assert "bad_thing" not in gallery.gallery_providing_things
|
||||
|
||||
# There is an error logged about bad_thing
|
||||
assert len(caplog.records) == 1
|
||||
record = caplog.records[0]
|
||||
assert "Data from bad_thing cannot be shown in gallery" in record.message
|
||||
assert record.levelname == "ERROR"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue