|
| 1 | +import sys |
| 2 | +from typing import Optional |
1 | 3 | from urllib.parse import urlparse |
2 | 4 |
|
3 | 5 | import pytest |
| 6 | +import uvicorn |
4 | 7 | from fastapi import FastAPI |
5 | 8 | from fastapi.testclient import TestClient |
6 | 9 | from packaging.version import Version |
7 | 10 | from pytest_mock import MockerFixture |
8 | 11 |
|
9 | 12 | import murfey |
10 | 13 | 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 |
12 | 15 | from murfey.server.api.bootstrap import pypi as pypi_router |
13 | 16 | from murfey.server.api.bootstrap import version as version_router |
14 | 17 | from murfey.util.api import url_path_for |
|
20 | 23 | client = TestClient(app) |
21 | 24 | base_url = str(client.base_url) |
22 | 25 |
|
| 26 | + |
23 | 27 | check_for_updates_test_matrix = ( |
24 | 28 | # Downgrade, upgrade, or keep client version? |
25 | 29 | ("downgrade",), |
@@ -113,3 +117,65 @@ def test_check_for_updates( |
113 | 117 |
|
114 | 118 | # Check that the query URL is correct |
115 | 119 | 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