Skip to content

Commit 4f33b93

Browse files
committed
Added unit test for '_get_visit_list'
1 parent d339cad commit 4f33b93

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

tests/util/test_client.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import json
22
import os
33
from pathlib import Path
4-
from unittest.mock import patch
4+
from unittest.mock import Mock, patch
5+
from urllib.parse import urlparse
56

67
from pytest import mark
78

8-
from murfey.util.client import read_config, set_default_acquisition_output
9+
from murfey.util.client import (
10+
_get_visit_list,
11+
read_config,
12+
set_default_acquisition_output,
13+
)
914

1015
test_read_config_params_matrix = (
1116
# Environment variable to set | Append to tmp_path
@@ -57,6 +62,62 @@ def test_read_config(
5762
assert dict(config["Murfey"]) == dict(mock_client_configuration["Murfey"])
5863

5964

65+
test_get_visit_list_params_matrix = (
66+
("http://0.0.0.0:8000",),
67+
("http://0.0.0.0:8000/api",),
68+
("http://murfey_server",),
69+
("http://murfey_server/api",),
70+
("http://murfey_server.com",),
71+
)
72+
73+
74+
@mark.parametrize("test_params", test_get_visit_list_params_matrix)
75+
@patch("murfey.util.client.requests")
76+
def test_get_visit_list(
77+
mock_request,
78+
test_params: tuple[str],
79+
mock_client_configuration,
80+
):
81+
# Unpack test params and set up other params
82+
(server_url,) = test_params
83+
instrument_name = mock_client_configuration["Murfey"]["instrument_name"]
84+
85+
# Construct the expected request response
86+
example_visits = [
87+
{
88+
"start": "1999-09-09T09:00:00",
89+
"end": "1999-09-11T09:00:00",
90+
"session_id": 123456789,
91+
"name": "cm12345-0",
92+
"beamline": "murfey",
93+
"proposal_title": "Commissioning Session 1",
94+
},
95+
{
96+
"start": "1999-09-09T09:00:00",
97+
"end": "1999-09-11T09:00:00",
98+
"session_id": 246913578,
99+
"name": "cm23456-1",
100+
"beamline": "murfey",
101+
"proposal_title": "Cryo-cycle 1999",
102+
},
103+
]
104+
mock_response = Mock()
105+
mock_response.status_code = 200
106+
mock_response.json.return_value = example_visits
107+
mock_request.get.return_value = mock_response
108+
109+
# read_config() has to be patched using fixture, so has to be done in function
110+
with patch("murfey.util.client.read_config", mock_client_configuration):
111+
visits = _get_visit_list(urlparse(server_url), instrument_name)
112+
113+
# Check that request was sent with the correct URL
114+
expected_url = f"{server_url}/instruments/{instrument_name}/visits_raw"
115+
mock_request.get.assert_called_once_with(expected_url)
116+
117+
# Check that expected outputs are correct (order-insensitive)
118+
assert {visit.dict() for visit in visits} == set(example_visits)
119+
120+
60121
def test_set_default_acquisition_output_normal_operation(tmp_path):
61122
output_dir = tmp_path / "settings.json"
62123
settings_json = {

0 commit comments

Comments
 (0)