"""Lambda test module.""" from unittest import mock import config import index import pytest @mock.patch.object(index, 'kinesis_client') @mock.patch.object(index, 'send_record', wraps=index.send_record) def test_handler( send_record_spy, mock_kinesis_client, lambda_event, aggregated_record): """Test handler function.""" result = index.handler(lambda_event, None) pk, ehk, data = aggregated_record.get_contents() send_record_spy.assert_called() mock_kinesis_client.put_record.assert_called_with( StreamName=config.KINESIS_STREAM_NAME, Data=data, PartitionKey=pk) assert result == {'status': 'OK'} @mock.patch.object(index, 'kinesis_client') @mock.patch.object(index, 'kinesis_agg') def test_handler_parsing_failure( mock_kinesis_agg, mock_kinesis_client, lambda_event_incorrect): """Test handler function failed to parse MSK record.""" mock_kinesis_agg.clear_and_get.return_value = None result = index.handler(lambda_event_incorrect, None) mock_kinesis_agg.add_user_record.assert_not_called() assert result == {'status': 'OK'} @mock.patch.object(index, 'kinesis_client') @mock.patch.object(index, 'kinesis_agg') def test_handler_failure( mock_kinesis_agg, mock_kinesis_client, lambda_event_malformed): """Test handler function failure.""" with pytest.raises(Exception): index.handler(lambda_event_malformed, None) @mock.patch.object(index, 'kinesis_client') def test_send_record(mock_kinesis_client, aggregated_record): """Test send record to kinesis.""" index.send_record(aggregated_record) pk, ehk, data = aggregated_record.get_contents() mock_kinesis_client.put_record.assert_called_with( StreamName=config.KINESIS_STREAM_NAME, Data=data, PartitionKey=pk) @mock.patch.object(index, 'kinesis_client') def test_send_empty_record(mock_kinesis_client): """Test send an empty (None) record to kinesis.""" index.send_record(None) mock_kinesis_client.put_record.assert_not_called()