Skip to content

Commit 2606e31

Browse files
committed
Baseline lint after rebase
1 parent ff1c13c commit 2606e31

File tree

12 files changed

+92
-156
lines changed

12 files changed

+92
-156
lines changed

lambdas/ack_backend/src/ack_processor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ def lambda_handler(event, _):
5252
update_ack_file(file_key, created_at_formatted_string, ack_data_rows)
5353

5454
if is_ack_processing_complete(message_id, total_ack_rows_processed):
55-
complete_batch_file_process(message_id, supplier, vaccine_type, created_at_formatted_string, file_key,
56-
total_ack_rows_processed)
55+
complete_batch_file_process(
56+
message_id, supplier, vaccine_type, created_at_formatted_string, file_key, total_ack_rows_processed
57+
)
5758

5859
return {
5960
"statusCode": 200,

lambdas/ack_backend/src/audit_table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Add the filename to the audit table and check for duplicates."""
2+
23
from typing import Optional
34
from common.clients import dynamodb_client, logger
45
from common.models.errors import UnhandledAuditTableError
@@ -33,8 +34,7 @@ def change_audit_table_status_to_processed(file_key: str, message_id: str) -> No
3334
def get_record_count_by_message_id(event_message_id: str) -> Optional[int]:
3435
"""Retrieves full audit entry by unique event message ID"""
3536
audit_record = dynamodb_client.get_item(
36-
TableName=AUDIT_TABLE_NAME,
37-
Key={AuditTableKeys.MESSAGE_ID: {"S": event_message_id}}
37+
TableName=AUDIT_TABLE_NAME, Key={AuditTableKeys.MESSAGE_ID: {"S": event_message_id}}
3838
)
3939

4040
record_count = audit_record.get("Item", {}).get(AuditTableKeys.RECORD_COUNT, {}).get("N")

lambdas/ack_backend/src/update_ack_file.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
from io import StringIO, BytesIO
55
from audit_table import change_audit_table_status_to_processed
66
from common.clients import get_s3_client, logger
7-
from constants import ACK_HEADERS, get_source_bucket_name, get_ack_bucket_name, COMPLETED_ACK_DIR, TEMP_ACK_DIR, \
8-
BATCH_FILE_PROCESSING_DIR, BATCH_FILE_ARCHIVE_DIR
7+
from constants import (
8+
ACK_HEADERS,
9+
get_source_bucket_name,
10+
get_ack_bucket_name,
11+
COMPLETED_ACK_DIR,
12+
TEMP_ACK_DIR,
13+
BATCH_FILE_PROCESSING_DIR,
14+
BATCH_FILE_ARCHIVE_DIR,
15+
)
916
from logging_decorators import complete_batch_file_process_logging_decorator
1017

1118

@@ -51,21 +58,15 @@ def complete_batch_file_process(
5158
vaccine_type: str,
5259
created_at_formatted_string: str,
5360
file_key: str,
54-
total_ack_rows_processed: int
61+
total_ack_rows_processed: int,
5562
) -> dict:
5663
"""Mark the batch file as processed. This involves moving the ack and original file to destinations and updating
5764
the audit table status"""
5865
ack_filename = f"{file_key.replace('.csv', f'_BusAck_{created_at_formatted_string}.csv')}"
5966

67+
move_file(get_ack_bucket_name(), f"{TEMP_ACK_DIR}/{ack_filename}", f"{COMPLETED_ACK_DIR}/{ack_filename}")
6068
move_file(
61-
get_ack_bucket_name(),
62-
f"{TEMP_ACK_DIR}/{ack_filename}",
63-
f"{COMPLETED_ACK_DIR}/{ack_filename}"
64-
)
65-
move_file(
66-
get_source_bucket_name(),
67-
f"{BATCH_FILE_PROCESSING_DIR}/{file_key}",
68-
f"{BATCH_FILE_ARCHIVE_DIR}/{file_key}"
69+
get_source_bucket_name(), f"{BATCH_FILE_PROCESSING_DIR}/{file_key}", f"{BATCH_FILE_ARCHIVE_DIR}/{file_key}"
6970
)
7071

7172
change_audit_table_status_to_processed(file_key, message_id)
@@ -75,7 +76,7 @@ def complete_batch_file_process(
7576
"file_key": file_key,
7677
"supplier": supplier,
7778
"vaccine_type": vaccine_type,
78-
"row_count": total_ack_rows_processed
79+
"row_count": total_ack_rows_processed,
7980
}
8081

8182

lambdas/ack_backend/src/utils_for_ack_lambda.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Utils for ack lambda"""
2+
23
from audit_table import get_record_count_by_message_id
34

45
_BATCH_EVENT_ID_TO_RECORD_COUNT_MAP: dict[str, int] = {}

lambdas/ack_backend/tests/test_ack_processor.py

Lines changed: 41 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -189,53 +189,22 @@ def test_lambda_handler_main(self):
189189
{
190190
"description": "Multiple messages: all with diagnostics (failure messages)",
191191
"messages": [
192-
{
193-
"row_id": "row^1",
194-
"diagnostics": DiagnosticsDictionaries.UNIQUE_ID_MISSING
195-
},
196-
{
197-
"row_id": "row^2",
198-
"diagnostics": DiagnosticsDictionaries.NO_PERMISSIONS
199-
},
200-
{
201-
"row_id": "row^3",
202-
"diagnostics": DiagnosticsDictionaries.RESOURCE_NOT_FOUND_ERROR
203-
},
192+
{"row_id": "row^1", "diagnostics": DiagnosticsDictionaries.UNIQUE_ID_MISSING},
193+
{"row_id": "row^2", "diagnostics": DiagnosticsDictionaries.NO_PERMISSIONS},
194+
{"row_id": "row^3", "diagnostics": DiagnosticsDictionaries.RESOURCE_NOT_FOUND_ERROR},
204195
],
205196
},
206197
{
207198
"description": "Multiple messages: mixture of success and failure messages",
208199
"messages": [
209-
{
210-
"row_id": "row^1",
211-
"imms_id": "TEST_IMMS_ID"
212-
},
213-
{
214-
"row_id": "row^2",
215-
"diagnostics": DiagnosticsDictionaries.UNIQUE_ID_MISSING
216-
},
217-
{
218-
"row_id": "row^3",
219-
"diagnostics": DiagnosticsDictionaries.CUSTOM_VALIDATION_ERROR
220-
},
221-
{
222-
"row_id": "row^4"
223-
},
224-
{
225-
"row_id": "row^5",
226-
"diagnostics": DiagnosticsDictionaries.CUSTOM_VALIDATION_ERROR
227-
},
228-
{
229-
"row_id": "row^6",
230-
"diagnostics": DiagnosticsDictionaries.CUSTOM_VALIDATION_ERROR
231-
},
232-
{
233-
"row_id": "row^7"
234-
},
235-
{
236-
"row_id": "row^8",
237-
"diagnostics": DiagnosticsDictionaries.IDENTIFIER_DUPLICATION_ERROR
238-
},
200+
{"row_id": "row^1", "imms_id": "TEST_IMMS_ID"},
201+
{"row_id": "row^2", "diagnostics": DiagnosticsDictionaries.UNIQUE_ID_MISSING},
202+
{"row_id": "row^3", "diagnostics": DiagnosticsDictionaries.CUSTOM_VALIDATION_ERROR},
203+
{"row_id": "row^4"},
204+
{"row_id": "row^5", "diagnostics": DiagnosticsDictionaries.CUSTOM_VALIDATION_ERROR},
205+
{"row_id": "row^6", "diagnostics": DiagnosticsDictionaries.CUSTOM_VALIDATION_ERROR},
206+
{"row_id": "row^7"},
207+
{"row_id": "row^8", "diagnostics": DiagnosticsDictionaries.IDENTIFIER_DUPLICATION_ERROR},
239208
],
240209
},
241210
{
@@ -244,12 +213,7 @@ def test_lambda_handler_main(self):
244213
},
245214
{
246215
"description": "Single row: malformed diagnostics info from forwarder",
247-
"messages": [
248-
{
249-
"row_id": "row^1",
250-
"diagnostics": "SHOULD BE A DICTIONARY, NOT A STRING"
251-
}
252-
],
216+
"messages": [{"row_id": "row^1", "diagnostics": "SHOULD BE A DICTIONARY, NOT A STRING"}],
253217
},
254218
]
255219

