Skip to content

Commit cda4d35

Browse files
committed
Migrated tests to reflect refactoring of 'murfey.client' submodules
1 parent 61c1ba5 commit cda4d35

File tree

3 files changed

+70
-66
lines changed

3 files changed

+70
-66
lines changed

tests/client/tui/__init__.py

Whitespace-only changes.

tests/client/tui/test_main.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from unittest import mock
2+
from unittest.mock import Mock
3+
from urllib.parse import urlparse
4+
5+
import pytest
6+
7+
from murfey.client.tui.main import _get_visit_list
8+
from murfey.util.models import Visit
9+
10+
test_get_visit_list_params_matrix = (
11+
("http://0.0.0.0:8000",),
12+
("http://0.0.0.0:8000/api",),
13+
("http://murfey_server",),
14+
("http://murfey_server/api",),
15+
("http://murfey_server.com",),
16+
)
17+
18+
19+
@pytest.mark.parametrize("test_params", test_get_visit_list_params_matrix)
20+
@mock.patch("murfey.client.requests")
21+
def test_get_visit_list(
22+
mock_request,
23+
test_params: tuple[str],
24+
mock_client_configuration,
25+
):
26+
# Unpack test params and set up other params
27+
(server_url,) = test_params
28+
instrument_name = mock_client_configuration["Murfey"]["instrument_name"]
29+
30+
# Construct the expected request response
31+
example_visits = [
32+
{
33+
"start": "1999-09-09T09:00:00",
34+
"end": "1999-09-11T09:00:00",
35+
"session_id": 123456789,
36+
"name": "cm12345-0",
37+
"beamline": "murfey",
38+
"proposal_title": "Commissioning Session 1",
39+
},
40+
{
41+
"start": "1999-09-09T09:00:00",
42+
"end": "1999-09-11T09:00:00",
43+
"session_id": 246913578,
44+
"name": "cm23456-1",
45+
"beamline": "murfey",
46+
"proposal_title": "Cryo-cycle 1999",
47+
},
48+
]
49+
mock_response = Mock()
50+
mock_response.status_code = 200
51+
mock_response.json.return_value = example_visits
52+
mock_request.get.return_value = mock_response
53+
54+
# read_config() has to be patched using fixture, so has to be done in function
55+
with mock.patch("murfey.util.client.read_config", mock_client_configuration):
56+
visits = _get_visit_list(urlparse(server_url), instrument_name)
57+
58+
# Check that request was sent with the correct URL
59+
expected_url = (
60+
f"{server_url}/session_control/instruments/{instrument_name}/visits_raw"
61+
)
62+
mock_request.get.assert_called_once_with(expected_url)
63+
64+
# Check that expected outputs are correct (order-sensitive)
65+
for v, visit in enumerate(visits):
66+
assert visit.dict() == Visit.parse_obj(example_visits[v]).dict()

tests/util/test_client.py

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import json
22
import os
33
from 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
108
from murfey.util.client import read_config, set_default_acquisition_output
11-
from murfey.util.models import Visit
129

1310
test_read_config_params_matrix = (
1411
# Environment variable to set | Append to tmp_path
@@ -27,7 +24,7 @@
2724
)
2825

2926

30-
@mark.parametrize("test_params", test_read_config_params_matrix)
27+
@pytest.mark.parametrize("test_params", test_read_config_params_matrix)
3128
def 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-
12260
def test_set_default_acquisition_output_normal_operation(tmp_path):
12361
output_dir = tmp_path / "settings.json"
12462
settings_json = {

0 commit comments

Comments
 (0)