"""Lambda test module.""" import json from pathlib import Path from unittest import mock from unittest.mock import call from unittest.mock import patch from src import app THIS_DIR = Path(__file__).parent @patch.object(app, 'logic') def test_handler_trigger(logic_mock): """Test handler function with sample dynamoDB event.""" logic_mock.parallel_download_curl_to_s3.return_value = { 'return_code': 0, 'duration': 12, 'n_threads': 20} context = {} event = json.loads((THIS_DIR / 'sample-trigger-event.json').read_text()) result = app.handler(event=event, context=context) assert logic_mock.parallel_download_curl_to_s3.call_args_list == [ call(tasks=event['tasks']) ] assert result == {'duration': 12, 'n_threads': 20, 'return_code': 0} @patch.object(app, 'boto3') @patch.object(app, 'logic') def test_handler_dynamodb(logick_mock, boto3_mock): """Test handler function with sample dynamoDB event.""" dynamodb_client_mock = boto3_mock.client.return_value logick_mock.parallel_download_curl_to_s3.return_value = { 'return_code': 0, 'duration': 12, 'n_threads': 20} context = {} event = json.loads((THIS_DIR / 'sample-dynamodb-event.json').read_text()) result = app.handler(event=event, context=context) assert dynamodb_client_mock.update_item.call_args_list == [ mock.call( TableName='dev-smart-downloader-tasks', Key={'jobId': {'S': '2023-11-09-test-2'}, 'taskId': {'S': 'GEN-ID'}}, UpdateExpression='set #v1 = :v1', ExpressionAttributeValues={':v1': {'S': 'PROCESSING'}}, ExpressionAttributeNames={'#v1': 'status'}), mock.call( TableName='dev-smart-downloader-tasks', Key={ 'jobId': {'S': '2023-11-09-test-2'}, 'taskId': {'S': 'GEN-ID'}}, UpdateExpression='set #v1 = :v1, #v2 = :v2, #v3 = :v3, #v4 = :v4', ExpressionAttributeValues={ ':v1': {'S': 'DONE'}, ':v2': {'N': '0'}, ':v3': {'N': '12'}, ':v4': {'N': '20'}}, ExpressionAttributeNames={ '#v1': 'status', '#v2': 'return_code', '#v3': 'duration', '#v4': 'n_threads'} ) ] assert result is None @patch.object(app, 'boto3') @patch.object(app, 'logic') def test_handler_dynamodb_event_modify_should_be_ignored(logick_mock, boto3_mock): """Test handler function with sample dynamoDB event.""" dynamodb_client_mock = boto3_mock.client.return_value logick_mock.parallel_download_curl_to_s3.return_value = {'return_code': 0, 'duration': 12, 'n_threads': 20} context = {} event = json.loads( (THIS_DIR / 'sample-dynamodb-event-modify.json').read_text()) result = app.handler(event=event, context=context) assert not dynamodb_client_mock.update_item.called assert not logick_mock.parallel_download_curl_to_s3.called assert result is None