@@ -278,60 +242,49 @@ def test_lambda_handler_updates_ack_file_but_does_not_mark_complete_when_records
278242
# Original source file had 100 records
279243
add_audit_entry_to_table(self.dynamodb_client, mock_batch_message_id, record_count=100)
280244
array_of_success_messages = [
281-
{**BASE_SUCCESS_MESSAGE, "row_id": f"{mock_batch_message_id}^{i}", "imms_id": f"imms_{i}",
282-
"local_id": f"local^{i}"}
245+
{
246+
**BASE_SUCCESS_MESSAGE,
247+
"row_id": f"{mock_batch_message_id}^{i}",
248+
"imms_id": f"imms_{i}",
249+
"local_id": f"local^{i}",
250+
}
283251
for i in range(1, 4)
284252
]
285-
test_event = {
286-
"Records": [
287-
{"body": json.dumps(array_of_success_messages)}
288-
]
289-
}
253+
test_event = {"Records": [{"body": json.dumps(array_of_success_messages)}]}
290254

291255
response = lambda_handler(event=test_event, context={})
292256

293257
self.assertEqual(response, EXPECTED_ACK_LAMBDA_RESPONSE_FOR_SUCCESS)
294258
validate_ack_file_content(
295259
self.s3_client,
296-
[
297-
*array_of_success_messages
298-
],
260+
[*array_of_success_messages],
299261
existing_file_content=ValidValues.ack_headers,
300262
)
301263
self.assert_ack_and_source_file_locations_correct(
302264
MOCK_MESSAGE_DETAILS.file_key,
303265
MOCK_MESSAGE_DETAILS.temp_ack_file_key,
304266
MOCK_MESSAGE_DETAILS.archive_ack_file_key,
305-
is_complete=False
267+
is_complete=False,
306268
)
307269
self.assert_audit_entry_status_equals(mock_batch_message_id, "Preprocessed")
308270

