|
17 | 17 | import os |
18 | 18 | from unittest.mock import MagicMock |
19 | 19 |
|
| 20 | +import pytest |
20 | 21 | from google.api_core.exceptions import NotFound |
21 | 22 | from google.cloud.bigquery import Dataset, DatasetReference, Table, TableReference |
22 | 23 | from google.cloud.bigquery.external_config import ExternalCatalogDatasetOptions, ExternalCatalogTableOptions |
23 | 24 | from pytest_mock import MockFixture |
24 | 25 |
|
25 | 26 | from pyiceberg.catalog.bigquery_metastore import ICEBERG_TABLE_TYPE_VALUE, TABLE_TYPE_PROP, BigQueryMetastoreCatalog |
26 | | -from pyiceberg.exceptions import NoSuchTableError |
| 27 | +from pyiceberg.exceptions import CommitStateUnknownException, NoSuchTableError |
27 | 28 | from pyiceberg.schema import Schema |
28 | 29 |
|
29 | 30 |
|
@@ -178,3 +179,101 @@ def test_list_namespaces(mocker: MockFixture) -> None: |
178 | 179 | assert ("dataset1",) in namespaces |
179 | 180 | assert ("dataset2",) in namespaces |
180 | 181 | client_mock.list_datasets.assert_called_once() |
| 182 | + |
| 183 | + |
| 184 | +def test_commit_table_create_path_uses_create_table(mocker: MockFixture) -> None: |
| 185 | + client_mock = MagicMock() |
| 186 | + client_mock.get_table.side_effect = NotFound("missing") |
| 187 | + mocker.patch("pyiceberg.catalog.bigquery_metastore.Client", return_value=client_mock) |
| 188 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 189 | + |
| 190 | + catalog = BigQueryMetastoreCatalog("test_catalog", **{"gcp.bigquery.project-id": "my-project"}) |
| 191 | + table = MagicMock() |
| 192 | + table.name.return_value = ("my-dataset", "my-table") |
| 193 | + |
| 194 | + staged = MagicMock() |
| 195 | + staged.metadata = MagicMock() |
| 196 | + staged.metadata_location = "gs://bucket/db/table/metadata/00001.metadata.json" |
| 197 | + staged.io = MagicMock() |
| 198 | + mocker.patch.object(catalog, "_update_and_stage_table", return_value=staged) |
| 199 | + mocker.patch.object(catalog, "_write_metadata") |
| 200 | + mocker.patch.object(catalog, "_make_new_table", return_value=MagicMock()) |
| 201 | + commit_response = MagicMock() |
| 202 | + commit_response.metadata_location = staged.metadata_location |
| 203 | + mocker.patch("pyiceberg.catalog.bigquery_metastore.CommitTableResponse", return_value=commit_response) |
| 204 | + |
| 205 | + response = catalog.commit_table(table, requirements=(), updates=()) |
| 206 | + |
| 207 | + client_mock.create_table.assert_called_once() |
| 208 | + client_mock.update_table.assert_not_called() |
| 209 | + assert response.metadata_location == staged.metadata_location |
| 210 | + |
| 211 | + |
| 212 | +def test_commit_table_update_path_uses_update_table(mocker: MockFixture) -> None: |
| 213 | + client_mock = MagicMock() |
| 214 | + current_bq_table = MagicMock() |
| 215 | + client_mock.get_table.return_value = current_bq_table |
| 216 | + mocker.patch("pyiceberg.catalog.bigquery_metastore.Client", return_value=client_mock) |
| 217 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 218 | + |
| 219 | + catalog = BigQueryMetastoreCatalog("test_catalog", **{"gcp.bigquery.project-id": "my-project"}) |
| 220 | + table = MagicMock() |
| 221 | + table.name.return_value = ("my-dataset", "my-table") |
| 222 | + |
| 223 | + current_table = MagicMock() |
| 224 | + current_table.metadata = MagicMock() |
| 225 | + current_table.metadata_location = "gs://bucket/db/table/metadata/00000.metadata.json" |
| 226 | + mocker.patch.object(catalog, "_convert_bigquery_table_to_iceberg_table", return_value=current_table) |
| 227 | + |
| 228 | + staged = MagicMock() |
| 229 | + staged.metadata = MagicMock() |
| 230 | + staged.metadata.location = "gs://bucket/db/table" |
| 231 | + staged.metadata_location = "gs://bucket/db/table/metadata/00001.metadata.json" |
| 232 | + staged.io = MagicMock() |
| 233 | + mocker.patch.object(catalog, "_update_and_stage_table", return_value=staged) |
| 234 | + mocker.patch.object(catalog, "_write_metadata") |
| 235 | + mocker.patch.object(catalog, "_create_table_parameters", return_value={"metadata_location": staged.metadata_location}) |
| 236 | + mocker.patch.object(catalog, "_create_external_catalog_table_options", return_value=MagicMock()) |
| 237 | + delete_old_metadata = mocker.patch.object(catalog, "_delete_old_metadata") |
| 238 | + commit_response = MagicMock() |
| 239 | + commit_response.metadata_location = staged.metadata_location |
| 240 | + mocker.patch("pyiceberg.catalog.bigquery_metastore.CommitTableResponse", return_value=commit_response) |
| 241 | + |
| 242 | + response = catalog.commit_table(table, requirements=(), updates=()) |
| 243 | + |
| 244 | + client_mock.update_table.assert_called_once_with(current_bq_table, ["external_catalog_table_options"]) |
| 245 | + client_mock.create_table.assert_not_called() |
| 246 | + delete_old_metadata.assert_called_once_with(staged.io, current_table.metadata, staged.metadata) |
| 247 | + assert response.metadata_location == staged.metadata_location |
| 248 | + |
| 249 | + |
| 250 | +def test_commit_table_raises_unknown_when_commit_status_is_unknown(mocker: MockFixture) -> None: |
| 251 | + client_mock = MagicMock() |
| 252 | + current_bq_table = MagicMock() |
| 253 | + client_mock.get_table.return_value = current_bq_table |
| 254 | + client_mock.update_table.side_effect = RuntimeError("boom") |
| 255 | + mocker.patch("pyiceberg.catalog.bigquery_metastore.Client", return_value=client_mock) |
| 256 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 257 | + |
| 258 | + catalog = BigQueryMetastoreCatalog("test_catalog", **{"gcp.bigquery.project-id": "my-project"}) |
| 259 | + table = MagicMock() |
| 260 | + table.name.return_value = ("my-dataset", "my-table") |
| 261 | + |
| 262 | + current_table = MagicMock() |
| 263 | + current_table.metadata = MagicMock() |
| 264 | + current_table.metadata_location = "gs://bucket/db/table/metadata/00000.metadata.json" |
| 265 | + mocker.patch.object(catalog, "_convert_bigquery_table_to_iceberg_table", return_value=current_table) |
| 266 | + |
| 267 | + staged = MagicMock() |
| 268 | + staged.metadata = MagicMock() |
| 269 | + staged.metadata.location = "gs://bucket/db/table" |
| 270 | + staged.metadata_location = "gs://bucket/db/table/metadata/00001.metadata.json" |
| 271 | + staged.io = MagicMock() |
| 272 | + mocker.patch.object(catalog, "_update_and_stage_table", return_value=staged) |
| 273 | + mocker.patch.object(catalog, "_write_metadata") |
| 274 | + mocker.patch.object(catalog, "_create_table_parameters", return_value={"metadata_location": staged.metadata_location}) |
| 275 | + mocker.patch.object(catalog, "_create_external_catalog_table_options", return_value=MagicMock()) |
| 276 | + mocker.patch.object(catalog, "_check_bigquery_commit_status", return_value="UNKNOWN") |
| 277 | + |
| 278 | + with pytest.raises(CommitStateUnknownException): |
| 279 | + catalog.commit_table(table, requirements=(), updates=()) |
0 commit comments