From ce76353c25583c717c5067929abadd7031ea0a1d Mon Sep 17 00:00:00 2001 From: farisbasha Date: Wed, 18 Nov 2026 10:51:31 +0530 Subject: [PATCH] Fix the BigQuery namespace-exists error message The message was a plain string, so it showed the literal "{database_name}" instead of the namespace name. Make it an f-string and add a test for create_namespace on an existing dataset. --- pyiceberg/catalog/bigquery_metastore.py | 2 +- tests/catalog/test_bigquery_metastore.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pyiceberg/catalog/bigquery_metastore.py b/pyiceberg/catalog/bigquery_metastore.py index 74d1c952bc..9b229ba0dd 100644 --- a/pyiceberg/catalog/bigquery_metastore.py +++ b/pyiceberg/catalog/bigquery_metastore.py @@ -178,7 +178,7 @@ def create_namespace(self, namespace: str | Identifier, properties: Properties = dataset.external_catalog_dataset_options = self._create_external_catalog_dataset_options(properties, dataset_ref) self.client.create_dataset(dataset) except Conflict as e: - raise NamespaceAlreadyExistsError("Namespace {database_name} already exists") from e + raise NamespaceAlreadyExistsError(f"Namespace {database_name} already exists") from e @override def load_table(self, identifier: str | Identifier) -> Table: diff --git a/tests/catalog/test_bigquery_metastore.py b/tests/catalog/test_bigquery_metastore.py index df40417ff1..4b4f283f58 100644 --- a/tests/catalog/test_bigquery_metastore.py +++ b/tests/catalog/test_bigquery_metastore.py @@ -18,13 +18,13 @@ from unittest.mock import MagicMock import pytest -from google.api_core.exceptions import NotFound +from google.api_core.exceptions import Conflict, NotFound from google.cloud.bigquery import Dataset, DatasetReference, Table, TableReference from google.cloud.bigquery.external_config import ExternalCatalogDatasetOptions, ExternalCatalogTableOptions from pytest_mock import MockFixture from pyiceberg.catalog.bigquery_metastore import ICEBERG_TABLE_TYPE_VALUE, TABLE_TYPE_PROP, BigQueryMetastoreCatalog -from pyiceberg.exceptions import NoSuchNamespaceError, NoSuchTableError +from pyiceberg.exceptions import NamespaceAlreadyExistsError, NoSuchNamespaceError, NoSuchTableError from pyiceberg.schema import Schema @@ -124,6 +124,19 @@ def test_drop_namespace(mocker: MockFixture, gcp_dataset_name: str) -> None: assert args[0].dataset_id == gcp_dataset_name +def test_create_namespace_already_exists(mocker: MockFixture, gcp_dataset_name: str) -> None: + client_mock = MagicMock() + client_mock.create_dataset.side_effect = Conflict("Already Exists") + mocker.patch("pyiceberg.catalog.bigquery_metastore.Client", return_value=client_mock) + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) + catalog = BigQueryMetastoreCatalog( + "test_catalog", **{"gcp.bigquery.project-id": "my-project", "warehouse": "gs://test-bucket/warehouse"} + ) + + with pytest.raises(NamespaceAlreadyExistsError, match=f"^Namespace {gcp_dataset_name} already exists$"): + catalog.create_namespace(gcp_dataset_name) + + def test_list_tables(mocker: MockFixture, gcp_dataset_name: str) -> None: client_mock = MagicMock()