Skip to content

Commit

Permalink
Additional unit tests for device_factory
Browse files Browse the repository at this point in the history
  • Loading branch information
rtuck99 committed Jan 15, 2025
1 parent 5441cc1 commit 0e2cccf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
16 changes: 14 additions & 2 deletions tests/fake_device_factory_beamline.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from unittest.mock import AsyncMock, MagicMock

import ophyd
from bluesky.protocols import Readable, Reading, SyncOrAsync
from event_model.documents.event_descriptor import DataKey
from ophyd_async.core import Device

from dodal.common.beamlines.beamline_utils import device_factory
from dodal.common.beamlines.beamline_utils import device_factory, device_instantiation
from dodal.devices.cryostream import CryoStream


Expand All @@ -27,8 +28,19 @@ def device_c() -> CryoStream:


@device_factory(skip=True)
def mock_device() -> ReadableDevice:
def mock_device(**kwargs) -> ReadableDevice:
device = MagicMock()
device.name = "mock_device"
device.connect = AsyncMock()
device.my_kwargs = kwargs
return device # type: ignore


@device_factory(skip=True)
def ophyd_v1_device(mock: bool = False, **kwargs) -> ophyd.Device:
device = device_instantiation(
ophyd.Device, "my_v1_device", "my_prefix", False, mock
)
device.wait_for_connection = MagicMock()
device.my_kwargs = kwargs
return device
48 changes: 47 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,56 @@ def test_device_factory_can_ignore_skip():
import tests.fake_device_factory_beamline as fake_beamline

devices, exceptions = make_all_devices(fake_beamline, include_skipped=True)
assert len(devices) == 3
assert len(devices) == 4
assert len(exceptions) == 0


def test_device_factory_can_construct_ophyd_v1_devices():
import tests.fake_device_factory_beamline as fake_beamline

device = fake_beamline.ophyd_v1_device(
connect_immediately=True, mock=True, connection_timeout=4.5
)

device.wait_for_connection.assert_called_once_with(timeout=4.5) # type: ignore


def test_device_factory_passes_kwargs_to_wrapped_factory_v1():
import tests.fake_device_factory_beamline as fake_beamline

device = fake_beamline.ophyd_v1_device(
connect_immediately=True,
mock=True,
my_int_kwarg=123,
my_str_kwarg="abc",
my_float_kwarg=1.23,
)

assert device.my_kwargs == {
"my_int_kwarg": 123,
"my_str_kwarg": "abc",
"my_float_kwarg": 1.23,
}


def test_device_factory_passes_kwargs_to_wrapped_factory_v2(RE: RunEngine):
import tests.fake_device_factory_beamline as fake_beamline

device = fake_beamline.mock_device(
connect_immediately=True,
mock=True,
my_int_kwarg=123,
my_str_kwarg="abc",
my_float_kwarg=1.23,
)

assert device.my_kwargs == {
"my_int_kwarg": 123,
"my_str_kwarg": "abc",
"my_float_kwarg": 1.23,
}


def test_fake_with_ophyd_sim_passed_to_device_factory(RE: RunEngine):
import tests.fake_device_factory_beamline as fake_beamline

Expand Down

0 comments on commit 0e2cccf

Please sign in to comment.