Skip to content

Commit

Permalink
Migrate qbpm to device_factory. Tidyup unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rtuck99 committed Jan 7, 2025
1 parent 8dc928d commit 4db1562
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
10 changes: 4 additions & 6 deletions src/dodal/beamlines/i03.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
16 changes: 2 additions & 14 deletions tests/common/beamlines/test_beamline_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
)
Expand Down
15 changes: 14 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

0 comments on commit 4db1562

Please sign in to comment.