Skip to content

Commit 9b9c738

Browse files
committed
Added test for instrument server endpoint to check for existence of multigrid controller for a given session id
1 parent 03799a8 commit 9b9c738

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

tests/instrument_server/test_api.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
from pathlib import Path
22
from typing import Optional
3-
from unittest.mock import ANY, Mock, patch
3+
from unittest.mock import ANY, MagicMock, Mock, patch
44
from urllib.parse import urlparse
55

6+
from fastapi import FastAPI
7+
from fastapi.testclient import TestClient
68
from pytest import mark
9+
from pytest_mock import MockerFixture
710

8-
from murfey.instrument_server.api import (
9-
GainReference,
10-
_get_murfey_url,
11-
upload_gain_reference,
12-
)
11+
from murfey.instrument_server.api import GainReference, _get_murfey_url
12+
from murfey.instrument_server.api import router as client_router
13+
from murfey.instrument_server.api import upload_gain_reference, validate_session_token
1314
from murfey.util import posix_path
1415
from murfey.util.api import url_path_for
1516

17+
18+
def set_up_test_client(session_id: Optional[int] = None):
19+
"""
20+
Helper function to set up a test client for the instrument server with validation
21+
checks disabled.
22+
"""
23+
# Set up the instrument server
24+
client_app = FastAPI()
25+
if session_id:
26+
client_app.dependency_overrides[validate_session_token] = lambda: session_id
27+
client_app.include_router(client_router)
28+
return TestClient(client_app)
29+
30+
1631
test_get_murfey_url_params_matrix = (
1732
# Server URL to use
1833
("default",),
@@ -57,6 +72,24 @@ def test_get_murfey_url(
5772
assert parsed_server.path == parsed_original.path
5873

5974

75+
def test_check_multigrid_controller_exists(mocker: MockerFixture):
76+
session_id = 1
77+
78+
# Patch out the multigrid controllers that have been stored in memory
79+
mocker.patch("murfey.instrument_server.api.controllers", {session_id: MagicMock()})
80+
81+
# Set up the test client
82+
client_server = set_up_test_client(session_id=session_id)
83+
url_path = url_path_for(
84+
"api.router", "check_multigrid_controller_exists", session_id=session_id
85+
)
86+
response = client_server.get(url_path)
87+
88+
# Check that the result is as expected
89+
assert response.status_code == 200
90+
assert response.json() == {"exists": True}
91+
92+
6093
test_upload_gain_reference_params_matrix = (
6194
# Rsync URL settings
6295
("http://1.1.1.1",), # When rsync_url is provided

0 commit comments

Comments
 (0)