Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Revise update_table_expiration sample #1457

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 45 additions & 0 deletions samples/snippets/update_table_expiration.py
Original file line number Diff line number Diff line change
@@ -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]
44 changes: 44 additions & 0 deletions samples/snippets/update_table_expiration_test.py
Original file line number Diff line number Diff line change
@@ -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