|
23 | 23 |
|
24 | 24 | from pyiceberg.catalog import Catalog |
25 | 25 | from pyiceberg.table import Table |
26 | | -from pyiceberg.table.refs import SnapshotRef |
| 26 | +from pyiceberg.table.refs import SnapshotRef, SnapshotRefType |
27 | 27 |
|
28 | 28 |
|
29 | 29 | @pytest.fixture |
@@ -107,6 +107,62 @@ def test_remove_branch(catalog: Catalog) -> None: |
107 | 107 | assert tbl.metadata.refs.get(branch_name, None) is None |
108 | 108 |
|
109 | 109 |
|
| 110 | +@pytest.mark.integration |
| 111 | +@pytest.mark.parametrize("catalog", [lf("session_catalog_hive"), lf("session_catalog")]) |
| 112 | +def test_replace_branch(catalog: Catalog) -> None: |
| 113 | + identifier = "default.test_table_snapshot_operations" |
| 114 | + tbl = catalog.load_table(identifier) |
| 115 | + assert len(tbl.history()) > 2 |
| 116 | + |
| 117 | + current_snapshot_id = tbl.history()[-1].snapshot_id |
| 118 | + older_snapshot_id = tbl.history()[-2].snapshot_id |
| 119 | + |
| 120 | + branch_name = "my-branch" |
| 121 | + tbl.manage_snapshots().create_branch(older_snapshot_id, branch_name, 1, 2, 3).commit() |
| 122 | + branch = tbl.metadata.refs.get(branch_name) |
| 123 | + assert branch is not None |
| 124 | + assert branch.snapshot_id == older_snapshot_id |
| 125 | + assert branch.snapshot_ref_type == SnapshotRefType.BRANCH |
| 126 | + assert branch.max_ref_age_ms == 1 |
| 127 | + assert branch.max_snapshot_age_ms == 2 |
| 128 | + assert branch.min_snapshots_to_keep == 3 |
| 129 | + |
| 130 | + tbl.manage_snapshots().replace_branch(branch_name=branch_name, snapshot_id=current_snapshot_id).commit() |
| 131 | + |
| 132 | + branch = tbl.metadata.refs.get(branch_name) |
| 133 | + assert branch is not None |
| 134 | + assert branch.snapshot_id == current_snapshot_id |
| 135 | + assert branch.snapshot_ref_type == SnapshotRefType.BRANCH |
| 136 | + assert branch.max_ref_age_ms == 1 |
| 137 | + assert branch.max_snapshot_age_ms == 2 |
| 138 | + assert branch.min_snapshots_to_keep == 3 |
| 139 | + |
| 140 | + |
| 141 | +@pytest.mark.integration |
| 142 | +@pytest.mark.parametrize("catalog", [lf("session_catalog_hive"), lf("session_catalog")]) |
| 143 | +def test_replace_missing_branch(catalog: Catalog) -> None: |
| 144 | + identifier = "default.test_table_snapshot_operations" |
| 145 | + tbl = catalog.load_table(identifier) |
| 146 | + snapshot_id = tbl.history()[-1].snapshot_id |
| 147 | + |
| 148 | + with pytest.raises(ValueError, match="Branch does not exist: test"): |
| 149 | + tbl.manage_snapshots().replace_branch(branch_name="test", snapshot_id=snapshot_id).commit() |
| 150 | + |
| 151 | + |
| 152 | +@pytest.mark.integration |
| 153 | +@pytest.mark.parametrize("catalog", [lf("session_catalog_hive"), lf("session_catalog")]) |
| 154 | +def test_replace_branch_with_tag(catalog: Catalog) -> None: |
| 155 | + identifier = "default.test_table_snapshot_operations" |
| 156 | + tbl = catalog.load_table(identifier) |
| 157 | + snapshot_id = tbl.history()[-1].snapshot_id |
| 158 | + |
| 159 | + tag_name = "my-tag" |
| 160 | + tbl.manage_snapshots().create_tag(snapshot_id=snapshot_id, tag_name=tag_name).commit() |
| 161 | + |
| 162 | + with pytest.raises(ValueError, match="Ref my-tag is not a branch"): |
| 163 | + tbl.manage_snapshots().replace_branch(branch_name=tag_name, snapshot_id=snapshot_id).commit() |
| 164 | + |
| 165 | + |
110 | 166 | @pytest.mark.integration |
111 | 167 | @pytest.mark.parametrize("catalog", [lf("session_catalog_hive"), lf("session_catalog")]) |
112 | 168 | def test_set_current_snapshot(catalog: Catalog) -> None: |
|
0 commit comments