"""Unit tests of helper funcions in dynamo_upload package.""" import pytest from dim_refresh_etl.flows.dynamo_sync.dynamo_upload import upload_utils @pytest.fixture def boto3_resource_mock(mocker): """Mock boto3 resource call.""" mock = mocker.patch( 'dim_refresh_etl.flows.dynamo_sync.dynamo_upload.' 'upload_utils.boto3.resource') return mock.return_value def test_cut_list(): """Test cut_list helper function.""" size = 2 result = upload_utils.cut_list(list(range(10)), size) assert len(list(result)) == size def test_keys_in_s3_location(mocker, boto3_resource_mock): """Test keys_in_s3_location helper function.""" bucket_mock = boto3_resource_mock.Bucket.return_value filter_mock = bucket_mock.objects.filter key_mock = mocker.Mock() key_mock.key = 's3_key' filter_mock.return_value = [key_mock] test_bucket_name = 'test_bucket_name' test_path = 'test_path' print(id(filter_mock)) result = upload_utils.keys_in_s3_location(test_bucket_name, test_path) assert result == ['s3://{}/{}'.format(test_bucket_name, key_mock.key)] def test_bucket_and_path_from_s3_list(): """Test bucket_and_path_from_s3_list function.""" test_bucket = 'test_bucket' test_path = 'test_path' test_s3_link = 's3://{}/{}'.format(test_bucket, test_path) bucket, path = upload_utils.bucket_and_path_from_s3_list(test_s3_link) assert bucket == test_bucket assert path == test_path def test_delete_s3_key(boto3_resource_mock): """Test delete_s3_key function.""" test_bucket = 'test_bucket' test_path = 'test_path' test_s3_link = 's3://{}/{}'.format(test_bucket, test_path) s3_object_mock = boto3_resource_mock.Object.return_value upload_utils.delete_s3_key(test_s3_link) s3_object_mock.delete.assert_called_once_with() def test_find_exception_with_error(): """Test find_exception with exception.""" test_exception = Exception('test error') test_seq = [None, test_exception, None] assert upload_utils.find_exception(test_seq) == test_exception def test_find_exception_without_error(): """Test find_exception successful.""" test_seq = [None, None] assert upload_utils.find_exception(test_seq) is None