|
| 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 | +from unittest.mock import MagicMock |
| 18 | +from uuid import uuid4 |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from pyiceberg.table import CommitTableResponse, Table |
| 23 | +from pyiceberg.table.update import SetSnapshotRefUpdate, TableUpdate |
| 24 | + |
| 25 | + |
| 26 | +def _mock_commit_response(table: Table) -> CommitTableResponse: |
| 27 | + return CommitTableResponse( |
| 28 | + metadata=table.metadata, |
| 29 | + metadata_location="s3://bucket/tbl", |
| 30 | + uuid=uuid4(), |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def _get_updates(mock_catalog: MagicMock) -> tuple[TableUpdate, ...]: |
| 35 | + args, _ = mock_catalog.commit_table.call_args |
| 36 | + return args[2] |
| 37 | + |
| 38 | + |
| 39 | +def test_set_current_snapshot_basic(table_v2: Table) -> None: |
| 40 | + snapshot_one = 3051729675574597004 |
| 41 | + |
| 42 | + table_v2.catalog = MagicMock() |
| 43 | + table_v2.catalog.commit_table.return_value = _mock_commit_response(table_v2) |
| 44 | + |
| 45 | + table_v2.manage_snapshots().set_current_snapshot(snapshot_id=snapshot_one).commit() |
| 46 | + |
| 47 | + table_v2.catalog.commit_table.assert_called_once() |
| 48 | + |
| 49 | + updates = _get_updates(table_v2.catalog) |
| 50 | + set_ref_updates = [u for u in updates if isinstance(u, SetSnapshotRefUpdate)] |
| 51 | + |
| 52 | + assert len(set_ref_updates) == 1 |
| 53 | + update = set_ref_updates[0] |
| 54 | + assert update.snapshot_id == snapshot_one |
| 55 | + assert update.ref_name == "main" |
| 56 | + assert update.type == "branch" |
| 57 | + |
| 58 | + |
| 59 | +def test_set_current_snapshot_unknown_id(table_v2: Table) -> None: |
| 60 | + invalid_snapshot_id = 1234567890000 |
| 61 | + table_v2.catalog = MagicMock() |
| 62 | + |
| 63 | + with pytest.raises(ValueError, match="Cannot set current snapshot to unknown snapshot id"): |
| 64 | + table_v2.manage_snapshots().set_current_snapshot(snapshot_id=invalid_snapshot_id).commit() |
| 65 | + |
| 66 | + table_v2.catalog.commit_table.assert_not_called() |
| 67 | + |
| 68 | + |
| 69 | +def test_set_current_snapshot_to_current(table_v2: Table) -> None: |
| 70 | + current_snapshot = table_v2.current_snapshot() |
| 71 | + assert current_snapshot is not None |
| 72 | + |
| 73 | + table_v2.catalog = MagicMock() |
| 74 | + table_v2.catalog.commit_table.return_value = _mock_commit_response(table_v2) |
| 75 | + |
| 76 | + table_v2.manage_snapshots().set_current_snapshot(snapshot_id=current_snapshot.snapshot_id).commit() |
| 77 | + |
| 78 | + table_v2.catalog.commit_table.assert_called_once() |
| 79 | + |
| 80 | + |
| 81 | +def test_set_current_snapshot_chained_with_tag(table_v2: Table) -> None: |
| 82 | + snapshot_one = 3051729675574597004 |
| 83 | + table_v2.catalog = MagicMock() |
| 84 | + table_v2.catalog.commit_table.return_value = _mock_commit_response(table_v2) |
| 85 | + |
| 86 | + (table_v2.manage_snapshots().set_current_snapshot(snapshot_id=snapshot_one).create_tag(snapshot_one, "my-tag").commit()) |
| 87 | + |
| 88 | + table_v2.catalog.commit_table.assert_called_once() |
| 89 | + |
| 90 | + updates = _get_updates(table_v2.catalog) |
| 91 | + set_ref_updates = [u for u in updates if isinstance(u, SetSnapshotRefUpdate)] |
| 92 | + |
| 93 | + assert len(set_ref_updates) == 2 |
| 94 | + assert {u.ref_name for u in set_ref_updates} == {"main", "my-tag"} |
| 95 | + |
| 96 | + |
| 97 | +def test_set_current_snapshot_with_extensive_snapshots(table_v2_with_extensive_snapshots: Table) -> None: |
| 98 | + snapshots = table_v2_with_extensive_snapshots.metadata.snapshots |
| 99 | + assert len(snapshots) > 100 |
| 100 | + |
| 101 | + target_snapshot = snapshots[50].snapshot_id |
| 102 | + |
| 103 | + table_v2_with_extensive_snapshots.catalog = MagicMock() |
| 104 | + table_v2_with_extensive_snapshots.catalog.commit_table.return_value = _mock_commit_response(table_v2_with_extensive_snapshots) |
| 105 | + |
| 106 | + table_v2_with_extensive_snapshots.manage_snapshots().set_current_snapshot(snapshot_id=target_snapshot).commit() |
| 107 | + |
| 108 | + table_v2_with_extensive_snapshots.catalog.commit_table.assert_called_once() |
| 109 | + |
| 110 | + updates = _get_updates(table_v2_with_extensive_snapshots.catalog) |
| 111 | + set_ref_updates = [u for u in updates if isinstance(u, SetSnapshotRefUpdate)] |
| 112 | + |
| 113 | + assert len(set_ref_updates) == 1 |
| 114 | + assert set_ref_updates[0].snapshot_id == target_snapshot |
| 115 | + |
| 116 | + |
| 117 | +def test_set_current_snapshot_by_ref_name(table_v2: Table) -> None: |
| 118 | + current_snapshot = table_v2.current_snapshot() |
| 119 | + assert current_snapshot is not None |
| 120 | + |
| 121 | + table_v2.catalog = MagicMock() |
| 122 | + table_v2.catalog.commit_table.return_value = _mock_commit_response(table_v2) |
| 123 | + |
| 124 | + table_v2.manage_snapshots().set_current_snapshot(ref_name="main").commit() |
| 125 | + |
| 126 | + updates = _get_updates(table_v2.catalog) |
| 127 | + set_ref_updates = [u for u in updates if isinstance(u, SetSnapshotRefUpdate)] |
| 128 | + |
| 129 | + assert len(set_ref_updates) == 1 |
| 130 | + assert set_ref_updates[0].snapshot_id == current_snapshot.snapshot_id |
| 131 | + assert set_ref_updates[0].ref_name == "main" |
| 132 | + |
| 133 | + |
| 134 | +def test_set_current_snapshot_unknown_ref(table_v2: Table) -> None: |
| 135 | + table_v2.catalog = MagicMock() |
| 136 | + |
| 137 | + with pytest.raises(ValueError, match="Cannot find matching snapshot ID for ref: nonexistent"): |
| 138 | + table_v2.manage_snapshots().set_current_snapshot(ref_name="nonexistent").commit() |
| 139 | + |
| 140 | + table_v2.catalog.commit_table.assert_not_called() |
| 141 | + |
| 142 | + |
| 143 | +def test_set_current_snapshot_requires_one_argument(table_v2: Table) -> None: |
| 144 | + table_v2.catalog = MagicMock() |
| 145 | + |
| 146 | + with pytest.raises(ValueError, match="Either snapshot_id or ref_name must be provided, not both"): |
| 147 | + table_v2.manage_snapshots().set_current_snapshot().commit() |
| 148 | + |
| 149 | + with pytest.raises(ValueError, match="Either snapshot_id or ref_name must be provided, not both"): |
| 150 | + table_v2.manage_snapshots().set_current_snapshot(snapshot_id=123, ref_name="main").commit() |
| 151 | + |
| 152 | + table_v2.catalog.commit_table.assert_not_called() |
| 153 | + |
| 154 | + |
| 155 | +def test_set_current_snapshot_chained_with_create_tag(table_v2: Table) -> None: |
| 156 | + snapshot_one = 3051729675574597004 |
| 157 | + table_v2.catalog = MagicMock() |
| 158 | + table_v2.catalog.commit_table.return_value = _mock_commit_response(table_v2) |
| 159 | + |
| 160 | + # create a tag and immediately use it to set current snapshot |
| 161 | + ( |
| 162 | + table_v2.manage_snapshots() |
| 163 | + .create_tag(snapshot_id=snapshot_one, tag_name="new-tag") |
| 164 | + .set_current_snapshot(ref_name="new-tag") |
| 165 | + .commit() |
| 166 | + ) |
| 167 | + |
| 168 | + table_v2.catalog.commit_table.assert_called_once() |
| 169 | + |
| 170 | + updates = _get_updates(table_v2.catalog) |
| 171 | + set_ref_updates = [u for u in updates if isinstance(u, SetSnapshotRefUpdate)] |
| 172 | + |
| 173 | + # should have the tag and the main branch update |
| 174 | + assert len(set_ref_updates) == 2 |
| 175 | + assert {u.ref_name for u in set_ref_updates} == {"new-tag", "main"} |
| 176 | + |
| 177 | + # The main branch should point to the same snapshot as the tag |
| 178 | + main_update = next(u for u in set_ref_updates if u.ref_name == "main") |
| 179 | + assert main_update.snapshot_id == snapshot_one |
0 commit comments