309271
@patch("utils_for_ack_lambda.get_record_count_by_message_id", return_value=500)
310-
def test_lambda_handler_uses_message_id_to_record_count_cache_to_reduce_ddb_calls(
311-
self,
312-
mock_get_record_count: Mock
313-
):
272+
def test_lambda_handler_uses_message_id_to_record_count_cache_to_reduce_ddb_calls(self, mock_get_record_count: Mock):
314273
"""The DynamoDB Audit table is used to store the total record count for each source file. To reduce calls each
315274
time - this test checks that we cache the value as this lambda is called many times for large files"""
316275
mock_batch_message_id = "622cdeea-461e-4a83-acb5-7871d47ddbcd"
317276

318277
# Original source file had 500 records
319278
add_audit_entry_to_table(self.dynamodb_client, mock_batch_message_id, record_count=500)
320279

321-
message_one = [{**BASE_SUCCESS_MESSAGE, "row_id": f"{mock_batch_message_id}^1", "imms_id": "imms_1",
322-
"local_id": "local^1"}]
323-
message_two = [{**BASE_SUCCESS_MESSAGE, "row_id": f"{mock_batch_message_id}^2", "imms_id": "imms_2",
324-
"local_id": "local^2"}]
325-
test_event_one = {
326-
"Records": [
327-
{"body": json.dumps(message_one)}
328-
]
329-
}
330-
test_event_two = {
331-
"Records": [
332-
{"body": json.dumps(message_two)}
333-
]
334-
}
280+
message_one = [
281+
{**BASE_SUCCESS_MESSAGE, "row_id": f"{mock_batch_message_id}^1", "imms_id": "imms_1", "local_id": "local^1"}
282+
]
283+
message_two = [
284+
{**BASE_SUCCESS_MESSAGE, "row_id": f"{mock_batch_message_id}^2", "imms_id": "imms_2", "local_id": "local^2"}
285+
]
286+
test_event_one = {"Records": [{"body": json.dumps(message_one)}]}
287+
test_event_two = {"Records": [{"body": json.dumps(message_two)}]}
335288

