Skip to content

Commit b9c7a26

Browse files
Add tests for utils module
1 parent 1018895 commit b9c7a26

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/dodal/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ def make_all_devices(module: Union[str, ModuleType, None] = None) -> Dict[str, A
8080

8181
def collect_factories(module: ModuleType) -> Iterable[Callable[..., Any]]:
8282
for var in module.__dict__.values():
83-
if callable(var) and is_device_factory(var):
83+
if callable(var) and _is_device_factory(var):
8484
yield var
8585

8686

87-
def is_device_factory(func: Callable[..., Any]) -> bool:
87+
def _is_device_factory(func: Callable[..., Any]) -> bool:
8888
return_type = signature(func).return_annotation
89-
return is_device_type(return_type)
89+
return _is_device_type(return_type)
9090

9191

92-
def is_device_type(obj: Type[Any]) -> bool:
92+
def _is_device_type(obj: Type[Any]) -> bool:
9393
is_class = inspect.isclass(obj)
9494
follows_protocols = any(
9595
map(lambda protocol: isinstance(obj, protocol), BLUESKY_PROTOCOLS)

tests/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from unittest.mock import patch
2+
3+
from dodal.utils import collect_factories, get_hostname, make_all_devices
4+
5+
6+
def test_finds_device_factories() -> None:
7+
import tests.fake_beamline as fake_beamline
8+
9+
factories = set(collect_factories(fake_beamline))
10+
11+
from tests.fake_beamline import device_a, device_b, device_c
12+
13+
assert {device_a, device_b, device_c} == factories
14+
15+
16+
def test_makes_devices() -> None:
17+
import tests.fake_beamline as fake_beamline
18+
19+
devices = make_all_devices(fake_beamline)
20+
assert {"readable", "motor", "cryo"} == devices.keys()
21+
22+
23+
def test_get_hostname() -> None:
24+
with patch("dodal.utils.socket.gethostname") as mock:
25+
mock.return_value = "a.b.c"
26+
assert get_hostname() == "a"

tests/fake_beamline.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from unittest.mock import MagicMock
2+
3+
from bluesky.protocols import Readable
4+
from ophyd import EpicsMotor
5+
6+
from dodal.devices.cryostream import Cryo
7+
8+
9+
def device_a() -> Readable:
10+
return _mock_with_name("readable")
11+
12+
13+
def device_b() -> EpicsMotor:
14+
return _mock_with_name("motor")
15+
16+
17+
def device_c() -> Cryo:
18+
return _mock_with_name("cryo")
19+
20+
21+
def not_device() -> int:
22+
return 5
23+
24+
25+
def _mock_with_name(name: str) -> MagicMock:
26+
mock = MagicMock()
27+
mock.name = name
28+
return mock

0 commit comments

Comments
 (0)