@@ -40,3 +40,74 @@ def test_create_branch(catalog: Catalog) -> None:
4040 branch_snapshot_id = tbl .history ()[- 2 ].snapshot_id
4141 tbl .manage_snapshots ().create_branch (snapshot_id = branch_snapshot_id , branch_name = "branch123" ).commit ()
4242 assert tbl .metadata .refs ["branch123" ] == SnapshotRef (snapshot_id = branch_snapshot_id , snapshot_ref_type = "branch" )
43+
44+
45+ @pytest .mark .integration
46+ @pytest .mark .parametrize ("catalog" , [pytest .lazy_fixture ("session_catalog_hive" ), pytest .lazy_fixture ("session_catalog" )])
47+ def test_manage_snapshots_context_manager (catalog : Catalog ) -> None :
48+ identifier = "default.test_table_snapshot_operations"
49+ tbl = catalog .load_table (identifier )
50+ assert len (tbl .history ()) > 3
51+ current_snapshot_id = tbl .current_snapshot ().snapshot_id # type: ignore
52+ rollback_snapshot_id = tbl .history ()[- 4 ].snapshot_id
53+ with tbl .manage_snapshots () as ms :
54+ ms .create_tag (snapshot_id = current_snapshot_id , tag_name = "testing" )
55+ ms .rollback_to_snapshot (snapshot_id = rollback_snapshot_id )
56+ assert tbl .current_snapshot ().snapshot_id is not current_snapshot_id # type: ignore
57+ assert tbl .metadata .refs ["testing" ].snapshot_id == current_snapshot_id
58+ assert tbl .metadata .refs ["main" ] == SnapshotRef (snapshot_id = rollback_snapshot_id , snapshot_ref_type = "branch" )
59+
60+
61+ @pytest .mark .integration
62+ @pytest .mark .parametrize ("catalog" , [pytest .lazy_fixture ("session_catalog_hive" ), pytest .lazy_fixture ("session_catalog" )])
63+ def test_rollback_to_snapshot (catalog : Catalog ) -> None :
64+ identifier = "default.test_table_snapshot_operations"
65+ tbl = catalog .load_table (identifier )
66+ assert len (tbl .history ()) > 3
67+ rollback_snapshot_id = tbl .history ()[- 3 ].snapshot_id
68+ current_snapshot_id = tbl .current_snapshot ().snapshot_id # type: ignore
69+ tbl .manage_snapshots ().rollback_to_snapshot (snapshot_id = rollback_snapshot_id ).commit ()
70+ assert tbl .current_snapshot ().snapshot_id is not current_snapshot_id # type: ignore
71+ assert tbl .metadata .refs ["main" ] == SnapshotRef (snapshot_id = rollback_snapshot_id , snapshot_ref_type = "branch" )
72+
73+
74+ @pytest .mark .integration
75+ @pytest .mark .parametrize ("catalog" , [pytest .lazy_fixture ("session_catalog_hive" ), pytest .lazy_fixture ("session_catalog" )])
76+ def test_rollback_to_timestamp (catalog : Catalog ) -> None :
77+ identifier = "default.test_table_snapshot_operations"
78+ tbl = catalog .load_table (identifier )
79+ assert len (tbl .history ()) > 3
80+ current_snapshot_id = tbl .current_snapshot ().snapshot_id # type: ignore
81+ timestamp = tbl .history ()[- 2 ].timestamp_ms
82+ expected_snapshot_id = tbl .history ()[- 3 ].snapshot_id
83+ # not inclusive of rollback_timestamp
84+ tbl .manage_snapshots ().rollback_to_timestamp (timestamp = timestamp ).commit ()
85+ assert tbl .current_snapshot ().snapshot_id is not current_snapshot_id # type: ignore
86+ assert tbl .metadata .refs ["main" ] == SnapshotRef (snapshot_id = expected_snapshot_id , snapshot_ref_type = "branch" )
87+
88+
89+ @pytest .mark .integration
90+ @pytest .mark .parametrize ("catalog" , [pytest .lazy_fixture ("session_catalog_hive" ), pytest .lazy_fixture ("session_catalog" )])
91+ def test_set_current_snapshot_with_snapshot_id (catalog : Catalog ) -> None :
92+ identifier = "default.test_table_snapshot_operations"
93+ tbl = catalog .load_table (identifier )
94+ assert len (tbl .history ()) > 3
95+ current_snapshot_id = tbl .current_snapshot ().snapshot_id # type: ignore
96+ expected_snapshot_id = tbl .history ()[- 3 ].snapshot_id
97+ tbl .manage_snapshots ().set_current_snapshot (snapshot_id = expected_snapshot_id ).commit ()
98+ assert tbl .current_snapshot ().snapshot_id is not current_snapshot_id # type: ignore
99+ assert tbl .metadata .refs ["main" ] == SnapshotRef (snapshot_id = expected_snapshot_id , snapshot_ref_type = "branch" )
100+
101+
102+ @pytest .mark .integration
103+ @pytest .mark .parametrize ("catalog" , [pytest .lazy_fixture ("session_catalog_hive" ), pytest .lazy_fixture ("session_catalog" )])
104+ def test_set_current_snapshot_with_ref_name (catalog : Catalog ) -> None :
105+ identifier = "default.test_table_snapshot_operations"
106+ tbl = catalog .load_table (identifier )
107+ assert len (tbl .history ()) > 3
108+ current_snapshot_id = tbl .current_snapshot ().snapshot_id # type: ignore
109+ expected_snapshot_id = tbl .history ()[- 3 ].snapshot_id
110+ tbl .manage_snapshots ().create_tag (snapshot_id = expected_snapshot_id , tag_name = "test-tag19" ).commit ()
111+ tbl .manage_snapshots ().set_current_snapshot (ref_name = "test-tag19" ).commit ()
112+ assert tbl .current_snapshot ().snapshot_id is not current_snapshot_id # type: ignore
113+ assert tbl .metadata .refs ["main" ] == SnapshotRef (snapshot_id = expected_snapshot_id , snapshot_ref_type = "branch" )
0 commit comments