|
| 1 | +from unittest.mock import Mock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from dodal.plans.wrapped import count |
| 5 | + |
| 6 | +from blueapi.client.client import BlueapiClient |
| 7 | +from blueapi.client.event_bus import EventBusClient |
| 8 | +from blueapi.client.rest import BlueapiRestClient |
| 9 | +from blueapi.client.user_client import UserClient |
| 10 | +from blueapi.service.model import DeviceResponse, PlanResponse |
| 11 | + |
| 12 | +BLUEAPI_CONFIG_PATH = ( |
| 13 | + "/workspaces/blueapi/tests/unit_tests/valid_example_config/client.yaml" |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class MockDevice: |
| 18 | + def __init__(self, device: str): |
| 19 | + self.device = device |
| 20 | + self.name = device |
| 21 | + |
| 22 | + |
| 23 | +class MockResponse: |
| 24 | + def __init__(self, devices: list): |
| 25 | + self.devices = devices |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(autouse=True) |
| 29 | +def client(): |
| 30 | + client = UserClient(BLUEAPI_CONFIG_PATH, "cm12345-1", callback=True) |
| 31 | + |
| 32 | + client._rest = Mock(BlueapiRestClient) |
| 33 | + client._events = Mock(EventBusClient) |
| 34 | + |
| 35 | + client._events.__enter__ = Mock(return_value=client._events) |
| 36 | + client._events.__exit__ = Mock(return_value=None) |
| 37 | + |
| 38 | + return client |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture(autouse=True) |
| 42 | +def client_without_callback(): |
| 43 | + client_without_callback = UserClient( |
| 44 | + BLUEAPI_CONFIG_PATH, "cm12345-1", callback=False |
| 45 | + ) |
| 46 | + |
| 47 | + return client_without_callback |
| 48 | + |
| 49 | + |
| 50 | +def test_blueapi_python_client(client: UserClient): |
| 51 | + assert isinstance(client, BlueapiClient) |
| 52 | + assert isinstance(client, UserClient) |
| 53 | + |
| 54 | + |
| 55 | +def test_blueapi_python_client_change_session(client: UserClient): |
| 56 | + new_session = "cm54321-1" |
| 57 | + client.change_session(new_session) |
| 58 | + assert client.instrument_session == new_session |
| 59 | + |
| 60 | + |
| 61 | +def test_blueapi_python_client_run(client: UserClient): |
| 62 | + # Patch instance methods so run executes but no re calls happen. |
| 63 | + with ( |
| 64 | + patch.object(client, "run_task", return_value=Mock()), |
| 65 | + patch.object( |
| 66 | + client, "create_and_start_task", return_value=Mock(task_id="t-fake") |
| 67 | + ), |
| 68 | + patch.object(client, "create_task", return_value=Mock(task_id="t-fake")), |
| 69 | + patch.object(client, "start_task", return_value=Mock(task_id="t-fake")), |
| 70 | + ): |
| 71 | + assert client._events is not None |
| 72 | + # Ensure the mocked event client can be used as a context manager if run uses it |
| 73 | + client._events.__enter__ = Mock(return_value=client._events) |
| 74 | + client._events.__exit__ = Mock(return_value=None) |
| 75 | + |
| 76 | + # Call run while the instance methods are patched |
| 77 | + client.run(count) |
| 78 | + client.run("count") |
| 79 | + |
| 80 | + |
| 81 | +def test_blueapi_python_client_without_callback_run( |
| 82 | + client_without_callback: UserClient, |
| 83 | +): |
| 84 | + # Patch instance methods so run executes but no calls happen |
| 85 | + with ( |
| 86 | + patch.object(client_without_callback, "run_task", return_value=Mock()), |
| 87 | + patch.object( |
| 88 | + client_without_callback, |
| 89 | + "create_and_start_task", |
| 90 | + return_value=Mock(task_id="t-fake"), |
| 91 | + ), |
| 92 | + patch.object( |
| 93 | + client_without_callback, "create_task", return_value=Mock(task_id="t-fake") |
| 94 | + ), |
| 95 | + patch.object( |
| 96 | + client_without_callback, "start_task", return_value=Mock(task_id="t-fake") |
| 97 | + ), |
| 98 | + ): |
| 99 | + # Ensure the mocked event client can be used as a context manager if run uses it |
| 100 | + client_without_callback._events = Mock(EventBusClient) |
| 101 | + client_without_callback._events.__enter__ = Mock( |
| 102 | + return_value=client_without_callback._events |
| 103 | + ) |
| 104 | + client_without_callback._events.__exit__ = Mock(return_value=None) |
| 105 | + |
| 106 | + client_without_callback.run(count) |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.parametrize( |
| 110 | + "plan, args, kwargs", |
| 111 | + ( |
| 112 | + ["plan", (), {}], |
| 113 | + [count, ["det1", "det2"], {}], |
| 114 | + [count, ["det1", "det2"], {"num": 2}], |
| 115 | + [count, (), {"detectors": ["det1", "det2"]}], |
| 116 | + ), |
| 117 | +) |
| 118 | +def test_run_with_valid_paraneters(client: UserClient, plan, args: tuple, kwargs: dict): |
| 119 | + # Patch instance methods so run executes but no re calls happen. |
| 120 | + with ( |
| 121 | + patch.object(client, "run_task", return_value=Mock()), |
| 122 | + patch.object( |
| 123 | + client, "create_and_start_task", return_value=Mock(task_id="t-fake") |
| 124 | + ), |
| 125 | + patch.object(client, "create_task", return_value=Mock(task_id="t-fake")), |
| 126 | + patch.object(client, "start_task", return_value=Mock(task_id="t-fake")), |
| 127 | + ): |
| 128 | + assert client._events is not None |
| 129 | + # Ensure the mocked event client can be used as a context manager if run uses it |
| 130 | + client._events.__enter__ = Mock(return_value=client._events) |
| 131 | + client._events.__exit__ = Mock(return_value=None) |
| 132 | + |
| 133 | + client.run(plan, *args, **kwargs) |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.parametrize( |
| 137 | + "plan, args, kwargs", |
| 138 | + ( |
| 139 | + [None, (), {}], |
| 140 | + ["plan", ["det1", "det2"], {}], |
| 141 | + ["plan", ["det1", "det2"], {"num": 2}], |
| 142 | + ), |
| 143 | +) |
| 144 | +def test_run_fails_with_invalid_parameters( |
| 145 | + client: UserClient, plan, args: tuple, kwargs: dict |
| 146 | +): |
| 147 | + # Patch instance methods so run executes but no re calls happen. |
| 148 | + with ( |
| 149 | + patch.object(client, "run_task", return_value=Mock()), |
| 150 | + patch.object( |
| 151 | + client, "create_and_start_task", return_value=Mock(task_id="t-fake") |
| 152 | + ), |
| 153 | + patch.object(client, "create_task", return_value=Mock(task_id="t-fake")), |
| 154 | + patch.object(client, "start_task", return_value=Mock(task_id="t-fake")), |
| 155 | + ): |
| 156 | + assert client._events is not None |
| 157 | + # Ensure the mocked event client can be used as a context manager if run uses it |
| 158 | + client._events.__enter__ = Mock(return_value=client._events) |
| 159 | + client._events.__exit__ = Mock(return_value=None) |
| 160 | + |
| 161 | + # Call run while the instance methods are patched |
| 162 | + with pytest.raises(ValueError): # noqa |
| 163 | + client.run(plan, *args, **kwargs) |
| 164 | + |
| 165 | + |
| 166 | +def test_return_detectors(client: UserClient): |
| 167 | + # Mock the expected detector list response |
| 168 | + |
| 169 | + # Create a method mock for get_detectors |
| 170 | + client.get_devices = Mock( |
| 171 | + DeviceResponse, |
| 172 | + return_value=MockResponse([MockDevice("det1"), MockDevice("det2")]), |
| 173 | + ) |
| 174 | + |
| 175 | + # Call the method under test |
| 176 | + result = client.return_detectors() |
| 177 | + |
| 178 | + # Verify the result matches our expected data |
| 179 | + |
| 180 | + # Verify the REST client was called correctly |
| 181 | + client.get_devices.assert_called_once() |
| 182 | + |
| 183 | + assert isinstance(result, list) |
| 184 | + |
| 185 | + |
| 186 | +def test_show_devices(client: UserClient): |
| 187 | + # Create a method mock for get_detectors |
| 188 | + client.get_devices = Mock( |
| 189 | + DeviceResponse, |
| 190 | + return_value=MockResponse([MockDevice("det1"), MockDevice("det2")]), |
| 191 | + ) |
| 192 | + |
| 193 | + client.show_devices() |
| 194 | + client.get_devices.assert_called_once() |
| 195 | + |
| 196 | + |
| 197 | +class MockPlan: |
| 198 | + def __init__(self, device: str): |
| 199 | + self.name = device |
| 200 | + |
| 201 | + |
| 202 | +class MockPlanResponse: |
| 203 | + def __init__(self, plans: list): |
| 204 | + self.plans = plans |
| 205 | + |
| 206 | + |
| 207 | +def test_show_plans(client: UserClient): |
| 208 | + # Create a method mock for get_detectors |
| 209 | + client.get_plans = Mock( |
| 210 | + PlanResponse, |
| 211 | + return_value=MockPlanResponse([MockPlan("count"), MockPlan("test")]), |
| 212 | + ) |
| 213 | + |
| 214 | + client.show_plans() |
| 215 | + client.get_plans.assert_called_once() |
0 commit comments