From 84627572dbc48326a7d59c33d6ffe3a2ca8e6dca Mon Sep 17 00:00:00 2001 From: Mario Torres Jr <105736410+Mattix23@users.noreply.github.com> Date: Fri, 6 Jan 2023 12:42:38 -0600 Subject: [PATCH] docs: revise label table code samples (#1451) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs: revise label table code samples * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * added TODO to clean up snippets.py Co-authored-by: Owl Bot Co-authored-by: Tim Swast --- docs/snippets.py | 3 +++ samples/snippets/label_table.py | 37 ++++++++++++++++++++++++++++ samples/snippets/label_table_test.py | 32 ++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 samples/snippets/label_table.py create mode 100644 samples/snippets/label_table_test.py diff --git a/docs/snippets.py b/docs/snippets.py index b9860e4da..a0c01870a 100644 --- a/docs/snippets.py +++ b/docs/snippets.py @@ -250,6 +250,9 @@ def test_manage_table_labels(client, to_delete): table = bigquery.Table(dataset.table(table_id), schema=SCHEMA) table = client.create_table(table) + # TODO(Mattix23): After code sample from https://github.com/googleapis/python-bigquery/pull/1451 + # is updated from cloud.google.com delete this. + # [START bigquery_label_table] # from google.cloud import bigquery # client = bigquery.Client() diff --git a/samples/snippets/label_table.py b/samples/snippets/label_table.py new file mode 100644 index 000000000..5fce08d62 --- /dev/null +++ b/samples/snippets/label_table.py @@ -0,0 +1,37 @@ +# Copyright 2022 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. + + +def label_table(table_id: str) -> None: + orig_table_id = table_id + # [START bigquery_label_table] + from google.cloud import bigquery + + client = bigquery.Client() + + # TODO(dev): Change table_id to the full name of the table you want to create. + table_id = "your-project.your_dataset.your_table_name" + + # [END bigquery_label_table] + table_id = orig_table_id + # [START bigquery_label_table] + table = client.get_table(table_id) # API request + + labels = {"color": "green"} + table.labels = labels + + table = client.update_table(table, ["labels"]) # API request + + print(f"Added {table.labels} to {table_id}.") + # [END bigquery_label_table] diff --git a/samples/snippets/label_table_test.py b/samples/snippets/label_table_test.py new file mode 100644 index 000000000..a77fb4b75 --- /dev/null +++ b/samples/snippets/label_table_test.py @@ -0,0 +1,32 @@ +# Copyright 2022 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. + +import typing + +import label_table + +if typing.TYPE_CHECKING: + import pytest + + +def test_label_table( + capsys: "pytest.CaptureFixture[str]", + table_id: str, +) -> None: + + label_table.label_table(table_id) + + out, _ = capsys.readouterr() + assert "color" in out + assert table_id in out