Repository navigation
Commit ee41a8c
feat(bigquery): add bigquery_upgrade_manifest_biglake_table sample to… (#14748)
* feat(bigquery): add bigquery_upgrade_manifest_biglake_table sample to upgrade a manifest external table to BigLake with automatic metadata caching
Problem:
The BigQuery documentation (https://cloud.google.com/bigquery/docs/query-open-table-format-using-manifest-files)
needs a standalone, tested Python code sample demonstrating how to
upgrade a manifest external table to BigLake with automatic metadata caching (region tag `bigquery_upgrade_manifest_biglake_table`).
Solution:
- Add `upgrade_manifest_biglake_table(project_id: str, dataset_id: str, table_id: str, connection_id: str, manifest_uri: str) -> None` in `bigquery/biglake/bigquery_upgrade_manifest_biglake_table.py`
(Upgrade a manifest-backed external table to a BigLake table with AUTOMATIC metadata caching and connection binding.)
- Add unit tests (`bigquery/biglake/bigquery_upgrade_manifest_biglake_table_test.py`) covering both the success
path and API error handling (`GoogleAPICallError`)
* fix(bigquery): report applied metadata cache settings in upgrade_manifest_biglake_table
Problem:
Review feedback on this sample pointed out two issues:
- It printed "Metadata Cache Mode: AUTOMATIC" as a hardcoded string, so
neither the sample nor its test verified that BigQuery applied the
setting.
- The verification query selected id, label and score, which only exist in
our test data, so the sample failed against any other manifest table.
Solution:
- After the CREATE statement, read the table back with client.get_table()
and print the metadataCacheMode from its external data configuration and
the table's max_staleness. ExternalConfig has no metadata_cache_mode
property in google-cloud-bigquery 3.47.0, so the value is read from the
public to_api_repr() output.
- Use SELECT * ... LIMIT 10 so the query works with any schema.
- The test mocks get_table(), asserts it is called with the table ID, and
checks the reported cache mode and max staleness.
* fix: Apply suggestion from @chalmerlowe
Co-authored-by: Chalmer Lowe <chalmer.lowe@gmail.com>
* test(bigquery): use autospec and real SDK objects in upgrade_manifest_biglake_table test
Problem:
Review feedback showed the test mostly verified its own mocks. MagicMock
accepts any attribute, so a typo in the sample or a change in the SDK still
passed. The get_table() result was a MagicMock whose to_api_repr() returned a
dictionary the test wrote itself, so the assertion on the cache mode checked
that dictionary rather than the sample's lookups on bigquery.Table. The test
also did not check that the DDL ran before the SELECT, and the not-found test
only exercised the mock now that the sample no longer handles errors.
Solution:
- Patch Client with autospec=True and spec_set=True, and create the query
jobs with mock.create_autospec(bigquery.QueryJob, instance=True,
spec_set=True), so only real attributes and methods are accepted.
- Return a real bigquery.Table built with Table.from_api_repr() (no network
call) from get_table(), so the sample's external_data_configuration and
max_staleness lookups run against the SDK class.
- Assert the client was created with the project, that the CREATE OR
REPLACE EXTERNAL TABLE statement ran first and the SELECT second, and that
get_table() was called with the table ID.
- Remove the not-found test, since the sample has no error handling of its
own to test.
A deliberate typo in the sample (table.max_stalenes) now fails the test with
AttributeError; the previous MagicMock-based test passed.
---------
Co-authored-by: Chalmer Lowe <chalmer.lowe@gmail.com>1 parent 50901ac commit ee41a8c
2 files changed
Lines changed: 165 additions & 0 deletions
File tree
- bigquery/biglake
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
0 commit comments