From 03194e0156ed9201cb36301967c5af117d7ef29c Mon Sep 17 00:00:00 2001 From: Jared Chapman Date: Fri, 15 Sep 2023 13:17:01 -0500 Subject: [PATCH] docs: Revise update_table_expiration sample (#1457) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs: Revise update_table_expiration sample * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot Co-authored-by: Tim Swast Co-authored-by: aribray <45905583+aribray@users.noreply.github.com> Co-authored-by: Anthonios Partheniou --- docs/snippets.py | 2 + samples/snippets/update_table_expiration.py | 45 +++++++++++++++++++ .../snippets/update_table_expiration_test.py | 44 ++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 samples/snippets/update_table_expiration.py create mode 100644 samples/snippets/update_table_expiration_test.py diff --git a/docs/snippets.py b/docs/snippets.py index e1d9ae839..d458b832c 100644 --- a/docs/snippets.py +++ b/docs/snippets.py @@ -220,6 +220,8 @@ def test_update_table_expiration(client, to_delete): table = bigquery.Table(dataset.table(table_id), schema=SCHEMA) table = client.create_table(table) + # TODO(thejaredchapman): After code sample has been updated from cloud.google.com delete this. + # [START bigquery_update_table_expiration] import datetime diff --git a/samples/snippets/update_table_expiration.py b/samples/snippets/update_table_expiration.py new file mode 100644 index 000000000..bf944800f --- /dev/null +++ b/samples/snippets/update_table_expiration.py @@ -0,0 +1,45 @@ +# 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 datetime + + +def update_table_expiration(table_id, expiration): + orig_table_id = table_id + orig_expiration = expiration + + # [START bigquery_update_table_expiration] + from google.cloud import bigquery + + client = bigquery.Client() + + # TODO(dev): Change table_id to the full name of the table you want to update. + table_id = "your-project.your_dataset.your_table_name" + + # TODO(dev): Set table to expire for desired days days from now. + expiration = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta( + days=5 + ) + # [END bigquery_update_table_expiration] + + table_id = orig_table_id + expiration = orig_expiration + + # [START bigquery_update_table_expiration] + table = client.get_table(table_id) # Make an API request. + table.expires = expiration + table = client.update_table(table, ["expires"]) # API request + + print(f"Updated {table_id}, expires {table.expires}.") + # [END bigquery_update_table_expiration] diff --git a/samples/snippets/update_table_expiration_test.py b/samples/snippets/update_table_expiration_test.py new file mode 100644 index 000000000..721bf53aa --- /dev/null +++ b/samples/snippets/update_table_expiration_test.py @@ -0,0 +1,44 @@ +# 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 datetime +import typing + +import update_table_expiration + +if typing.TYPE_CHECKING: + import pathlib + + import pytest + + +def test_update_table_expiration( + capsys: "pytest.CaptureFixture[str]", + table_id: str, + tmp_path: "pathlib.Path", +) -> None: + + # This was not needed for function, only for test + expiration = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta( + days=5 + ) + + update_table_expiration.update_table_expiration(table_id, expiration) + + out, _ = capsys.readouterr() + assert "Updated" in out + assert table_id in out + assert str(expiration.day) in out + assert str(expiration.month) in out + assert str(expiration.year) in out