|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import json |
| 19 | +import os |
| 20 | +import uuid |
| 21 | +from typing import Any, Dict |
| 22 | + |
| 23 | +import pytest |
| 24 | +from pytest_mock import MockFixture |
| 25 | + |
| 26 | +from pyiceberg.serializers import ToOutputFile |
| 27 | +from pyiceberg.table import StaticTable |
| 28 | +from pyiceberg.table.metadata import TableMetadataV1 |
| 29 | + |
| 30 | + |
| 31 | +def test_legacy_current_snapshot_id( |
| 32 | + mocker: MockFixture, tmp_path_factory: pytest.TempPathFactory, example_table_metadata_no_snapshot_v1: Dict[str, Any] |
| 33 | +) -> None: |
| 34 | + from pyiceberg.io.pyarrow import PyArrowFileIO |
| 35 | + |
| 36 | + metadata_location = str(tmp_path_factory.mktemp("metadata") / f"{uuid.uuid4()}.metadata.json") |
| 37 | + metadata = TableMetadataV1(**example_table_metadata_no_snapshot_v1) |
| 38 | + ToOutputFile.table_metadata(metadata, PyArrowFileIO().new_output(location=metadata_location), overwrite=True) |
| 39 | + static_table = StaticTable.from_metadata(metadata_location) |
| 40 | + assert static_table.metadata.current_snapshot_id is None |
| 41 | + |
| 42 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 43 | + |
| 44 | + ToOutputFile.table_metadata(metadata, PyArrowFileIO().new_output(location=metadata_location), overwrite=True) |
| 45 | + with PyArrowFileIO().new_input(location=metadata_location).open() as input_stream: |
| 46 | + metadata_json_bytes = input_stream.read() |
| 47 | + assert json.loads(metadata_json_bytes)['current-snapshot-id'] == -1 |
| 48 | + backwards_compatible_static_table = StaticTable.from_metadata(metadata_location) |
| 49 | + assert backwards_compatible_static_table.metadata.current_snapshot_id is None |
| 50 | + assert backwards_compatible_static_table.metadata == static_table.metadata |
0 commit comments