"""Unit tests for lambda module.""" from mock import patch from mock import Mock import pytest import index # noqa import config # noqa @patch('s3.upload_key') @patch('smart_open.smart_open') def test_split(smart_open, upload_key_mock): """Test split function.""" # mocking lines_number = 10 chunk_max_line_count = 4 smart_open.side_effect = [['line %s' % i for i in range(lines_number)]] source_bucket = 'source_bucket' source_key = 'source_key' new_bucket = 'new_bucket' new_key = 'new_key' # test function call index.split( source_bucket, source_key, new_bucket, new_key, chunk_max_line_count=chunk_max_line_count) # checking assert upload_key_mock.call_count == 3 @pytest.mark.parametrize( ['size', 'expecting_result'], [(1, False), (config.MAX_FILE_SIZE + 1, True)]) @patch('boto3.client') def test_need_split(client_mock, size, expecting_result): """Test _need_split function.""" client_object_mock = Mock() client_object_mock.head_object.return_value = {'ContentLength': size} client_mock.return_value = client_object_mock # head_object(Bucket=bucket, Key=key) test_bucket = 'test_bucket' test_key = 'test_key' # test function call result = index._need_split(test_bucket, test_key) # checking assert result == expecting_result @pytest.fixture(params=config.FILE_TYPES) def file_type(request): """File type fixture.""" return request.param def test__identify_retailer(): """Test _identify_retailer function.""" key = 'test/key-apple-music-sos' assert 'apple-music-sos' == index._identify_retailer(key) key = 'test/key-spotify-sos' assert 'spotify-sos' == index._identify_retailer(key) def test_get_file_type(file_type): """Test _get_file_type function.""" result = index._get_file_type(file_type) assert result == file_type def test_get_file_type_fail(): """Test _get_file_type function failure.""" with pytest.raises(ValueError): index._get_file_type('bad_file_name') @patch('s3.copy_key') @patch('index.split') @patch('index._need_split') @patch('index._get_file_type') def test_process_key_big_file( _get_file_type_mock, _need_split_mock, split_mock, copy_key_mock): """Test process_key function.""" # mocking test_file_type = 'test_file_type' _get_file_type_mock.return_value = test_file_type test_bucket = 'test_bucket' test_key = 'test/key-apple-music-sos' test_file_name = test_key.split('/')[-1] retailer = index._identify_retailer(test_key) new_key_path_value = ''.join( [config.TARGET_S3_PATH['suffix'].format( retailer=retailer), test_file_type, '/', test_file_name]) _need_split_mock.return_value = True # test function call index.process_key(test_bucket, test_key) # checking split_mock.assert_called_once_with( test_bucket, test_key, test_bucket, new_key_path_value) @patch('s3.copy_key') @patch('index.split') @patch('index._need_split') @patch('index._get_file_type') def test_process_key_small_file( _get_file_type_mock, _need_split_mock, split_mock, copy_key_mock): """Test process_key function.""" # mocking test_file_type = 'test_file_type' _get_file_type_mock.return_value = test_file_type test_bucket = 'test_bucket' test_key = 'test/key-spotify-sos' test_file_name = test_key.split('/')[-1] retailer = index._identify_retailer(test_key) new_key_path_value = ''.join( [config.TARGET_S3_PATH['suffix'].format( retailer=retailer), test_file_type, '/', test_file_name]) _need_split_mock.return_value = False # test function call index.process_key(test_bucket, test_key) # chekcing copy_key_mock.assert_called_once_with( test_bucket, test_key, test_bucket, new_key_path_value) @patch('s3.delete_key') @patch('index.process_key') @patch('util.object_is_file') @patch('index.logger.info') @patch('util.extract_triggered_key') def test_handler_triggered_on_file( extract_triggered_key_mock, logger_info_mock, object_is_file_mock, process_key_mock, s3_delete_key_mock ): """Test handler function triggered on File.""" # mocking test_bucket = 'test_bucket' test_key = 'test_key' extract_triggered_key_mock.return_value = (test_bucket, test_key) object_is_file_mock.return_value = True # test function call index.handler(Mock(), Mock()) # chekcing object_is_file_mock.assert_called_once_with(test_key) process_key_mock.assert_called_once_with(test_bucket, test_key) s3_delete_key_mock.assert_called_once_with(test_bucket, test_key) assert logger_info_mock.call_count == 3 @patch('s3.delete_key') @patch('index.process_key') @patch('util.object_is_file') @patch('index.logger.info') @patch('util.extract_triggered_key') def test_handler_triggered_on_directory( extract_triggered_key_mock, logger_info_mock, object_is_file_mock, process_key_mock, s3_delete_key_mock ): """Test handler function triggered on File.""" # mocking test_bucket = 'test_bucket' test_key = 'test_key' extract_triggered_key_mock.return_value = (test_bucket, test_key) object_is_file_mock.return_value = False # test function call index.handler(Mock(), Mock()) # chekcing object_is_file_mock.assert_called_once_with(test_key) assert logger_info_mock.call_count == 1