Skip to content

Commit c0b5f45

Browse files
Added operators and tests.
1 parent 0456482 commit c0b5f45

File tree

2 files changed

+401
-36
lines changed

2 files changed

+401
-36
lines changed

src/eligibility_signposting_api/services/eligibility_services.py

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,16 @@ def evaluate_rule(iteration_rule: IterationRule, attribute_value: Any) -> bool:
142142
return int(attribute_value) < low_comparator or int(attribute_value) > high_comparator
143143

144144
case RuleOperator.is_empty:
145-
msg = f"{iteration_rule.operator} not implemented"
146-
raise NotImplementedError(msg)
145+
return (
146+
attribute_value is None or
147+
all(item.strip() == '' for item in attribute_value.split(','))
148+
)
147149

148150
case RuleOperator.is_not_empty:
149-
msg = f"{iteration_rule.operator} not implemented"
150-
raise NotImplementedError(msg)
151+
return (
152+
attribute_value is not None and
153+
any(item.strip() != '' for item in attribute_value.split(','))
154+
)
151155

152156
case RuleOperator.is_true:
153157
return attribute_value is True
@@ -161,12 +165,67 @@ def evaluate_rule(iteration_rule: IterationRule, attribute_value: Any) -> bool:
161165
cutoff = today_date + relativedelta(days=int(iteration_rule.comparator))
162166
return (attribute_date <= cutoff) if attribute_date else False
163167

168+
case RuleOperator.day_lt:
169+
attribute_date = datetime.strptime(str(attribute_value), '%Y%m%d') if attribute_value else None # noqa: DTZ007
170+
today_date = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
171+
cutoff = today_date + relativedelta(days=int(iteration_rule.comparator))
172+
return (attribute_date < cutoff) if attribute_date else False
173+
164174
case RuleOperator.day_gte:
165175
attribute_date = datetime.strptime(str(attribute_value),'%Y%m%d') if attribute_value else None # noqa: DTZ007
166176
today_date = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
167177
cutoff = today_date + relativedelta(days=int(iteration_rule.comparator))
168178
return (attribute_date >= cutoff) if attribute_date else False
169179

180+
case RuleOperator.day_gt:
181+
attribute_date = datetime.strptime(str(attribute_value),'%Y%m%d') if attribute_value else None # noqa: DTZ007
182+
today_date = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
183+
cutoff = today_date + relativedelta(days=int(iteration_rule.comparator))
184+
return (attribute_date > cutoff) if attribute_date else False
185+
186+
187+
case RuleOperator.week_lte:
188+
attribute_date = datetime.strptime(str(attribute_value), '%Y%m%d') if attribute_value else None # noqa: DTZ007
189+
today_date = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
190+
cutoff = today_date + relativedelta(weeks=int(iteration_rule.comparator))
191+
return (attribute_date <= cutoff) if attribute_date else False
192+
193+
case RuleOperator.week_lt:
194+
attribute_date = datetime.strptime(str(attribute_value), '%Y%m%d') if attribute_value else None # noqa: DTZ007
195+
today_date = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
196+
cutoff = today_date + relativedelta(weeks=int(iteration_rule.comparator))
197+
return (attribute_date < cutoff) if attribute_date else False
198+
199+
case RuleOperator.week_gte:
200+
attribute_date = datetime.strptime(str(attribute_value),'%Y%m%d') if attribute_value else None # noqa: DTZ007
201+
today_date = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
202+
cutoff = today_date + relativedelta(weeks=int(iteration_rule.comparator))
203+
return (attribute_date >= cutoff) if attribute_date else False
204+
205+
case RuleOperator.week_gt:
206+
attribute_date = datetime.strptime(str(attribute_value),'%Y%m%d') if attribute_value else None # noqa: DTZ007
207+
today_date = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
208+
cutoff = today_date + relativedelta(weeks=int(iteration_rule.comparator))
209+
return (attribute_date > cutoff) if attribute_date else False
210+
211+
212+
case RuleOperator.year_lte:
213+
attribute_date = datetime.strptime(str(attribute_value), '%Y%m%d') if attribute_value else None # noqa: DTZ007
214+
today_date = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
215+
cutoff = today_date + relativedelta(years=int(iteration_rule.comparator))
216+
return (attribute_date <= cutoff) if attribute_date else False
217+
218+
case RuleOperator.year_lt:
219+
attribute_date = datetime.strptime(str(attribute_value), '%Y%m%d') if attribute_value else None # noqa: DTZ007
220+
today_date = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
221+
cutoff = today_date + relativedelta(years=int(iteration_rule.comparator))
222+
return (attribute_date < cutoff) if attribute_date else False
223+
224+
case RuleOperator.year_gte:
225+
attribute_date = datetime.strptime(str(attribute_value),'%Y%m%d') if attribute_value else None # noqa: DTZ007
226+
today_date = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
227+
cutoff = today_date + relativedelta(years=int(iteration_rule.comparator))
228+
return (attribute_date >= cutoff) if attribute_date else False
170229

171230
case RuleOperator.year_gt:
172231
attribute_date = datetime.strptime(str(attribute_value),
@@ -175,6 +234,7 @@ def evaluate_rule(iteration_rule: IterationRule, attribute_value: Any) -> bool:
175234
cutoff = today + relativedelta(years=int(iteration_rule.comparator))
176235
return (attribute_date > cutoff) if attribute_date else False
177236

237+
178238
case _:
179239
msg = f"{iteration_rule.operator} not implemented"
180240
raise NotImplementedError(msg)

0 commit comments

Comments
 (0)