336289
response = lambda_handler(event=test_event_one, context={})
337290
self.assertEqual(response, EXPECTED_ACK_LAMBDA_RESPONSE_FOR_SUCCESS)
@@ -342,16 +295,14 @@ def test_lambda_handler_uses_message_id_to_record_count_cache_to_reduce_ddb_call
342295
mock_get_record_count.assert_called_once_with(mock_batch_message_id)
343296
validate_ack_file_content(
344297
self.s3_client,
345-
[
346-
*message_one, *message_two
347-
],
298+
[*message_one, *message_two],
348299
existing_file_content=ValidValues.ack_headers,
349300
)
350301
self.assert_ack_and_source_file_locations_correct(
351302
MOCK_MESSAGE_DETAILS.file_key,
352303
MOCK_MESSAGE_DETAILS.temp_ack_file_key,
353304
MOCK_MESSAGE_DETAILS.archive_ack_file_key,
354-
is_complete=False
305+
is_complete=False,
355306
)
356307
self.assertEqual(_BATCH_EVENT_ID_TO_RECORD_COUNT_MAP[mock_batch_message_id], 500)
357308
self.assert_audit_entry_status_equals(mock_batch_message_id, "Preprocessed")
@@ -379,32 +330,27 @@ def test_lambda_handler_updates_ack_file_and_marks_complete_when_all_records_pro
379330
)
380331

381332
array_of_success_messages = [
382-
{**BASE_SUCCESS_MESSAGE, "row_id": f"{mock_batch_message_id}^{i}", "imms_id": f"imms_{i}",
383-
"local_id": f"local^{i}"}
333+
{
334+
**BASE_SUCCESS_MESSAGE,
335+
"row_id": f"{mock_batch_message_id}^{i}",
336+
"imms_id": f"imms_{i}",
337+
"local_id": f"local^{i}",
338+
}
384339
for i in range(50, 101)
385340
]
386-
test_event = {
387-
"Records": [
388-
{"body": json.dumps(array_of_success_messages)}
389-
]
390-
}
341+
test_event = {"Records": [{"body": json.dumps(array_of_success_messages)}]}
391342

392343
response = lambda_handler(event=test_event, context={})
393344

394345
self.assertEqual(response, EXPECTED_ACK_LAMBDA_RESPONSE_FOR_SUCCESS)
395346
validate_ack_file_content(
396-
self.s3_client,
397-
[
398-
*array_of_success_messages
399-
],
400-
existing_file_content=existing_ack_content,
401-
is_complete=True
347+
self.s3_client, [*array_of_success_messages], existing_file_content=existing_ack_content, is_complete=True
402348
)
403349
self.assert_ack_and_source_file_locations_correct(
404350
MOCK_MESSAGE_DETAILS.file_key,
405351
MOCK_MESSAGE_DETAILS.temp_ack_file_key,
406352
MOCK_MESSAGE_DETAILS.archive_ack_file_key,
407-
is_complete=True
353+
is_complete=True,
408354
)
409355
self.assert_audit_entry_status_equals(mock_batch_message_id, "Processed")
410356

lambdas/ack_backend/tests/test_audit_table.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ def test_get_record_count_by_message_id_returns_the_record_count(self):
3434
test_message_id = "1234"
3535

3636
self.mock_dynamodb_client.get_item.return_value = {
37-
"Item": {
38-
"message_id": {"S": test_message_id},
39-
"record_count": {"N": "1000"}
40-
}
37+
"Item": {"message_id": {"S": test_message_id}, "record_count": {"N": "1000"}}
4138
}
4239

