|
| 1 | +# |
| 2 | +# Copyright (2024) The Delta Lake Project Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | + |
| 20 | +def _is_matching_delta_file(file_name, version): |
| 21 | + expected_prefix = f"{version:020}." |
| 22 | + return ( |
| 23 | + file_name.startswith(expected_prefix) and |
| 24 | + file_name.endswith(".json") and |
| 25 | + ".tmp" not in file_name |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def _find_matching_delta_files(items, version): |
| 30 | + matching_files = [] |
| 31 | + for item in items: |
| 32 | + file_name = os.path.basename(item["Key"]) |
| 33 | + if _is_matching_delta_file(file_name, version): |
| 34 | + matching_files.append(file_name) |
| 35 | + return matching_files |
| 36 | + |
| 37 | + |
| 38 | +def validate_delta_file_listing_response(response, listing_prefix, version, should_exist): |
| 39 | + if 'Contents' not in response: |
| 40 | + assert not should_exist, ( |
| 41 | + f"Listing for prefix {listing_prefix} did not return any files even though it " |
| 42 | + f"should have." |
| 43 | + ) |
| 44 | + return |
| 45 | + |
| 46 | + expected_count = 1 if should_exist else 0 |
| 47 | + matching_files = _find_matching_delta_files(response['Contents'], version) |
| 48 | + assert len(matching_files) == expected_count, ( |
| 49 | + f"Expected {expected_count} matching files for version {version} under prefix " |
| 50 | + f"{listing_prefix}, but found {len(matching_files)}: {matching_files}" |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def test_should_fail_if_should_exist_is_true_and_s3_listing_lacks_contents(): |
| 55 | + test_name = "it('should fail if should_exist is true and S3 listing lacks Contents')" |
| 56 | + try: |
| 57 | + validate_delta_file_listing_response( |
| 58 | + response={}, |
| 59 | + listing_prefix="tables/test/_delta_log/00000000000000000001.", |
| 60 | + version=1, |
| 61 | + should_exist=True |
| 62 | + ) |
| 63 | + except AssertionError as error: |
| 64 | + assert "did not return any files" in str(error), ( |
| 65 | + f"{test_name}: unexpected assertion message: {error}" |
| 66 | + ) |
| 67 | + return |
| 68 | + |
| 69 | + raise AssertionError(f"{test_name}: expected missing Contents to fail") |
| 70 | + |
| 71 | + |
| 72 | +def test_should_match_delta_json_files_if_version_has_uuid_suffix(): |
| 73 | + test_name = "it('should match Delta JSON files if version has UUID suffix')" |
| 74 | + response = { |
| 75 | + 'Contents': [ |
| 76 | + {'Key': 'tables/test/_delta_log/_staged_commits/00000000000000000001.uuid.json'}, |
| 77 | + {'Key': 'tables/test/_delta_log/_staged_commits/00000000000000000001.uuid.json.tmp'} |
| 78 | + ] |
| 79 | + } |
| 80 | + |
| 81 | + try: |
| 82 | + validate_delta_file_listing_response( |
| 83 | + response=response, |
| 84 | + listing_prefix="tables/test/_delta_log/_staged_commits/00000000000000000001.", |
| 85 | + version=1, |
| 86 | + should_exist=True |
| 87 | + ) |
| 88 | + except AssertionError as error: |
| 89 | + raise AssertionError(f"{test_name}: unexpected assertion: {error}") |
| 90 | + |
| 91 | + |
| 92 | +def test_should_fail_if_should_exist_is_true_and_matching_file_has_non_json_suffix(): |
| 93 | + test_name = ( |
| 94 | + "it('should fail if should_exist is true and matching file has non-json suffix')" |
| 95 | + ) |
| 96 | + try: |
| 97 | + validate_delta_file_listing_response( |
| 98 | + response={ |
| 99 | + 'Contents': [ |
| 100 | + {'Key': 'tables/test/_delta_log/00000000000000000001.json.backup'} |
| 101 | + ] |
| 102 | + }, |
| 103 | + listing_prefix="tables/test/_delta_log/00000000000000000001.", |
| 104 | + version=1, |
| 105 | + should_exist=True |
| 106 | + ) |
| 107 | + except AssertionError as error: |
| 108 | + assert "Expected 1 matching files for version 1" in str(error), ( |
| 109 | + f"{test_name}: unexpected assertion message: {error}" |
| 110 | + ) |
| 111 | + return |
| 112 | + |
| 113 | + raise AssertionError(f"{test_name}: expected non-json suffix to fail") |
| 114 | + |
| 115 | + |
| 116 | +def run_listing_validation_self_tests(): |
| 117 | + test_should_fail_if_should_exist_is_true_and_s3_listing_lacks_contents() |
| 118 | + test_should_match_delta_json_files_if_version_has_uuid_suffix() |
| 119 | + test_should_fail_if_should_exist_is_true_and_matching_file_has_non_json_suffix() |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + run_listing_validation_self_tests() |
0 commit comments