"""S3 module unit tests.""" import botocore from mock import patch from mock import Mock import pytest import s3 @patch('s3.client') def test_upload_key(boto3_client_mock): """Test upload_key function.""" test_bucket = 'bucket' test_key = 'key' test_data = 'data' # test function call s3.upload_key(test_bucket, test_key, test_data) # checking boto3_client_mock.put_object.assert_called_once_with( Bucket=test_bucket, Key=test_key, Body=test_data) @patch('s3.resource') def test_copy_key(boto3_resource_mock): """Test copy_key function.""" # mocking bucket_mock = Mock() boto3_resource_mock.Bucket.return_value = bucket_mock test_source_bucket = 'source_bucket' test_source_key = 'source_key' test_new_key = 'new_key' # test function call s3.copy_key( test_source_bucket, test_source_key, test_source_bucket, test_new_key) # checking bucket_mock.copy.assert_called_once_with( {'Bucket': test_source_bucket, 'Key': test_source_key}, test_new_key) @patch('s3.client') def test_delete_key(boto3_client_mock): """Test delete key.""" # mocking test_bucket = 'bucket' test_key = 'key' # test function call s3.delete_key(test_bucket, test_key) # checking boto3_client_mock.delete_object.assert_called_once_with( Bucket=test_bucket, Key=test_key) @patch('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.key_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_key_exists_negative(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.key_exists(test_bucket, test_key) # checking boto3_client_mock.head_object.assert_called_once_with( Bucket=test_bucket, Key=test_key) assert not result @patch('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.key_exists(test_bucket, test_key)