4340
self.assertEqual(audit_table.get_record_count_by_message_id(test_message_id), 1000)
@@ -46,10 +43,6 @@ def test_get_record_count_by_message_id_returns_none_if_record_count_not_set(sel
4643
"""Test that if the record count has not yet been set on the audit item then None is returned"""
4744
test_message_id = "1234"
4845

49-
self.mock_dynamodb_client.get_item.return_value = {
50-
"Item": {
51-
"message_id": {"S": test_message_id}
52-
}
53-
}
46+
self.mock_dynamodb_client.get_item.return_value = {"Item": {"message_id": {"S": test_message_id}}}
5447

5548
self.assertIsNone(audit_table.get_record_count_by_message_id(test_message_id))

lambdas/ack_backend/tests/test_splunk_logging.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -379,17 +379,18 @@ def test_splunk_update_ack_file_not_logged(self):
379379
patch("common.log_decorator.send_log_to_firehose") as mock_send_log_to_firehose, # noqa: E999
380380
patch("common.log_decorator.logger") as mock_logger, # noqa: E999
381381
patch("ack_processor.is_ack_processing_complete", return_value=False),
382-
patch("update_ack_file.change_audit_table_status_to_processed")
383-
as mock_change_audit_table_status_to_processed, # noqa: E999
382+
patch(
383+
"update_ack_file.change_audit_table_status_to_processed"
384+
) as mock_change_audit_table_status_to_processed, # noqa: E999
384385
): # noqa: E999
385386
result = lambda_handler(generate_event(messages), context={})
386387

387388
self.assertEqual(result, EXPECTED_ACK_LAMBDA_RESPONSE_FOR_SUCCESS)
388389

389390
expected_secondlast_logger_info_data = {
390-
**ValidValues.mock_message_expected_log_value,
391-
"message_id": "test^98",
392-
}
391+
**ValidValues.mock_message_expected_log_value,
392+
"message_id": "test^98",
393+
}
393394
expected_last_logger_info_data = self.expected_lambda_handler_logs(success=True, number_of_rows=98)
394395

395396
all_logger_info_call_args = self.extract_all_call_args_for_logger_info(mock_logger)
@@ -419,22 +420,23 @@ def test_splunk_update_ack_file_logged(self):
419420
patch("common.log_decorator.send_log_to_firehose") as mock_send_log_to_firehose, # noqa: E999
420421
patch("common.log_decorator.logger") as mock_logger, # noqa: E999
421422
patch("ack_processor.is_ack_processing_complete", return_value=True),
422-
patch("update_ack_file.change_audit_table_status_to_processed")
423-
as mock_change_audit_table_status_to_processed, # noqa: E999
423+
patch(
424+
"update_ack_file.change_audit_table_status_to_processed"
425+
) as mock_change_audit_table_status_to_processed, # noqa: E999
424426
): # noqa: E999
425427
result = lambda_handler(generate_event(messages), context={})
426428

427429
self.assertEqual(result, EXPECTED_ACK_LAMBDA_RESPONSE_FOR_SUCCESS)
428430

429431
expected_thirdlast_logger_info_data = {
430-
**ValidValues.mock_message_expected_log_value,
431-
"message_id": "test^99",
432-
}
432+
**ValidValues.mock_message_expected_log_value,
433+
"message_id": "test^99",
434+
}
433435
expected_secondlast_logger_info_data = {
434-
**ValidValues.upload_ack_file_expected_log,
435-
"message_id": "test",
436-
"time_taken": "1.0s"
437-
}
436+
**ValidValues.upload_ack_file_expected_log,
437+
"message_id": "test",
438+
"time_taken": "1.0s",
439+
}
438440
expected_last_logger_info_data = self.expected_lambda_handler_logs(
439441
success=True, number_of_rows=99, ingestion_complete=True
440442
)

lambdas/ack_backend/tests/test_update_ack_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
generate_expected_ack_file_row,
2222
generate_sample_existing_ack_content,
2323
generate_expected_ack_content,
24-
MOCK_MESSAGE_DETAILS
24+
MOCK_MESSAGE_DETAILS,
2525
)
2626

2727
from unittest.mock import patch

0 commit comments

Comments
 (0)