"""S3 module unit tests.""" from unittest.mock import patch import botocore import pytest from src import s3 @patch('src.s3.client') def test_key_exists_positive(boto3_client_mock): """Test key_exists function.""" # mocking test_bucket = 'bucket' test_key = 'key' # test function call result = s3.object_exists(test_bucket, test_key) # checking boto3_client_mock.head_object.assert_called_once_with( Bucket=test_bucket, Key=test_key, ExpectedBucketOwner='1234567890') assert result @patch('src.s3.client') def test_key_exists_not_exist(boto3_client_mock): """Test key_exists function.""" # mocking test_bucket = 'bucket' test_key = 'key' error = {'Error': {'Code': '404'}} # errot = MagicMock boto3_client_mock.head_object.side_effect = ( botocore.exceptions.ClientError(error, 'foo')) # test function call result = s3.object_exists(test_bucket, test_key) # checking boto3_client_mock.head_object.assert_called_once_with( Bucket=test_bucket, Key=test_key, ExpectedBucketOwner='1234567890') assert not result @patch('src.s3.client') def test_key_exists_internal_error(boto3_client_mock): """Test key_exists function.""" # mocking test_bucket = 'bucket' test_key = 'key' error = {'Error': {'Code': '500'}} # error = MagicMock boto3_client_mock.head_object.side_effect = ( botocore.exceptions.ClientError(error, 'foo')) # test function call with pytest.raises(botocore.exceptions.ClientError): s3.object_exists(test_bucket, test_key) boto3_client_mock.head_object.assert_called_once_with( Bucket=test_bucket, Key=test_key, ExpectedBucketOwner='1234567890') @patch('src.s3.client') def test_key_exists_fail(boto3_client_mock): """Test key_exists function.""" # mocking test_bucket = 'bucket' test_key = 'key' boto3_client_mock.head_object.side_effect = ValueError('foo') # test function call with pytest.raises(ValueError): s3.object_exists(test_bucket, test_key)