"""S3 module unit tests.""" from unittest.mock import patch from unittest.mock import MagicMock import botocore import pytest from src import s3 @pytest.fixture def temporary_file_descriptors_data(): """Correct input descriptors data.""" return [ { 'key': 'img/jpg/test_image_2000_2000.jpg', 'output_image_buffer': None, }, { 'key': 'img/jpg/test_image_110_110.jpg', 'output_image_buffer': None, }, { 'key': 'img/tif/test_image.tif', 'output_image_buffer': None, } ] @patch('src.s3.client') def test_put_object(boto3_client_mock): """Test put_object function.""" test_bucket = 'bucket' test_key = 'key' test_data = 'data' # test function call s3.put_object(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('src.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, ExpectedBucketOwner='1234567890') @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) @patch('src.s3.put_object') def test_upload_file_objects( put_object_mock, temporary_file_descriptors_data): """Test for download_file_object call.""" # mocking test_bucket = 'bucket' descriptors_count = len(temporary_file_descriptors_data) buffers = [MagicMock() for i in range(descriptors_count)] input_descriptors = [] for descriptor in zip(temporary_file_descriptors_data, buffers): result_descriptor = dict.copy(descriptor[0]) result_descriptor['output_image_buffer'] = descriptor[1] input_descriptors.append(result_descriptor) # test function call s3.upload_file_objects(input_descriptors, test_bucket) # checking for descriptor in input_descriptors: descriptor['output_image_buffer'].seek.assert_called_once_with(0) put_object_mock.assert_any_call( bucket=test_bucket, key=descriptor['key'], data=descriptor['output_image_buffer'], ContentType='binary/octet' ) def test_upload_file_objects_failure(temporary_file_descriptors_data): """Test for download_file_object failure call.""" # mocking test_bucket = 'bucket' descriptors_count = len(temporary_file_descriptors_data) buffers = [MagicMock() for i in range(descriptors_count)] input_descriptors = [] for descriptor in zip(temporary_file_descriptors_data, buffers): result_descriptor = dict.copy(descriptor[0]) result_descriptor['output_image_buffer'] = descriptor[1] input_descriptors.append(result_descriptor) buffers[0].side_effect = Exception('Mock Exception!') # test function call and checking with pytest.raises(Exception): s3.upload_file_objects(input_descriptors, test_bucket)