Skip to content

Commit 76a4911

Browse files
committed
Added unit test to check that instrument server starts correctly
1 parent 72945e2 commit 76a4911

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

tests/instrument_server/test_init.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import sys
2+
from typing import Optional
13
from urllib.parse import urlparse
24

35
import pytest
6+
import uvicorn
47
from fastapi import FastAPI
58
from fastapi.testclient import TestClient
69
from packaging.version import Version
710
from pytest_mock import MockerFixture
811

912
import murfey
1013
from murfey.client.update import UPDATE_SUCCESS
11-
from murfey.instrument_server import check_for_updates
14+
from murfey.instrument_server import check_for_updates, start_instrument_server
1215
from murfey.server.api.bootstrap import pypi as pypi_router
1316
from murfey.server.api.bootstrap import version as version_router
1417
from murfey.util.api import url_path_for
@@ -20,6 +23,7 @@
2023
client = TestClient(app)
2124
base_url = str(client.base_url)
2225

26+
2327
check_for_updates_test_matrix = (
2428
# Downgrade, upgrade, or keep client version?
2529
("downgrade",),
@@ -113,3 +117,65 @@ def test_check_for_updates(
113117

114118
# Check that the query URL is correct
115119
mock_get.assert_called_once_with(version_check_url.geturl())
120+
121+
122+
start_instrument_server_test_matrix = (
123+
# Host | Port
124+
(
125+
None,
126+
None,
127+
), # Test default values
128+
(
129+
"127.0.0.1",
130+
8000,
131+
), # Test manually included values
132+
)
133+
134+
135+
@pytest.mark.parametrize("test_params", start_instrument_server_test_matrix)
136+
def test_start_instrument_server(
137+
mocker: MockerFixture, test_params: tuple[Optional[str], Optional[int]]
138+
):
139+
140+
# Unpack test params
141+
host, port = test_params
142+
143+
# Patch the Uvicorn Server instance
144+
mock_server = mocker.patch("uvicorn.Server")
145+
# Disable 'run'; we just want to confirm it's called correctly
146+
mock_server.run.return_value = lambda: None
147+
148+
# Patch the websocket instance
149+
mock_wsapp = mocker.patch("murfey.client.websocket.WSApp")
150+
mock_wsapp.return_value = mocker.Mock() # Disable functionality
151+
152+
# Construct the expected Uvicorn Config object and save it as a dict
153+
expected_config = vars(
154+
uvicorn.Config(
155+
"murfey.instrument_server.main:app",
156+
host=host if host is not None else "0.0.0.0",
157+
port=port if port is not None else 8001,
158+
log_config=None,
159+
ws_ping_interval=300,
160+
ws_ping_timeout=300,
161+
)
162+
)
163+
164+
# Construct the arguments to pass to the instrument server
165+
sys.argv = [
166+
"murfey.instrument_server",
167+
]
168+
169+
# Add host and port if they're present
170+
if host is not None:
171+
sys.argv.extend(["--host", host])
172+
if port is not None:
173+
sys.argv.extend(["--port", str(port)])
174+
175+
# Run the function
176+
start_instrument_server()
177+
178+
# Check that the server was called with the correct arguments
179+
args, kwargs = mock_server.call_args
180+
actual_config = vars(kwargs["config"])
181+
assert expected_config == actual_config

0 commit comments

Comments
 (0)