-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_derived_values.py
More file actions
310 lines (257 loc) · 11.5 KB
/
test_derived_values.py
File metadata and controls
310 lines (257 loc) · 11.5 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
"""
Integration tests for derived values functionality.
These tests verify the end-to-end flow of the ADD_DAYS derived value function,
demonstrating how NEXT_DOSE_DUE is calculated from LAST_SUCCESSFUL_DATE.
Example API response format:
{
"processedSuggestions": [
{
"actions": [
{
"actionType": "DataValue",
"actionCode": "DateOfLastVaccination",
"description": "20260128"
},
{
"actionType": "DataValue",
"actionCode": "DateOfNextEarliestVaccination",
"description": "20260429"
}
]
}
]
}
"""
from http import HTTPStatus
from botocore.client import BaseClient
from brunns.matchers.data import json_matching as is_json_that
from brunns.matchers.werkzeug import is_werkzeug_response as is_response
from flask.testing import FlaskClient
from hamcrest import (
all_of,
assert_that,
greater_than_or_equal_to,
has_entries,
has_item,
has_key,
has_length,
is_not,
none,
)
from eligibility_signposting_api.model.consumer_mapping import ConsumerId, ConsumerMapping
from eligibility_signposting_api.model.eligibility_status import NHSNumber
from tests.integration.conftest import UNIQUE_CONSUMER_HEADER
class TestDerivedValues:
"""Integration tests for the ADD_DAYS derived value functionality."""
def test_add_days_calculates_next_dose_due_from_last_successful_date(
self,
client: FlaskClient,
person_with_covid_vaccination: NHSNumber,
consumer_id: ConsumerId,
consumer_to_active_campaign_config_with_derived_values_mapping: ConsumerMapping, # noqa: ARG002
secretsmanager_client: BaseClient, # noqa: ARG002
):
"""
Test that the ADD_DAYS function correctly calculates the next dose date.
Given:
- A person with COVID vaccination on 2026-01-28 (20260128)
- A campaign config with actions using:
- [[TARGET.COVID.LAST_SUCCESSFUL_DATE]] for DateOfLastVaccination
- [[TARGET.COVID.NEXT_DOSE_DUE:ADD_DAYS(91)]] for DateOfNextEarliestVaccination
Expected:
- DateOfLastVaccination shows "20260128"
- DateOfNextEarliestVaccination shows "20260429" (2026-01-28 + 91 days = 2026-04-29)
This demonstrates the use case from the requirement:
"actions": [
{"actionType": "DataValue", "actionCode": "DateOfLastVaccination", "description": "20260128"},
{"actionType": "DataValue", "actionCode": "DateOfNextEarliestVaccination", "description": "20260429"}
]
"""
# Given
headers = {"nhs-login-nhs-number": str(person_with_covid_vaccination), UNIQUE_CONSUMER_HEADER: str(consumer_id)}
# When
response = client.get(
f"/patient-check/{person_with_covid_vaccination}?includeActions=Y",
headers=headers,
)
# Then - verify response is successful
assert_that(
response,
is_response().with_status_code(HTTPStatus.OK).and_text(is_json_that(has_key("processedSuggestions"))),
)
# Extract the processed suggestions
body = response.get_json()
assert_that(body, is_not(none()))
processed_suggestions = body.get("processedSuggestions", [])
# Find the COVID condition
covid_suggestion = next(
(s for s in processed_suggestions if s.get("condition") == "COVID"),
None,
)
assert_that(covid_suggestion, is_not(none()))
# Extract actions
actions = covid_suggestion.get("actions", []) # type: ignore[union-attr]
expected_actions_count = 2
assert_that(actions, has_length(greater_than_or_equal_to(expected_actions_count)))
# Verify DateOfLastVaccination shows the raw date
assert_that(
actions,
has_item(has_entries(actionType="DataValue", actionCode="DateOfLastVaccination", description="20260128")),
)
# Verify DateOfNextEarliestVaccination shows the calculated date (2026-01-28 + 91 days = 2026-04-29)
assert_that(
actions,
has_item(
has_entries(actionType="DataValue", actionCode="DateOfNextEarliestVaccination", description="20260429")
),
)
def test_add_days_with_formatted_date_output(
self,
client: FlaskClient,
person_with_covid_vaccination: NHSNumber,
consumer_id: ConsumerId,
consumer_to_active_campaign_config_with_derived_values_formatted_mapping: ConsumerMapping, # noqa: ARG002
secretsmanager_client: BaseClient, # noqa: ARG002
):
"""
Test that ADD_DAYS can be combined with DATE formatting.
Given:
- A person with COVID vaccination on 2026-01-28
- A campaign config using [[TARGET.COVID.NEXT_DOSE_DUE:ADD_DAYS(91):DATE(%d %B %Y)]]
Expected:
- DateOfNextEarliestVaccination shows "29 April 2026" (formatted output)
"""
# Given
headers = {"nhs-login-nhs-number": str(person_with_covid_vaccination), UNIQUE_CONSUMER_HEADER: str(consumer_id)}
# When
response = client.get(
f"/patient-check/{person_with_covid_vaccination}?includeActions=Y",
headers=headers,
)
# Then
assert_that(
response,
is_response().with_status_code(HTTPStatus.OK).and_text(is_json_that(has_key("processedSuggestions"))),
)
body = response.get_json()
assert_that(body, is_not(none()))
processed_suggestions = body.get("processedSuggestions", [])
covid_suggestion = next(
(s for s in processed_suggestions if s.get("condition") == "COVID"),
None,
)
assert_that(covid_suggestion, is_not(none()))
actions = covid_suggestion.get("actions", []) # type: ignore[union-attr]
# Verify the formatted date output
assert_that(
actions,
has_item(has_entries(actionCode="DateOfNextEarliestVaccination", description="29 April 2026")),
)
class TestMultipleActionsWithAddDays:
"""Test that multiple actions can use ADD_DAYS with different parameters."""
def test_multiple_actions_with_different_add_days_parameters(
self,
client: FlaskClient,
consumer_id: ConsumerId,
consumer_to_active_campaign_config_with_multiple_add_days_mapping: ConsumerMapping, # noqa: ARG002
person_with_covid_vaccination: NHSNumber,
):
"""
Test that multiple actions can use the same function (ADD_DAYS) with different parameters.
This test verifies that when a campaign has multiple actions that use ADD_DAYS with different
parameters (e.g., ADD_DAYS(91) and ADD_DAYS(61)), both calculations are performed correctly.
Given:
- A person with a COVID vaccination date of 2026-01-28 (20260128)
- A campaign config with three actions:
1. DateOfLastVaccination: Returns the raw LAST_SUCCESSFUL_DATE
2. DateOfNextDoseAt91Days: Returns NEXT_DOSE_DUE with ADD_DAYS(91)
3. DateOfNextDoseAt61Days: Returns NEXT_DOSE_DUE with ADD_DAYS(61)
When:
- The /patient-check endpoint is called with includeActions=Y
Then:
- DateOfLastVaccination should be "20260128" (raw date)
- DateOfNextDoseAt91Days should be "20260429" (2026-01-28 + 91 days)
- DateOfNextDoseAt61Days should be "20260330" (2026-01-28 + 61 days)
This addresses the reviewer's suggestion to test multiple actions using the same
function with different parameters.
"""
# Given
headers = {"nhs-login-nhs-number": str(person_with_covid_vaccination), UNIQUE_CONSUMER_HEADER: str(consumer_id)}
# When
response = client.get(
f"/patient-check/{person_with_covid_vaccination}?includeActions=Y",
headers=headers,
)
# Then
assert_that(
response,
is_response().with_status_code(HTTPStatus.OK).and_text(is_json_that(has_key("processedSuggestions"))),
)
body = response.get_json()
assert_that(body, is_not(none()))
processed_suggestions = body.get("processedSuggestions", [])
covid_suggestion = next(
(s for s in processed_suggestions if s.get("condition") == "COVID"),
None,
)
assert_that(covid_suggestion, is_not(none()))
actions = covid_suggestion.get("actions", []) # type: ignore[union-attr]
# Verify all three actions are present with correct values
assert_that(
actions,
all_of(
has_item(has_entries(actionCode="DateOfLastVaccination", description="20260128")),
has_item(has_entries(actionCode="DateOfNextDoseAt91Days", description="20260429")),
has_item(has_entries(actionCode="DateOfNextDoseAt61Days", description="20260330")),
),
)
class TestCustomTargetAttributeNames:
"""Test that custom target attribute names work with derived values in integration."""
def test_custom_target_attribute_with_derived_value(
self,
client: FlaskClient,
person_with_covid_vaccination: NHSNumber,
consumer_id: ConsumerId,
consumer_to_active_campaign_config_with_custom_target_attributes_mapping: ConsumerMapping, # noqa: ARG002
secretsmanager_client: BaseClient, # noqa: ARG002
):
"""
Test that custom target attribute names like NEXT_BOOKING_AVAILABLE work with derived values.
This tests the issue reported in production where:
[[TARGET.COVID.NEXT_BOOKING_AVAILABLE:ADD_DAYS(71, LAST_SUCCESSFUL_DATE):DATE(%d %B %Y)]]
was raising a ValueError.
Given:
- A person with COVID vaccination on 2026-01-28
- A campaign config using a custom target attribute: NEXT_BOOKING_AVAILABLE
- The token: [[TARGET.COVID.NEXT_BOOKING_AVAILABLE:ADD_DAYS(71, LAST_SUCCESSFUL_DATE):DATE(%d %B %Y)]]
Expected:
- Should calculate 2026-01-28 + 71 days = 2026-04-09
- Should format as "09 April 2026"
- Should NOT raise ValueError about invalid attribute name
"""
# Given
headers = {"nhs-login-nhs-number": str(person_with_covid_vaccination), UNIQUE_CONSUMER_HEADER: str(consumer_id)}
# When
response = client.get(
f"/patient-check/{person_with_covid_vaccination}?includeActions=Y",
headers=headers,
)
# Then
assert_that(
response,
is_response().with_status_code(HTTPStatus.OK).and_text(is_json_that(has_key("processedSuggestions"))),
)
body = response.get_json()
assert_that(body, is_not(none()))
processed_suggestions = body.get("processedSuggestions", [])
covid_suggestion = next(
(s for s in processed_suggestions if s.get("condition") == "COVID"),
None,
)
assert_that(covid_suggestion, is_not(none()))
actions = covid_suggestion.get("actions", []) # type: ignore[union-attr]
# Verify the custom target attribute with derived value works correctly
assert_that(
actions,
has_item(has_entries(actionCode="NextBookingAvailable", description="09 April 2026")),
)