Skip to content

Commit b566594

Browse files
Merge pull request #76 from NHSDigital/feature/ELI-237-offset-date-operators
ELI-237 Offset date operators
2 parents e63b214 + f66e4ea commit b566594

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/eligibility_signposting_api/services/rules/operators.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,12 @@ def _matches(self, item: str | None) -> bool:
177177

178178

179179
class RangeOperator(Operator, ABC):
180-
def __init__(self, rule_value: str) -> None:
181-
super().__init__(rule_value=rule_value)
180+
low_comparator: int
181+
high_comparator: int
182+
183+
def __post_init__(self) -> None:
184+
super().__post_init__()
185+
182186
low_comparator_str, high_comparator_str = str(self.rule_value).split(",")
183187
self.low_comparator = min(int(low_comparator_str), int(high_comparator_str))
184188
self.high_comparator = max(int(low_comparator_str), int(high_comparator_str))
@@ -227,8 +231,17 @@ def _matches(self, item: str | None) -> bool:
227231

228232

229233
class DateOperator(Operator, ABC):
234+
OFFSET_PATTERN: ClassVar[str] = r"(?P<rule_value>[^\[]+)\[\[OFFSET:(?P<offset>\d{8})\]\]"
230235
delta_type: ClassVar[str]
231236
comparator: ClassVar[Callable[[date, date], bool]]
237+
offset: date | None = None
238+
239+
def __post_init__(self) -> None:
240+
super().__post_init__()
241+
242+
if self.rule_value and (match := re.match(self.OFFSET_PATTERN, self.rule_value)):
243+
self.rule_value = match.group("rule_value")
244+
self.offset = datetime.strptime(match.group("offset"), "%Y%m%d").replace(tzinfo=UTC).date()
232245

233246
@property
234247
def today(self) -> date:
@@ -242,7 +255,7 @@ def get_attribute_date(item: str | None) -> date | None:
242255
def cutoff(self) -> date:
243256
delta = relativedelta()
244257
setattr(delta, self.delta_type, int(self.rule_value))
245-
return self.today + delta
258+
return (self.offset if self.offset else self.today) + delta
246259

247260
def _matches(self, item: str | None) -> bool:
248261
item = item if item is not None else self.item_default

tests/unit/services/operators/test_operators.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@
519519
("20260425", RuleOperator.year_gte, "2", False, "Past year"),
520520
("20270425", RuleOperator.year_gte, "2", True, "Present year"),
521521
("20280425", RuleOperator.year_gte, "2", True, "Future year"),
522+
("19680720", RuleOperator.year_gte, "-57[[OFFSET:20250721]]", False, "Include anyone older than Simon"),
523+
("19680721", RuleOperator.year_gte, "-57[[OFFSET:20250721]]", True, "Exclude anyone the same age as Simon"),
524+
("19680722", RuleOperator.year_gte, "-57[[OFFSET:20250721]]", True, "Exclude anyone younger than Simon"),
522525
]
523526

524527
# Year greater than
@@ -529,6 +532,9 @@
529532
("20200424", RuleOperator.year_gt, "-5", False, "Exclude anyone under 5 on the day - 5 yesterday"),
530533
("20200425", RuleOperator.year_gt, "-5", False, "Exclude anyone under 5 on the day- 5 today"),
531534
("20200426", RuleOperator.year_gt, "-5", True, "Exclude anyone under 5 on the day - 5 tomorrow"),
535+
("19680720", RuleOperator.year_gt, "-57[[OFFSET:20250721]]", False, "Include anyone older than Simon"),
536+
("19680721", RuleOperator.year_gt, "-57[[OFFSET:20250721]]", False, "Include anyone the same age as Simon"),
537+
("19680722", RuleOperator.year_gt, "-57[[OFFSET:20250721]]", True, "Exclude anyone younger than Simon"),
532538
]
533539

534540

0 commit comments

Comments
 (0)