Skip to content

Commit 46a1e50

Browse files
committed
Refactor expected MNS fields population in API tests to handle unset values gracefully. Added checks to prevent overwriting existing context attributes for GP code and patient age, and improved error handling for age calculation to avoid failures during teardown.
1 parent 331ce47 commit 46a1e50

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

tests/e2e_automation/features/APITests/steps/common_steps.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,18 @@ def normalize(value: str) -> str:
438438

439439

440440
def populate_expected_mns_fields(context):
441-
context.gp_code = get_gp_code_by_nhs_number(context.patient.identifier[0].value)
442-
context.patient_age = calculate_age(context.patient.birthDate, context.immunization_object.occurrenceDateTime)
441+
if getattr(context, "gp_code", None) is None:
442+
context.gp_code = get_gp_code_by_nhs_number(context.patient.identifier[0].value)
443+
444+
if getattr(context, "patient_age", None) is None:
445+
try:
446+
context.patient_age = calculate_age(
447+
context.patient.birthDate, context.immunization_object.occurrenceDateTime
448+
)
449+
except ValueError:
450+
# Some negative update scenarios intentionally inject invalid DOB values.
451+
# Keep patient_age unset here instead of failing teardown validation setup.
452+
context.patient_age = None
443453

444454

445455
def validate_sqs_message(context, message_body, action):
@@ -483,10 +493,11 @@ def validate_sqs_message(context, message_body, action):
483493
f"msn event for {action} Source application mismatch: expected {context.supplier_name}, got {message_body.filtering.sourceapplication}",
484494
)
485495

486-
check.is_true(
487-
message_body.filtering.subjectage == context.patient_age,
488-
f"msn event for {action} Age mismatch: expected {context.patient_age}, got {message_body.filtering.subjectage}",
489-
)
496+
if context.patient_age is not None:
497+
check.is_true(
498+
message_body.filtering.subjectage == context.patient_age,
499+
f"msn event for {action} Age mismatch: expected {context.patient_age}, got {message_body.filtering.subjectage}",
500+
)
490501

491502
check.is_true(
492503
message_body.filtering.immunisationtype == context.vaccine_type.upper(),

0 commit comments

Comments
 (0)