Skip to content

Commit 4c511de

Browse files
committed
Add system tests WIP
1 parent ccfd83c commit 4c511de

File tree

7 files changed

+64
-9
lines changed

7 files changed

+64
-9
lines changed

src/daq_config_server/converters/_converter_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ def parse_lut_to_dict(
3131
"""Converts a lookup table to a dict, containing the names of each column and
3232
the rows as a 2D list.
3333
34-
Any args after the contents should be a tuple of (column name, type | None).
34+
Any args after the contents provide the column names and optionally, python types
35+
for values in a column to be converted to. e.g: (energy_EV, float), (pixels, int).
3536
If a type is provided, the values in that column will be converted to that type.
36-
Otherwise, the type will be inferred. Please include units in the column name.
37+
Otherwise, the type will be inferred. Units should be included in the column name.
3738
"""
3839
data: list[list[Any]] = []
3940
column_names = [param[0] for param in params]

src/daq_config_server/converters/_file_converter_map.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from collections.abc import Callable
22
from typing import Any
33

4+
from tests.constants import ServerFilePaths
5+
46
from daq_config_server.converters._converters import (
57
beamline_parameters_to_dict,
68
beamline_pitch_lut_to_dict,
@@ -12,6 +14,7 @@
1214
)
1315

1416
FILE_TO_CONVERTER_MAP: dict[str, Callable[[str], Any]] = {
17+
str(ServerFilePaths.GOOD_LUT): undulator_energy_gap_lut_to_dict, # For system tests
1518
"/dls_sw/i23/software/aithre/aithre_display.configuration": display_config_to_dict,
1619
"/dls_sw/i03/software/gda_versions/var/display.configuration": display_config_to_dict,
1720
"/dls_sw/i04/software/bluesky/scratch/display.configuration": display_config_to_dict,

tests/conftest.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from collections.abc import Callable, Generator
2+
from typing import Any
3+
from unittest.mock import patch
4+
5+
import pytest
6+
7+
from daq_config_server.converters._converters import (
8+
beamline_parameters_to_dict,
9+
display_config_to_dict,
10+
undulator_energy_gap_lut_to_dict,
11+
xml_to_dict,
12+
)
13+
from tests.constants import ServerFilePaths, TestDataPaths
14+
15+
16+
@pytest.fixture
17+
def mock_file_converter_map() -> Generator[dict[str, Callable[[str], Any]], None, None]:
18+
with patch(
19+
"daq_config_server.converters._file_converter_map.FILE_TO_CONVERTER_MAP",
20+
{
21+
str(TestDataPaths.TEST_GOOD_XML_PATH): xml_to_dict,
22+
str(
23+
TestDataPaths.TEST_BEAMLINE_PARAMETERS_PATH
24+
): beamline_parameters_to_dict,
25+
str(TestDataPaths.TEST_GOOD_LUT_PATH): undulator_energy_gap_lut_to_dict,
26+
str(TestDataPaths.TEST_GOOD_DISPLAY_CONFIG_PATH): display_config_to_dict,
27+
str(ServerFilePaths.GOOD_LUT): undulator_energy_gap_lut_to_dict,
28+
},
29+
) as mock_map:
30+
yield mock_map

tests/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class ServerFilePaths:
5252
GOOD_JSON_FILE = Path("/tests/test_data/test_good_json.json")
5353
BAD_JSON_FILE = Path("/tests/test_data/test_bad_json")
5454
FILE_IN_GOOD_DIR = Path("/tests/test_data/test_bad_json")
55+
GOOD_LUT = Path("/tests/test_data/test_good_lut.txt")
5556

5657

5758
TEST_CONFIG_PATH = TEST_DATA_DIR_PATH.joinpath("test_config.yaml")

tests/system_tests/test_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77
import requests
88

9+
import daq_config_server.converters._file_converter_map as file_converter_map
910
from daq_config_server.client import ConfigServer
1011
from tests.constants import (
1112
ServerFilePaths,
@@ -104,3 +105,20 @@ def test_request_with_file_not_on_whitelist(server: ConfigServer):
104105
server.get_file_contents(
105106
file_path,
106107
)
108+
109+
110+
@pytest.mark.requires_local_server
111+
def test_request_for_file_with_converter_works(server: ConfigServer):
112+
expected = {
113+
"column_names": ["energy_eV", "gap_mm"],
114+
"data": [[5700, 5.4606], [5760, 5.5], [6000, 5.681], [6500, 6.045]],
115+
}
116+
result = server.get_file_contents(ServerFilePaths.GOOD_LUT, dict)
117+
assert result == expected
118+
119+
120+
@pytest.mark.requires_local_server
121+
def test_all_files_in_file_converter_map_can_be_converted_to_dict(server: ConfigServer):
122+
for filename in file_converter_map.FILE_TO_CONVERTER_MAP.keys():
123+
result = server.get_file_contents(filename, dict)
124+
assert isinstance(result, dict)

tests/unit_tests/test_convert.py

Whitespace-only changes.

tests/unit_tests/test_converters.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import json
2+
from collections.abc import Callable
23
from pathlib import Path
34
from typing import Any
4-
from unittest.mock import MagicMock, patch
5+
from unittest.mock import MagicMock
56

67
import pytest
78

@@ -27,14 +28,15 @@ def test_all_files_in_dict_can_be_parsed_with_no_errors():
2728
get_converted_file_contents(Path(filename))
2829

2930

30-
def test_get_converted_file_contents_uses_converter_if_file_in_map():
31+
def test_get_converted_file_contents_uses_converter_if_file_in_map(
32+
mock_file_converter_map: dict[str, Callable[[str], Any]],
33+
):
3134
file_to_convert = TestDataPaths.TEST_GOOD_XML_PATH
3235
mock_convert_function = MagicMock()
33-
with patch(
34-
"daq_config_server.converters._file_converter_map.FILE_TO_CONVERTER_MAP",
35-
{str(file_to_convert): mock_convert_function},
36-
):
37-
get_converted_file_contents(file_to_convert)
36+
mock_file_converter_map[str(TestDataPaths.TEST_GOOD_XML_PATH)] = (
37+
mock_convert_function
38+
)
39+
get_converted_file_contents(file_to_convert)
3840

3941
mock_convert_function.assert_called_once()
4042

0 commit comments

Comments
 (0)