|
| 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 | +import os |
| 18 | +from unittest.mock import MagicMock |
| 19 | + |
| 20 | +from google.api_core.exceptions import NotFound |
| 21 | +from google.cloud.bigquery import Dataset, DatasetReference, Table, TableReference |
| 22 | +from google.cloud.bigquery.external_config import ExternalCatalogDatasetOptions, ExternalCatalogTableOptions |
| 23 | +from pytest_mock import MockFixture |
| 24 | + |
| 25 | +from pyiceberg.catalog.bigquery_metastore import ICEBERG_TABLE_TYPE_VALUE, TABLE_TYPE_PROP, BigQueryMetastoreCatalog |
| 26 | +from pyiceberg.exceptions import NoSuchTableError |
| 27 | +from pyiceberg.schema import Schema |
| 28 | + |
| 29 | + |
| 30 | +def dataset_mock() -> Dataset: |
| 31 | + d = Dataset(DatasetReference(dataset_id="my-dataset", project="my-project")) |
| 32 | + d.external_catalog_dataset_options = ExternalCatalogDatasetOptions( |
| 33 | + default_storage_location_uri="gs://test-bucket/iceberg-dataset" |
| 34 | + ) |
| 35 | + return d |
| 36 | + |
| 37 | + |
| 38 | +def table_mock() -> Table: |
| 39 | + t = Table(TableReference(dataset_ref=DatasetReference(dataset_id="my-dataset", project="my-project"), table_id="my-table")) |
| 40 | + t.external_catalog_table_options = ExternalCatalogTableOptions( |
| 41 | + parameters={ |
| 42 | + "metadata_location": "gs://alexstephen-test-bq-bucket/my_iceberg_database_aaaaaaaaaaaaaaaaaaaa.db/my_iceberg_table-bbbbbbbbbbbbbbbbbbbb/metadata/12343-aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa.metadata", |
| 43 | + TABLE_TYPE_PROP: ICEBERG_TABLE_TYPE_VALUE, |
| 44 | + } |
| 45 | + ) |
| 46 | + return t |
| 47 | + |
| 48 | +def test_create_table_with_database_location( |
| 49 | + mocker: MockFixture, _bucket_initialize: None, table_schema_nested: Schema, gcp_dataset_name: str, table_name: str |
| 50 | +) -> None: |
| 51 | + # Setup mocks for GCP. |
| 52 | + client_mock = MagicMock() |
| 53 | + client_mock.get_dataset.return_value = dataset_mock() |
| 54 | + client_mock.get_table.return_value = table_mock() |
| 55 | + |
| 56 | + # Setup mocks for GCS. |
| 57 | + file_mock = MagicMock() |
| 58 | + |
| 59 | + mocker.patch('pyiceberg.catalog.bigquery_metastore.Client', return_value=client_mock) |
| 60 | + mocker.patch('pyiceberg.catalog.bigquery_metastore.FromInputFile.table_metadata', return_value=file_mock) |
| 61 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 62 | + |
| 63 | + catalog_name = "test_ddb_catalog" |
| 64 | + identifier = (gcp_dataset_name, table_name) |
| 65 | + test_catalog = BigQueryMetastoreCatalog(catalog_name, **{"gcp.project-id": "alexstephen-test-1", "warehouse": "gs://alexstephen-test-bq-bucket/"}) |
| 66 | + test_catalog.create_namespace(namespace=gcp_dataset_name) |
| 67 | + table = test_catalog.create_table(identifier, table_schema_nested) |
| 68 | + assert table.name() == identifier |
| 69 | + |
| 70 | +def test_drop_table_with_database_location( |
| 71 | + mocker: MockFixture, _bucket_initialize: None, table_schema_nested: Schema, gcp_dataset_name: str, table_name: str |
| 72 | +) -> None: |
| 73 | + # Setup mocks for GCP. |
| 74 | + client_mock = MagicMock() |
| 75 | + client_mock.get_dataset.return_value = dataset_mock() |
| 76 | + client_mock.get_table.return_value = table_mock() |
| 77 | + |
| 78 | + # Setup mocks for GCS. |
| 79 | + file_mock = MagicMock() |
| 80 | + |
| 81 | + mocker.patch('pyiceberg.catalog.bigquery_metastore.Client', return_value=client_mock) |
| 82 | + mocker.patch('pyiceberg.catalog.bigquery_metastore.FromInputFile.table_metadata', return_value=file_mock) |
| 83 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 84 | + |
| 85 | + catalog_name = "test_ddb_catalog" |
| 86 | + identifier = (gcp_dataset_name, table_name) |
| 87 | + test_catalog = BigQueryMetastoreCatalog(catalog_name, **{"gcp.project-id": "alexstephen-test-1", "warehouse": "gs://alexstephen-test-bq-bucket/"}) |
| 88 | + test_catalog.create_namespace(namespace=gcp_dataset_name) |
| 89 | + table = test_catalog.create_table(identifier, table_schema_nested) |
| 90 | + test_catalog.drop_table(identifier) |
| 91 | + |
| 92 | + client_mock.get_table.side_effect = NotFound("Table Not Found") |
| 93 | + mocker.patch('pyiceberg.catalog.bigquery_metastore.Client', return_value=client_mock) |
| 94 | + |
| 95 | + # Expect that the table no longer exists. |
| 96 | + try: |
| 97 | + test_catalog.load_table(identifier) |
| 98 | + raise AssertionError() |
| 99 | + except NoSuchTableError as e: |
| 100 | + assert True |
| 101 | + |
| 102 | + |
| 103 | +def test_drop_namespace(mocker: MockFixture, gcp_dataset_name: str) -> None: |
| 104 | + client_mock = MagicMock() |
| 105 | + mocker.patch('pyiceberg.catalog.bigquery_metastore.Client', return_value=client_mock) |
| 106 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 107 | + |
| 108 | + catalog_name = "test_catalog" |
| 109 | + test_catalog = BigQueryMetastoreCatalog(catalog_name, **{"gcp.project-id": "alexstephen-test-1"}) |
| 110 | + |
| 111 | + test_catalog.drop_namespace(gcp_dataset_name) |
| 112 | + client_mock.delete_dataset.assert_called_once() |
| 113 | + args, _ = client_mock.delete_dataset.call_args |
| 114 | + assert isinstance(args[0], Dataset) |
| 115 | + assert args[0].dataset_id == gcp_dataset_name |
| 116 | + |
| 117 | + |
| 118 | +def test_list_tables(mocker: MockFixture, gcp_dataset_name: str) -> None: |
| 119 | + client_mock = MagicMock() |
| 120 | + |
| 121 | + # Mock list_tables to return an iterator of TableListItem |
| 122 | + table_list_item_1 = MagicMock() |
| 123 | + table_list_item_1.table_id = "iceberg_table_A" |
| 124 | + table_list_item_1.reference = TableReference( |
| 125 | + dataset_ref=DatasetReference(project="my-project", dataset_id=gcp_dataset_name), table_id="iceberg_table_A" |
| 126 | + ) |
| 127 | + |
| 128 | + table_list_item_2 = MagicMock() |
| 129 | + table_list_item_2.table_id = "iceberg_table_B" |
| 130 | + table_list_item_2.reference = TableReference( |
| 131 | + dataset_ref=DatasetReference(project="my-project", dataset_id=gcp_dataset_name), table_id="iceberg_table_B" |
| 132 | + ) |
| 133 | + |
| 134 | + client_mock.list_tables.return_value = iter([table_list_item_1, table_list_item_2]) |
| 135 | + |
| 136 | + # Mock get_table to always return a table that is considered an Iceberg table. |
| 137 | + # The table_mock() function already creates a table with the necessary Iceberg properties. |
| 138 | + client_mock.get_table.return_value = table_mock() |
| 139 | + |
| 140 | + mocker.patch('pyiceberg.catalog.bigquery_metastore.Client', return_value=client_mock) |
| 141 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 142 | + |
| 143 | + catalog_name = "test_catalog" |
| 144 | + test_catalog = BigQueryMetastoreCatalog(catalog_name, **{"gcp.project-id": "my-project"}) |
| 145 | + |
| 146 | + tables = test_catalog.list_tables(gcp_dataset_name) |
| 147 | + |
| 148 | + # Assert that all tables returned by client.list_tables are listed |
| 149 | + assert len(tables) == 2 |
| 150 | + assert (gcp_dataset_name, "iceberg_table_A") in tables |
| 151 | + assert (gcp_dataset_name, "iceberg_table_B") in tables |
| 152 | + |
| 153 | + client_mock.list_tables.assert_called_once_with(dataset=DatasetReference(project="my-project", dataset_id=gcp_dataset_name)) |
| 154 | + |
| 155 | +def test_list_namespaces(mocker: MockFixture) -> None: |
| 156 | + client_mock = MagicMock() |
| 157 | + dataset_item_1 = Dataset(DatasetReference(project="my-project", dataset_id="dataset1")) |
| 158 | + dataset_item_2 = Dataset(DatasetReference(project="my-project", dataset_id="dataset2")) |
| 159 | + client_mock.list_datasets.return_value = iter([dataset_item_1, dataset_item_2]) |
| 160 | + |
| 161 | + mocker.patch('pyiceberg.catalog.bigquery_metastore.Client', return_value=client_mock) |
| 162 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 163 | + |
| 164 | + catalog_name = "test_catalog" |
| 165 | + test_catalog = BigQueryMetastoreCatalog(catalog_name, **{"gcp.project-id": "my-project"}) |
| 166 | + |
| 167 | + namespaces = test_catalog.list_namespaces() |
| 168 | + assert len(namespaces) == 2 |
| 169 | + assert ("dataset1",) in namespaces |
| 170 | + assert ("dataset2",) in namespaces |
| 171 | + client_mock.list_datasets.assert_called_once() |
0 commit comments