Use semver library for checking sangaboard firmware version.

This commit is contained in:
Julian Stirling 2025-12-02 11:55:56 +00:00
parent 797dda3514
commit 240b7d1ba5
3 changed files with 83 additions and 31 deletions

View file

@ -1,8 +1,14 @@
"""Tests for the Sangaboard thing."""
import logging
import pytest
from openflexure_microscope_server.things.stage.sangaboard import SangaboardThing
from openflexure_microscope_server.things.stage.sangaboard import (
SangaboardThing,
REQUIRED_VERSION,
RECOMMENDED_VERSION,
)
@pytest.fixture
@ -15,6 +21,58 @@ def mock_sanga_thing(mocker):
def test_check_old_firmware(mock_sanga_thing):
"""Check firmware prior to version 1 throws a Runtime Error."""
mock_sanga_thing._sangaboard.firmware_version.return_value = "0.1.1"
mock_sanga_thing._sangaboard.firmware_version = "0.1.1"
with pytest.raises(RuntimeError):
mock_sanga_thing.check_firmware()
def test_firmware_before_recommended(mock_sanga_thing, caplog):
"""Check required version warns if it is lower than the recommended version."""
if REQUIRED_VERSION == RECOMMENDED_VERSION:
# If required is currently the same as recommended then this test isn't valid
return
mock_sanga_thing._sangaboard.firmware_version = str(REQUIRED_VERSION)
with caplog.at_level(logging.WARNING):
mock_sanga_thing.check_firmware()
assert len(caplog.records) == 1
msg = (
f"Sangaboard firmware version {REQUIRED_VERSION} is below the recommended "
f"{RECOMMENDED_VERSION}."
)
assert caplog.records[0].message == msg
def test_dev_is_before_recommended(mock_sanga_thing, caplog):
"""Check that a dev version of the recommended version warns."""
if REQUIRED_VERSION == RECOMMENDED_VERSION:
# If required is currently the same as recommended then this test isn't valid
return
dev_version = RECOMMENDED_VERSION.replace(prerelease="dev1")
mock_sanga_thing._sangaboard.firmware_version = str(dev_version)
with caplog.at_level(logging.WARNING):
mock_sanga_thing.check_firmware()
assert len(caplog.records) == 1
msg = (
f"Sangaboard firmware version {dev_version} is below the recommended "
f"{RECOMMENDED_VERSION}."
)
assert caplog.records[0].message == msg
def test_valid_firmware(mock_sanga_thing, caplog):
"""Check valid firmware doesn't error."""
versions = [
RECOMMENDED_VERSION, # Recommended
RECOMMENDED_VERSION.bump_major(), # Higher versions
RECOMMENDED_VERSION.bump_minor(),
RECOMMENDED_VERSION.bump_patch(),
RECOMMENDED_VERSION.bump_patch().replace(prerelease="dev1"),
]
# Note that dev1 will not warn in check version, but will warn in the underlying
# Sangaboard library as it is a pre-release.
for version in versions:
mock_sanga_thing._sangaboard.firmware_version = str(version)
with caplog.at_level(logging.WARNING):
mock_sanga_thing.check_firmware()
assert len(caplog.records) == 0