"""S3 module unit tests.""" import botocore from unittest.mock import patch from unittest.mock import Mock import pytest import s3 @patch('s3.client') def test_get_object(boto3_client_mock): """Test get_object function.""" test_bucket = 'bucket' test_key = 'key' # test function call s3.get_object(test_bucket, test_key) # checking boto3_client_mock.get_object.assert_called_once_with( Bucket=test_bucket, Key=test_key) @patch('s3.client') def test_head_object(boto3_client_mock): """Test head_object function.""" test_bucket = 'bucket' test_key = 'key' # test function call s3.head_object(test_bucket, test_key) # checking boto3_client_mock.head_object.assert_called_once_with( Bucket=test_bucket, Key=test_key) @patch('s3.client') def test_object_exists_positive(boto3_client_mock): """Test object_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) assert result @patch('s3.client') def test_object_exists_not_found(boto3_client_mock): """Test object_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) assert not result assert result.status == 404 assert result.errors['message'] == ( "'key' not found in bucket 'bucket'!") @patch('s3.client') def test_object_exists_internal_error(boto3_client_mock): """Test object_exists function.""" # mocking test_bucket = 'bucket' test_key = 'key' error = {'Error': {'Code': '500'}} 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) assert not result assert result.status == 500 assert result.errors['message'] == ( "Failed to head s3 object 'key' at bucket 'bucket' with error: " "'An error occurred (500) when calling the foo operation: Unknown'") @patch('s3.client') def test_object_exists_fail(boto3_client_mock): """Test object_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) @patch('s3.client') def test_object_exists_access_denied(boto3_client_mock): """Test for failure of object_exists function call.""" # mocking test_bucket = 'bucket' test_key = 'key' error = {'Error': {'Code': '403'}} 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) assert not result assert result.status == 403 assert result.errors['message'] == ( "Access denied for 'key' in bucket 'bucket'.") @patch('s3.resource') def test_download_object(boto3_mock): """Test download_object function.""" bucket = Mock() boto3_mock.Bucket.return_value = bucket test_bucket = 'bucket' test_key = 'key' local_filename = 'file_to_process' # test function call s3.download_object(test_bucket, test_key, local_filename) # checking bucket.download_file.assert_called_once_with( test_key, local_filename)