from unittest.mock import MagicMock import boto3 import pytest def monkeypatch_boto(monkeypatch): """Patch utility modules to simplify tests. Args: monkeypatch (_pytest.monkeypatch.monkeypatch): py.test monkeypatcher Returns: tuple: patched update_status_for_avro module, mock of the dynamodb client to test function calls. """ mock_dynamodb_client = MagicMock() monkeypatch.setattr( mock_dynamodb_client, 'put_item', MagicMock(return_value=None)) monkeypatch.setattr( boto3, 'client', MagicMock(return_value=mock_dynamodb_client)) from processing_accounting.lambda_functions import update_status_for_avro return update_status_for_avro, mock_dynamodb_client @pytest.fixture def mock_event(): return { 'Records': [ { 'eventVersion': '2.0', 'eventTime': '1970-01-01T00:00:00.000Z', 'requestParameters': { 'sourceIPAddress': '127.0.0.1' }, 's3': { 'configurationId': 'testConfigRule', 'object': { 'eTag': '0123456789abcdef0123456789abcdef', 'sequencer': '0A1B2C3D4E5F678901', 'key': 'schematized_files/202%2C203%2C204_label_all_' 'all_month/' 'user_id_type%3D18805L/test_file', 'size': 1024 }, 'bucket': { 'arn': 'arn:aws:s3:::dev-statement-detail-exports', 'name': 'dev-statement_detail_exports', 'ownerIdentity': { 'principalId': 'EXAMPLE' } }, 's3SchemaVersion': '1.0' }, 'responseElements': { 'x-amz-id-2': 'EXAMPLE123/5678abcdefghijklambdaisawesome/' 'mnopqrstuvwxyzABCDEFGH', 'x-amz-request-id': 'EXAMPLE123456789' }, 'awsRegion': 'us-east-1', 'eventName': 'ObjectCreated:Put', 'userIdentity': { 'principalId': 'EXAMPLE' }, 'eventSource': 'aws:s3' } ] } def test_get_bucket_key(monkeypatch, mock_event): """Test _get_bucket_key method """ update_status_for_avro, _ = monkeypatch_boto(monkeypatch) bucket, key = update_status_for_avro._get_bucket_key(mock_event) assert bucket == 'dev-statement_detail_exports' assert key == ( 'schematized_files/202,203,204_label_all_all_month/' 'user_id_type=18805L/test_file') def test_parse_s3_object_key(monkeypatch): """Test _parse_key """ update_status_for_avro, _ = monkeypatch_boto(monkeypatch) mock_key = ( 'schematized_files/202,203,204_label_all_all_month/' 'user_id_type=18805L/test_file') resp = update_status_for_avro._parse_s3_object_key(mock_key) assert hasattr(resp, 'user_id_type') and resp.user_id_type == '18805L' assert hasattr(resp, 'user_params') and resp.user_params == ( '202,203,204__all__AVRO__en_US') assert hasattr(resp, 'period_ids') and resp.period_ids == '202,203,204' def test_lambda_handler(monkeypatch, mock_event): """Test lambda_handler """ update_status_for_avro, mock_dynamodb_client = monkeypatch_boto( monkeypatch) mock_result = dict( TableName=update_status_for_avro.TABLE_NAME, Item={ 'user_id_type': { 'S': '18805L' }, 'user_params': { 'S': '202,203,204__all__AVRO__en_US' }, 'file_type': { 'S': 'AVRO' }, 'period_ids': { 'S': '202,203,204' }, 's3_path': { 'S': ( 's3://dev-statement_detail_exports/schematized_files/' '202,203,204_label_all_all_month/user_id_type=18805L/' 'test_file') }, 'status': { 'S': 'GENERATED' } }) update_status_for_avro.lambda_handler(mock_event, None) mock_dynamodb_client.put_item.assert_any_call(**mock_result)