"""Module tests AWS Lambda handler.""" import json from unittest.mock import Mock from unittest.mock import patch import pytest from flows.projections import aws_lambda @pytest.fixture def event_data(): """S3 upload event data fixture.""" return { 'Records': [{ 'eventVersion': '2.0', 's3': { 'object': { 'eTag': '0123456789abcdef0123456789abcdef', 'sequencer': '0A1B2C3D4E5F678901', 'key': 'KEY', 'size': 1024}, 'bucket': { 'arn': 'arn:aws:s3:::mybucket', 'name': 'BUCKET', 'ownerIdentity': {'principalId': 'EXAMPLE'}}, 's3SchemaVersion': '1.0'}}]} @patch('flows.projections.aws_lambda.urllib') @patch('flows.projections.aws_lambda.boto3') def test_lambda_handler(boto3, urllib, event_data): """Test for AWS Lambda handler method.""" key = 'KEY' client = boto3.client = Mock() start_execution = boto3.client().start_workflow_execution = Mock() urllib.unquote_plus = Mock(return_value=key) aws_lambda.lambda_handler(event_data, Mock()) assert client.called assert start_execution.called actual_result = json.loads(start_execution.call_args[1]['input']) assert actual_result['s3_key'] == key assert actual_result['projection_type'] == 'regular' @patch('flows.projections.aws_lambda.urllib') @patch('flows.projections.aws_lambda.boto3') def test_lambda_handler_original_projection(boto3, urllib, event_data): """Test for AWS Lambda handler method for original_projections key.""" key = 'film-transparency/original_projections/drop/key.csv' client = boto3.client = Mock() start_execution = boto3.client().start_workflow_execution = Mock() urllib.unquote_plus = Mock(return_value=key) aws_lambda.lambda_handler(event_data, Mock()) assert client.called assert start_execution.called assert start_execution.call_args[1]['input'] actual_result = json.loads(start_execution.call_args[1]['input']) assert actual_result['s3_key'] == key assert actual_result['projection_type'] == 'original'