Skip to content

Commit e373cc1

Browse files
eli 425 - handle value error in token replace (#327)
Co-authored-by: ayeshalshukri1-nhs <[email protected]>
1 parent 3d56b43 commit e373cc1

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/eligibility_signposting_api/services/processors/token_processor.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ def handle_token_not_found(parsed_token: ParsedToken, token: str) -> Never:
118118
raise ValueError(message)
119119

120120
@staticmethod
121-
def apply_formatting[T](attribute: dict[str, T], attribute_value: str, date_format: str | None) -> str:
121+
def apply_formatting[T](attributes: dict[str, T], attribute_name: str, date_format: str | None) -> str:
122122
try:
123-
attribute_data = attribute.get(attribute_value)
123+
attribute_data = attributes.get(attribute_name)
124124
if (date_format or date_format == "") and attribute_data:
125125
replace_with_date_object = datetime.strptime(str(attribute_data), "%Y%m%d").replace(tzinfo=UTC)
126126
replace_with = replace_with_date_object.strftime(str(date_format))
@@ -130,3 +130,6 @@ def apply_formatting[T](attribute: dict[str, T], attribute_value: str, date_form
130130
except AttributeError as error:
131131
message = "Invalid token format"
132132
raise AttributeError(message) from error
133+
except Exception as error:
134+
message = "Invalid value error"
135+
raise ValueError(message) from error

tests/unit/processor/__init__.py

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pytest
2+
3+
from eligibility_signposting_api.services.processors.token_processor import TokenProcessor
4+
5+
6+
@pytest.mark.parametrize(
7+
("date_format", "expected"),
8+
[
9+
(None, "20250510"),
10+
("%Y-%m-%d", "2025-05-10"),
11+
("%d/%m/%Y", "10/05/2025"),
12+
("%b %d, %Y", "May 10, 2025"),
13+
("%A, %d %B %Y", "Saturday, 10 May 2025"),
14+
("", ""),
15+
],
16+
)
17+
def test_apply_formatting_returns_valid_value(date_format, expected):
18+
attribute = {"DATE_OF_BIRTH": "20250510"}
19+
attribute_value = "DATE_OF_BIRTH"
20+
21+
actual = TokenProcessor.apply_formatting(attribute, attribute_value, date_format)
22+
assert actual == expected
23+
24+
25+
def test_apply_formatting_attribute_not_present():
26+
invalid_attribute = {"DATE_OF_BIRTH": "20250510"}
27+
attribute_value = "ICE_CREAM"
28+
date_format = None
29+
30+
actual = TokenProcessor.apply_formatting(invalid_attribute, attribute_value, date_format)
31+
32+
assert actual == ""
33+
34+
35+
def test_apply_formatting_raises_attribute_error():
36+
invalid_attribute = "not_a_dict"
37+
attribute_value = "DATE_OF_BIRTH"
38+
date_format = None
39+
40+
with pytest.raises(AttributeError) as exc_info:
41+
TokenProcessor.apply_formatting(invalid_attribute, attribute_value, date_format)
42+
43+
assert isinstance(exc_info.value, AttributeError)
44+
assert str(exc_info.value) == "Invalid token format"
45+
46+
47+
def test_apply_formatting_raises_value_error_on_invalid_date():
48+
attribute = {"DATE_OF_BIRTH": "invalid"}
49+
attribute_value = "DATE_OF_BIRTH"
50+
date_format = "%Y-%m-%d"
51+
52+
with pytest.raises(ValueError, match="Invalid value error$"):
53+
TokenProcessor.apply_formatting(attribute, attribute_value, date_format)

0 commit comments

Comments
 (0)