Skip to content

Commit 8f6ea30

Browse files
authored
Fix the BigQuery namespace-exists error message (#4415)
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.
1 parent a1a4f77 commit 8f6ea30

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎pyiceberg/catalog/bigquery_metastore.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def create_namespace(self, namespace: str | Identifier, properties: Properties =
178178
dataset.external_catalog_dataset_options = self._create_external_catalog_dataset_options(properties, dataset_ref)
179179
self.client.create_dataset(dataset)
180180
except Conflict as e:
181-
raise NamespaceAlreadyExistsError("Namespace {database_name} already exists") from e
181+
raise NamespaceAlreadyExistsError(f"Namespace {database_name} already exists") from e
182182

183183
@override
184184
def load_table(self, identifier: str | Identifier) -> Table:

‎tests/catalog/test_bigquery_metastore.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
from unittest.mock import MagicMock
1919

2020
import pytest
21-
from google.api_core.exceptions import NotFound
21+
from google.api_core.exceptions import Conflict, NotFound
2222
from google.cloud.bigquery import Dataset, DatasetReference, Table, TableReference
2323
from google.cloud.bigquery.external_config import ExternalCatalogDatasetOptions, ExternalCatalogTableOptions
2424
from pytest_mock import MockFixture
2525

2626
from pyiceberg.catalog.bigquery_metastore import ICEBERG_TABLE_TYPE_VALUE, TABLE_TYPE_PROP, BigQueryMetastoreCatalog
27-
from pyiceberg.exceptions import NoSuchNamespaceError, NoSuchTableError
27+
from pyiceberg.exceptions import NamespaceAlreadyExistsError, NoSuchNamespaceError, NoSuchTableError
2828
from pyiceberg.schema import Schema
2929

3030

@@ -124,6 +124,19 @@ def test_drop_namespace(mocker: MockFixture, gcp_dataset_name: str) -> None:
124124
assert args[0].dataset_id == gcp_dataset_name
125125

126126

127+
def test_create_namespace_already_exists(mocker: MockFixture, gcp_dataset_name: str) -> None:
128+
client_mock = MagicMock()
129+
client_mock.create_dataset.side_effect = Conflict("Already Exists")
130+
mocker.patch("pyiceberg.catalog.bigquery_metastore.Client", return_value=client_mock)
131+
mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"})
132+
catalog = BigQueryMetastoreCatalog(
133+
"test_catalog", **{"gcp.bigquery.project-id": "my-project", "warehouse": "gs://test-bucket/warehouse"}
134+
)
135+
136+
with pytest.raises(NamespaceAlreadyExistsError, match=f"^Namespace {gcp_dataset_name} already exists$"):
137+
catalog.create_namespace(gcp_dataset_name)
138+
139+
127140
def test_list_tables(mocker: MockFixture, gcp_dataset_name: str) -> None:
128141
client_mock = MagicMock()
129142

0 commit comments

Comments
 (0)