Skip to content

Commit 245ee6a

Browse files
committed
Added unit test for 'upload_gain_reference()' API endpoint
1 parent 360a345 commit 245ee6a

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from typing import Optional
2+
from unittest.mock import Mock, patch
3+
from urllib.parse import urlparse
4+
5+
from fastapi import FastAPI
6+
from fastapi.testclient import TestClient
7+
from pytest import mark
8+
9+
from murfey.instrument_server.api import router
10+
11+
app = FastAPI()
12+
app.include_router(router)
13+
client = TestClient(app)
14+
15+
16+
test_upload_gain_reference_params_matrix = (
17+
# Rsync URL | Rsync module | Gain reference directory |
18+
(
19+
"http://1.1.1.1",
20+
"data",
21+
"/c/ProgramData/Gatan/Gain Reference",
22+
), # When rsync_url is provided
23+
(
24+
"",
25+
"data",
26+
"/c/ProgramData/Gatan/Gain Reference",
27+
), # When rsync_url is blank
28+
(
29+
None,
30+
"data",
31+
"/c/ProgramData/Gatan/Gain Reference",
32+
), # When rsync_url not provided
33+
)
34+
35+
36+
@mark.parametrize("test_params", test_upload_gain_reference_params_matrix)
37+
@patch("murfey.instrument_server.api.subprocess.run")
38+
@patch("murfey.instrument_server.api.urlparse", wraps=urlparse)
39+
@patch("murfey.instrument_server.api._get_murfey_url")
40+
@patch("murfey.instrument_server.api.requests.get")
41+
def test_upload_gain_reference(
42+
mock_get,
43+
mock_server_url,
44+
spy_parse,
45+
mock_run,
46+
test_params: tuple[Optional[str], str, str],
47+
):
48+
49+
# Create a mock machine config base on the test params
50+
rsync_url, rsync_module, gain_ref_dir = test_params
51+
server_url = "http://0.0.0.0:8000"
52+
mock_machine_config = {
53+
"rsync_module": rsync_module,
54+
"gain_reference_directory": gain_ref_dir,
55+
}
56+
if rsync_url is not None:
57+
mock_machine_config["rsync_url"] = rsync_url
58+
59+
# Assign expected values to the mock objects
60+
mock_get.return_value = Mock(status_code=200, json=lambda: mock_machine_config)
61+
mock_server_url.return_value = server_url
62+
mock_run.return_value = Mock(returncode=0)
63+
64+
# Construct payload and submit post request
65+
payload = {
66+
"gain_path": f"{gain_ref_dir}/gain.mrc",
67+
"visit_path": "2025/aa00000-0",
68+
"gain_destination_dir": "processing",
69+
}
70+
response = client.post(
71+
"/instruments/m02/session/1/upload_gain_reference", json=payload
72+
)
73+
74+
# Check that the machine config request was called
75+
mock_get.assert_called_once()
76+
77+
# If no rsync_url key is provided, or rsync_url key is empty,
78+
# This should default to the Murfey URL
79+
if not rsync_url:
80+
assert spy_parse.return_value == urlparse(server_url)
81+
else:
82+
assert spy_parse.return_value == urlparse(rsync_url)
83+
84+
# Check that the subprocess was run
85+
mock_run.assert_called_once()
86+
87+
# Check that the endpoint function ran through to completion successfully
88+
assert response.status_code == 200
89+
assert response.json() == {"success": True}

0 commit comments

Comments
 (0)