|
| 1 | +import unittest |
| 2 | +import os |
| 3 | +from io import BytesIO |
| 4 | +from unittest.mock import patch |
| 5 | +from batch_processor import process_csv_to_fhir |
| 6 | +from tests.utils_for_recordprocessor_tests.utils_for_recordprocessor_tests import create_patch |
| 7 | + |
| 8 | + |
| 9 | +class TestProcessorEdgeCases(unittest.TestCase): |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + self.mock_logger_info = create_patch("logging.Logger.info") |
| 13 | + self.mock_logger_warning = create_patch("logging.Logger.warning") |
| 14 | + self.mock_logger_error = create_patch("logging.Logger.error") |
| 15 | + self.mock_send_to_kinesis = create_patch("batch_processor.send_to_kinesis") |
| 16 | + self.mock_map_target_disease = create_patch("batch_processor.map_target_disease") |
| 17 | + self.mock_s3_get_object = create_patch("utils_for_recordprocessor.s3_client.get_object") |
| 18 | + self.mock_s3_put_object = create_patch("utils_for_recordprocessor.s3_client.put_object") |
| 19 | + self.mock_make_and_move = create_patch("file_level_validation.make_and_upload_ack_file") |
| 20 | + self.mock_move_file = create_patch("file_level_validation.move_file") |
| 21 | + self.mock_get_permitted_operations = create_patch("file_level_validation.get_permitted_operations") |
| 22 | + self.mock_firehose_client = create_patch("logging_decorator.firehose_client") |
| 23 | + self.mock_update_audit_table_status = create_patch("batch_processor.update_audit_table_status") |
| 24 | + |
| 25 | + def tearDown(self): |
| 26 | + patch.stopall() |
| 27 | + |
| 28 | + def expand_test_data(self, data: list[bytes], num_rows: int) -> list[bytes]: |
| 29 | + n_rows = len(data) - 1 # Exclude header |
| 30 | + |
| 31 | + if n_rows < num_rows: |
| 32 | + multiplier = (num_rows // n_rows) + 1 |
| 33 | + header = data[0:1] |
| 34 | + body = data[1:] * multiplier |
| 35 | + data = header + body |
| 36 | + data = data[:num_rows + 1] |
| 37 | + return data |
| 38 | + |
| 39 | + def create_test_data_from_file(self, file_name: str) -> list[bytes]: |
| 40 | + test_csv_path = os.path.join( |
| 41 | + os.path.dirname(__file__), "test_data", file_name |
| 42 | + ) |
| 43 | + with open(test_csv_path, "rb") as f: |
| 44 | + data = f.readlines() |
| 45 | + return data |
| 46 | + |
| 47 | + def insert_cp1252_at_end(self, data: list[bytes], new_text: bytes, field: int) -> list[bytes]: |
| 48 | + for i in reversed(range(len(data))): |
| 49 | + line = data[i] |
| 50 | + # Split fields by pipe |
| 51 | + fields = line.strip().split(b"|") |
| 52 | + fields[field] = new_text |
| 53 | + # Reconstruct the line |
| 54 | + data[i] = b"|".join(fields) + b"\n" |
| 55 | + break |
| 56 | + return data |
| 57 | + |
| 58 | + def test_process_large_file_cp1252(self): |
| 59 | + """ Test processing a large file with cp1252 encoding """ |
| 60 | + n_rows = 500 |
| 61 | + data = self.create_test_data_from_file("test-batch-data.csv") |
| 62 | + data = self.expand_test_data(data, n_rows) |
| 63 | + data = self.insert_cp1252_at_end(data, b'D\xe9cembre', 2) |
| 64 | + ret1 = {"Body": BytesIO(b"".join(data))} |
| 65 | + ret2 = {"Body": BytesIO(b"".join(data))} |
| 66 | + self.mock_s3_get_object.side_effect = [ret1, ret2] |
| 67 | + self.mock_map_target_disease.return_value = "some disease" |
| 68 | + |
| 69 | + message_body = { |
| 70 | + "vaccine_type": "vax-type-1", |
| 71 | + "supplier": "test-supplier", |
| 72 | + } |
| 73 | + self.mock_map_target_disease.return_value = "some disease" |
| 74 | + |
| 75 | + n_rows_processed = process_csv_to_fhir(message_body) |
| 76 | + self.assertEqual(n_rows_processed, n_rows) |
| 77 | + self.assertEqual(self.mock_send_to_kinesis.call_count, n_rows) |
| 78 | + # check logger.warning called for decode error |
| 79 | + self.mock_logger_warning.assert_called() |
| 80 | + warning_call_args = self.mock_logger_warning.call_args[0][0] |
| 81 | + self.assertTrue(warning_call_args.startswith("Encoding Error: 'utf-8' codec can't decode byte 0xe9")) |
| 82 | + |
| 83 | + def test_process_large_file_utf8(self): |
| 84 | + """ Test processing a large file with utf-8 encoding """ |
| 85 | + n_rows = 500 |
| 86 | + data = self.create_test_data_from_file("test-batch-data.csv") |
| 87 | + data = self.expand_test_data(data, n_rows) |
| 88 | + ret1 = {"Body": BytesIO(b"".join(data))} |
| 89 | + ret2 = {"Body": BytesIO(b"".join(data))} |
| 90 | + self.mock_s3_get_object.side_effect = [ret1, ret2] |
| 91 | + self.mock_map_target_disease.return_value = "some disease" |
| 92 | + |
| 93 | + message_body = { |
| 94 | + "vaccine_type": "vax-type-1", |
| 95 | + "supplier": "test-supplier", |
| 96 | + } |
| 97 | + self.mock_map_target_disease.return_value = "some disease" |
| 98 | + |
| 99 | + n_rows_processed = process_csv_to_fhir(message_body) |
| 100 | + self.assertEqual(n_rows_processed, n_rows) |
| 101 | + self.assertEqual(self.mock_send_to_kinesis.call_count, n_rows) |
| 102 | + self.mock_logger_warning.assert_not_called() |
| 103 | + self.mock_logger_error.assert_not_called() |
| 104 | + |
| 105 | + def test_process_small_file_cp1252(self): |
| 106 | + """ Test processing a small file with cp1252 encoding """ |
| 107 | + data = self.create_test_data_from_file("test-batch-data-cp1252.csv") |
| 108 | + data = self.insert_cp1252_at_end(data, b'D\xe9cembre', 2) |
| 109 | + data = [line if line.endswith(b"\n") else line + b"\n" for line in data] |
| 110 | + n_rows = len(data) - 1 # Exclude header |
| 111 | + |
| 112 | + ret1 = {"Body": BytesIO(b"".join(data))} |
| 113 | + ret2 = {"Body": BytesIO(b"".join(data))} |
| 114 | + self.mock_s3_get_object.side_effect = [ret1, ret2] |
| 115 | + self.mock_map_target_disease.return_value = "some disease" |
| 116 | + |
| 117 | + message_body = { |
| 118 | + "vaccine_type": "vax-type-1", |
| 119 | + "supplier": "test-supplier", |
| 120 | + } |
| 121 | + |
| 122 | + self.mock_map_target_disease.return_value = "some disease" |
| 123 | + |
| 124 | + n_rows_processed = process_csv_to_fhir(message_body) |
| 125 | + self.assertEqual(n_rows_processed, n_rows) |
| 126 | + self.assertEqual(self.mock_send_to_kinesis.call_count, n_rows) |
| 127 | + self.mock_logger_warning.assert_called() |
| 128 | + warning_call_args = self.mock_logger_warning.call_args[0][0] |
| 129 | + self.assertTrue(warning_call_args.startswith("Invalid Encoding detected")) |
| 130 | + |
| 131 | + def test_process_small_file_utf8(self): |
| 132 | + """ Test processing a small file with utf-8 encoding """ |
| 133 | + data = self.create_test_data_from_file("test-batch-data.csv") |
| 134 | + data = [line if line.endswith(b"\n") else line + b"\n" for line in data] |
| 135 | + n_rows = len(data) - 1 # Exclude header |
| 136 | + |
| 137 | + ret1 = {"Body": BytesIO(b"".join(data))} |
| 138 | + ret2 = {"Body": BytesIO(b"".join(data))} |
| 139 | + self.mock_s3_get_object.side_effect = [ret1, ret2] |
| 140 | + self.mock_map_target_disease.return_value = "some disease" |
| 141 | + |
| 142 | + message_body = { |
| 143 | + "vaccine_type": "vax-type-1", |
| 144 | + "supplier": "test-supplier", |
| 145 | + } |
| 146 | + self.mock_map_target_disease.return_value = "some disease" |
| 147 | + |
| 148 | + n_rows_processed = process_csv_to_fhir(message_body) |
| 149 | + self.assertEqual(n_rows_processed, n_rows) |
| 150 | + self.assertEqual(self.mock_send_to_kinesis.call_count, n_rows) |
| 151 | + self.mock_logger_warning.assert_not_called() |
| 152 | + self.mock_logger_error.assert_not_called() |
0 commit comments