Skip to content

Commit a7dba6d

Browse files
committed
Update tests
1 parent 857ca3e commit a7dba6d

File tree

4 files changed

+61
-32
lines changed

4 files changed

+61
-32
lines changed

src/daq_config_server/converters/_converters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def check_zoom_levels_match_required(self):
5555
):
5656
raise ValueError(
5757
f"Zoom levels {set(self.zoom_levels.keys())} "
58-
+ "do not match required zoom levels: {self.required_zoom_levels}"
58+
f"do not match required zoom levels: {self.required_zoom_levels}"
5959
)
6060
return self
6161

tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from daq_config_server.converters._converters import (
88
beamline_parameters_to_dict,
99
display_config_to_dict,
10-
undulator_energy_gap_lut_to_dict,
10+
undulator_energy_gap_lut,
1111
xml_to_dict,
1212
)
1313
from tests.constants import ServerFilePaths, TestDataPaths
@@ -22,9 +22,9 @@ def mock_file_converter_map() -> Generator[dict[str, Callable[[str], Any]], None
2222
str(
2323
TestDataPaths.TEST_BEAMLINE_PARAMETERS_PATH
2424
): beamline_parameters_to_dict,
25-
str(TestDataPaths.TEST_GOOD_LUT_PATH): undulator_energy_gap_lut_to_dict,
25+
str(TestDataPaths.TEST_GOOD_LUT_PATH): undulator_energy_gap_lut,
2626
str(TestDataPaths.TEST_GOOD_DISPLAY_CONFIG_PATH): display_config_to_dict,
27-
str(ServerFilePaths.GOOD_LUT): undulator_energy_gap_lut_to_dict,
27+
str(ServerFilePaths.GOOD_LUT): undulator_energy_gap_lut,
2828
},
2929
) as mock_map:
3030
yield mock_map

tests/system_tests/test_client.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
import pytest
77
import requests
8+
from pydantic import ValidationError
89

910
import daq_config_server.converters._file_converter_map as file_converter_map
1011
from daq_config_server.client import ConfigServer
12+
from daq_config_server.converters._converter_utils import GenericLut
13+
from daq_config_server.converters._converters import DisplayConfig
1114
from tests.constants import (
1215
ServerFilePaths,
1316
TestDataPaths,
@@ -111,12 +114,33 @@ def test_request_with_file_not_on_whitelist(server: ConfigServer):
111114
def test_request_for_file_with_converter_works(server: ConfigServer):
112115
expected = {
113116
"column_names": ["energy_eV", "gap_mm"],
114-
"data": [[5700, 5.4606], [5760, 5.5], [6000, 5.681], [6500, 6.045]],
117+
"rows": [[5700, 5.4606], [5760, 5.5], [6000, 5.681], [6500, 6.045]],
115118
}
116119
result = server.get_file_contents(ServerFilePaths.GOOD_LUT, dict)
117120
assert result == expected
118121

119122

123+
@pytest.mark.requires_local_server
124+
def test_request_for_file_with_converter_works_with_pydantic_model(
125+
server: ConfigServer,
126+
):
127+
expected = GenericLut(
128+
column_names=["energy_eV", "gap_mm"],
129+
rows=[[5700, 5.4606], [5760, 5.5], [6000, 5.681], [6500, 6.045]],
130+
)
131+
result = server.get_file_contents(ServerFilePaths.GOOD_LUT, GenericLut)
132+
assert isinstance(result, GenericLut)
133+
assert result == expected
134+
135+
136+
@pytest.mark.requires_local_server
137+
def test_request_for_file_with_converter_with_wrong_pydantic_model_errors(
138+
server: ConfigServer,
139+
):
140+
with pytest.raises(ValidationError):
141+
server.get_file_contents(ServerFilePaths.GOOD_LUT, DisplayConfig)
142+
143+
120144
@pytest.mark.requires_local_server
121145
def test_all_files_in_file_converter_map_can_be_converted_to_dict(server: ConfigServer):
122146
for filename in file_converter_map.FILE_TO_CONVERTER_MAP.keys():

tests/unit_tests/test_converters.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
from daq_config_server.converters._converter_utils import (
99
ConverterParseError,
10-
parse_lut_to_dict,
10+
GenericLut,
11+
parse_lut,
1112
parse_value,
1213
remove_comments,
1314
)
1415
from daq_config_server.converters._converters import (
16+
DisplayConfig,
17+
DisplayConfigData,
1518
beamline_parameters_to_dict,
1619
display_config_to_dict,
1720
xml_to_dict,
@@ -45,46 +48,48 @@ def test_error_is_raised_if_file_cant_be_parsed(
4548
def test_parse_lut_to_dict_gives_expected_result_and_can_be_jsonified():
4649
with open(TestDataPaths.TEST_GOOD_LUT_PATH) as f:
4750
contents = f.read()
48-
expected = {
49-
"column_names": ["energy_eV", "gap_mm"],
50-
"data": [[5700, 5.4606], [5760, 5.5], [6000, 5.681], [6500, 6.045]],
51-
}
52-
result = parse_lut_to_dict(contents, ("energy_eV", int), ("gap_mm", float))
51+
expected = GenericLut(
52+
column_names=["energy_eV", "gap_mm"],
53+
rows=[[5700, 5.4606], [5760, 5.5], [6000, 5.681], [6500, 6.045]],
54+
)
55+
result = parse_lut(contents, ("energy_eV", int), ("gap_mm", float))
5356
assert result == expected
54-
json.dumps(result)
57+
result.model_dump_json()
5558

5659

5760
def test_parsing_bad_lut_causes_error():
5861
with open(TestDataPaths.TEST_BAD_LUT_PATH) as f:
5962
contents = f.read()
6063
with pytest.raises(IndexError):
61-
parse_lut_to_dict(contents, ("energy_eV", int), ("gap_mm", float))
64+
parse_lut(contents, ("energy_eV", int), ("gap_mm", float))
6265

6366

6467
def test_display_config_to_dict_gives_expected_result_and_can_be_jsonified():
6568
with open(TestDataPaths.TEST_GOOD_DISPLAY_CONFIG_PATH) as f:
6669
contents = f.read()
67-
expected = {
68-
"1.0": {
69-
"bottomRightX": 410,
70-
"bottomRightY": 278,
71-
"crosshairX": 541,
72-
"crosshairY": 409,
73-
"topLeftX": 383,
74-
"topLeftY": 253,
75-
},
76-
"2.5": {
77-
"bottomRightX": 388,
78-
"bottomRightY": 322,
79-
"crosshairX": 551,
80-
"crosshairY": 410,
81-
"topLeftX": 340,
82-
"topLeftY": 283,
83-
},
84-
}
70+
expected = DisplayConfig(
71+
zoom_levels={
72+
1.0: DisplayConfigData(
73+
bottomRightX=410,
74+
bottomRightY=278,
75+
crosshairX=541,
76+
crosshairY=409,
77+
topLeftX=383,
78+
topLeftY=253,
79+
),
80+
2.5: DisplayConfigData(
81+
bottomRightX=388,
82+
bottomRightY=322,
83+
crosshairX=551,
84+
crosshairY=410,
85+
topLeftX=340,
86+
topLeftY=283,
87+
),
88+
}
89+
)
8590
result = display_config_to_dict(contents)
8691
assert result == expected
87-
json.dumps(result)
92+
json.dumps(result.model_dump())
8893

8994

9095
def test_xml_to_dict_gives_expected_result_and_can_be_jsonified():

0 commit comments

Comments
 (0)