11import json
22import os
33from pathlib import Path
4- from unittest .mock import Mock , patch
5- from urllib .parse import urlparse
4+ from unittest import mock
65
7- from pytest import mark
6+ import pytest
87
9- from murfey .client import _get_visit_list
108from murfey .util .client import read_config , set_default_acquisition_output
11- from murfey .util .models import Visit
129
1310test_read_config_params_matrix = (
1411 # Environment variable to set | Append to tmp_path
2724)
2825
2926
30- @mark .parametrize ("test_params" , test_read_config_params_matrix )
27+ @pytest . mark .parametrize ("test_params" , test_read_config_params_matrix )
3128def test_read_config (
3229 test_params : tuple [str , str ],
3330 tmp_path ,
@@ -53,72 +50,13 @@ def test_read_config(
5350 mock_client_configuration .write (file )
5451
5552 # Patch the OS environment variable and run the function
56- with patch .dict (os .environ , env_var_dict , clear = False ):
53+ with mock . patch .dict (os .environ , env_var_dict , clear = False ):
5754 config = read_config ()
5855
5956 # Compare returned config with mock one
6057 assert dict (config ["Murfey" ]) == dict (mock_client_configuration ["Murfey" ])
6158
6259
63- test_get_visit_list_params_matrix = (
64- ("http://0.0.0.0:8000" ,),
65- ("http://0.0.0.0:8000/api" ,),
66- ("http://murfey_server" ,),
67- ("http://murfey_server/api" ,),
68- ("http://murfey_server.com" ,),
69- )
70-
71-
72- @mark .parametrize ("test_params" , test_get_visit_list_params_matrix )
73- @patch ("murfey.client.requests" )
74- def test_get_visit_list (
75- mock_request ,
76- test_params : tuple [str ],
77- mock_client_configuration ,
78- ):
79- # Unpack test params and set up other params
80- (server_url ,) = test_params
81- instrument_name = mock_client_configuration ["Murfey" ]["instrument_name" ]
82-
83- # Construct the expected request response
84- example_visits = [
85- {
86- "start" : "1999-09-09T09:00:00" ,
87- "end" : "1999-09-11T09:00:00" ,
88- "session_id" : 123456789 ,
89- "name" : "cm12345-0" ,
90- "beamline" : "murfey" ,
91- "proposal_title" : "Commissioning Session 1" ,
92- },
93- {
94- "start" : "1999-09-09T09:00:00" ,
95- "end" : "1999-09-11T09:00:00" ,
96- "session_id" : 246913578 ,
97- "name" : "cm23456-1" ,
98- "beamline" : "murfey" ,
99- "proposal_title" : "Cryo-cycle 1999" ,
100- },
101- ]
102- mock_response = Mock ()
103- mock_response .status_code = 200
104- mock_response .json .return_value = example_visits
105- mock_request .get .return_value = mock_response
106-
107- # read_config() has to be patched using fixture, so has to be done in function
108- with patch ("murfey.util.client.read_config" , mock_client_configuration ):
109- visits = _get_visit_list (urlparse (server_url ), instrument_name )
110-
111- # Check that request was sent with the correct URL
112- expected_url = (
113- f"{ server_url } /session_control/instruments/{ instrument_name } /visits_raw"
114- )
115- mock_request .get .assert_called_once_with (expected_url )
116-
117- # Check that expected outputs are correct (order-sensitive)
118- for v , visit in enumerate (visits ):
119- assert visit .dict () == Visit .parse_obj (example_visits [v ]).dict ()
120-
121-
12260def test_set_default_acquisition_output_normal_operation (tmp_path ):
12361 output_dir = tmp_path / "settings.json"
12462 settings_json = {
0 commit comments