Skip to content

Commit f5b0b51

Browse files
author
ASubaran
committed
added code
1 parent 6d96c20 commit f5b0b51

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

e2e_batch/utils.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def wait_for_ack_file(ack_prefix, input_file_name, timeout=120):
104104
return key
105105
time.sleep(5)
106106
raise AssertionError(
107-
f"Ack file matching '{search_pattern}' not found in bucket {ACK_BUCKET} within {timeout} seconds."
108-
)
107+
f"Ack file matching '{search_pattern}' not found in bucket {ACK_BUCKET} within {timeout} seconds."
108+
)
109109

110110

111111
def get_file_content_from_s3(bucket, key):
@@ -116,45 +116,54 @@ def get_file_content_from_s3(bucket, key):
116116

117117

118118
def check_ack_file_content(content, response_code, operation_outcome):
119-
"""Parse the acknowledgment (ACK) CSV file and verify that:
120-
1. 'HEADER_RESPONSE_CODE' matches the expected response code.
121-
2. If 'HEADER_RESPONSE_CODE' is 'Fatal Error', check 'OPERATION_OUTCOME'.
122-
3. If 'OPERATION_OUTCOME' is 'Ok', extract 'LOCAL_ID', convert it to IdentifierPK,
123-
fetch PK from DynamoDB, and ensure it matches 'IMMS_ID'."""
124-
119+
"""Parse the acknowledgment (ACK) CSV file and verify its content."""
125120
reader = csv.DictReader(content.splitlines(), delimiter="|")
126121
rows = list(reader)
127122
for i, row in enumerate(rows):
128-
if "HEADER_RESPONSE_CODE" not in row:
129-
raise AssertionError(f"Row {i + 1} does not have a 'HEADER_RESPONSE_CODE' column.")
130-
if row["HEADER_RESPONSE_CODE"].strip() != response_code:
131-
raise AssertionError(
132-
f"Row {i + 1}: Expected RESPONSE '{response_code}', but found '{row['HEADER_RESPONSE_CODE']}'"
133-
)
123+
validate_header_response_code(row, i, response_code)
134124
if row["HEADER_RESPONSE_CODE"].strip() == "Fatal Error":
135-
if row["OPERATION_OUTCOME"].strip() != operation_outcome:
136-
raise AssertionError(
137-
f"Row {i + 1}: Expected RESPONSE '{operation_outcome}', but found '{row['OPERATION_OUTCOME']}'"
138-
)
139-
125+
validate_fatal_error(row, i, operation_outcome)
140126
if row["HEADER_RESPONSE_CODE"].strip() == "OK":
141-
if "LOCAL_ID" not in row:
142-
raise AssertionError(f"Row {i + 1} does not have a 'LOCAL_ID' column.")
143-
144-
# Extract LOCAL_ID and convert to IdentifierPK
145-
try:
146-
local_id, unique_id_uri = row["LOCAL_ID"].split("^")
147-
identifier_pk = f"{unique_id_uri}#{local_id}"
148-
except ValueError:
149-
raise AssertionError(f"Row {i + 1}: Invalid LOCAL_ID format - {row['LOCAL_ID']}")
150-
151-
# Fetch PK from DynamoDB
152-
dynamo_pk = fetch_pk_from_dynamodb(identifier_pk)
153-
# Compare DynamoDB PK with IMMS_ID
154-
if dynamo_pk != row["IMMS_ID"]:
155-
raise AssertionError(
156-
f"Row {i + 1}: Mismatch - DynamoDB PK '{dynamo_pk}' does not match ACK file IMMS_ID '{row['IMMS_ID']}'"
157-
)
127+
validate_ok_response(row, i)
128+
129+
130+
def validate_header_response_code(row, index, expected_code):
131+
"""Ensure HEADER_RESPONSE_CODE exists and matches expected response code."""
132+
if "HEADER_RESPONSE_CODE" not in row:
133+
raise AssertionError(f"Row {index + 1} does not have a 'HEADER_RESPONSE_CODE' column.")
134+
if row["HEADER_RESPONSE_CODE"].strip() != expected_code:
135+
raise AssertionError(
136+
f"Row {index + 1}: Expected RESPONSE '{expected_code}', but found '{row['HEADER_RESPONSE_CODE']}'"
137+
)
138+
139+
140+
def validate_fatal_error(row, index, expected_outcome):
141+
"""Ensure OPERATION_OUTCOME matches expected outcome for Fatal Error responses."""
142+
if row["OPERATION_OUTCOME"].strip() != expected_outcome:
143+
raise AssertionError(
144+
f"Row {index + 1}: Expected RESPONSE '{expected_outcome}', but found '{row['OPERATION_OUTCOME']}'"
145+
)
146+
147+
148+
def validate_ok_response(row, index):
149+
"""Validate LOCAL_ID format and verify PK match from DynamoDB for OK responses."""
150+
if "LOCAL_ID" not in row:
151+
raise AssertionError(f"Row {index + 1} does not have a 'LOCAL_ID' column.")
152+
identifier_pk = extract_identifier_pk(row, index)
153+
dynamo_pk = fetch_pk_from_dynamodb(identifier_pk)
154+
if dynamo_pk != row["IMMS_ID"]:
155+
raise AssertionError(
156+
f"Row {index + 1}: Mismatch - DynamoDB PK '{dynamo_pk}' does not match ACK file IMMS_ID '{row['IMMS_ID']}'"
157+
)
158+
159+
160+
def extract_identifier_pk(row, index):
161+
"""Extract LOCAL_ID and convert to IdentifierPK."""
162+
try:
163+
local_id, unique_id_uri = row["LOCAL_ID"].split("^")
164+
return f"{unique_id_uri}#{local_id}"
165+
except ValueError:
166+
raise AssertionError(f"Row {index + 1}: Invalid LOCAL_ID format - {row['LOCAL_ID']}")
158167

159168

160169
def fetch_pk_from_dynamodb(identifier_pk):

0 commit comments

Comments
 (0)