Skip to content

Commit 95052b6

Browse files
committed
Added unit test for update checker
1 parent 45864a9 commit 95052b6

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

tests/instrument_server/__init__.py

Whitespace-only changes.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from unittest import mock
2+
from urllib.parse import urlparse
3+
4+
import pytest
5+
from fastapi import FastAPI
6+
from fastapi.testclient import TestClient
7+
8+
import murfey
9+
from murfey.instrument_server import check_for_updates
10+
from murfey.server.api.bootstrap import bootstrap as bootstrap_router
11+
from murfey.server.api.bootstrap import pypi as pypi_router
12+
from murfey.util.api import url_path_for
13+
14+
# Set up a test router with only the essential endpoints
15+
app = FastAPI()
16+
for router in [pypi_router, bootstrap_router]:
17+
app.include_router(router)
18+
client = TestClient(app)
19+
20+
21+
check_for_updates_test_matrix = (
22+
# Downgrade, upgrade, or keep client version?
23+
("downgrade",),
24+
("upgrade",),
25+
("keep",),
26+
)
27+
28+
29+
@pytest.mark.parametrize("test_params", check_for_updates_test_matrix)
30+
def test_check_for_updates(
31+
test_params: tuple[str],
32+
):
33+
34+
# Unpack test params
35+
(handle_client_version,) = test_params
36+
37+
with (
38+
mock.patch("murfey.instrument_server.urlparse") as mock_parse,
39+
mock.patch("murfey.client.update.requests.get") as mock_get,
40+
):
41+
# Return the test client URL
42+
api_base = urlparse("http://testserver", allow_fragments=False)
43+
mock_parse.return_value = api_base
44+
check_for_updates()
45+
46+
# Modify client version as needed
47+
current_version = murfey.__version__
48+
supported_client_version = murfey.__supported_client_version__
49+
50+
# Check that a request was sent to the test_client with the correct URL
51+
proxy_path = api_base.path.rstrip("/")
52+
version_check_url = api_base._replace(
53+
path=f"{proxy_path}{url_path_for('bootstrap.version', 'get_version')}",
54+
query=f"client_version={current_version}",
55+
)
56+
mock_get.assert_any_call(version_check_url.geturl())
57+
58+
# Construct the mock response
59+
mock_response = mock.Mock()
60+
mock_response.json.return_value = {
61+
"server": current_version,
62+
"oldest-supported-client": supported_client_version,
63+
"client-needs-update": True,
64+
"client-needs-downgrade": False,
65+
}
66+
mock_response.status_code = 200
67+
mock_get.return_value = mock_response
68+
69+
pass

0 commit comments

Comments
 (0)