From 4db15621d2b5b0461dbd5f260a0f9975e74356f5 Mon Sep 17 00:00:00 2001 From: Robert Tuck Date: Tue, 7 Jan 2025 16:06:22 +0000 Subject: [PATCH] Migrate qbpm to device_factory. Tidyup unit tests --- src/dodal/beamlines/i03.py | 10 ++++------ tests/common/beamlines/test_beamline_utils.py | 16 ++-------------- tests/conftest.py | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/dodal/beamlines/i03.py b/src/dodal/beamlines/i03.py index ea69fb9107..704a86b541 100644 --- a/src/dodal/beamlines/i03.py +++ b/src/dodal/beamlines/i03.py @@ -412,14 +412,12 @@ def diamond_filter() -> DiamondFilter[I03Filters]: ) -def qbpm(wait_for_connection: bool = True, fake_with_ophyd_sim: bool = False) -> QBPM: +@device_factory() +def qbpm() -> QBPM: """Get the i03 qbpm device, instantiate it if it hasn't already been. If this is called when already instantiated in i03, it will return the existing object. """ - return device_instantiation( - QBPM, + return QBPM( + f"{PREFIX.beamline_prefix}-DI-QBPM-01:", "qbpm", - "-DI-QBPM-01:", - wait_for_connection, - fake_with_ophyd_sim, ) diff --git a/tests/common/beamlines/test_beamline_utils.py b/tests/common/beamlines/test_beamline_utils.py index a6f3e7869e..57f2629370 100644 --- a/tests/common/beamlines/test_beamline_utils.py +++ b/tests/common/beamlines/test_beamline_utils.py @@ -86,26 +86,14 @@ def test_instantiate_v2_function_fake_makes_fake(): def test_clear_devices(RE): devices, exceptions = make_all_devices(i03, fake_with_ophyd_sim=True) assert ( - len(beamline_utils.ACTIVE_DEVICES) == len(devices.keys()) - 28 + # These are the only 3 devices remaining in i03 that are still OphydV1 + len(beamline_utils.ACTIVE_DEVICES) == len(["flux", "s4_slit_gaps", "eiger"]) and len(exceptions) == 0 ) beamline_utils.clear_devices() assert beamline_utils.ACTIVE_DEVICES == {} -def test_device_is_new_after_clearing(RE): - def _make_devices_and_get_id(): - devices, _ = make_all_devices(i03, fake_with_ophyd_sim=True) - return [id(device) for device in devices.values()] - - ids_1 = [_make_devices_and_get_id()] - ids_2 = [_make_devices_and_get_id()] - assert ids_1 == ids_2 - beamline_utils.clear_devices() - ids_3 = [_make_devices_and_get_id()] - assert ids_1 != ids_3 - - @pytest.mark.parametrize( "kwargs,expected_timeout", [({}, 5.0), ({"timeout": 15.0}, 15.0)] ) diff --git a/tests/conftest.py b/tests/conftest.py index 967665a503..03ce94d1d0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,8 +5,9 @@ import pytest from conftest import mock_attributes_table +from dodal.beamlines import i03 from dodal.common.beamlines import beamline_parameters, beamline_utils -from dodal.utils import make_all_devices +from dodal.utils import collect_factories, make_all_devices @pytest.fixture(scope="function") @@ -32,3 +33,15 @@ def mock_beamline_module_filepaths(bl_name, bl_module): beamline_parameters.BEAMLINE_PARAMETER_PATHS[bl_name] = ( "tests/test_data/i04_beamlineParameters" ) + + +@pytest.fixture(scope="session") +def i03_device_factories(): + return [f for f in collect_factories(i03).values() if hasattr(f, "cache_clear")] + + +@pytest.fixture(scope="function", autouse=True) +def clear_device_factory_caches_after_every_test(i03_device_factories): + yield None + for f in i03_device_factories: + f.cache_clear() # type: ignore