|
1 | 1 | import os |
2 | | -from unittest.mock import patch |
| 2 | +from unittest.mock import Mock, patch |
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 | from bluesky import RunEngine |
|
13 | 13 |
|
14 | 14 | from dodal import __version__ |
15 | 15 | from dodal.cli import main |
| 16 | +from dodal.device_manager import DeviceManager |
16 | 17 | from dodal.utils import AnyDevice, OphydV1Device, OphydV2Device |
17 | 18 |
|
18 | 19 | # Test with an example beamline, device instantiation is already tested |
@@ -297,6 +298,34 @@ def test_cli_connect_when_devices_error( |
297 | 298 | ) |
298 | 299 |
|
299 | 300 |
|
| 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 | + |
300 | 329 | def _mock_connect( |
301 | 330 | *args, |
302 | 331 | runner: CliRunner, |
|
0 commit comments