Skip to content

Commit 718295a

Browse files
Add some tests
1 parent e80dbd1 commit 718295a

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/country_workspace/utils/fields.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
RecordPreprocessor = Callable[[Record], Record]
1515

1616

17+
TO_REMOVE = "_h_c", "_h_f", "_i_c", "_i_f"
18+
19+
1720
def clean_field_name(v: str) -> str:
1821
"""Normalize a field name by removing specific substrings (case-insensitive) and converting it to lowercase.
1922
@@ -24,8 +27,7 @@ def clean_field_name(v: str) -> str:
2427
str: The cleaned field name.
2528
2629
"""
27-
to_remove = ("_h_c", "_h_f", "_i_c", "_i_f")
28-
return reduce(lambda name, substr: name.replace(substr, ""), to_remove, v.lower())
30+
return reduce(lambda name, substr: name.replace(substr, ""), TO_REMOVE, v.lower())
2931

3032

3133
class ExtraFieldInRecordError(Exception):

tests/utils/test_utils_fields.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import pytest
1+
from unittest.mock import MagicMock
22

3-
from country_workspace.utils.fields import clean_field_name
3+
import pytest
44

5-
TO_REMOVE = ("_h_c", "_h_f", "_i_c", "_i_f")
5+
from country_workspace.utils.fields import (
6+
clean_field_name,
7+
TO_REMOVE,
8+
ExtraFieldInRecordError,
9+
create_json_record_preprocessor,
10+
)
611

712

813
@pytest.mark.parametrize(
@@ -15,3 +20,18 @@
1520
)
1621
def test_clean_field_name(input_value, expected_output):
1722
assert clean_field_name(input_value) == expected_output
23+
24+
25+
def test_extra_field_in_record_error_format() -> None:
26+
assert ", ".join(extra_fields := ("a", "b", "c")) in str(ExtraFieldInRecordError(*extra_fields))
27+
28+
29+
def test_create_json_record_preprocessor_raises_on_extra_fields() -> None:
30+
config = {
31+
"fail_if_alien": True,
32+
}
33+
checker = MagicMock()
34+
preprocessor = create_json_record_preprocessor(config, checker)
35+
36+
with pytest.raises(ExtraFieldInRecordError):
37+
preprocessor({"foo": "bar"})

0 commit comments

Comments
 (0)