Skip to content

refactor(bigquery): move tests into tests/ and apply review feedback to merged samples - #14754

Merged
iennae merged 2 commits into
mainfrom
fix/bq-merged-samples-cleanup
Nov 17, 2026
Merged

iennae merged 2 commits into
mainfrom
fix/bq-merged-samples-cleanup

Conversation

@iennae

@iennae iennae commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Applies reviewer feedback from the BigQuery Data Cloud sample PRs to the 21 samples that have already merged, and moves their unit tests out of the sample folders.

Layout: tests in tests/

In bigquery/{stored_procedures,transactions,search,vector_search,biglake,federated_queries,data_profile_scan}:

  • Each *_test.py moves to tests/<sample>_test.py.
  • Adds __init__.py to each folder and to its tests/; tests import the sample with from .. import <sample>.
  • This matches cloud_scheduler/snippets/. noxfile-template.py already collects a tests directory.

Review feedback

  • Setup and SELECT run as separate queries: bigquery_multi_statement_transaction and bigquery_query_iceberg_table now run the script/DDL and the follow-up SELECT as two client.query() calls.
  • No generic try/except: removed the stderr-printing try/except blocks (and the unused sys / google.api_core.exceptions imports), so GoogleAPICallError propagates to the caller.
  • Tests: patch the client with monkeypatch.setattr(<sample>.bigquery, "Client", ...) / mock.patch.object, and assert pytest.raises(exceptions.GoogleAPICallError) on the error path.
  • Keeps the earlier fixes on this branch (IF NOT EXISTS in bigquery_create_search_index, removing unused mock attributes).

Testing

Ran in each of the 7 folders with the noxfile-template lint arguments (google import order, flake8-annotations), plus black and pytest: lint passes, 42 tests pass.

The 15 open single-sample PRs (#14737, #14739, #14741–#14753) will be rebased onto this layout once it merges.

@iennae
iennae requested review from a team and chalmerlowe as code owners November 3, 2026 03:32
@product-auto-label product-auto-label Bot added api: bigquery Issues related to the BigQuery API. samples Issues that are directly related to samples. labels Nov 3, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates BigQuery search and vector index scripts and their corresponding tests. Specifically, it modifies the search index creation to use 'CREATE SEARCH INDEX IF NOT EXISTS' and updates the exception handling to catch 'exceptions.NotFound' instead of 'exceptions.AlreadyExists'. Additionally, it refactors error messages for missing tables or datasets in the vector index drop script and updates unit tests to use empty list results for mock query jobs. There are no review comments, so I have no feedback to provide.

@iennae
iennae force-pushed the fix/bq-merged-samples-cleanup branch from 2ac3d30 to 260d2cb Compare November 3, 2026 23:32

@amcolin amcolin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

… 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.
…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 <sample>` (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(<sample>.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.
@iennae
iennae force-pushed the fix/bq-merged-samples-cleanup branch from 260d2cb to 0930c0b Compare November 17, 2026 04:43
@iennae iennae changed the title fix(bigquery): restore IF NOT EXISTS in create_search_index and clean up test mocks refactor(bigquery): move tests into tests/ and apply review feedback to merged samples Nov 17, 2026
@iennae
iennae merged commit 746fac4 into main Nov 17, 2026
14 of 19 checks passed
@iennae
iennae deleted the fix/bq-merged-samples-cleanup branch November 17, 2026 04:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigquery Issues related to the BigQuery API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants