11from pathlib import Path
22from typing import Optional
3- from unittest .mock import ANY , Mock , patch
3+ from unittest .mock import ANY , MagicMock , patch
44from urllib .parse import urlparse
55
6- from pytest import mark
6+ import pytest
7+ from fastapi import FastAPI
8+ from fastapi .testclient import TestClient
9+ from pytest_mock import MockerFixture
710
8- from murfey .instrument_server .api import (
9- GainReference ,
10- _get_murfey_url ,
11- upload_gain_reference ,
12- )
11+ from murfey .instrument_server .api import _get_murfey_url
12+ from murfey .instrument_server .api import router as client_router
13+ from murfey .instrument_server .api import validate_session_token
1314from murfey .util import posix_path
1415from murfey .util .api import url_path_for
1516
17+
18+ def set_up_test_client (session_id : Optional [int ] = None ):
19+ """
20+ Helper function to set up a test client for the instrument server with validation
21+ checks disabled.
22+ """
23+ # Set up the instrument server
24+ client_app = FastAPI ()
25+ if session_id :
26+ client_app .dependency_overrides [validate_session_token ] = lambda : session_id
27+ client_app .include_router (client_router )
28+ return TestClient (client_app )
29+
30+
1631test_get_murfey_url_params_matrix = (
1732 # Server URL to use
1833 ("default" ,),
2338)
2439
2540
26- @mark .parametrize ("test_params" , test_get_murfey_url_params_matrix )
41+ @pytest . mark .parametrize ("test_params" , test_get_murfey_url_params_matrix )
2742def test_get_murfey_url (
2843 test_params : tuple [str ],
2944 mock_client_configuration , # From conftest.py
@@ -57,6 +72,24 @@ def test_get_murfey_url(
5772 assert parsed_server .path == parsed_original .path
5873
5974
75+ def test_check_multigrid_controller_exists (mocker : MockerFixture ):
76+ session_id = 1
77+
78+ # Patch out the multigrid controllers that have been stored in memory
79+ mocker .patch ("murfey.instrument_server.api.controllers" , {session_id : MagicMock ()})
80+
81+ # Set up the test client
82+ client_server = set_up_test_client (session_id = session_id )
83+ url_path = url_path_for (
84+ "api.router" , "check_multigrid_controller_exists" , session_id = session_id
85+ )
86+ response = client_server .get (url_path )
87+
88+ # Check that the result is as expected
89+ assert response .status_code == 200
90+ assert response .json () == {"exists" : True }
91+
92+
6093test_upload_gain_reference_params_matrix = (
6194 # Rsync URL settings
6295 ("http://1.1.1.1" ,), # When rsync_url is provided
@@ -65,25 +98,23 @@ def test_get_murfey_url(
6598)
6699
67100
68- @mark .parametrize ("test_params" , test_upload_gain_reference_params_matrix )
69- @patch ("murfey.instrument_server.api.subprocess" )
70- @patch ("murfey.instrument_server.api.tokens" )
71- @patch ("murfey.instrument_server.api._get_murfey_url" )
72- @patch ("murfey.instrument_server.api.requests" )
101+ @pytest .mark .parametrize ("test_params" , test_upload_gain_reference_params_matrix )
73102def test_upload_gain_reference (
74- mock_request ,
75- mock_get_server_url ,
76- mock_tokens ,
77- mock_subprocess ,
103+ mocker : MockerFixture ,
78104 test_params : tuple [Optional [str ]],
79105):
80-
81106 # Unpack test parameters and define other ones
82107 (rsync_url_setting ,) = test_params
83- server_url = "http ://0.0.0.0:8000 "
108+ server_url = "https ://murfey.server.test "
84109 instrument_name = "murfey"
85110 session_id = 1
86111
112+ # Mock out objects
113+ mock_request = mocker .patch ("murfey.instrument_server.api.requests" )
114+ mock_get_server_url = mocker .patch ("murfey.instrument_server.api._get_murfey_url" )
115+ mock_subprocess = mocker .patch ("murfey.instrument_server.api.subprocess" )
116+ mocker .patch ("murfey.instrument_server.api.tokens" , {session_id : ANY })
117+
87118 # Create a mock machine config base on the test params
88119 rsync_module = "data"
89120 gain_ref_dir = "C:/ProgramData/Gatan/Gain Reference"
@@ -95,12 +126,12 @@ def test_upload_gain_reference(
95126 mock_machine_config ["rsync_url" ] = rsync_url_setting
96127
97128 # Assign expected values to the mock objects
98- mock_response = Mock ()
129+ mock_response = MagicMock ()
99130 mock_response .status_code = 200
100131 mock_response .json .return_value = mock_machine_config
101132 mock_request .get .return_value = mock_response
102133 mock_get_server_url .return_value = server_url
103- mock_subprocess .run .return_value = Mock (returncode = 0 )
134+ mock_subprocess .run .return_value = MagicMock (returncode = 0 )
104135
105136 # Construct payload and pass request to function
106137 gain_ref_file = f"{ gain_ref_dir } /gain.mrc"
@@ -111,13 +142,18 @@ def test_upload_gain_reference(
111142 "visit_path" : visit_path ,
112143 "gain_destination_dir" : gain_dest_dir ,
113144 }
114- result = upload_gain_reference (
145+
146+ # Set up instrument server test client
147+ client_server = set_up_test_client (session_id = session_id )
148+
149+ # Poke the endpoint with the expected data
150+ url_path = url_path_for (
151+ "api.router" ,
152+ "upload_gain_reference" ,
115153 instrument_name = instrument_name ,
116154 session_id = session_id ,
117- gain_reference = GainReference (
118- ** payload ,
119- ),
120155 )
156+ response = client_server .post (url_path , json = payload )
121157
122158 # Check that the machine config request was called
123159 machine_config_url = f"{ server_url } { url_path_for ('session_control.router' , 'machine_info_by_instrument' , instrument_name = instrument_name )} "
@@ -145,4 +181,4 @@ def test_upload_gain_reference(
145181 )
146182
147183 # Check that the function ran through to completion successfully
148- assert result == {"success" : True }
184+ assert response . json () == {"success" : True }
0 commit comments