Skip to content

Commit 73beba4

Browse files
committed
Try testing the function by itself, and not as a FastAPI call
1 parent 2a489e7 commit 73beba4

File tree

1 file changed

+49
-51
lines changed

1 file changed

+49
-51
lines changed
Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,91 @@
1+
from pathlib import Path
12
from typing import Optional
23
from unittest.mock import Mock, patch
34
from urllib.parse import urlparse
45

5-
from fastapi import FastAPI
6-
from fastapi.testclient import TestClient
76
from pytest import mark
87

9-
from murfey.instrument_server.api import router
10-
11-
app = FastAPI()
12-
app.include_router(router)
13-
client = TestClient(app)
14-
8+
from murfey.instrument_server.api import GainReference, upload_gain_reference
9+
from murfey.util import posix_path
1510

1611
test_upload_gain_reference_params_matrix = (
17-
# Rsync URL | Rsync module | Gain reference directory |
18-
(
19-
"http://1.1.1.1", # When rsync_url is provided
20-
"data",
21-
"/c/ProgramData/Gatan/Gain Reference",
22-
),
23-
(
24-
"", # When rsync_url is blank
25-
"data",
26-
"/c/ProgramData/Gatan/Gain Reference",
27-
),
28-
(
29-
None, # When rsync_url not provided
30-
"data",
31-
"/c/ProgramData/Gatan/Gain Reference",
32-
),
12+
# Rsync URL
13+
("http://1.1.1.1",), # When rsync_url is provided
14+
("",), # When rsync_url is blank
15+
(None,), # When rsync_url not provided
3316
)
3417

3518

3619
@mark.parametrize("test_params", test_upload_gain_reference_params_matrix)
3720
@patch("murfey.instrument_server.api.subprocess")
38-
@patch("murfey.instrument_server.api.urlparse", wraps=urlparse)
3921
@patch("murfey.instrument_server.api._get_murfey_url")
4022
@patch("murfey.instrument_server.api.requests")
4123
def test_upload_gain_reference(
4224
mock_request,
4325
mock_get_server_url,
44-
spy_parse,
4526
mock_subprocess,
4627
test_params: tuple[Optional[str], str, str],
4728
):
4829

4930
# Create a mock machine config base on the test params
50-
rsync_url, rsync_module, gain_ref_dir = test_params
31+
rsync_url_setting, rsync_module, gain_ref_dir = test_params
5132
server_url = "http://0.0.0.0:8000"
33+
rsync_module = "data"
34+
gain_ref_dir = "C:/ProgramData/Gatan/Gain Reference"
5235
mock_machine_config = {
5336
"rsync_module": rsync_module,
5437
"gain_reference_directory": gain_ref_dir,
5538
}
56-
if rsync_url is not None:
57-
mock_machine_config["rsync_url"] = rsync_url
39+
if rsync_url_setting is not None:
40+
mock_machine_config["rsync_url"] = rsync_url_setting
5841

5942
# Assign expected values to the mock objects
6043
mock_response = Mock()
6144
mock_response.status_code = 200
6245
mock_response.json.return_value = mock_machine_config
6346
mock_request.get.return_value = mock_response
6447
mock_get_server_url.return_value = server_url
65-
mock_subprocess.run.return_value = Mock(returncode=0)
48+
mock_subprocess.run.return_value = Mock(
49+
returncode=0, stderr="An error has occurred."
50+
)
6651

67-
# Construct payload and submit post request
52+
# Construct payload and pass request to function
53+
gain_ref_file = f"{gain_ref_dir}/gain.mrc"
54+
visit_path = "2025/aa00000-0"
55+
gain_dest_dir = "processing"
6856
payload = {
69-
"gain_path": f"{gain_ref_dir}/gain.mrc",
70-
"visit_path": "2025/aa00000-0",
71-
"gain_destination_dir": "processing",
57+
"gain_path": gain_ref_file,
58+
"visit_path": visit_path,
59+
"gain_destination_dir": gain_dest_dir,
7260
}
73-
response = client.post(
74-
"/instruments/m02/sessions/1/upload_gain_reference", json=payload
61+
result = upload_gain_reference(
62+
instrument_name="murfey",
63+
session_id=1,
64+
gain_reference=GainReference(
65+
**payload,
66+
),
7567
)
7668

77-
# # Check that the machine config request was called
78-
# mock_request.get.assert_called_once()
69+
# Check that the machine config request was called
70+
mock_request.get.assert_called_once()
7971

80-
# # If no rsync_url key is provided, or rsync_url key is empty,
81-
# # This should default to the Murfey URL
82-
# returned_urlparse = spy_parse.return_value
83-
# expected_urlparse = urlparse(server_url) if not rsync_url else urlparse(rsync_url)
84-
# assert expected_urlparse.scheme == returned_urlparse.scheme
85-
# assert expected_urlparse.netloc == returned_urlparse.netloc
86-
# assert expected_urlparse.path == returned_urlparse.path
87-
88-
# # Check that the subprocess was run
89-
# mock_subprocess.run.assert_called_once()
72+
# Check that the subprocess was run with the expected arguments
73+
# If no rsync_url key is provided, or rsync_url key is empty,
74+
# It should default to the server URL
75+
expected_rsync_url = (
76+
urlparse(server_url) if not rsync_url_setting else urlparse(rsync_url_setting)
77+
)
78+
expected_rsync_path = f"{expected_rsync_url.hostname}::{rsync_module}/{visit_path}/{gain_dest_dir}/gain.mrc"
79+
expected_rsync_cmd = [
80+
"rsync",
81+
posix_path(Path(gain_ref_file)),
82+
expected_rsync_path,
83+
]
84+
mock_subprocess.run.assert_called_once_with(
85+
expected_rsync_cmd,
86+
capture_output=True,
87+
text=True,
88+
)
9089

91-
# Check that the endpoint function ran through to completion successfully
92-
assert response.status_code == 200
93-
assert response.json() == {"success": True}
90+
# Check that the function ran through to completion successfully
91+
assert result == {"success": True}

0 commit comments

Comments
 (0)