Update how the application configuration is read for LabThings 0.0.17

This commit is contained in:
Julian Stirling 2026-03-04 14:03:14 +00:00
parent 45903f92a6
commit 7e4ec53d78
7 changed files with 37 additions and 62 deletions

View file

@ -36,6 +36,8 @@ def test_successful_start(mocker, caplog):
"openflexure_microscope_server.server.lt.ThingServer.from_config",
return_value=mock_server,
)
# Also mock configure_logging or it will touch a load of code and log.
mock_log_configure = mocker.patch.object(ofm_server, "configure_logging")
# Also mock customisation or it will try to access the hard drive and make files.
mock_customise = mocker.patch.object(ofm_server, "customise_server")
# And mock uvicorn.run as we don't want to start a server process
@ -44,7 +46,9 @@ def test_successful_start(mocker, caplog):
# Run the mock CLI
ofm_server.serve_from_cli(["-c", SIM_CONFIG])
# Check that the server was customised and the run
# Check that the logging was configures, the server was customised, and uvicorn was
# run
assert mock_log_configure.call_count == 1
assert mock_customise.call_count == 1
assert mock_uvicorn_run.call_count == 1
# Read the log config that was set

View file

@ -60,7 +60,6 @@ def test_customise_server(mocker):
# but also to limit excess coverage reporting.
mocked_add_static = mocker.patch.object(ofm_server, "add_static_files")
mocked_v2_endpoints = mocker.patch.object(ofm_server, "add_v2_endpoints")
mocked_configure_logging = mocker.patch.object(ofm_server, "configure_logging")
mocked_retrieve_log = mocker.patch.object(ofm_server, "retrieve_log")
mocked_retrieve_log_file = mocker.patch.object(ofm_server, "retrieve_log_from_file")
@ -77,7 +76,6 @@ def test_customise_server(mocker):
# Check each internal customisation function is called
assert mocked_add_static.call_count == 1
assert mocked_v2_endpoints.call_count == 1
assert mocked_configure_logging.call_count == 1
# Check app.get adds two routes
assert mock_app.get.call_count == 2
assert wrapper.call_count == 2
@ -122,10 +120,12 @@ def test_full_config_from_args(config, log_dir, data_dir, mocker):
},
)
args = Namespace(config=config, json=None)
lt_conf, application_config = ofm_server._full_config_from_args(args)
lt_conf = ofm_server._full_config_from_args(args)
# If json is supplied OFM config is default. As the json is passed dircectly to
# The Pydantic Mocel in LabThings. No custom data allowed.
assert isinstance(lt_conf, ThingServerConfig)
assert isinstance(application_config, OFMApplicationData)
# Check the configuration validates
application_config = OFMApplicationData(**lt_conf.application_config)
# And has the correct values
assert application_config.log_folder == log_dir
assert application_config.data_folder == data_dir

View file

@ -41,7 +41,7 @@ from openflexure_microscope_server.things.smart_scan import (
# Use our own dir in the root temp dir not a dynamically generated one so we
# have some control of when it is deleted
SCAN_DIR = os.path.join(tempfile.gettempdir(), "scans")
SCAN_DIR = os.path.join(tempfile.gettempdir(), "smartscanthing")
def _clear_scan_dir() -> None:
@ -53,15 +53,15 @@ def _clear_scan_dir() -> None:
@pytest.fixture
def smart_scan_thing(mocker):
"""Return a smart scan thing as a fixture."""
mocker.patch(
"openflexure_microscope_server.things.get_data_directory_from_server",
return_value=SCAN_DIR,
)
return create_thing_without_server(
thing = create_thing_without_server(
SmartScanThing,
default_workflow="mock-_all_workflows",
mock_all_slots=True,
)
type(thing._thing_server_interface).application_config = mocker.PropertyMock(
return_value={"data_folder": tempfile.gettempdir()}
)
return thing
@pytest.fixture
@ -81,15 +81,15 @@ def custom_smart_scan_thing(default_workflow, all_workflows, mocker):
This allows setting a default workflow and to adjust all workflows from simple
single item mock from `mock_all_slots`.
"""
mocker.patch(
"openflexure_microscope_server.things.get_data_directory_from_server",
return_value=SCAN_DIR,
)
smart_scan_thing = create_thing_without_server(
SmartScanThing,
default_workflow=default_workflow,
mock_all_slots=True,
)
interface_type = type(smart_scan_thing._thing_server_interface)
interface_type.application_config = mocker.PropertyMock(
return_value={"data_folder": tempfile.gettempdir()}
)
# Pop the existing mock workflow and add specified ones (if any)
smart_scan_thing._all_workflows.pop("mock-_all_workflows")
for key, thing in all_workflows.items():