"""Lambda test module.""" import json from os import environ import boto3 from mock import ANY from mock import MagicMock import moto import pytest import index import mysql @pytest.fixture def event(message_body): """Mock SNS event response.""" return { 'Records': [ { 'EventVersion': '1.0', 'EventSubscriptionArn': 'even', 'EventSource': 'aws:sns', 'Sns': { 'SignatureVersion': '1', 'MessageId': '95df01b4-ee98-5cb9-9903-4c221d41eb5e', 'Message': message_body, 'Type': 'Notification', 'UnsubscribeUrl': 'EXAMPLE', 'TopicArn': 'arn', 'Subject': 'TestInvoke' } } ] } @moto.mock_s3() def test_lambda_handler(monkeypatch): """Test index.lambda_handler function succeed.""" monkeypatch.setattr(mysql, 'execute', MagicMock(return_value=True)) swf_reponse = {'runId': '1234'} mock_swf = MagicMock() mock_swf.start_workflow_execution = MagicMock(return_value=swf_reponse) boto3.client = MagicMock(return_value=mock_swf) message_body = { 'correlation_id': '080af3e4-57f2-11e7-aae0-ba9eeb362c0e', 'message': 'Successfully ingested', 'source': 'digital', 'upcs': ['889845260455', '190374485784']} domain = environ.get('SWF_DOMAIN') or 'dev' response = index.lambda_handler(event(json.dumps(message_body)), None) assert response == swf_reponse swf_context = { 'correlation_id': message_body['correlation_id'], 'upcs': 'upcs'} mock_swf.start_workflow_execution.assert_called_once_with( input=json.dumps(swf_context), domain=domain, taskList={'name': 'ows_ft_etl_distribution_fee'}, executionStartToCloseTimeout='7200', workflowId='ows_ft_etl_distribution_fee_{cid}'.format( cid=message_body['correlation_id']), workflowType={ 'name': 'ows_ft_etl_distribution_fee', 'version': '1.0'}) @moto.mock_s3() def test_lambda_handler_string(monkeypatch): """Test index.lambda_handler function when msg is string.""" monkeypatch.setattr(mysql, 'execute', MagicMock(return_value=True)) swf_reponse = {'runId': '1234'} mock_swf = MagicMock() mock_swf.start_workflow_execution = MagicMock(return_value=swf_reponse) boto3.client = MagicMock(return_value=mock_swf) message_body = 'hello world' response = index.lambda_handler(event(message_body), None) assert not response @moto.mock_s3() def test_lambda_handler_source(monkeypatch): """Test index.lambda_handler function with invalid source.""" monkeypatch.setattr(mysql, 'execute', MagicMock(return_value=True)) swf_reponse = {'runId': '1234'} mock_swf = MagicMock() mock_swf.start_workflow_execution = MagicMock(return_value=swf_reponse) boto3.client = MagicMock(return_value=mock_swf) message_body = { 'correlation_id': '080af3e4-57f2-11e7-aae0-ba9eeb362c0e', 'message': 'Successfully ingested', 'source': 'projection', 'upc': '190374485784'} response = index.lambda_handler(event(json.dumps(message_body)), None) assert not response @moto.mock_s3() def test_lambda_handler_upcs(monkeypatch): """Test index.lambda_handler function with no upcs.""" monkeypatch.setattr(mysql, 'execute', MagicMock(return_value=True)) swf_reponse = {'runId': '1234'} mock_swf = MagicMock() mock_swf.start_workflow_execution = MagicMock(return_value=swf_reponse) boto3.client = MagicMock(return_value=mock_swf) message_body = { 'correlation_id': '080af3e4-57f2-11e7-aae0-ba9eeb362c0e', 'message': 'Successfully ingested', 'source': 'projection'} response = index.lambda_handler(event(json.dumps(message_body)), None) assert not response def test_create_log(monkeypatch): """Test create().""" monkeypatch.setattr(mysql, 'execute', MagicMock(return_value=True)) actual = index.create_log('cid123', ['190374485784']) assert mysql.execute.called assert actual def test_add_run_id_to_log(monkeypatch): """Test add_run_id_to_log().""" monkeypatch.setattr(mysql, 'execute', MagicMock(return_value=True)) actual = index.add_run_id_to_log('cid123', 'wflow123') assert mysql.execute.called assert actual def test_create_etl_context(monkeypatch): """Test create_etl_context().""" monkeypatch.setattr(mysql, 'execute', MagicMock()) correlation_id = '1234-5678.1.2.3.4' upcs = ['123', '234', '345'] expected = { 'context_key': 'upcs', 'correlation_id': correlation_id.split('.')[0], 'data': json.dumps(upcs)} index.create_etl_context(correlation_id, upcs) mysql.execute.assert_called_with(ANY, expected) def test_create_etl_context_skip(monkeypatch): """Test create_etl_context() when params are DatabaseParam style.""" monkeypatch.setattr(mysql, 'execute', MagicMock()) index.create_etl_context('foo', 'upcs') mysql.execute.assert_not_called()