Skip to content

Commit 46f888a

Browse files
committed
Move tests
1 parent 9b7f299 commit 46f888a

File tree

7 files changed

+336
-314
lines changed

7 files changed

+336
-314
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import json
2+
3+
import pytest
4+
from tests.constants import TestDataPaths
5+
6+
from daq_config_server.converters.beamline_parameters._converters import (
7+
beamline_parameters_to_dict,
8+
)
9+
10+
11+
def test_beamline_parameters_to_dict_gives_expected_result():
12+
with open(TestDataPaths.TEST_BEAMLINE_PARAMETERS_PATH) as f:
13+
contents = f.read()
14+
with open(TestDataPaths.EXPECTED_BEAMLINE_PARAMETERS_JSON_PATH) as f:
15+
expected = json.load(f)
16+
result = beamline_parameters_to_dict(contents)
17+
assert result == expected
18+
19+
20+
def test_bad_beamline_parameters_with_non_keyword_string_value_causes_error():
21+
with open(TestDataPaths.TEST_BAD_BEAMLINE_PARAMETERS_PATH) as f:
22+
contents = f.read()
23+
with pytest.raises(ValueError, match="malformed node or string"):
24+
beamline_parameters_to_dict(contents)
25+
26+
27+
def test_beam_line_parameters_with_repeated_key_causes_error():
28+
input = "thing = 1\nthing = 2"
29+
with pytest.raises(ValueError, match="Repeated key in parameters: thing"):
30+
beamline_parameters_to_dict(input)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from collections.abc import Callable
2+
from typing import Any
3+
from unittest.mock import MagicMock
4+
5+
import pytest
6+
from tests.constants import (
7+
TestDataPaths,
8+
)
9+
10+
from daq_config_server.converters._converter_utils import (
11+
ConverterParseError,
12+
)
13+
from daq_config_server.converters.beamline_parameters._converters import (
14+
beamline_parameters_to_dict,
15+
)
16+
from daq_config_server.converters.convert import get_converted_file_contents
17+
from daq_config_server.converters.lookup_tables.models import GenericLookupTable
18+
19+
20+
def test_get_converted_file_contents_uses_converter_if_file_in_map(
21+
mock_file_converter_map: dict[str, Callable[[str], Any]],
22+
):
23+
file_to_convert = TestDataPaths.TEST_GOOD_XML_PATH
24+
mock_convert_function = MagicMock()
25+
mock_file_converter_map[str(file_to_convert)] = mock_convert_function
26+
get_converted_file_contents(file_to_convert)
27+
28+
mock_convert_function.assert_called_once()
29+
30+
31+
def test_get_converted_file_contents_converts_pydantic_model_to_dict(
32+
mock_file_converter_map: dict[str, Callable[[str], Any]],
33+
):
34+
file_to_convert = TestDataPaths.TEST_GOOD_LUT_PATH
35+
model = GenericLookupTable(
36+
column_names=["column1", "column2"], rows=[[1, 2], [2, 3]]
37+
)
38+
mock_convert_function = MagicMock(return_value=model)
39+
mock_file_converter_map[str(file_to_convert)] = mock_convert_function
40+
result = get_converted_file_contents(file_to_convert)
41+
assert isinstance(result, dict)
42+
mock_convert_function.assert_called_once()
43+
44+
45+
def test_error_is_raised_if_file_cant_be_parsed(
46+
mock_file_converter_map: dict[str, Callable[[str], Any]],
47+
):
48+
file_to_convert = TestDataPaths.TEST_BAD_BEAMLINE_PARAMETERS_PATH
49+
mock_file_converter_map[str(file_to_convert)] = beamline_parameters_to_dict
50+
with pytest.raises(ConverterParseError):
51+
get_converted_file_contents(file_to_convert)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import Any
2+
3+
import pytest
4+
5+
from daq_config_server.converters._converter_utils import parse_value, remove_comments
6+
7+
8+
def test_remove_comments_works_as_expected():
9+
input = [
10+
"This line should not be changed",
11+
"This should stay # this should go",
12+
"#This entire line should go",
13+
" # as should this one",
14+
"# and this one",
15+
"",
16+
" ",
17+
" whitespace should be stripped ",
18+
]
19+
expected_output = [
20+
"This line should not be changed",
21+
"This should stay",
22+
"whitespace should be stripped",
23+
]
24+
assert remove_comments(input) == expected_output
25+
26+
27+
@pytest.mark.parametrize(
28+
"value, convert_to, expected_parsed_value",
29+
[
30+
(" 2.0 ", None, 2.0),
31+
(" 3 ", None, 3),
32+
("5.0", float, 5.0),
33+
("5.0", int, 5),
34+
],
35+
)
36+
def test_parse_value_works_as_expected(
37+
value: str, convert_to: type, expected_parsed_value: Any
38+
):
39+
parsed_value = parse_value(value, convert_to)
40+
assert parsed_value == expected_parsed_value
41+
assert type(parsed_value) is type(expected_parsed_value)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import json
2+
3+
import pytest
4+
from tests.constants import TestDataPaths
5+
6+
from daq_config_server.converters.display_config._converters import (
7+
display_config_to_model,
8+
)
9+
from daq_config_server.converters.display_config.models import (
10+
DisplayConfig,
11+
DisplayConfigData,
12+
)
13+
14+
15+
def test_display_config_to_model_gives_expected_result_and_can_be_jsonified():
16+
with open(TestDataPaths.TEST_GOOD_DISPLAY_CONFIG_PATH) as f:
17+
contents = f.read()
18+
expected = DisplayConfig(
19+
zoom_levels={
20+
1.0: DisplayConfigData(
21+
bottomRightX=410,
22+
bottomRightY=278,
23+
crosshairX=541,
24+
crosshairY=409,
25+
topLeftX=383,
26+
topLeftY=253,
27+
),
28+
2.5: DisplayConfigData(
29+
bottomRightX=388,
30+
bottomRightY=322,
31+
crosshairX=551,
32+
crosshairY=410,
33+
topLeftX=340,
34+
topLeftY=283,
35+
),
36+
}
37+
)
38+
result = display_config_to_model(contents)
39+
assert result == expected
40+
json.dumps(result.model_dump())
41+
42+
43+
def test_display_config_with_wrong_zoom_levels_causes_error():
44+
zoom_levels = {
45+
1.0: DisplayConfigData(
46+
bottomRightX=410,
47+
bottomRightY=278,
48+
crosshairX=541,
49+
crosshairY=409,
50+
topLeftX=383,
51+
topLeftY=253,
52+
),
53+
2.5: DisplayConfigData(
54+
bottomRightX=388,
55+
bottomRightY=322,
56+
crosshairX=551,
57+
crosshairY=410,
58+
topLeftX=340,
59+
topLeftY=283,
60+
),
61+
}
62+
with pytest.raises(
63+
ValueError,
64+
match="Zoom levels {1.0, 2.5} do not match required zoom levels: {1.0, 3.0}",
65+
):
66+
DisplayConfig(zoom_levels=zoom_levels, required_zoom_levels=({1.0, 3.0}))
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import pytest
2+
from tests.constants import TestDataPaths
3+
4+
from daq_config_server.converters.lookup_tables._converters import (
5+
beamline_pitch_lut,
6+
beamline_roll_lut,
7+
detector_xy_lut,
8+
parse_lut,
9+
undulator_energy_gap_lut,
10+
)
11+
from daq_config_server.converters.lookup_tables.models import GenericLookupTable
12+
13+
14+
def test_parse_lut_to_dict_gives_expected_result_and_can_be_jsonified():
15+
with open(TestDataPaths.TEST_GOOD_LUT_PATH) as f:
16+
contents = f.read()
17+
expected = GenericLookupTable(
18+
column_names=["energy_eV", "gap_mm"],
19+
rows=[[5700, 5.4606], [5760, 5.5], [6000, 5.681], [6500, 6.045]],
20+
)
21+
result = parse_lut(contents, ("energy_eV", int), ("gap_mm", float))
22+
assert result == expected
23+
result.model_dump_json()
24+
25+
26+
def test_parsing_bad_lut_causes_error():
27+
with open(TestDataPaths.TEST_BAD_LUT_PATH) as f:
28+
contents = f.read()
29+
with pytest.raises(IndexError):
30+
parse_lut(contents, ("energy_eV", int), ("gap_mm", float))
31+
32+
33+
def test_lut_with_different_number_of_row_items_to_column_names_causes_error():
34+
with pytest.raises(ValueError, match=" does not match number of columns:"):
35+
GenericLookupTable(
36+
column_names=["column1", "column2"], rows=[[1, 2], [1, 2, 3]]
37+
)
38+
39+
40+
def test_detector_xy_lut_gives_expected_results():
41+
input = (
42+
"# distance beamY beamX (values from mosflm)\n"
43+
"Units mm mm mm\n"
44+
"150 152.2 166.26\n"
45+
"800 152.08 160.96\n"
46+
)
47+
expected = GenericLookupTable(
48+
column_names=["detector_distances_mm", "beam_centre_x_mm", "beam_centre_y_mm"],
49+
rows=[[150, 152.2, 166.26], [800, 152.08, 160.96]],
50+
)
51+
result = detector_xy_lut(input)
52+
assert result == expected
53+
54+
55+
def test_beamline_pitch_lut_gives_expected_result():
56+
input = (
57+
"# Bragg pitch\n"
58+
"# Degree values for pitch are interpreted as mrad\n"
59+
"# The values cannot change direction.\n"
60+
"# last update 2025/01/15 NP\n"
61+
"Units Deg mrad\n"
62+
"Units Deg Deg\n"
63+
"16.40956 -0.62681\n"
64+
"14.31123 -0.61833\n"
65+
"12.69285 -0.61243\n"
66+
"11.40557 -0.60849\n"
67+
)
68+
expected = GenericLookupTable(
69+
column_names=["bragg_angle_deg", "pitch_mrad"],
70+
rows=[
71+
[16.40956, -0.62681],
72+
[14.31123, -0.61833],
73+
[12.69285, -0.61243],
74+
[11.40557, -0.60849],
75+
],
76+
)
77+
result = beamline_pitch_lut(input)
78+
assert result == expected
79+
80+
81+
def test_beamline_roll_lut_gives_expected_result():
82+
input = (
83+
"#Bragg angle against roll( absolute number)\n"
84+
"#reloadLookupTables()\n"
85+
"# last update 2024/06/20 NP\n"
86+
"Units Deg mrad\n"
87+
"26.4095 2.6154\n"
88+
"6.3075 2.6154\n"
89+
)
90+
expected = GenericLookupTable(
91+
column_names=["bragg_angle_deg", "roll_mrad"],
92+
rows=[[26.4095, 2.6154], [6.3075, 2.6154]],
93+
)
94+
result = beamline_roll_lut(input)
95+
assert result == expected
96+
97+
98+
def test_undulator_gap_lut_gives_expected_result():
99+
input = (
100+
"#######################\n"
101+
"# #\n"
102+
"# 5.5mm CPMU 20/11/22 #\n"
103+
"# #\n"
104+
"Units eV mm\n"
105+
"5700 5.4606\n"
106+
"5760 5.5\n"
107+
"6000 5.681\n"
108+
"6500 6.045\n"
109+
)
110+
expected = GenericLookupTable(
111+
column_names=["energy_eV", "gap_mm"],
112+
rows=[[5700, 5.4606], [5760, 5.5], [6000, 5.681], [6500, 6.045]],
113+
)
114+
result = undulator_energy_gap_lut(input)
115+
assert result == expected
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import json
2+
3+
import xmltodict
4+
from tests.constants import TestDataPaths
5+
6+
7+
def test_xml_to_dict_gives_expected_result_and_can_be_jsonified():
8+
with open(TestDataPaths.TEST_GOOD_XML_PATH) as f:
9+
contents = f.read()
10+
expected = {
11+
"JCameraManSettings": {
12+
"levels": {
13+
"zoomLevel": [
14+
{
15+
"level": "1.0",
16+
"micronsPerXPixel": "2.432",
17+
"micronsPerYPixel": "2.432",
18+
"position": "0",
19+
},
20+
{
21+
"level": "1.5",
22+
"micronsPerXPixel": "1.888",
23+
"micronsPerYPixel": "1.888",
24+
"position": "16.3",
25+
},
26+
],
27+
},
28+
"tolerance": "1.0",
29+
},
30+
}
31+
result = xmltodict.parse(contents)
32+
assert result == expected
33+
json.dumps(result)

0 commit comments

Comments
 (0)