import json import pytest import constants LAMBDA_NAME = constants.LAMBDA_VECTOR_CLOSE_BATCH DMS_ID = 465 DMS_ID_NO_FILES = 187 @pytest.mark.parametrize('alchemy_db', ['art_relations'], indirect=True) @pytest.mark.parametrize('test_var', [ { 'detail_status': 'delivered', 'message': f'Dropped delivery.complete file for DMS: {DMS_ID}', }, { 'detail_status': 'metadata_error', 'message': 'No batch info found with delivered status in batch', } ]) def test_lambda_close_batch( test_var, alchemy_db, lambda_client, cloudwatch_log_handler ): """Test close-batch lambda updates batch to closed status \n https://www.notion.so/Batch-Cleanup-80e6edf96e5049a49ebb03bf6cda7766""" alchemy_db.select_database('dd') batch_id = alchemy_db.delivery_batch.get_batch_id(DMS_ID) # update encoding queue detail status eq_detail_ids = alchemy_db.delivery_batch.get_batch_encoding_queue_detail_records( batch_id ) for eqd in eq_detail_ids: detail_id = eqd[0] alchemy_db.encoding_queue.update_queue_detail_status( detail_id, test_var['detail_status'], ) # update batch status and invoke lambda alchemy_db.delivery_batch.update_batch_status( batch_id, 'queued_for_closing', ) lambda_event = { 'Records': [ { 'body': json.dumps({"batch_id": batch_id}), } ], } lambda_client.invoke(LAMBDA_NAME, lambda_event) alchemy_db.delivery_batch.assert_status(batch_id, 'closed') # assert cloudwatch messages if test_var['detail_status'] == 'delivered': test_var['message'] += f' and batch_id: {batch_id}' cloudwatch_log_handler.assert_lambda_logs_message( LAMBDA_NAME, test_var['message'], ) def test_lambda_close_batch_id_0(lambda_client, cloudwatch_log_handler): """Test close-batch refuses batch_id = 0 \n https://www.notion.so/Batch-Cleanup-80e6edf96e5049a49ebb03bf6cda7766""" lambda_event = { 'Records': [{ 'body': json.dumps({"batch_id": 0}), }]} lambda_response = lambda_client.invoke( LAMBDA_NAME, lambda_event, assertion=False) assert lambda_response['StatusCode'] == 200 assert 'FunctionError' in lambda_response cloudwatch_log_handler.assert_lambda_logs_message( LAMBDA_NAME, "Batch ID must be greater than 0.") def test_lambda_close_batch_no_messages(lambda_client, cloudwatch_log_handler): """Test close-batch without messages \n https://www.notion.so/Batch-Cleanup-80e6edf96e5049a49ebb03bf6cda7766""" lambda_event = {'Records': []} lambda_client.invoke(LAMBDA_NAME, lambda_event) cloudwatch_log_handler.assert_lambda_logs_message( LAMBDA_NAME, "No messages to process") @pytest.mark.parametrize('alchemy_db', ['art_relations'], indirect=True) def test_lambda_batch_no_files(alchemy_db, lambda_client, cloudwatch_log_handler): """Test close-batch when No files to close batch on \n https://www.notion.so/Batch-Cleanup-80e6edf96e5049a49ebb03bf6cda7766""" alchemy_db.select_database('dd') batch_id = alchemy_db.delivery_batch.get_batch_id(DMS_ID_NO_FILES) alchemy_db.delivery_batch.update_batch_status(batch_id, 'queued_for_closing') lambda_event = { 'Records': [{ 'body': json.dumps({"batch_id": batch_id}), }]} lambda_client.invoke(LAMBDA_NAME, lambda_event) alchemy_db.delivery_batch.assert_status(batch_id, 'in-flight') cloudwatch_log_handler.assert_lambda_logs_message( LAMBDA_NAME, 'No files to close batch on')