From f09bf3e55fff7f0a8a85df8fd69c021332188c94 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Tue, 3 Nov 2026 23:31:56 +0000 Subject: [PATCH 1/2] fix(bigquery): restore IF NOT EXISTS in create_search_index and clean up test mocks Problem: Following review feedback across the BigQuery sample PRs (#14714, #14715, #14717): - `bigquery_create_search_index.py` should use `CREATE SEARCH INDEX IF NOT EXISTS` and handle `exceptions.NotFound` when the parent table or dataset is missing. - `bigquery_drop_vector_index.py` `NotFound` error message should reflect that `DROP VECTOR INDEX IF EXISTS` raises `NotFound` when the parent table or dataset is missing. - Unit tests in `bigquery_create_search_index_test.py`, `bigquery_drop_vector_index_test.py`, and `bigquery_query_manifest_external_table_test.py` defined unused mock attributes (`mock_row` on DDL statements and `mock_job.statement_type` on SELECT queries). Solution: - Restore `CREATE SEARCH INDEX IF NOT EXISTS` and catch `exceptions.NotFound` in `bigquery/search/bigquery_create_search_index.py` and its unit test. - Update the `NotFound` error message in `bigquery/vector_search/bigquery_drop_vector_index.py`. - Remove unused `mock_row` and `mock_job.statement_type` setup in the three corresponding unit test files. --- .../bigquery_query_manifest_external_table_test.py | 5 +++-- bigquery/search/bigquery_create_search_index.py | 8 ++++---- bigquery/search/bigquery_create_search_index_test.py | 7 ++++--- bigquery/vector_search/bigquery_drop_vector_index.py | 4 ++-- bigquery/vector_search/bigquery_drop_vector_index_test.py | 7 ++++--- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/bigquery/biglake/bigquery_query_manifest_external_table_test.py b/bigquery/biglake/bigquery_query_manifest_external_table_test.py index cfe10fcdfa..b6f06dc9c0 100644 --- a/bigquery/biglake/bigquery_query_manifest_external_table_test.py +++ b/bigquery/biglake/bigquery_query_manifest_external_table_test.py @@ -26,7 +26,6 @@ def test_query_manifest_external_table_success( mock_client = mock.MagicMock() mock_job = mock.MagicMock() mock_job.job_id = "example-job-123" - mock_job.statement_type = "SCRIPT" mock_row = mock.MagicMock() mock_job.result.return_value = [mock_row] mock_client.query.return_value = mock_job @@ -54,7 +53,9 @@ def test_query_manifest_external_table_error( capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.NotFound("resource error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("resource error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_query_manifest_external_table.bigquery, "Client", diff --git a/bigquery/search/bigquery_create_search_index.py b/bigquery/search/bigquery_create_search_index.py index 6aeff84cc6..ff690d0019 100644 --- a/bigquery/search/bigquery_create_search_index.py +++ b/bigquery/search/bigquery_create_search_index.py @@ -38,7 +38,7 @@ def create_search_index( table_ref = f"{project_id}.{dataset_id}.{table_id}" # Indexing ALL COLUMNS enables full-text SEARCH queries across every STRING or JSON field. sql = f""" - CREATE SEARCH INDEX `{index_name}` + CREATE SEARCH INDEX IF NOT EXISTS `{index_name}` ON `{table_ref}`(ALL COLUMNS); """ @@ -46,10 +46,10 @@ def create_search_index( print(f"Creating search index {index_name} on table: {table_ref}") query_job = client.query(sql) query_job.result() - except exceptions.AlreadyExists as err: + except exceptions.NotFound as err: print( - f"error: search index '{index_name}' already exists on table " - f"'{table_id}' in dataset '{dataset_id}' for project '{project_id}': {err.message}", + f"error: table '{table_id}' or dataset '{dataset_id}' not found " + f"for project '{project_id}': {err.message}", file=sys.stderr, ) return diff --git a/bigquery/search/bigquery_create_search_index_test.py b/bigquery/search/bigquery_create_search_index_test.py index f1a77df6bb..48242a0501 100644 --- a/bigquery/search/bigquery_create_search_index_test.py +++ b/bigquery/search/bigquery_create_search_index_test.py @@ -27,8 +27,7 @@ def test_create_search_index_success( mock_job = mock.MagicMock() mock_job.job_id = "example-job-123" mock_job.statement_type = "SCRIPT" - mock_row = mock.MagicMock() - mock_job.result.return_value = [mock_row] + mock_job.result.return_value = [] mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_create_search_index.bigquery, @@ -53,7 +52,9 @@ def test_create_search_index_error( capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.AlreadyExists("resource error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("resource error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_create_search_index.bigquery, "Client", diff --git a/bigquery/vector_search/bigquery_drop_vector_index.py b/bigquery/vector_search/bigquery_drop_vector_index.py index 91c9c45e94..974d712e97 100644 --- a/bigquery/vector_search/bigquery_drop_vector_index.py +++ b/bigquery/vector_search/bigquery_drop_vector_index.py @@ -45,8 +45,8 @@ def drop_vector_index( query_job.result() except exceptions.NotFound as err: print( - f"error: table '{table_id}' or vector index '{index_name}' not found in " - f"dataset '{dataset_id}' for project '{project_id}': {err.message}", + f"error: table '{table_id}' or dataset '{dataset_id}' not found in " + f"project '{project_id}': {err.message}", file=sys.stderr, ) return diff --git a/bigquery/vector_search/bigquery_drop_vector_index_test.py b/bigquery/vector_search/bigquery_drop_vector_index_test.py index bf64f1f808..f441584f66 100644 --- a/bigquery/vector_search/bigquery_drop_vector_index_test.py +++ b/bigquery/vector_search/bigquery_drop_vector_index_test.py @@ -27,8 +27,7 @@ def test_drop_vector_index_success( mock_job = mock.MagicMock() mock_job.job_id = "example-job-123" mock_job.statement_type = "SCRIPT" - mock_row = mock.MagicMock() - mock_job.result.return_value = [mock_row] + mock_job.result.return_value = [] mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_drop_vector_index.bigquery, @@ -53,7 +52,9 @@ def test_drop_vector_index_error( capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.NotFound("resource error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("resource error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_drop_vector_index.bigquery, "Client", From 0930c0bb66b1a9e056c7d49186409250f620ecda Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Tue, 17 Nov 2026 04:04:44 +0000 Subject: [PATCH 2/2] refactor(bigquery): move tests into tests/ and apply review feedback to merged samples Problem: Reviewers on the BigQuery Data Cloud sample PRs asked for three changes: combined DDL + SELECT scripts were hard to read, every sample wrapped its API call in a generic try/except that printed to stderr and swallowed the error, and the unit tests patched the client with raw strings and asserted on stderr text. Each area folder also mixed *_test.py files with the samples themselves, cluttering the folders readers browse. Solution: - Move every unit test in bigquery/{stored_procedures,transactions,search, vector_search,biglake,federated_queries,data_profile_scan} into a tests/ subdirectory, add __init__.py to each area and its tests/ folder, and import the sample with `from .. import ` (same layout as cloud_scheduler/snippets). noxfile-template already collects `tests`. - Run DDL/scripts and the follow-up SELECT as separate client.query() calls (bigquery_query_iceberg_table, bigquery_multi_statement_transaction). - Remove the generic try/except stderr blocks (and the now-unused sys and google.api_core.exceptions imports) so GoogleAPICallError propagates. - Tests patch the client with monkeypatch.setattr(.bigquery, "Client", ...) / mock.patch.object and assert pytest.raises(exceptions.GoogleAPICallError) for the error path. Verified per area with the noxfile-template flake8 arguments (google import order, flake8-annotations), black, and pytest: 42 passed. --- bigquery/biglake/__init__.py | 13 +++++ .../bigquery_create_manifest_biglake_table.py | 25 +-------- .../biglake/bigquery_query_iceberg_table.py | 56 ++++++++----------- .../bigquery_query_manifest_external_table.py | 26 ++------- bigquery/biglake/tests/__init__.py | 13 +++++ ...uery_create_manifest_biglake_table_test.py | 36 ++++++------ .../bigquery_query_iceberg_table_test.py | 39 ++++++------- ...uery_query_manifest_external_table_test.py | 21 +++---- bigquery/data_profile_scan/__init__.py | 13 +++++ .../dataplex_delete_data_profile_scan.py | 24 +------- .../dataplex_get_data_profile_scan.py | 20 +------ bigquery/data_profile_scan/tests/__init__.py | 13 +++++ .../dataplex_delete_data_profile_scan_test.py | 19 +++---- .../dataplex_get_data_profile_scan_test.py | 19 +++---- bigquery/federated_queries/__init__.py | 13 +++++ .../bigquery_federated_query_alloydb.py | 24 +------- .../bigquery_federated_query_cloud_sql.py | 24 +------- ...igquery_federated_view_schema_cloud_sql.py | 24 +------- bigquery/federated_queries/tests/__init__.py | 13 +++++ .../bigquery_federated_query_alloydb_test.py | 21 ++++--- ...bigquery_federated_query_cloud_sql_test.py | 21 ++++--- ...ry_federated_view_schema_cloud_sql_test.py | 17 +++--- bigquery/search/__init__.py | 13 +++++ .../search/bigquery_create_search_index.py | 24 +------- bigquery/search/bigquery_drop_search_index.py | 24 +------- .../bigquery_get_search_index_status.py | 24 +------- .../search/bigquery_search_exclude_columns.py | 26 +-------- bigquery/search/bigquery_search_table.py | 24 +------- .../bigquery_search_with_analyzer_options.py | 28 ++-------- .../bigquery_search_with_text_analyzer.py | 26 +-------- bigquery/search/tests/__init__.py | 13 +++++ .../bigquery_create_search_index_test.py | 19 +++---- .../bigquery_drop_search_index_test.py | 23 ++++---- .../bigquery_get_search_index_status_test.py | 34 +++++------ .../bigquery_search_exclude_columns_test.py | 15 ++--- .../{ => tests}/bigquery_search_table_test.py | 19 +++---- ...query_search_with_analyzer_options_test.py | 19 +++---- ...bigquery_search_with_text_analyzer_test.py | 15 ++--- bigquery/stored_procedures/__init__.py | 13 +++++ .../bigquery_call_procedure.py | 24 +------- .../bigquery_call_system_procedure.py | 24 +------- .../bigquery_drop_procedure.py | 23 +------- bigquery/stored_procedures/tests/__init__.py | 13 +++++ .../bigquery_call_procedure_test.py | 23 ++++---- .../bigquery_call_system_procedure_test.py | 30 +++++----- .../bigquery_drop_procedure_test.py | 19 +++---- bigquery/transactions/__init__.py | 13 +++++ .../bigquery_multi_statement_transaction.py | 35 ++++-------- bigquery/transactions/tests/__init__.py | 13 +++++ ...gquery_multi_statement_transaction_test.py | 52 +++++++++-------- bigquery/vector_search/__init__.py | 13 +++++ .../bigquery_create_vector_index_tree_ah.py | 24 +------- .../bigquery_drop_vector_index.py | 24 +------- bigquery/vector_search/tests/__init__.py | 13 +++++ ...gquery_create_vector_index_tree_ah_test.py | 23 ++++---- .../bigquery_drop_vector_index_test.py | 19 +++---- 56 files changed, 513 insertions(+), 725 deletions(-) create mode 100644 bigquery/biglake/__init__.py create mode 100644 bigquery/biglake/tests/__init__.py rename bigquery/biglake/{ => tests}/bigquery_create_manifest_biglake_table_test.py (66%) rename bigquery/biglake/{ => tests}/bigquery_query_iceberg_table_test.py (67%) rename bigquery/biglake/{ => tests}/bigquery_query_manifest_external_table_test.py (81%) create mode 100644 bigquery/data_profile_scan/__init__.py create mode 100644 bigquery/data_profile_scan/tests/__init__.py rename bigquery/data_profile_scan/{ => tests}/dataplex_delete_data_profile_scan_test.py (83%) rename bigquery/data_profile_scan/{ => tests}/dataplex_get_data_profile_scan_test.py (85%) create mode 100644 bigquery/federated_queries/__init__.py create mode 100644 bigquery/federated_queries/tests/__init__.py rename bigquery/federated_queries/{ => tests}/bigquery_federated_query_alloydb_test.py (79%) rename bigquery/federated_queries/{ => tests}/bigquery_federated_query_cloud_sql_test.py (79%) rename bigquery/federated_queries/{ => tests}/bigquery_federated_view_schema_cloud_sql_test.py (83%) create mode 100644 bigquery/search/__init__.py create mode 100644 bigquery/search/tests/__init__.py rename bigquery/search/{ => tests}/bigquery_create_search_index_test.py (83%) rename bigquery/search/{ => tests}/bigquery_drop_search_index_test.py (79%) rename bigquery/search/{ => tests}/bigquery_get_search_index_status_test.py (70%) rename bigquery/search/{ => tests}/bigquery_search_exclude_columns_test.py (86%) rename bigquery/search/{ => tests}/bigquery_search_table_test.py (83%) rename bigquery/search/{ => tests}/bigquery_search_with_analyzer_options_test.py (82%) rename bigquery/search/{ => tests}/bigquery_search_with_text_analyzer_test.py (85%) create mode 100644 bigquery/stored_procedures/__init__.py create mode 100644 bigquery/stored_procedures/tests/__init__.py rename bigquery/stored_procedures/{ => tests}/bigquery_call_procedure_test.py (80%) rename bigquery/stored_procedures/{ => tests}/bigquery_call_system_procedure_test.py (74%) rename bigquery/stored_procedures/{ => tests}/bigquery_drop_procedure_test.py (84%) create mode 100644 bigquery/transactions/__init__.py create mode 100644 bigquery/transactions/tests/__init__.py rename bigquery/transactions/{ => tests}/bigquery_multi_statement_transaction_test.py (58%) create mode 100644 bigquery/vector_search/__init__.py create mode 100644 bigquery/vector_search/tests/__init__.py rename bigquery/vector_search/{ => tests}/bigquery_create_vector_index_tree_ah_test.py (80%) rename bigquery/vector_search/{ => tests}/bigquery_drop_vector_index_test.py (83%) diff --git a/bigquery/biglake/__init__.py b/bigquery/biglake/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/biglake/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/biglake/bigquery_create_manifest_biglake_table.py b/bigquery/biglake/bigquery_create_manifest_biglake_table.py index ec495e9b0a..3f1c858c43 100644 --- a/bigquery/biglake/bigquery_create_manifest_biglake_table.py +++ b/bigquery/biglake/bigquery_create_manifest_biglake_table.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_create_manifest_biglake_table] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -52,25 +49,9 @@ def create_manifest_biglake_table( ); """ - try: - print(f"Creating manifest-backed BigLake table: {table_ref}") - query_job = client.query(sql) - query_job.result() - except exceptions.NotFound as err: - print( - f"error: dataset '{dataset_id}' or connection '{connection_id}' not found " - f"for project '{project_id}' (manifest '{manifest_uri}'): {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to create BigLake table '{table_id}' in dataset '{dataset_id}' " - f"for project '{project_id}' (connection '{connection_id}', " - f"manifest '{manifest_uri}'): {err.message}", - file=sys.stderr, - ) - return + print(f"Creating manifest-backed BigLake table: {table_ref}") + query_job = client.query(sql) + query_job.result() print(f"Resource ID: {table_ref}") print(f"Connection ID: {connection_id}") diff --git a/bigquery/biglake/bigquery_query_iceberg_table.py b/bigquery/biglake/bigquery_query_iceberg_table.py index bfc59cbbbf..79a4b0eb6c 100644 --- a/bigquery/biglake/bigquery_query_iceberg_table.py +++ b/bigquery/biglake/bigquery_query_iceberg_table.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_query_iceberg_table] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -43,38 +40,29 @@ def query_iceberg_table( table_ref = f"{project_id}.{dataset_id}.{table_id}" # BigQuery Iceberg external tables point to a specific Iceberg metadata JSON snapshot; # re-running CREATE OR REPLACE EXTERNAL TABLE updates the snapshot URI with schema autodetection. - query = ( - f"CREATE OR REPLACE EXTERNAL TABLE `{table_ref}` " - f"WITH CONNECTION `{connection_id}` " - "OPTIONS (" - " format = 'ICEBERG'," - f" uris = ['{metadata_uri}']" - "); " - "SELECT order_id, user_id, status, num_of_item " - f"FROM `{table_ref}` " - "ORDER BY order_id ASC;" - ) + ddl_sql = f""" + CREATE OR REPLACE EXTERNAL TABLE `{table_ref}` + WITH CONNECTION `{connection_id}` + OPTIONS ( + format = 'ICEBERG', + uris = ['{metadata_uri}'] + ); + """ + select_sql = f""" + SELECT + order_id, + user_id, + status, + num_of_item + FROM `{table_ref}` + ORDER BY order_id ASC; + """ + + print(f"Refreshing Iceberg metadata and querying table: {table_ref}") + client.query(ddl_sql).result() - try: - print(f"Refreshing Iceberg metadata and querying table: {table_ref}") - query_job = client.query(query) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: Apache Iceberg table '{table_id}' in dataset '{dataset_id}' " - f"for project '{project_id}' (connection '{connection_id}', " - f"metadata '{metadata_uri}') was not found: {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: could not refresh or query Apache Iceberg table '{table_id}' " - f"in dataset '{dataset_id}' for project '{project_id}' " - f"(connection '{connection_id}', metadata '{metadata_uri}'): {err.message}", - file=sys.stderr, - ) - return + query_job = client.query(select_sql) + rows = list(query_job.result()) print(f"Resource ID: {table_ref}") print(f"Job ID: {query_job.job_id}") diff --git a/bigquery/biglake/bigquery_query_manifest_external_table.py b/bigquery/biglake/bigquery_query_manifest_external_table.py index bf2d121fd6..05073bc927 100644 --- a/bigquery/biglake/bigquery_query_manifest_external_table.py +++ b/bigquery/biglake/bigquery_query_manifest_external_table.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_query_manifest_external_table] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -54,25 +51,10 @@ def query_manifest_external_table( "SELECT id, label, score " f"FROM `{table_ref}` " "ORDER BY id ASC " "LIMIT 10" ) - try: - print(f"Configuring manifest URI and querying external table: {table_ref}") - client.update_table(table, ["external_data_configuration"]) - query_job = client.query(query) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: manifest external table '{table_id}' in dataset '{dataset_id}' " - f"for project '{project_id}' (manifest '{manifest_uri}') was not found: {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: could not query manifest external table '{table_id}' in dataset " - f"'{dataset_id}' for project '{project_id}' (manifest '{manifest_uri}'): {err.message}", - file=sys.stderr, - ) - return + print(f"Configuring manifest URI and querying external table: {table_ref}") + client.update_table(table, ["external_data_configuration"]) + query_job = client.query(query) + rows = list(query_job.result()) print(f"Resource ID: {table_ref}") print(f"Job ID: {query_job.job_id}") diff --git a/bigquery/biglake/tests/__init__.py b/bigquery/biglake/tests/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/biglake/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/biglake/bigquery_create_manifest_biglake_table_test.py b/bigquery/biglake/tests/bigquery_create_manifest_biglake_table_test.py similarity index 66% rename from bigquery/biglake/bigquery_create_manifest_biglake_table_test.py rename to bigquery/biglake/tests/bigquery_create_manifest_biglake_table_test.py index 92d5af6938..1c76a8aad5 100644 --- a/bigquery/biglake/bigquery_create_manifest_biglake_table_test.py +++ b/bigquery/biglake/tests/bigquery_create_manifest_biglake_table_test.py @@ -14,15 +14,18 @@ from unittest import mock -import bigquery_create_manifest_biglake_table from google.api_core import exceptions import pytest +from .. import bigquery_create_manifest_biglake_table + def test_create_manifest_biglake_table_success( capsys: pytest.CaptureFixture[str], ) -> None: - with mock.patch("google.cloud.bigquery.Client") as mock_client_cls: + with mock.patch.object( + bigquery_create_manifest_biglake_table.bigquery, "Client" + ) as mock_client_cls: mock_client = mock_client_cls.return_value mock_job = mock.MagicMock() mock_job.job_id = "example-job-123" @@ -47,20 +50,19 @@ def test_create_manifest_biglake_table_success( assert "Statement Type: CREATE_EXTERNAL_TABLE" in captured.out -def test_create_manifest_biglake_table_not_found( - capsys: pytest.CaptureFixture[str], -) -> None: - with mock.patch("google.cloud.bigquery.Client") as mock_client_cls: +def test_create_manifest_biglake_table_not_found() -> None: + with mock.patch.object( + bigquery_create_manifest_biglake_table.bigquery, "Client" + ) as mock_client_cls: mock_client = mock_client_cls.return_value - mock_client.query.side_effect = exceptions.NotFound("Dataset not found") - - bigquery_create_manifest_biglake_table.create_manifest_biglake_table( - project_id="example-project", - dataset_id="example_dataset", - connection_id="example-project.us.example_biglake_conn", - manifest_uri="gs://example-bucket/manifests/symlink.manifest", - ) + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("Dataset not found") + mock_client.query.return_value = mock_job - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example_dataset" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_create_manifest_biglake_table.create_manifest_biglake_table( + project_id="example-project", + dataset_id="example_dataset", + connection_id="example-project.us.example_biglake_conn", + manifest_uri="gs://example-bucket/manifests/symlink.manifest", + ) diff --git a/bigquery/biglake/bigquery_query_iceberg_table_test.py b/bigquery/biglake/tests/bigquery_query_iceberg_table_test.py similarity index 67% rename from bigquery/biglake/bigquery_query_iceberg_table_test.py rename to bigquery/biglake/tests/bigquery_query_iceberg_table_test.py index 7ae38d6862..c00d1e0c89 100644 --- a/bigquery/biglake/bigquery_query_iceberg_table_test.py +++ b/bigquery/biglake/tests/bigquery_query_iceberg_table_test.py @@ -14,21 +14,24 @@ from unittest import mock -import bigquery_query_iceberg_table from google.api_core import exceptions import pytest +from .. import bigquery_query_iceberg_table + def test_query_iceberg_table_success( monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_job = mock.MagicMock() - mock_job.job_id = "example-job-123" + mock_ddl_job = mock.MagicMock() + mock_ddl_job.result.return_value = [] + mock_select_job = mock.MagicMock() + mock_select_job.job_id = "example-job-123" mock_row = mock.MagicMock() - mock_job.result.return_value = [mock_row] - mock_client.query.return_value = mock_job + mock_select_job.result.return_value = [mock_row] + mock_client.query.side_effect = [mock_ddl_job, mock_select_job] monkeypatch.setattr( bigquery_query_iceberg_table.bigquery, "Client", @@ -46,29 +49,27 @@ def test_query_iceberg_table_success( captured = capsys.readouterr() assert "Resource ID:" in captured.out assert "example-project" in captured.out - assert mock_client.query.called + assert mock_client.query.call_count == 2 def test_query_iceberg_table_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.NotFound("resource error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("resource error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_query_iceberg_table.bigquery, "Client", lambda project=None: mock_client, ) - bigquery_query_iceberg_table.query_iceberg_table( - project_id="example-project", - dataset_id="example_dataset", - table_id="example_iceberg_table", - connection_id="example-project.us.example_iceberg_conn", - metadata_uri="gs://example-bucket/warehouse/orders_ns/customer_orders/metadata/v2.metadata.json", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_query_iceberg_table.query_iceberg_table( + project_id="example-project", + dataset_id="example_dataset", + table_id="example_iceberg_table", + connection_id="example-project.us.example_iceberg_conn", + metadata_uri="gs://example-bucket/warehouse/orders_ns/customer_orders/metadata/v2.metadata.json", + ) diff --git a/bigquery/biglake/bigquery_query_manifest_external_table_test.py b/bigquery/biglake/tests/bigquery_query_manifest_external_table_test.py similarity index 81% rename from bigquery/biglake/bigquery_query_manifest_external_table_test.py rename to bigquery/biglake/tests/bigquery_query_manifest_external_table_test.py index b6f06dc9c0..5ff1f9a3d6 100644 --- a/bigquery/biglake/bigquery_query_manifest_external_table_test.py +++ b/bigquery/biglake/tests/bigquery_query_manifest_external_table_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_query_manifest_external_table from google.api_core import exceptions import pytest +from .. import bigquery_query_manifest_external_table + def test_query_manifest_external_table_success( monkeypatch: pytest.MonkeyPatch, @@ -50,7 +51,6 @@ def test_query_manifest_external_table_success( def test_query_manifest_external_table_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() mock_job = mock.MagicMock() @@ -62,13 +62,10 @@ def test_query_manifest_external_table_error( lambda project=None: mock_client, ) - bigquery_query_manifest_external_table.query_manifest_external_table( - project_id="example-project", - dataset_id="example_dataset", - table_id="example_manifest_table", - manifest_uri="gs://example-bucket/manifests/symlink.manifest", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_query_manifest_external_table.query_manifest_external_table( + project_id="example-project", + dataset_id="example_dataset", + table_id="example_manifest_table", + manifest_uri="gs://example-bucket/manifests/symlink.manifest", + ) diff --git a/bigquery/data_profile_scan/__init__.py b/bigquery/data_profile_scan/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/data_profile_scan/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/data_profile_scan/dataplex_delete_data_profile_scan.py b/bigquery/data_profile_scan/dataplex_delete_data_profile_scan.py index 6e9ba19b76..b1ccd92f48 100644 --- a/bigquery/data_profile_scan/dataplex_delete_data_profile_scan.py +++ b/bigquery/data_profile_scan/dataplex_delete_data_profile_scan.py @@ -13,9 +13,6 @@ # limitations under the License. # [START dataplex_delete_data_profile_scan] -import sys - -from google.api_core import exceptions from google.cloud import dataplex_v1 @@ -38,24 +35,9 @@ def delete_data_profile_scan( # Deleting a DataScan is a long-running operation that removes the scan configuration. request = dataplex_v1.DeleteDataScanRequest(name=scan_name) - try: - print(f"Deleting Dataplex data profile scan: {scan_name}") - operation = client.delete_data_scan(request=request) - operation.result() - except exceptions.NotFound as err: - print( - f"error: data profile scan '{data_scan_id}' not found in location " - f"'{location}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to delete data profile scan '{data_scan_id}' in location " - f"'{location}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Deleting Dataplex data profile scan: {scan_name}") + operation = client.delete_data_scan(request=request) + operation.result() print(f"Resource ID: {scan_name}") print("Deleted: True") diff --git a/bigquery/data_profile_scan/dataplex_get_data_profile_scan.py b/bigquery/data_profile_scan/dataplex_get_data_profile_scan.py index 2c0b58413a..014f27025f 100644 --- a/bigquery/data_profile_scan/dataplex_get_data_profile_scan.py +++ b/bigquery/data_profile_scan/dataplex_get_data_profile_scan.py @@ -13,9 +13,6 @@ # limitations under the License. # [START dataplex_get_data_profile_scan] -import sys - -from google.api_core import exceptions from google.cloud import dataplex_v1 @@ -41,22 +38,7 @@ def get_data_profile_scan( view=dataplex_v1.GetDataScanRequest.DataScanView.FULL, ) - try: - scan = client.get_data_scan(request=request) - except exceptions.NotFound as err: - print( - f"error: data profile scan '{data_scan_id}' not found in location " - f"'{location}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to retrieve data profile scan '{data_scan_id}' in location " - f"'{location}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + scan = client.get_data_scan(request=request) row_count = ( scan.data_profile_result.row_count if scan.data_profile_result else "N/A" diff --git a/bigquery/data_profile_scan/tests/__init__.py b/bigquery/data_profile_scan/tests/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/data_profile_scan/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/data_profile_scan/dataplex_delete_data_profile_scan_test.py b/bigquery/data_profile_scan/tests/dataplex_delete_data_profile_scan_test.py similarity index 83% rename from bigquery/data_profile_scan/dataplex_delete_data_profile_scan_test.py rename to bigquery/data_profile_scan/tests/dataplex_delete_data_profile_scan_test.py index f05cf4bba0..00ea2b85ea 100644 --- a/bigquery/data_profile_scan/dataplex_delete_data_profile_scan_test.py +++ b/bigquery/data_profile_scan/tests/dataplex_delete_data_profile_scan_test.py @@ -14,10 +14,11 @@ from unittest import mock -import dataplex_delete_data_profile_scan from google.api_core import exceptions import pytest +from .. import dataplex_delete_data_profile_scan + def test_delete_data_profile_scan_success( monkeypatch: pytest.MonkeyPatch, @@ -51,7 +52,6 @@ def test_delete_data_profile_scan_success( def test_delete_data_profile_scan_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() mock_client.delete_data_scan.side_effect = exceptions.NotFound("scan not found") @@ -61,12 +61,9 @@ def test_delete_data_profile_scan_error( lambda: mock_client, ) - dataplex_delete_data_profile_scan.delete_data_profile_scan( - project_id="example-project", - location="us-central1", - data_scan_id="example-profile-scan", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-profile-scan" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + dataplex_delete_data_profile_scan.delete_data_profile_scan( + project_id="example-project", + location="us-central1", + data_scan_id="example-profile-scan", + ) diff --git a/bigquery/data_profile_scan/dataplex_get_data_profile_scan_test.py b/bigquery/data_profile_scan/tests/dataplex_get_data_profile_scan_test.py similarity index 85% rename from bigquery/data_profile_scan/dataplex_get_data_profile_scan_test.py rename to bigquery/data_profile_scan/tests/dataplex_get_data_profile_scan_test.py index afd77805c7..a6d1798200 100644 --- a/bigquery/data_profile_scan/dataplex_get_data_profile_scan_test.py +++ b/bigquery/data_profile_scan/tests/dataplex_get_data_profile_scan_test.py @@ -14,10 +14,11 @@ from unittest import mock -import dataplex_get_data_profile_scan from google.api_core import exceptions import pytest +from .. import dataplex_get_data_profile_scan + def test_get_data_profile_scan_success( monkeypatch: pytest.MonkeyPatch, @@ -55,7 +56,6 @@ def test_get_data_profile_scan_success( def test_get_data_profile_scan_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() mock_client.get_data_scan.side_effect = exceptions.NotFound("scan not found") @@ -65,12 +65,9 @@ def test_get_data_profile_scan_error( lambda: mock_client, ) - dataplex_get_data_profile_scan.get_data_profile_scan( - project_id="example-project", - location="us-central1", - data_scan_id="example-profile-scan", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-profile-scan" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + dataplex_get_data_profile_scan.get_data_profile_scan( + project_id="example-project", + location="us-central1", + data_scan_id="example-profile-scan", + ) diff --git a/bigquery/federated_queries/__init__.py b/bigquery/federated_queries/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/federated_queries/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/federated_queries/bigquery_federated_query_alloydb.py b/bigquery/federated_queries/bigquery_federated_query_alloydb.py index 67fc7d307f..257195723e 100644 --- a/bigquery/federated_queries/bigquery_federated_query_alloydb.py +++ b/bigquery/federated_queries/bigquery_federated_query_alloydb.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_federated_query_alloydb] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -52,24 +49,9 @@ def federated_query_alloydb( LIMIT 10; """ - try: - print(f"Running AlloyDB federated query via connection: {connection_id}") - query_job = client.query(query) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: AlloyDB connection '{connection_id}' not found in project " - f"'{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: could not run AlloyDB federated query on connection " - f"'{connection_id}' in project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Running AlloyDB federated query via connection: {connection_id}") + query_job = client.query(query) + rows = list(query_job.result()) print(f"Resource ID: {connection_id}") print(f"Job ID: {query_job.job_id}") diff --git a/bigquery/federated_queries/bigquery_federated_query_cloud_sql.py b/bigquery/federated_queries/bigquery_federated_query_cloud_sql.py index 78956455ee..259cb70073 100644 --- a/bigquery/federated_queries/bigquery_federated_query_cloud_sql.py +++ b/bigquery/federated_queries/bigquery_federated_query_cloud_sql.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_federated_query_cloud_sql] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -52,24 +49,9 @@ def federated_query_cloud_sql( LIMIT 10; """ - try: - print(f"Running Cloud SQL federated query via connection: {connection_id}") - query_job = client.query(query) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: Cloud SQL connection '{connection_id}' not found in project " - f"'{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: could not run Cloud SQL federated query on connection " - f"'{connection_id}' in project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Running Cloud SQL federated query via connection: {connection_id}") + query_job = client.query(query) + rows = list(query_job.result()) print(f"Resource ID: {connection_id}") print(f"Job ID: {query_job.job_id}") diff --git a/bigquery/federated_queries/bigquery_federated_view_schema_cloud_sql.py b/bigquery/federated_queries/bigquery_federated_view_schema_cloud_sql.py index abe7670b49..f87fe05b9e 100644 --- a/bigquery/federated_queries/bigquery_federated_view_schema_cloud_sql.py +++ b/bigquery/federated_queries/bigquery_federated_view_schema_cloud_sql.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_federated_view_schema_cloud_sql] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -47,24 +44,9 @@ def federated_view_schema_cloud_sql( ORDER BY column_name ASC; """ - try: - print(f"Inspecting Cloud SQL table schema via connection: {connection_id}") - query_job = client.query(query) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: Cloud SQL connection '{connection_id}' not found in project " - f"'{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: could not inspect Cloud SQL table schema on connection " - f"'{connection_id}' in project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Inspecting Cloud SQL table schema via connection: {connection_id}") + query_job = client.query(query) + rows = list(query_job.result()) print(f"Resource ID: {connection_id}") print(f"Columns Returned: {len(rows)}") diff --git a/bigquery/federated_queries/tests/__init__.py b/bigquery/federated_queries/tests/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/federated_queries/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/federated_queries/bigquery_federated_query_alloydb_test.py b/bigquery/federated_queries/tests/bigquery_federated_query_alloydb_test.py similarity index 79% rename from bigquery/federated_queries/bigquery_federated_query_alloydb_test.py rename to bigquery/federated_queries/tests/bigquery_federated_query_alloydb_test.py index 6b2705a6f8..6bb6f03333 100644 --- a/bigquery/federated_queries/bigquery_federated_query_alloydb_test.py +++ b/bigquery/federated_queries/tests/bigquery_federated_query_alloydb_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_federated_query_alloydb from google.api_core import exceptions import pytest +from .. import bigquery_federated_query_alloydb + def test_federated_query_alloydb_success( monkeypatch: pytest.MonkeyPatch, @@ -48,21 +49,19 @@ def test_federated_query_alloydb_success( def test_federated_query_alloydb_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.NotFound("resource error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("resource error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_federated_query_alloydb.bigquery, "Client", lambda project=None: mock_client, ) - bigquery_federated_query_alloydb.federated_query_alloydb( - project_id="example-project", - connection_id="example-project.us-central1.example-alloydb-conn", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_federated_query_alloydb.federated_query_alloydb( + project_id="example-project", + connection_id="example-project.us-central1.example-alloydb-conn", + ) diff --git a/bigquery/federated_queries/bigquery_federated_query_cloud_sql_test.py b/bigquery/federated_queries/tests/bigquery_federated_query_cloud_sql_test.py similarity index 79% rename from bigquery/federated_queries/bigquery_federated_query_cloud_sql_test.py rename to bigquery/federated_queries/tests/bigquery_federated_query_cloud_sql_test.py index c75e88964a..3043a8897f 100644 --- a/bigquery/federated_queries/bigquery_federated_query_cloud_sql_test.py +++ b/bigquery/federated_queries/tests/bigquery_federated_query_cloud_sql_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_federated_query_cloud_sql from google.api_core import exceptions import pytest +from .. import bigquery_federated_query_cloud_sql + def test_federated_query_cloud_sql_success( monkeypatch: pytest.MonkeyPatch, @@ -48,21 +49,19 @@ def test_federated_query_cloud_sql_success( def test_federated_query_cloud_sql_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.NotFound("resource error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("resource error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_federated_query_cloud_sql.bigquery, "Client", lambda project=None: mock_client, ) - bigquery_federated_query_cloud_sql.federated_query_cloud_sql( - project_id="example-project", - connection_id="example-project.us.example-cloudsql-conn", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_federated_query_cloud_sql.federated_query_cloud_sql( + project_id="example-project", + connection_id="example-project.us.example-cloudsql-conn", + ) diff --git a/bigquery/federated_queries/bigquery_federated_view_schema_cloud_sql_test.py b/bigquery/federated_queries/tests/bigquery_federated_view_schema_cloud_sql_test.py similarity index 83% rename from bigquery/federated_queries/bigquery_federated_view_schema_cloud_sql_test.py rename to bigquery/federated_queries/tests/bigquery_federated_view_schema_cloud_sql_test.py index 7d3394e5a1..049f818068 100644 --- a/bigquery/federated_queries/bigquery_federated_view_schema_cloud_sql_test.py +++ b/bigquery/federated_queries/tests/bigquery_federated_view_schema_cloud_sql_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_federated_view_schema_cloud_sql from google.api_core import exceptions import pytest +from .. import bigquery_federated_view_schema_cloud_sql + def test_federated_view_schema_cloud_sql_success( monkeypatch: pytest.MonkeyPatch, @@ -47,7 +48,6 @@ def test_federated_view_schema_cloud_sql_success( def test_federated_view_schema_cloud_sql_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() mock_job = mock.MagicMock() @@ -59,11 +59,8 @@ def test_federated_view_schema_cloud_sql_error( lambda project=None: mock_client, ) - bigquery_federated_view_schema_cloud_sql.federated_view_schema_cloud_sql( - project_id="example-project", - connection_id="example-project.us.example-cloudsql-conn", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-cloudsql-conn" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_federated_view_schema_cloud_sql.federated_view_schema_cloud_sql( + project_id="example-project", + connection_id="example-project.us.example-cloudsql-conn", + ) diff --git a/bigquery/search/__init__.py b/bigquery/search/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/search/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/search/bigquery_create_search_index.py b/bigquery/search/bigquery_create_search_index.py index ff690d0019..712de869a7 100644 --- a/bigquery/search/bigquery_create_search_index.py +++ b/bigquery/search/bigquery_create_search_index.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_create_search_index] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -42,24 +39,9 @@ def create_search_index( ON `{table_ref}`(ALL COLUMNS); """ - try: - print(f"Creating search index {index_name} on table: {table_ref}") - query_job = client.query(sql) - query_job.result() - except exceptions.NotFound as err: - print( - f"error: table '{table_id}' or dataset '{dataset_id}' not found " - f"for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to create search index '{index_name}' on table " - f"'{table_id}' in dataset '{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Creating search index {index_name} on table: {table_ref}") + query_job = client.query(sql) + query_job.result() print(f"Resource ID: {table_ref}") print(f"Index Name: {index_name}") diff --git a/bigquery/search/bigquery_drop_search_index.py b/bigquery/search/bigquery_drop_search_index.py index dfbca87192..7484435ff3 100644 --- a/bigquery/search/bigquery_drop_search_index.py +++ b/bigquery/search/bigquery_drop_search_index.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_drop_search_index] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -39,24 +36,9 @@ def drop_search_index( table_ref = f"{project_id}.{dataset_id}.{table_id}" sql = f"DROP SEARCH INDEX IF EXISTS `{index_name}` ON `{table_ref}`;" - try: - print(f"Dropping search index {index_name} from table: {table_ref}") - query_job = client.query(sql) - query_job.result() - except exceptions.NotFound as err: - print( - f"error: table '{table_id}' or dataset '{dataset_id}' not found in " - f"project '{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to drop search index '{index_name}' on table '{table_id}' " - f"in dataset '{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Dropping search index {index_name} from table: {table_ref}") + query_job = client.query(sql) + query_job.result() print(f"Resource ID: {table_ref}") print(f"Index Name: {index_name}") diff --git a/bigquery/search/bigquery_get_search_index_status.py b/bigquery/search/bigquery_get_search_index_status.py index 0542ae42a9..fc9204ce4e 100644 --- a/bigquery/search/bigquery_get_search_index_status.py +++ b/bigquery/search/bigquery_get_search_index_status.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_get_search_index_status] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -50,24 +47,9 @@ def get_search_index_status( WHERE table_name = @table_id; """ - try: - print(f"Checking search index status for table: {table_ref}") - query_job = client.query(sql, job_config=job_config) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: dataset '{dataset_id}' or table '{table_id}' not found in " - f"project '{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to get search index status for table '{table_id}' in " - f"dataset '{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Checking search index status for table: {table_ref}") + query_job = client.query(sql, job_config=job_config) + rows = list(query_job.result()) index_name = rows[0].index_name if rows else "NONE" index_status = rows[0].index_status if rows else "NONE" diff --git a/bigquery/search/bigquery_search_exclude_columns.py b/bigquery/search/bigquery_search_exclude_columns.py index 993e1b0b9c..10f4764f6b 100644 --- a/bigquery/search/bigquery_search_exclude_columns.py +++ b/bigquery/search/bigquery_search_exclude_columns.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_search_exclude_columns] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -54,26 +51,9 @@ def search_exclude_columns( LIMIT 10; """ - try: - print( - f"Searching {table_ref} excluding address columns for term: {search_query}" - ) - query_job = client.query(sql, job_config=job_config) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: table '{table_ref}' not found when querying from project " - f"'{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to search '{table_ref}' with excluded columns in project " - f"'{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Searching {table_ref} excluding address columns for term: {search_query}") + query_job = client.query(sql, job_config=job_config) + rows = list(query_job.result()) print(f"Resource ID: {table_ref}") print(f"Job ID: {query_job.job_id}") diff --git a/bigquery/search/bigquery_search_table.py b/bigquery/search/bigquery_search_table.py index f03d384015..af6273c9de 100644 --- a/bigquery/search/bigquery_search_table.py +++ b/bigquery/search/bigquery_search_table.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_search_table] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -51,24 +48,9 @@ def search_table( LIMIT 10; """ - try: - print(f"Searching table {table_ref} for term: {search_query}") - query_job = client.query(sql, job_config=job_config) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: table '{table_ref}' not found when querying from project " - f"'{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to search table '{table_ref}' in project " - f"'{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Searching table {table_ref} for term: {search_query}") + query_job = client.query(sql, job_config=job_config) + rows = list(query_job.result()) print(f"Resource ID: {table_ref}") print(f"Job ID: {query_job.job_id}") diff --git a/bigquery/search/bigquery_search_with_analyzer_options.py b/bigquery/search/bigquery_search_with_analyzer_options.py index 178e9a54f3..76cfd44825 100644 --- a/bigquery/search/bigquery_search_with_analyzer_options.py +++ b/bigquery/search/bigquery_search_with_analyzer_options.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_search_with_analyzer_options] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -56,26 +53,11 @@ def search_with_analyzer_options( LIMIT 10; """ - try: - print( - f"Searching {table_ref} with custom stop-word analyzer_options for: {search_query}" - ) - query_job = client.query(sql, job_config=job_config) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: table '{table_ref}' not found when querying from project " - f"'{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to search '{table_ref}' with analyzer_options in project " - f"'{project_id}': {err.message}", - file=sys.stderr, - ) - return + print( + f"Searching {table_ref} with custom stop-word analyzer_options for: {search_query}" + ) + query_job = client.query(sql, job_config=job_config) + rows = list(query_job.result()) print(f"Resource ID: {table_ref}") print("Analyzer: LOG_ANALYZER") diff --git a/bigquery/search/bigquery_search_with_text_analyzer.py b/bigquery/search/bigquery_search_with_text_analyzer.py index 686522ff7e..043fd7ce32 100644 --- a/bigquery/search/bigquery_search_with_text_analyzer.py +++ b/bigquery/search/bigquery_search_with_text_analyzer.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_search_with_text_analyzer] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -52,26 +49,9 @@ def search_with_text_analyzer( LIMIT 10; """ - try: - print( - f"Searching {table_ref} with analyzer {analyzer_name} for: {search_query}" - ) - query_job = client.query(sql, job_config=job_config) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: table '{table_ref}' not found when querying from project " - f"'{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to search '{table_ref}' with analyzer '{analyzer_name}' " - f"in project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Searching {table_ref} with analyzer {analyzer_name} for: {search_query}") + query_job = client.query(sql, job_config=job_config) + rows = list(query_job.result()) print(f"Resource ID: {table_ref}") print(f"Analyzer: {analyzer_name}") diff --git a/bigquery/search/tests/__init__.py b/bigquery/search/tests/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/search/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/search/bigquery_create_search_index_test.py b/bigquery/search/tests/bigquery_create_search_index_test.py similarity index 83% rename from bigquery/search/bigquery_create_search_index_test.py rename to bigquery/search/tests/bigquery_create_search_index_test.py index 48242a0501..39dce8f6d4 100644 --- a/bigquery/search/bigquery_create_search_index_test.py +++ b/bigquery/search/tests/bigquery_create_search_index_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_create_search_index from google.api_core import exceptions import pytest +from .. import bigquery_create_search_index + def test_create_search_index_success( monkeypatch: pytest.MonkeyPatch, @@ -49,7 +50,6 @@ def test_create_search_index_success( def test_create_search_index_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() mock_job = mock.MagicMock() @@ -61,12 +61,9 @@ def test_create_search_index_error( lambda project=None: mock_client, ) - bigquery_create_search_index.create_search_index( - project_id="example-project", - dataset_id="example_dataset", - table_id="example_table", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_create_search_index.create_search_index( + project_id="example-project", + dataset_id="example_dataset", + table_id="example_table", + ) diff --git a/bigquery/search/bigquery_drop_search_index_test.py b/bigquery/search/tests/bigquery_drop_search_index_test.py similarity index 79% rename from bigquery/search/bigquery_drop_search_index_test.py rename to bigquery/search/tests/bigquery_drop_search_index_test.py index 11a3a420c1..b43480e59d 100644 --- a/bigquery/search/bigquery_drop_search_index_test.py +++ b/bigquery/search/tests/bigquery_drop_search_index_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_drop_search_index from google.api_core import exceptions import pytest +from .. import bigquery_drop_search_index + def test_drop_search_index_success( monkeypatch: pytest.MonkeyPatch, @@ -49,22 +50,20 @@ def test_drop_search_index_success( def test_drop_search_index_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.NotFound("resource error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("resource error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_drop_search_index.bigquery, "Client", lambda project=None: mock_client, ) - bigquery_drop_search_index.drop_search_index( - project_id="example-project", - dataset_id="example_dataset", - table_id="example_table", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_drop_search_index.drop_search_index( + project_id="example-project", + dataset_id="example_dataset", + table_id="example_table", + ) diff --git a/bigquery/search/bigquery_get_search_index_status_test.py b/bigquery/search/tests/bigquery_get_search_index_status_test.py similarity index 70% rename from bigquery/search/bigquery_get_search_index_status_test.py rename to bigquery/search/tests/bigquery_get_search_index_status_test.py index 18ab817187..81abed8ffb 100644 --- a/bigquery/search/bigquery_get_search_index_status_test.py +++ b/bigquery/search/tests/bigquery_get_search_index_status_test.py @@ -14,15 +14,18 @@ from unittest import mock -import bigquery_get_search_index_status from google.api_core import exceptions import pytest +from .. import bigquery_get_search_index_status + def test_get_search_index_status_success( capsys: pytest.CaptureFixture[str], ) -> None: - with mock.patch("google.cloud.bigquery.Client") as mock_client_cls: + with mock.patch.object( + bigquery_get_search_index_status.bigquery, "Client" + ) as mock_client_cls: mock_client = mock_client_cls.return_value mock_job = mock.MagicMock() mock_job.job_id = "example-job-123" @@ -50,19 +53,18 @@ def test_get_search_index_status_success( assert "Coverage Percentage: 100%" in captured.out -def test_get_search_index_status_not_found( - capsys: pytest.CaptureFixture[str], -) -> None: - with mock.patch("google.cloud.bigquery.Client") as mock_client_cls: +def test_get_search_index_status_not_found() -> None: + with mock.patch.object( + bigquery_get_search_index_status.bigquery, "Client" + ) as mock_client_cls: mock_client = mock_client_cls.return_value - mock_client.query.side_effect = exceptions.NotFound("Dataset not found") - - bigquery_get_search_index_status.get_search_index_status( - project_id="example-project", - dataset_id="example_dataset", - table_id="example_table", - ) + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("Dataset not found") + mock_client.query.return_value = mock_job - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example_table" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_get_search_index_status.get_search_index_status( + project_id="example-project", + dataset_id="example_dataset", + table_id="example_table", + ) diff --git a/bigquery/search/bigquery_search_exclude_columns_test.py b/bigquery/search/tests/bigquery_search_exclude_columns_test.py similarity index 86% rename from bigquery/search/bigquery_search_exclude_columns_test.py rename to bigquery/search/tests/bigquery_search_exclude_columns_test.py index 43b8b417a7..64d67083f9 100644 --- a/bigquery/search/bigquery_search_exclude_columns_test.py +++ b/bigquery/search/tests/bigquery_search_exclude_columns_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_search_exclude_columns from google.api_core import exceptions import pytest +from .. import bigquery_search_exclude_columns + def test_search_exclude_columns_success( monkeypatch: pytest.MonkeyPatch, @@ -46,7 +47,6 @@ def test_search_exclude_columns_success( def test_search_exclude_columns_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() mock_job = mock.MagicMock() @@ -58,10 +58,7 @@ def test_search_exclude_columns_error( lambda project=None: mock_client, ) - bigquery_search_exclude_columns.search_exclude_columns( - project_id="example-project", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_search_exclude_columns.search_exclude_columns( + project_id="example-project", + ) diff --git a/bigquery/search/bigquery_search_table_test.py b/bigquery/search/tests/bigquery_search_table_test.py similarity index 83% rename from bigquery/search/bigquery_search_table_test.py rename to bigquery/search/tests/bigquery_search_table_test.py index d1ff83fbc0..352b786a8b 100644 --- a/bigquery/search/bigquery_search_table_test.py +++ b/bigquery/search/tests/bigquery_search_table_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_search_table from google.api_core import exceptions import pytest +from .. import bigquery_search_table + def test_search_table_success( monkeypatch: pytest.MonkeyPatch, @@ -49,20 +50,18 @@ def test_search_table_success( def test_search_table_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.NotFound("resource error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("resource error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_search_table.bigquery, "Client", lambda project=None: mock_client, ) - bigquery_search_table.search_table( - project_id="example-project", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_search_table.search_table( + project_id="example-project", + ) diff --git a/bigquery/search/bigquery_search_with_analyzer_options_test.py b/bigquery/search/tests/bigquery_search_with_analyzer_options_test.py similarity index 82% rename from bigquery/search/bigquery_search_with_analyzer_options_test.py rename to bigquery/search/tests/bigquery_search_with_analyzer_options_test.py index 7d69d79490..1c9d7366ce 100644 --- a/bigquery/search/bigquery_search_with_analyzer_options_test.py +++ b/bigquery/search/tests/bigquery_search_with_analyzer_options_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_search_with_analyzer_options from google.api_core import exceptions import pytest +from .. import bigquery_search_with_analyzer_options + def test_search_with_analyzer_options_success( monkeypatch: pytest.MonkeyPatch, @@ -47,20 +48,18 @@ def test_search_with_analyzer_options_success( def test_search_with_analyzer_options_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.NotFound("resource error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("resource error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_search_with_analyzer_options.bigquery, "Client", lambda project=None: mock_client, ) - bigquery_search_with_analyzer_options.search_with_analyzer_options( - project_id="example-project", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_search_with_analyzer_options.search_with_analyzer_options( + project_id="example-project", + ) diff --git a/bigquery/search/bigquery_search_with_text_analyzer_test.py b/bigquery/search/tests/bigquery_search_with_text_analyzer_test.py similarity index 85% rename from bigquery/search/bigquery_search_with_text_analyzer_test.py rename to bigquery/search/tests/bigquery_search_with_text_analyzer_test.py index a58ee3815c..d0a225774d 100644 --- a/bigquery/search/bigquery_search_with_text_analyzer_test.py +++ b/bigquery/search/tests/bigquery_search_with_text_analyzer_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_search_with_text_analyzer from google.api_core import exceptions import pytest +from .. import bigquery_search_with_text_analyzer + def test_search_with_text_analyzer_success( monkeypatch: pytest.MonkeyPatch, @@ -46,7 +47,6 @@ def test_search_with_text_analyzer_success( def test_search_with_text_analyzer_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() mock_job = mock.MagicMock() @@ -58,10 +58,7 @@ def test_search_with_text_analyzer_error( lambda project=None: mock_client, ) - bigquery_search_with_text_analyzer.search_with_text_analyzer( - project_id="example-project", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_search_with_text_analyzer.search_with_text_analyzer( + project_id="example-project", + ) diff --git a/bigquery/stored_procedures/__init__.py b/bigquery/stored_procedures/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/stored_procedures/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/stored_procedures/bigquery_call_procedure.py b/bigquery/stored_procedures/bigquery_call_procedure.py index 5dd3c443da..9048444992 100644 --- a/bigquery/stored_procedures/bigquery_call_procedure.py +++ b/bigquery/stored_procedures/bigquery_call_procedure.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_call_procedure] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -48,24 +45,9 @@ def call_procedure( SELECT out_total AS total_words; """ - try: - print(f"Calling stored procedure: {routine_ref}") - query_job = client.query(sql, job_config=job_config) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: stored procedure '{procedure_id}' not found in dataset " - f"'{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to call stored procedure '{procedure_id}' in dataset " - f"'{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Calling stored procedure: {routine_ref}") + query_job = client.query(sql, job_config=job_config) + rows = list(query_job.result()) total_words = rows[0].total_words if rows else 0 print(f"Resource ID: {routine_ref}") diff --git a/bigquery/stored_procedures/bigquery_call_system_procedure.py b/bigquery/stored_procedures/bigquery_call_system_procedure.py index 40fdd48db0..02d7f74d0f 100644 --- a/bigquery/stored_procedures/bigquery_call_system_procedure.py +++ b/bigquery/stored_procedures/bigquery_call_system_procedure.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_call_system_procedure] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -43,24 +40,9 @@ def call_system_procedure( ) sql = "CALL BQ.REFRESH_MATERIALIZED_VIEW(@view_name);" - try: - print(f"Refreshing materialized view via system procedure: {mv_ref}") - query_job = client.query(sql, job_config=job_config) - query_job.result() - except exceptions.NotFound as err: - print( - f"error: materialized view '{materialized_view_id}' not found in dataset " - f"'{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to refresh materialized view '{materialized_view_id}' in dataset " - f"'{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Refreshing materialized view via system procedure: {mv_ref}") + query_job = client.query(sql, job_config=job_config) + query_job.result() print(f"Resource ID: {mv_ref}") print(f"Job ID: {query_job.job_id}") diff --git a/bigquery/stored_procedures/bigquery_drop_procedure.py b/bigquery/stored_procedures/bigquery_drop_procedure.py index d654e8d533..fb2c378fa2 100644 --- a/bigquery/stored_procedures/bigquery_drop_procedure.py +++ b/bigquery/stored_procedures/bigquery_drop_procedure.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_drop_procedure] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -38,23 +35,9 @@ def drop_procedure( # Use IF EXISTS so dropping an already-removed routine succeeds idempotently. sql = f"DROP PROCEDURE IF EXISTS `{routine_ref}`;" - try: - print(f"Dropping stored procedure: {routine_ref}") - query_job = client.query(sql) - query_job.result() - except exceptions.NotFound as err: - print( - f"error: dataset '{dataset_id}' or project '{project_id}' not found: {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to drop stored procedure '{procedure_id}' in dataset " - f"'{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Dropping stored procedure: {routine_ref}") + query_job = client.query(sql) + query_job.result() print(f"Resource ID: {routine_ref}") print(f"Job ID: {query_job.job_id}") diff --git a/bigquery/stored_procedures/tests/__init__.py b/bigquery/stored_procedures/tests/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/stored_procedures/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/stored_procedures/bigquery_call_procedure_test.py b/bigquery/stored_procedures/tests/bigquery_call_procedure_test.py similarity index 80% rename from bigquery/stored_procedures/bigquery_call_procedure_test.py rename to bigquery/stored_procedures/tests/bigquery_call_procedure_test.py index ec960ccc1c..245dcf9720 100644 --- a/bigquery/stored_procedures/bigquery_call_procedure_test.py +++ b/bigquery/stored_procedures/tests/bigquery_call_procedure_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_call_procedure from google.api_core import exceptions import pytest +from .. import bigquery_call_procedure + def test_call_procedure_success( monkeypatch: pytest.MonkeyPatch, @@ -50,22 +51,20 @@ def test_call_procedure_success( def test_call_procedure_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.NotFound("resource error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("resource error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_call_procedure.bigquery, "Client", lambda project=None: mock_client, ) - bigquery_call_procedure.call_procedure( - project_id="example-project", - dataset_id="example_dataset", - procedure_id="example_procedure", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_call_procedure.call_procedure( + project_id="example-project", + dataset_id="example_dataset", + procedure_id="example_procedure", + ) diff --git a/bigquery/stored_procedures/bigquery_call_system_procedure_test.py b/bigquery/stored_procedures/tests/bigquery_call_system_procedure_test.py similarity index 74% rename from bigquery/stored_procedures/bigquery_call_system_procedure_test.py rename to bigquery/stored_procedures/tests/bigquery_call_system_procedure_test.py index d42040e03e..d157751474 100644 --- a/bigquery/stored_procedures/bigquery_call_system_procedure_test.py +++ b/bigquery/stored_procedures/tests/bigquery_call_system_procedure_test.py @@ -14,15 +14,18 @@ from unittest import mock -import bigquery_call_system_procedure from google.api_core import exceptions import pytest +from .. import bigquery_call_system_procedure + def test_call_system_procedure_success( capsys: pytest.CaptureFixture[str], ) -> None: - with mock.patch("google.cloud.bigquery.Client") as mock_client_cls: + with mock.patch.object( + bigquery_call_system_procedure.bigquery, "Client" + ) as mock_client_cls: mock_client = mock_client_cls.return_value mock_job = mock.MagicMock() mock_job.job_id = "example-job-123" @@ -43,21 +46,18 @@ def test_call_system_procedure_success( assert "System Procedure: BQ.REFRESH_MATERIALIZED_VIEW" in captured.out -def test_call_system_procedure_not_found( - capsys: pytest.CaptureFixture[str], -) -> None: - with mock.patch("google.cloud.bigquery.Client") as mock_client_cls: +def test_call_system_procedure_not_found() -> None: + with mock.patch.object( + bigquery_call_system_procedure.bigquery, "Client" + ) as mock_client_cls: mock_client = mock_client_cls.return_value mock_job = mock.MagicMock() mock_job.result.side_effect = exceptions.NotFound("View not found") mock_client.query.return_value = mock_job - bigquery_call_system_procedure.call_system_procedure( - project_id="example-project", - dataset_id="example_dataset", - materialized_view_id="example_mv", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example_mv" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_call_system_procedure.call_system_procedure( + project_id="example-project", + dataset_id="example_dataset", + materialized_view_id="example_mv", + ) diff --git a/bigquery/stored_procedures/bigquery_drop_procedure_test.py b/bigquery/stored_procedures/tests/bigquery_drop_procedure_test.py similarity index 84% rename from bigquery/stored_procedures/bigquery_drop_procedure_test.py rename to bigquery/stored_procedures/tests/bigquery_drop_procedure_test.py index d8ca42195c..a717c00931 100644 --- a/bigquery/stored_procedures/bigquery_drop_procedure_test.py +++ b/bigquery/stored_procedures/tests/bigquery_drop_procedure_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_drop_procedure from google.api_core import exceptions import pytest +from .. import bigquery_drop_procedure + def test_drop_procedure_success( monkeypatch: pytest.MonkeyPatch, @@ -49,7 +50,6 @@ def test_drop_procedure_success( def test_drop_procedure_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() mock_job = mock.MagicMock() @@ -61,12 +61,9 @@ def test_drop_procedure_error( lambda project=None: mock_client, ) - bigquery_drop_procedure.drop_procedure( - project_id="example-project", - dataset_id="example_dataset", - procedure_id="example_procedure", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_drop_procedure.drop_procedure( + project_id="example-project", + dataset_id="example_dataset", + procedure_id="example_procedure", + ) diff --git a/bigquery/transactions/__init__.py b/bigquery/transactions/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/transactions/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/transactions/bigquery_multi_statement_transaction.py b/bigquery/transactions/bigquery_multi_statement_transaction.py index 30fa62f403..7e38cda388 100644 --- a/bigquery/transactions/bigquery_multi_statement_transaction.py +++ b/bigquery/transactions/bigquery_multi_statement_transaction.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_multi_statement_transaction] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -41,7 +38,7 @@ def run_multi_statement_transaction( target_ref = f"{project_id}.{dataset_id}.{target_table_id}" # BigQuery scopes a multi-statement transaction to a single script query job, spawning child # jobs for each statement and rolling back uncommitted changes on error. - sql = f""" + transaction_sql = f""" BEGIN BEGIN TRANSACTION; MERGE `{target_ref}` AS inv @@ -57,35 +54,25 @@ def run_multi_statement_transaction( ROLLBACK TRANSACTION; RAISE USING MESSAGE = @@error.message; END; + """ + select_sql = f""" SELECT COUNT(*) AS inventory_rows FROM `{target_ref}`; """ - try: - print(f"Running multi-statement transaction into: {target_ref}") - query_job = client.query(sql) - rows = list(query_job.result()) - except exceptions.NotFound as err: - print( - f"error: tables '{source_table_id}' or '{target_table_id}' not found in dataset " - f"'{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: multi-statement transaction failed on '{source_table_id}' and " - f"'{target_table_id}' in dataset '{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Running multi-statement transaction into: {target_ref}") + transaction_job = client.query(transaction_sql) + transaction_job.result() + + count_job = client.query(select_sql) + rows = list(count_job.result()) inventory_rows = rows[0].inventory_rows if rows else 0 print(f"Resource ID: {target_ref}") - print(f"Child Jobs: {query_job.num_child_jobs}") + print(f"Child Jobs: {transaction_job.num_child_jobs}") print(f"Inventory Rows: {inventory_rows}") - print(f"Job ID: {query_job.job_id}") + print(f"Job ID: {transaction_job.job_id}") print(f"Source Table: {source_ref}") print("Transaction State: COMMITTED") diff --git a/bigquery/transactions/tests/__init__.py b/bigquery/transactions/tests/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/transactions/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/transactions/bigquery_multi_statement_transaction_test.py b/bigquery/transactions/tests/bigquery_multi_statement_transaction_test.py similarity index 58% rename from bigquery/transactions/bigquery_multi_statement_transaction_test.py rename to bigquery/transactions/tests/bigquery_multi_statement_transaction_test.py index 7593c0a910..063dca5238 100644 --- a/bigquery/transactions/bigquery_multi_statement_transaction_test.py +++ b/bigquery/transactions/tests/bigquery_multi_statement_transaction_test.py @@ -14,23 +14,30 @@ from unittest import mock -import bigquery_multi_statement_transaction from google.api_core import exceptions import pytest +from .. import bigquery_multi_statement_transaction + def test_run_multi_statement_transaction_success( capsys: pytest.CaptureFixture[str], ) -> None: - with mock.patch("google.cloud.bigquery.Client") as mock_client_cls: + with mock.patch.object( + bigquery_multi_statement_transaction.bigquery, "Client" + ) as mock_client_cls: mock_client = mock_client_cls.return_value - mock_job = mock.MagicMock() - mock_job.job_id = "example-job-123" - mock_job.num_child_jobs = 3 + mock_tx_job = mock.MagicMock() + mock_tx_job.job_id = "example-job-123" + mock_tx_job.num_child_jobs = 3 + mock_tx_job.result.return_value = [] + + mock_count_job = mock.MagicMock() mock_row = mock.MagicMock() mock_row.inventory_rows = 3 - mock_job.result.return_value = [mock_row] - mock_client.query.return_value = mock_job + mock_count_job.result.return_value = [mock_row] + + mock_client.query.side_effect = [mock_tx_job, mock_count_job] bigquery_multi_statement_transaction.run_multi_statement_transaction( project_id="example-project", @@ -39,7 +46,7 @@ def test_run_multi_statement_transaction_success( target_table_id="example_inventory", ) - mock_client.query.assert_called_once() + assert mock_client.query.call_count == 2 captured = capsys.readouterr() assert ( "Resource ID: example-project.example_dataset.example_inventory" @@ -50,20 +57,19 @@ def test_run_multi_statement_transaction_success( assert "Transaction State: COMMITTED" in captured.out -def test_run_multi_statement_transaction_not_found( - capsys: pytest.CaptureFixture[str], -) -> None: - with mock.patch("google.cloud.bigquery.Client") as mock_client_cls: +def test_run_multi_statement_transaction_not_found() -> None: + with mock.patch.object( + bigquery_multi_statement_transaction.bigquery, "Client" + ) as mock_client_cls: mock_client = mock_client_cls.return_value - mock_client.query.side_effect = exceptions.NotFound("Table not found") - - bigquery_multi_statement_transaction.run_multi_statement_transaction( - project_id="example-project", - dataset_id="example_dataset", - source_table_id="example_new_arrivals", - target_table_id="example_inventory", - ) + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("Table not found") + mock_client.query.return_value = mock_job - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example_inventory" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_multi_statement_transaction.run_multi_statement_transaction( + project_id="example-project", + dataset_id="example_dataset", + source_table_id="example_new_arrivals", + target_table_id="example_inventory", + ) diff --git a/bigquery/vector_search/__init__.py b/bigquery/vector_search/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/vector_search/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/vector_search/bigquery_create_vector_index_tree_ah.py b/bigquery/vector_search/bigquery_create_vector_index_tree_ah.py index cf392a36d5..9a7ddcd980 100644 --- a/bigquery/vector_search/bigquery_create_vector_index_tree_ah.py +++ b/bigquery/vector_search/bigquery_create_vector_index_tree_ah.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_create_vector_index_tree_ah] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -47,24 +44,9 @@ def create_vector_index_tree_ah( ); """ - try: - print(f"Creating TreeAH vector index {index_name} on table: {table_ref}") - query_job = client.query(sql) - query_job.result() - except exceptions.NotFound as err: - print( - f"error: table '{table_id}' or dataset '{dataset_id}' not found in " - f"project '{project_id}' when creating index '{index_name}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to create TreeAH vector index '{index_name}' on table " - f"'{table_id}' in dataset '{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Creating TreeAH vector index {index_name} on table: {table_ref}") + query_job = client.query(sql) + query_job.result() print(f"Resource ID: {table_ref}") print(f"Index Name: {index_name}") diff --git a/bigquery/vector_search/bigquery_drop_vector_index.py b/bigquery/vector_search/bigquery_drop_vector_index.py index 974d712e97..8154c527ba 100644 --- a/bigquery/vector_search/bigquery_drop_vector_index.py +++ b/bigquery/vector_search/bigquery_drop_vector_index.py @@ -13,9 +13,6 @@ # limitations under the License. # [START bigquery_drop_vector_index] -import sys - -from google.api_core import exceptions from google.cloud import bigquery @@ -39,24 +36,9 @@ def drop_vector_index( table_ref = f"{project_id}.{dataset_id}.{table_id}" sql = f"DROP VECTOR INDEX IF EXISTS `{index_name}` ON `{table_ref}`;" - try: - print(f"Dropping vector index {index_name} from table: {table_ref}") - query_job = client.query(sql) - query_job.result() - except exceptions.NotFound as err: - print( - f"error: table '{table_id}' or dataset '{dataset_id}' not found in " - f"project '{project_id}': {err.message}", - file=sys.stderr, - ) - return - except exceptions.GoogleAPICallError as err: - print( - f"error: failed to drop vector index '{index_name}' on table '{table_id}' " - f"in dataset '{dataset_id}' for project '{project_id}': {err.message}", - file=sys.stderr, - ) - return + print(f"Dropping vector index {index_name} from table: {table_ref}") + query_job = client.query(sql) + query_job.result() print(f"Resource ID: {table_ref}") print(f"Index Name: {index_name}") diff --git a/bigquery/vector_search/tests/__init__.py b/bigquery/vector_search/tests/__init__.py new file mode 100644 index 0000000000..75edc9bba7 --- /dev/null +++ b/bigquery/vector_search/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/bigquery/vector_search/bigquery_create_vector_index_tree_ah_test.py b/bigquery/vector_search/tests/bigquery_create_vector_index_tree_ah_test.py similarity index 80% rename from bigquery/vector_search/bigquery_create_vector_index_tree_ah_test.py rename to bigquery/vector_search/tests/bigquery_create_vector_index_tree_ah_test.py index cb0ed08be2..d15ac4f4d1 100644 --- a/bigquery/vector_search/bigquery_create_vector_index_tree_ah_test.py +++ b/bigquery/vector_search/tests/bigquery_create_vector_index_tree_ah_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_create_vector_index_tree_ah from google.api_core import exceptions import pytest +from .. import bigquery_create_vector_index_tree_ah + def test_create_vector_index_tree_ah_success( monkeypatch: pytest.MonkeyPatch, @@ -52,22 +53,20 @@ def test_create_vector_index_tree_ah_success( def test_create_vector_index_tree_ah_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() - mock_client.query.side_effect = exceptions.NotFound("ddl error") + mock_job = mock.MagicMock() + mock_job.result.side_effect = exceptions.NotFound("ddl error") + mock_client.query.return_value = mock_job monkeypatch.setattr( bigquery_create_vector_index_tree_ah.bigquery, "Client", lambda project=None: mock_client, ) - bigquery_create_vector_index_tree_ah.create_vector_index_tree_ah( - project_id="example-project", - dataset_id="example_dataset", - table_id="example_embeddings", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example_tree_ah_index" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_create_vector_index_tree_ah.create_vector_index_tree_ah( + project_id="example-project", + dataset_id="example_dataset", + table_id="example_embeddings", + ) diff --git a/bigquery/vector_search/bigquery_drop_vector_index_test.py b/bigquery/vector_search/tests/bigquery_drop_vector_index_test.py similarity index 83% rename from bigquery/vector_search/bigquery_drop_vector_index_test.py rename to bigquery/vector_search/tests/bigquery_drop_vector_index_test.py index f441584f66..cc9066e976 100644 --- a/bigquery/vector_search/bigquery_drop_vector_index_test.py +++ b/bigquery/vector_search/tests/bigquery_drop_vector_index_test.py @@ -14,10 +14,11 @@ from unittest import mock -import bigquery_drop_vector_index from google.api_core import exceptions import pytest +from .. import bigquery_drop_vector_index + def test_drop_vector_index_success( monkeypatch: pytest.MonkeyPatch, @@ -49,7 +50,6 @@ def test_drop_vector_index_success( def test_drop_vector_index_error( monkeypatch: pytest.MonkeyPatch, - capsys: pytest.CaptureFixture[str], ) -> None: mock_client = mock.MagicMock() mock_job = mock.MagicMock() @@ -61,12 +61,9 @@ def test_drop_vector_index_error( lambda project=None: mock_client, ) - bigquery_drop_vector_index.drop_vector_index( - project_id="example-project", - dataset_id="example_dataset", - table_id="example_embeddings", - ) - - captured = capsys.readouterr() - assert "error:" in captured.err - assert "example-project" in captured.err + with pytest.raises(exceptions.GoogleAPICallError): + bigquery_drop_vector_index.drop_vector_index( + project_id="example-project", + dataset_id="example_dataset", + table_id="example_embeddings", + )