Skip to content

Commit fc08540

Browse files
committed
typo fix
1 parent 1614261 commit fc08540

File tree

1 file changed

+165
-165
lines changed

1 file changed

+165
-165
lines changed

tests/test_helpers.py

Lines changed: 165 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,165 @@
1-
# Copyright The NOMAD Authors.
2-
#
3-
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
15-
# limitations under the License.
16-
#
17-
"""
18-
Tests for helper functions
19-
"""
20-
21-
import pytest
22-
import datetime
23-
from typing import Dict
24-
25-
from pynxtools_xps.reader_utils import extract_unit
26-
27-
from pynxtools_xps.value_mappers import get_units_for_key, parse_datetime
28-
29-
30-
@pytest.mark.parametrize(
31-
"unit_key, unit_map, expected_unit",
32-
[
33-
# Test cases with direct keys in the unit_map
34-
("detector/detector_voltage", {"detector/detector_voltage": "V"}, "V"),
35-
("temperature", {"temperature": "K"}, "K"),
36-
# Test cases with keys that don't exist in the unit_map
37-
("nonexistent_key", {"some_key": "m"}, None),
38-
# Test cases with regex pattern in the key
39-
(
40-
"detector/[detector_voltage]",
41-
{"detector/detector_voltage": "V"},
42-
"detector_voltage",
43-
),
44-
("sensor/[sensor_current]", {"sensor/sensor_current": "A"}, "sensor_current"),
45-
# Test cases with key that includes a regex but isn't mapped
46-
("not_mapped/[value]", {}, "value"),
47-
],
48-
)
49-
def test_get_units_for_key(unit_key: str, unit_map: Dict[str, str], expected_unit: str):
50-
result = get_units_for_key(unit_key, unit_map)
51-
assert result == expected_unit, f"Expected {expected_unit} but got {result}"
52-
53-
54-
@pytest.mark.parametrize(
55-
"key, value, unit_missing, expected_value, expected_unit",
56-
[
57-
# Test cases with explicit units
58-
("x_position", "0 mm", {}, 0, "mm"),
59-
("polar_rotation", "0 deg", {}, 0, "deg"),
60-
("analyser_work_function", "4.506eV", {}, 4.506, "eV"),
61-
("temperature", "300K", {}, 300, "K"),
62-
("pressure", "1.01bar", {}, 1.01, "bar"),
63-
# Test cases without explicit units
64-
("voltage", "5.0", {"voltage": "V"}, 5.0, "V"),
65-
("current", "10", {"current": "A"}, 10, "A"),
66-
# Test cases with scientific notation
67-
("distance", "1.23e-10m", {}, 1.23e-10, "m"),
68-
("charge", "1.602e-19C", {}, 1.602e-19, "C"),
69-
# Test cases with missing unit in value and unit_missing dictionary
70-
("energy", "1000", {"energy": "J"}, 1000, "J"),
71-
("mass", "0.5", {"mass": "kg"}, 0.5, "kg"),
72-
],
73-
)
74-
def test_extract_unit(
75-
key: str,
76-
value: str,
77-
unit_missing: Dict[str, str],
78-
expected_value: str,
79-
expected_unit: str,
80-
):
81-
result_value, result_unit = extract_unit(key, value, unit_missing)
82-
83-
assert isinstance(
84-
result_value, type(expected_value)
85-
), f"Expected type {type(expected_value)} but got type {type(result_value)}"
86-
87-
assert (
88-
result_value == expected_value
89-
), f"Expected {expected_value} but got {result_value}"
90-
assert (
91-
result_unit == expected_unit
92-
), f"Expected {expected_unit} but got {result_unit}"
93-
94-
95-
@pytest.mark.parametrize(
96-
"datetime_string, possible_date_formats, tzinfo, expected_iso8601",
97-
[
98-
# Test cases with valid datetime strings and formats
99-
(
100-
"2023-10-23 14:30:00",
101-
["%Y-%m-%d %H:%M:%S"],
102-
datetime.timezone.utc,
103-
"2023-10-23T14:30:00+00:00",
104-
),
105-
(
106-
"23/10/2023 14:30",
107-
["%d/%m/%Y %H:%M"],
108-
datetime.timezone.utc,
109-
"2023-10-23T14:30:00+00:00",
110-
),
111-
(
112-
"October 23, 2023 14:30",
113-
["%B %d, %Y %H:%M"],
114-
datetime.timezone.utc,
115-
"2023-10-23T14:30:00+00:00",
116-
),
117-
(
118-
"2023-10-23T14:30:00Z",
119-
["%Y-%m-%dT%H:%M:%SZ"],
120-
datetime.timezone.utc,
121-
"2023-10-23T14:30:00+00:00",
122-
),
123-
(
124-
"2023-10-23T14:30:00+0200",
125-
["%Y-%m-%dT%H:%M:%S%z"],
126-
datetime.timezone(datetime.timedelta(hours=2)),
127-
"2023-10-23T14:30:00+02:00",
128-
),
129-
# Test cases with timezone information
130-
(
131-
"2023-10-23 14:30:00",
132-
["%Y-%m-%d %H:%M:%S"],
133-
datetime.timezone(datetime.timedelta(hours=1)),
134-
"2023-10-23T14:30:00+01:00",
135-
),
136-
# Test case with missing timezone should still return UTC
137-
(
138-
"2023-10-23 14:30:00",
139-
["%Y-%m-%d %H:%M:%S"],
140-
None,
141-
"2023-10-23T14:30:00",
142-
),
143-
],
144-
)
145-
def test_parse_datetime(
146-
datetime_string, possible_date_formats, tzinfo, expected_iso8601
147-
):
148-
result = parse_datetime(datetime_string, possible_date_formats, tzinfo)
149-
assert result == expected_iso8601, f"Expected {expected_iso8601} but got {result}"
150-
151-
152-
@pytest.mark.parametrize(
153-
"datetime_string, possible_date_formats",
154-
[
155-
# Test cases that should raise ValueError
156-
("invalid date string", ["%Y-%m-%d %H:%M:%S"]),
157-
("2023-10-23 invalid", ["%Y-%m-%d %H:%M:%S"]),
158-
("23-10-2023", ["%Y-%m-%d %H:%M:%S"]),
159-
],
160-
)
161-
def test_parse_datetime_invalid(datetime_string, possible_date_formats):
162-
with pytest.raises(
163-
ValueError, match="Date and time could not be converted to ISO 8601 format."
164-
):
165-
parse_datetime(datetime_string, possible_date_formats)
1+
# Copyright The NOMAD Authors.
2+
#
3+
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
"""
18+
Tests for helper functions
19+
"""
20+
21+
import pytest
22+
import datetime
23+
from typing import Dict
24+
25+
from pynxtools_xps.reader_utils import extract_unit
26+
27+
from pynxtools_xps.value_mappers import get_units_for_key, parse_datetime
28+
29+
30+
@pytest.mark.parametrize(
31+
"unit_key, unit_map, expected_unit",
32+
[
33+
# Test cases with direct keys in the unit_map
34+
("detector/detector_voltage", {"detector/detector_voltage": "V"}, "V"),
35+
("temperature", {"temperature": "K"}, "K"),
36+
# Test cases with keys that don't exist in the unit_map
37+
("nonexistent_key", {"some_key": "m"}, None),
38+
# Test cases with regex pattern in the key
39+
(
40+
"detector/[detector_voltage]",
41+
{"detector/detector_voltage": "V"},
42+
"detector_voltage",
43+
),
44+
("sensor/[sensor_current]", {"sensor/sensor_current": "A"}, "sensor_current"),
45+
# Test cases with key that includes a regex but isn't mapped
46+
("not_mapped/[value]", {}, "value"),
47+
],
48+
)
49+
def test_get_units_for_key(unit_key: str, unit_map: Dict[str, str], expected_unit: str):
50+
result = get_units_for_key(unit_key, unit_map)
51+
assert result == expected_unit, f"Expected {expected_unit} but got {result}"
52+
53+
54+
@pytest.mark.parametrize(
55+
"key, value, unit_missing, expected_value, expected_unit",
56+
[
57+
# Test cases with explicit units
58+
("x_position", "0 mm", {}, 0, "mm"),
59+
("polar_rotation", "0 deg", {}, 0, "deg"),
60+
("analyser_work_function", "4.506eV", {}, 4.506, "eV"),
61+
("temperature", "300K", {}, 300, "K"),
62+
("pressure", "1.01bar", {}, 1.01, "bar"),
63+
# Test cases without explicit units
64+
("voltage", "5.0", {"voltage": "V"}, 5.0, "V"),
65+
("current", "10", {"current": "A"}, 10, "A"),
66+
# Test cases with scientific notation
67+
("distance", "1.23e-10m", {}, 1.23e-10, "m"),
68+
("charge", "1.602e-19C", {}, 1.602e-19, "C"),
69+
# Test cases with missing unit in value and unit_missing dictionary
70+
("energy", "1000", {"energy": "J"}, 1000, "J"),
71+
("mass", "0.5", {"mass": "kg"}, 0.5, "kg"),
72+
],
73+
)
74+
def test_extract_unit(
75+
key: str,
76+
value: str,
77+
unit_missing: Dict[str, str],
78+
expected_value: str,
79+
expected_unit: str,
80+
):
81+
result_value, result_unit = extract_unit(key, value, unit_missing)
82+
83+
assert isinstance(
84+
result_value, type(expected_value)
85+
), f"Expected type {type(expected_value)} but got type {type(result_value)}"
86+
87+
assert (
88+
result_value == expected_value
89+
), f"Expected {expected_value} but got {result_value}"
90+
assert (
91+
result_unit == expected_unit
92+
), f"Expected {expected_unit} but got {result_unit}"
93+
94+
95+
@pytest.mark.parametrize(
96+
"datetime_string, possible_date_formats, tzinfo, expected_iso8601",
97+
[
98+
# Test cases with valid datetime strings and formats
99+
(
100+
"2023-10-23 14:30:00",
101+
["%Y-%m-%d %H:%M:%S"],
102+
datetime.timezone.utc,
103+
"2023-10-23T14:30:00+00:00",
104+
),
105+
(
106+
"23/10/2023 14:30",
107+
["%d/%m/%Y %H:%M"],
108+
datetime.timezone.utc,
109+
"2023-10-23T14:30:00+00:00",
110+
),
111+
(
112+
"October 23, 2023 14:30",
113+
["%B %d, %Y %H:%M"],
114+
datetime.timezone.utc,
115+
"2023-10-23T14:30:00+00:00",
116+
),
117+
(
118+
"2023-10-23T14:30:00Z",
119+
["%Y-%m-%dT%H:%M:%SZ"],
120+
datetime.timezone.utc,
121+
"2023-10-23T14:30:00+00:00",
122+
),
123+
(
124+
"2023-10-23T14:30:00+0200",
125+
["%Y-%m-%dT%H:%M:%S%z"],
126+
datetime.timezone(datetime.timedelta(hours=2)),
127+
"2023-10-23T14:30:00+02:00",
128+
),
129+
# Test cases with timezone information
130+
(
131+
"2023-10-23 14:30:00",
132+
["%Y-%m-%d %H:%M:%S"],
133+
datetime.timezone(datetime.timedelta(hours=1)),
134+
"2023-10-23T14:30:00+01:00",
135+
),
136+
# Test case with missing timezone should still return UTC
137+
(
138+
"2023-10-23 14:30:00",
139+
["%Y-%m-%d %H:%M:%S"],
140+
None,
141+
"2023-10-23T14:30:00",
142+
),
143+
],
144+
)
145+
def test_parse_datetime(
146+
datetime_string, possible_date_formats, tzinfo, expected_iso8601
147+
):
148+
result = parse_datetime(datetime_string, possible_date_formats, tzinfo)
149+
assert result == expected_iso8601, f"Expected {expected_iso8601} but got {result}"
150+
151+
152+
@pytest.mark.parametrize(
153+
"datetime_string, possible_date_formats",
154+
[
155+
# Test cases that should raise ValueError
156+
("invalid date string", ["%Y-%m-%d %H:%M:%S"]),
157+
("2023-10-23 invalid", ["%Y-%m-%d %H:%M:%S"]),
158+
("23-10-2023", ["%Y-%m-%d %H:%M:%S"]),
159+
],
160+
)
161+
def test_parse_datetime_invalid(datetime_string, possible_date_formats):
162+
with pytest.raises(
163+
ValueError, match="Date and time could not be converted to ISO 8601 format."
164+
):
165+
parse_datetime(datetime_string, possible_date_formats)

0 commit comments

Comments
 (0)