Skip to content

Commit 9a6a678

Browse files
committed
unit tests - failures=1, errors=4
1 parent 9b1dd8a commit 9a6a678

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

recordprocessor/src/batch_processor.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ def process_csv_to_fhir(incoming_message_body: dict) -> None:
2020
"""
2121
encoder = "utf-8" # default encoding
2222
try:
23-
interim_message_body = file_level_validation(incoming_message_body=incoming_message_body, encoder=encoder)
23+
# and encoder to the incoming message body
24+
incoming_message_body["encoder"] = encoder
25+
interim_message_body = file_level_validation(incoming_message_body=incoming_message_body)
2426
except InvalidEncoding as error:
2527
logger.warning("Invalid Encoding detected in process_csv_to_fhir: %s", error)
2628
# retry with cp1252 encoding
2729
encoder = "cp1252"
2830
try:
29-
interim_message_body = file_level_validation(incoming_message_body=incoming_message_body, encoder=encoder)
31+
incoming_message_body["encoder"] = encoder
32+
interim_message_body = file_level_validation(incoming_message_body=incoming_message_body)
3033
except Exception as error:
3134
logger.error(f"Error in file_level_validation with {encoder} encoding: %s", error)
3235
return 0
@@ -102,8 +105,12 @@ def process_rows(file_id, vaccine, supplier, file_key, allowed_operations, creat
102105
total_rows_processed_count += 1
103106
logger.info("Total rows processed: %s", total_rows_processed_count)
104107
except Exception as error: # pylint: disable=broad-exception-caught
108+
# if error reason is 'invalid continuation byte', then it's a decode error
105109
logger.error("Error processing row %s: %s", row_count, error)
106-
return total_rows_processed_count, error
110+
if hasattr(error, 'reason') and error.reason == "invalid continuation byte":
111+
return total_rows_processed_count, error
112+
else:
113+
raise error
107114
return total_rows_processed_count, None
108115

109116

recordprocessor/src/file_level_validation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def move_file(bucket_name: str, source_file_key: str, destination_file_key: str)
6161

6262

6363
@file_level_validation_logging_decorator
64-
def file_level_validation(incoming_message_body: dict, encoder: str) -> dict:
64+
def file_level_validation(incoming_message_body: dict) -> dict:
6565
"""
6666
Validates that the csv headers are correct and that the supplier has permission to perform at least one of
6767
the requested operations. Uploades the inf ack file and moves the source file to the processing folder.
@@ -76,6 +76,7 @@ def file_level_validation(incoming_message_body: dict, encoder: str) -> dict:
7676
file_key = incoming_message_body.get("filename")
7777
permission = incoming_message_body.get("permission")
7878
created_at_formatted_string = incoming_message_body.get("created_at_formatted_string")
79+
encoder = incoming_message_body.get("encoder", "utf-8")
7980

8081
# Fetch the data
8182
csv_reader = get_csv_content_dict_reader(file_key, encoder=encoder)
@@ -99,9 +100,10 @@ def file_level_validation(incoming_message_body: dict, encoder: str) -> dict:
99100
"csv_dict_reader": csv_reader,
100101
}
101102
except (InvalidHeaders, NoOperationPermissions, Exception) as error:
102-
if error.reason == "invalid continuation byte" and encoder == "utf-8":
103-
# propagate the error to trigger a retry with cp1252 encoding
104-
raise InvalidEncoding(f"Error File encoding {encoder} is invalid.")
103+
reason = getattr(error, 'reason', None)
104+
if reason is not None:
105+
if reason == "invalid continuation byte" and encoder == "utf-8":
106+
raise InvalidEncoding(f"Error File encoding {encoder} is invalid.")
105107
logger.error("Error in file_level_validation: %s", error)
106108

107109
# NOTE: The Exception may occur before the file_id, file_key and created_at_formatted_string are assigned

0 commit comments

Comments
 (0)