|
1 | 1 | import uuid |
2 | 2 | from pathlib import Path |
| 3 | +from typing import Any |
3 | 4 | from unittest.mock import MagicMock, Mock, patch |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 | import requests |
7 | 8 | import responses |
8 | 9 | from packaging.version import Version |
| 10 | +from responses import DELETE, GET, PUT, matchers |
9 | 11 |
|
10 | 12 | from blueapi import __version__ |
11 | 13 | from blueapi.client.rest import ( |
|
20 | 22 | ) |
21 | 23 | from blueapi.config import OIDCConfig |
22 | 24 | from blueapi.service.authentication import SessionCacheManager, SessionManager |
23 | | -from blueapi.service.model import EnvironmentResponse |
| 25 | +from blueapi.service.model import ( |
| 26 | + DeviceModel, |
| 27 | + EnvironmentResponse, |
| 28 | + PlanModel, |
| 29 | + TaskResponse, |
| 30 | + TasksListResponse, |
| 31 | + WorkerTask, |
| 32 | +) |
| 33 | +from blueapi.worker.event import WorkerState |
| 34 | +from blueapi.worker.task import Task |
| 35 | +from blueapi.worker.task_worker import TrackableTask |
24 | 36 |
|
25 | 37 |
|
26 | 38 | @pytest.fixture |
@@ -231,3 +243,116 @@ def test_server_and_client_versions( |
231 | 243 | ) |
232 | 244 | else: |
233 | 245 | mock_logger.assert_not_called() |
| 246 | + |
| 247 | + |
| 248 | +@pytest.mark.parametrize( |
| 249 | + "method_name,args,http_method,path,response,result", |
| 250 | + [ |
| 251 | + ( |
| 252 | + "get_plan", |
| 253 | + ("foo",), |
| 254 | + GET, |
| 255 | + "/plans/foo", |
| 256 | + '{"name": "foo"}', |
| 257 | + PlanModel(name="foo"), |
| 258 | + ), |
| 259 | + ( |
| 260 | + "get_device", |
| 261 | + ("foo",), |
| 262 | + GET, |
| 263 | + "/devices/foo", |
| 264 | + '{"name": "foo", "protocols": []}', |
| 265 | + DeviceModel(name="foo", protocols=[]), |
| 266 | + ), |
| 267 | + ( |
| 268 | + "get_task", |
| 269 | + ("foo",), |
| 270 | + GET, |
| 271 | + "/tasks/foo", |
| 272 | + '{"task_id": "foo", "task": {"name": "bar"}}', |
| 273 | + TrackableTask(task_id="foo", task=Task(name="bar")), |
| 274 | + ), |
| 275 | + ( |
| 276 | + "get_all_tasks", |
| 277 | + (), |
| 278 | + GET, |
| 279 | + "/tasks", |
| 280 | + '{"tasks": [{"task_id": "foo", "task": {"name": "bar"}}]}', |
| 281 | + TasksListResponse( |
| 282 | + tasks=[TrackableTask(task_id="foo", task=Task(name="bar"))] |
| 283 | + ), |
| 284 | + ), |
| 285 | + ( |
| 286 | + "get_active_task", |
| 287 | + (), |
| 288 | + GET, |
| 289 | + "/worker/task", |
| 290 | + '{"task_id": "foo"}', |
| 291 | + WorkerTask(task_id="foo"), |
| 292 | + ), |
| 293 | + ( |
| 294 | + "clear_task", |
| 295 | + ("foo",), |
| 296 | + DELETE, |
| 297 | + "/tasks/foo", |
| 298 | + '{"task_id": "foo"}', |
| 299 | + TaskResponse(task_id="foo"), |
| 300 | + ), |
| 301 | + ], |
| 302 | +) |
| 303 | +@responses.activate |
| 304 | +def test_individual_endpoints( |
| 305 | + rest: BlueapiRestClient, |
| 306 | + # input args |
| 307 | + method_name: str, |
| 308 | + args: tuple[Any], |
| 309 | + # setup args |
| 310 | + http_method: str, |
| 311 | + path: str, |
| 312 | + response: str, |
| 313 | + result: Any, |
| 314 | +): |
| 315 | + responses.add(http_method, "http://localhost:8000" + path, body=response) |
| 316 | + |
| 317 | + method = getattr(rest, method_name) |
| 318 | + actual = method(*args) |
| 319 | + assert actual == result |
| 320 | + |
| 321 | + |
| 322 | +@pytest.mark.parametrize( |
| 323 | + "method_name,args,data,response,result", |
| 324 | + [ |
| 325 | + ( |
| 326 | + "set_state", |
| 327 | + (WorkerState.PAUSED,), |
| 328 | + {"new_state": "PAUSED", "defer": False}, |
| 329 | + "PAUSED", |
| 330 | + WorkerState.PAUSED, |
| 331 | + ), |
| 332 | + ( |
| 333 | + "cancel_current_task", |
| 334 | + (WorkerState.ABORTING, "no reason"), |
| 335 | + {"new_state": "ABORTING", "reason": "no reason"}, |
| 336 | + "ABORTING", |
| 337 | + WorkerState.ABORTING, |
| 338 | + ), |
| 339 | + ], |
| 340 | +) |
| 341 | +@responses.activate |
| 342 | +def test_set_state( |
| 343 | + rest: BlueapiRestClient, |
| 344 | + method_name: str, |
| 345 | + args: tuple[Any], |
| 346 | + data: Any, |
| 347 | + response: str, |
| 348 | + result: Any, |
| 349 | +): |
| 350 | + responses.add( |
| 351 | + PUT, |
| 352 | + "http://localhost:8000/worker/state", |
| 353 | + match=[matchers.json_params_matcher(data)], |
| 354 | + json=response, |
| 355 | + ) |
| 356 | + method = getattr(rest, method_name) |
| 357 | + res = method(*args) |
| 358 | + assert res == result |
0 commit comments