"""Test create_ingestion_lock handler.""" from unittest.mock import MagicMock, patch from botocore.errorfactory import ClientError import pytest from src.exceptions import LockfileExistsException from src.index import handler @patch('src.index.s3_client') @patch('src.index.config') def test_handler_lockfile_exists( mock_config, mock_s3_client, context_event ): """Test the main handler when lockfile exists.""" mock_head_object = MagicMock() mock_s3_client.head_object = mock_head_object upc = context_event['product']['upc'] expected_bucket = 'bucket' expected_s3_path = 'path/' expected_lockfile = f'{upc}.lock' mock_config.INGESTION_LOCK_S3_BUCKET = expected_bucket mock_config.INGESTION_LOCK_S3_PATH = expected_s3_path mock_config.INGESTION_S3_EXPECTED_OWNER = 'test_owner' with pytest.raises(LockfileExistsException): handler(context_event, None) mock_head_object.assert_called_once_with( Bucket=expected_bucket, Key=f'{expected_s3_path}{expected_lockfile}', ExpectedBucketOwner='test_owner' ) @patch('src.index.s3_client') @patch('src.index.config') def test_handler_creates_lockfile( mock_config, mock_s3_client, context_event, ): """Test the main handler creates lockfile.""" mock_head_object = MagicMock( side_effect=ClientError( error_response={}, operation_name='') ) mock_s3_client.head_object.side_effect = mock_head_object mock_put_object = MagicMock() mock_s3_client.put_object = mock_put_object upc = context_event['product']['upc'] expected_bucket = 'bucket' expected_s3_path = 'path/' expected_lockfile = f'{upc}.lock' mock_config.INGESTION_LOCK_S3_BUCKET = expected_bucket mock_config.INGESTION_LOCK_S3_PATH = expected_s3_path mock_config.INGESTION_S3_EXPECTED_OWNER = 'test_owner' handler(context_event, None) mock_head_object.assert_called_once_with( Bucket=expected_bucket, Key=f'{expected_s3_path}{expected_lockfile}', ExpectedBucketOwner='test_owner' ) mock_put_object.assert_called_once_with( Bucket=expected_bucket, Key=f'{expected_s3_path}{expected_lockfile}', ExpectedBucketOwner='test_owner' )