Skip to content

Commit 6e29bb8

Browse files
committed
Add tests for device manager use in CLI
1 parent ad4e532 commit 6e29bb8

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

tests/test_cli.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from unittest.mock import patch
2+
from unittest.mock import Mock, patch
33

44
import pytest
55
from bluesky import RunEngine
@@ -13,6 +13,7 @@
1313

1414
from dodal import __version__
1515
from dodal.cli import main
16+
from dodal.device_manager import DeviceManager
1617
from dodal.utils import AnyDevice, OphydV1Device, OphydV2Device
1718

1819
# Test with an example beamline, device instantiation is already tested
@@ -297,6 +298,34 @@ def test_cli_connect_when_devices_error(
297298
)
298299

299300

301+
@patch.dict(os.environ, clear=True)
302+
def test_missing_device_manager(runner: CliRunner):
303+
with patch("dodal.cli.importlib"):
304+
with pytest.raises(
305+
ValueError,
306+
match="Name 'devices' could not be found or is not a DeviceManager",
307+
):
308+
print("Invoking connect")
309+
runner.invoke(
310+
main, ["connect", "-n", "devices", "i22"], catch_exceptions=False
311+
)
312+
print("Ran connect")
313+
314+
315+
@patch.dict(os.environ, clear=True)
316+
@pytest.mark.parametrize("mock", [True, False], ids=["live", "sim"].__getitem__)
317+
def test_device_manager_init(runner: CliRunner, mock: bool):
318+
with patch("dodal.cli.importlib") as mock_import:
319+
dm = mock_import.import_module("dodal.beamlines.i22")
320+
dm.devices = Mock(spec=DeviceManager)
321+
mock_import.reset_mock()
322+
runner.invoke(
323+
main, ["connect", "-n", "devices", "i22"] + (["-s"] if mock else [])
324+
)
325+
mock_import.import_module.assert_called_once_with("dodal.beamlines.i22")
326+
dm.devices.build_and_connect.assert_called_once_with(mock=mock)
327+
328+
300329
def _mock_connect(
301330
*args,
302331
runner: CliRunner,

0 commit comments

Comments
 (0)