|
| 1 | +from pathlib import Path |
1 | 2 | from typing import Optional |
2 | 3 | from unittest.mock import Mock, patch |
3 | 4 | from urllib.parse import urlparse |
4 | 5 |
|
5 | | -from fastapi import FastAPI |
6 | | -from fastapi.testclient import TestClient |
7 | 6 | from pytest import mark |
8 | 7 |
|
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 |
15 | 10 |
|
16 | 11 | 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 |
33 | 16 | ) |
34 | 17 |
|
35 | 18 |
|
36 | 19 | @mark.parametrize("test_params", test_upload_gain_reference_params_matrix) |
37 | 20 | @patch("murfey.instrument_server.api.subprocess") |
38 | | -@patch("murfey.instrument_server.api.urlparse", wraps=urlparse) |
39 | 21 | @patch("murfey.instrument_server.api._get_murfey_url") |
40 | 22 | @patch("murfey.instrument_server.api.requests") |
41 | 23 | def test_upload_gain_reference( |
42 | 24 | mock_request, |
43 | 25 | mock_get_server_url, |
44 | | - spy_parse, |
45 | 26 | mock_subprocess, |
46 | 27 | test_params: tuple[Optional[str], str, str], |
47 | 28 | ): |
48 | 29 |
|
49 | 30 | # 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 |
51 | 32 | server_url = "http://0.0.0.0:8000" |
| 33 | + rsync_module = "data" |
| 34 | + gain_ref_dir = "C:/ProgramData/Gatan/Gain Reference" |
52 | 35 | mock_machine_config = { |
53 | 36 | "rsync_module": rsync_module, |
54 | 37 | "gain_reference_directory": gain_ref_dir, |
55 | 38 | } |
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 |
58 | 41 |
|
59 | 42 | # Assign expected values to the mock objects |
60 | 43 | mock_response = Mock() |
61 | 44 | mock_response.status_code = 200 |
62 | 45 | mock_response.json.return_value = mock_machine_config |
63 | 46 | mock_request.get.return_value = mock_response |
64 | 47 | 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 | + ) |
66 | 51 |
|
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" |
68 | 56 | 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, |
72 | 60 | } |
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 | + ), |
75 | 67 | ) |
76 | 68 |
|
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() |
79 | 71 |
|
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 | + ) |
90 | 89 |
|
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