-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_derived_values.py
More file actions
433 lines (338 loc) · 16.8 KB
/
test_derived_values.py
File metadata and controls
433 lines (338 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
from unittest.mock import MagicMock
from hamcrest import assert_that, calling, equal_to, is_, raises, same_instance
from eligibility_signposting_api.services.processors.derived_values import (
AddDaysHandler,
DerivedValueContext,
DerivedValueHandler,
DerivedValueRegistry,
get_registry,
)
class TestAddDaysHandler:
"""Tests for the AddDaysHandler class."""
def test_calculate_adds_default_days_to_date(self):
"""Test that calculate adds the default days to a date."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args=None,
date_format=None,
)
result = handler.calculate(context)
# 2025-01-01 + 91 days = 2025-04-02
assert_that(result, is_(equal_to("20250402")))
def test_calculate_with_function_args_override(self):
"""Test that function args override default days."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args="30", # Override with 30 days
date_format=None,
)
result = handler.calculate(context)
# 2025-01-01 + 30 days = 2025-01-31
assert_that(result, is_(equal_to("20250131")))
def test_get_source_attribute_from_args(self):
"""Test that source attribute is extracted from function args."""
handler = AddDaysHandler()
# Test with source attribute provided
source = handler.get_source_attribute("target", "91, CUSTOM_SOURCE")
assert_that(source, is_(equal_to("CUSTOM_SOURCE")))
# Test with only days provided (should fallback to default mapping or target)
source = handler.get_source_attribute("NEXT_DOSE_DUE", "91")
assert_that(source, is_(equal_to("LAST_SUCCESSFUL_DATE")))
# Test with empty args
source = handler.get_source_attribute("NEXT_DOSE_DUE", None)
assert_that(source, is_(equal_to("LAST_SUCCESSFUL_DATE")))
def test_calculate_with_args_source_override(self):
"""Test calculation with source attribute in args."""
handler = AddDaysHandler(default_days=91)
# Note: In the real flow, get_source_attribute is called before context creation
# to set source_attribute in the context. Checking if calculate handles the complex args correctly
# for days parsing.
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "CUSTOM_DATE": "20250101"}],
attribute_name="COVID",
source_attribute="CUSTOM_DATE", # This would have been resolved by get_source_attribute
function_args="30, CUSTOM_DATE",
date_format=None,
)
result = handler.calculate(context)
# 2025-01-01 + 30 days = 2025-01-31
assert_that(result, is_(equal_to("20250131")))
def test_calculate_with_blank_days_and_source_override(self):
"""Test that blank days arg with override falls back to defaults."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args=", LAST_SUCCESSFUL_DATE",
date_format=None,
)
result = handler.calculate(context)
# No explicit days provided, so default 91 days should be used
assert_that(result, is_(equal_to("20250402")))
def test_source_override_trims_whitespace_and_case(self):
"""Test override parsing handles whitespace and lowercase inputs."""
handler = AddDaysHandler(default_days=91)
source = handler.get_source_attribute("DOSE_DUE", " 30 , last_successful_date ")
assert_that(source, is_(equal_to("LAST_SUCCESSFUL_DATE")))
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID",
source_attribute=source,
function_args=" 30 , last_successful_date ",
date_format=None,
)
result = handler.calculate(context)
# 2025-01-01 + 30 days = 2025-01-31
assert_that(result, is_(equal_to("20250131")))
def test_calculate_with_missing_custom_source_returns_empty(self):
"""Test that missing custom source attribute returns empty string."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID"}],
attribute_name="COVID",
source_attribute="CUSTOM_DATE",
function_args="30, CUSTOM_DATE",
date_format=None,
)
result = handler.calculate(context)
assert_that(result, is_(equal_to("")))
def test_calculate_with_vaccine_specific_days(self):
"""Test that vaccine-specific days are used when configured."""
handler = AddDaysHandler(
default_days=91,
vaccine_type_days={"FLU": 365},
)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "FLU", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="FLU",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args=None,
date_format=None,
)
result = handler.calculate(context)
# 2025-01-01 + 365 days = 2026-01-01
assert_that(result, is_(equal_to("20260101")))
def test_calculate_with_date_format(self):
"""Test that date format is applied to output."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args=None,
date_format="%d %B %Y",
)
result = handler.calculate(context)
assert_that(result, is_(equal_to("02 April 2025")))
def test_calculate_returns_empty_when_source_not_found(self):
"""Test that empty string is returned when source date not found."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID"}], # No LAST_SUCCESSFUL_DATE
attribute_name="COVID",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args=None,
date_format=None,
)
result = handler.calculate(context)
assert_that(result, is_(equal_to("")))
def test_calculate_returns_empty_when_vaccine_not_found(self):
"""Test that empty string is returned when vaccine type not found."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "FLU", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID", # Looking for COVID but data has FLU
source_attribute="LAST_SUCCESSFUL_DATE",
function_args=None,
date_format=None,
)
result = handler.calculate(context)
assert_that(result, is_(equal_to("")))
def test_calculate_with_invalid_date_raises_error(self):
"""Test that invalid date format raises ValueError."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "invalid"}],
attribute_name="COVID",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args=None,
date_format=None,
)
assert_that(
calling(handler.calculate).with_args(context),
raises(ValueError, pattern="Invalid date format"),
)
def test_calculate_with_invalid_function_args_raises_error(self):
"""Test that non-integer function args raises ValueError."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args="abc", # Invalid: not an integer
date_format=None,
)
assert_that(
calling(handler.calculate).with_args(context),
raises(ValueError, pattern="Invalid days argument 'abc' for ADD_DAYS function"),
)
def test_get_source_attribute_maps_derived_to_source(self):
"""Test that get_source_attribute maps derived attributes correctly."""
handler = AddDaysHandler()
assert_that(handler.get_source_attribute("NEXT_DOSE_DUE"), is_(equal_to("LAST_SUCCESSFUL_DATE")))
def test_get_source_attribute_returns_original_if_not_mapped(self):
"""Test that unmapped attributes return themselves."""
handler = AddDaysHandler()
assert_that(handler.get_source_attribute("UNKNOWN_ATTR"), is_(equal_to("UNKNOWN_ATTR")))
def test_function_args_priority_over_vaccine_config(self):
"""Test that function args take priority over vaccine-specific config."""
handler = AddDaysHandler(
default_days=91,
vaccine_type_days={"COVID": 120},
)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args="30", # Should take priority over 120
date_format=None,
)
result = handler.calculate(context)
# 2025-01-01 + 30 days = 2025-01-31
assert_that(result, is_(equal_to("20250131")))
def test_calculate_returns_empty_when_source_attribute_is_none(self):
"""Test that empty string is returned when source_attribute is None."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID",
source_attribute=None, # This should cause early return None in _find_source_date
function_args=None,
date_format=None,
attribute_level="TARGET",
)
result = handler.calculate(context)
assert_that(result, is_(equal_to("")))
def test_calculate_returns_empty_when_source_attribute_is_empty(self):
"""Test that empty string is returned when source_attribute is empty string."""
handler = AddDaysHandler(default_days=91)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID",
source_attribute="", # This should cause early return None in _find_source_date
function_args=None,
date_format=None,
attribute_level="TARGET",
)
result = handler.calculate(context)
assert_that(result, is_(equal_to("")))
class TestDerivedValueRegistry:
"""Tests for the DerivedValueRegistry class."""
def test_register_and_get_handler(self):
"""Test registering and retrieving a handler."""
registry = DerivedValueRegistry()
handler = AddDaysHandler()
registry.register(handler)
retrieved = registry.get_handler("ADD_DAYS")
assert_that(retrieved, same_instance(handler)) # type: ignore[call-overload]
def test_get_handler_case_insensitive(self):
"""Test that handler lookup is case insensitive."""
registry = DerivedValueRegistry()
handler = AddDaysHandler()
registry.register(handler)
assert_that(registry.get_handler("add_days"), same_instance(handler)) # type: ignore[call-overload]
assert_that(registry.get_handler("Add_Days"), same_instance(handler)) # type: ignore[call-overload]
def test_has_handler_returns_true_when_exists(self):
"""Test has_handler returns True for registered handlers."""
registry = DerivedValueRegistry()
registry.register(AddDaysHandler())
assert_that(registry.has_handler("ADD_DAYS"), is_(True))
def test_has_handler_returns_false_when_not_exists(self):
"""Test has_handler returns False for unregistered handlers."""
registry = DerivedValueRegistry()
assert_that(registry.has_handler("UNKNOWN"), is_(False))
def test_calculate_delegates_to_correct_handler(self):
"""Test that calculate delegates to the correct handler."""
registry = DerivedValueRegistry()
# Create a mock handler
mock_handler = MagicMock()
mock_handler.function_name = "TEST_FUNC"
mock_handler.calculate.return_value = "mock_result"
# Register both real and mock handlers
registry.register(AddDaysHandler(default_days=91))
registry.register(mock_handler)
context = DerivedValueContext(
person_data=[{"ATTRIBUTE_TYPE": "COVID", "LAST_SUCCESSFUL_DATE": "20250101"}],
attribute_name="COVID",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args=None,
date_format=None,
)
# Call with the mock handler's function name
result = registry.calculate(function_name="TEST_FUNC", context=context)
# Verify the mock handler was called with the context
mock_handler.calculate.assert_called_once_with(context)
assert_that(result, is_(equal_to("mock_result")))
def test_calculate_raises_for_unknown_function(self):
"""Test that calculate raises ValueError for unknown functions."""
registry = DerivedValueRegistry()
context = DerivedValueContext(
person_data=[],
attribute_name="COVID",
source_attribute="LAST_SUCCESSFUL_DATE",
function_args=None,
date_format=None,
)
assert_that(
calling(registry.calculate).with_args(function_name="UNKNOWN", context=context),
raises(ValueError, pattern="No handler registered"),
)
def test_is_derived_attribute_returns_true_for_derived(self):
"""Test is_derived_attribute for known derived attributes."""
registry = DerivedValueRegistry()
registry.register(AddDaysHandler())
assert_that(registry.is_derived_attribute("NEXT_DOSE_DUE"), is_(True))
def test_is_derived_attribute_returns_false_for_non_derived(self):
"""Test is_derived_attribute for non-derived attributes."""
registry = DerivedValueRegistry()
registry.register(AddDaysHandler())
assert_that(registry.is_derived_attribute("LAST_SUCCESSFUL_DATE"), is_(False))
def test_default_handlers_are_registered(self):
"""Test that default handlers from the module are registered."""
registry = DerivedValueRegistry()
# The default ADD_DAYS handler should be registered via __init__.py
assert_that(registry.has_handler("ADD_DAYS"), is_(True))
def test_global_registry_has_default_handlers(self):
"""Test that the exported registry singleton sees default handlers."""
registry = get_registry()
assert_that(registry.has_handler("ADD_DAYS"), is_(True))
def test_register_normalizes_function_name(self):
"""Test that registering handlers works regardless of name casing."""
class LowercaseHandler(DerivedValueHandler):
function_name = "custom_func"
def calculate(self, context: DerivedValueContext) -> str: # noqa: ARG002
return ""
def get_source_attribute(self, target_attribute: str, function_args: str | None = None) -> str: # noqa: ARG002
return target_attribute
registry = DerivedValueRegistry()
registry.register(LowercaseHandler())
assert_that(registry.has_handler("CUSTOM_FUNC"), is_(True))
def test_clear_defaults_removes_default_handlers(self):
"""Test that clear_defaults removes all default handlers."""
# Save current defaults using public method
saved_defaults = DerivedValueRegistry.get_default_handlers()
try:
DerivedValueRegistry.clear_defaults()
# New registry should have no handlers
registry = DerivedValueRegistry()
assert_that(registry.has_handler("ADD_DAYS"), is_(False))
finally:
# Restore defaults for other tests using public method
DerivedValueRegistry.set_default_handlers(saved_defaults)