Skip to content

Commit b5ccbfe

Browse files
Mattix23tswast
andauthoredJan 13, 2023
docs: revise get table labels code sample, add TODO to clean up snipp… (#1464)
* docs: revise get table labels code sample, add TODO to clean up snippets.py * added a test with labels * Update samples/snippets/get_table_labels_test.py Co-authored-by: Tim Swast <[email protected]>
1 parent 6088129 commit b5ccbfe

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
 

‎docs/snippets.py

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ def test_manage_table_labels(client, to_delete):
179179
table.labels = labels
180180
table = client.create_table(table)
181181

182+
# TODO(Mattix23): After code sample is updated from cloud.google.com delete this
183+
182184
# [START bigquery_get_table_labels]
183185
# from google.cloud import bigquery
184186
# client = bigquery.Client()

‎samples/snippets/get_table_labels.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def get_table_labels(table_id: str) -> None:
17+
orig_table_id = table_id
18+
# [START bigquery_get_table_labels]
19+
from google.cloud import bigquery
20+
21+
client = bigquery.Client()
22+
23+
# TODO(dev): Change table_id to the full name of the table you want to create.
24+
table_id = "your-project.your_dataset.your_table_name"
25+
26+
# [END bigquery_get_table_labels]
27+
table_id = orig_table_id
28+
29+
# [START bigquery_get_table_labels]
30+
table = client.get_table(table_id) # API Request
31+
32+
# View table labels
33+
print(f"Table ID: {table_id}.")
34+
if table.labels:
35+
for label, value in table.labels.items():
36+
print(f"\t{label}: {value}")
37+
else:
38+
print("\tTable has no labels defined.")
39+
# [END bigquery_get_table_labels]
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import typing
16+
17+
from google.cloud import bigquery
18+
19+
import get_table_labels
20+
21+
if typing.TYPE_CHECKING:
22+
import pytest
23+
24+
25+
def test_get_table_labels(
26+
capsys: "pytest.CaptureFixture[str]",
27+
table_id: str,
28+
bigquery_client: bigquery.Client,
29+
) -> None:
30+
table = bigquery_client.get_table(table_id)
31+
table.labels = {"color": "green"}
32+
bigquery_client.update_table(table, ["labels"])
33+
34+
get_table_labels.get_table_labels(table_id)
35+
36+
out, _ = capsys.readouterr()
37+
assert table_id in out
38+
assert "color" in out
39+
40+
41+
def test_get_table_labels_no_label(
42+
capsys: "pytest.CaptureFixture[str]",
43+
table_id: str,
44+
) -> None:
45+
46+
get_table_labels.get_table_labels(table_id)
47+
48+
out, _ = capsys.readouterr()
49+
assert "no labels defined" in out
50+
assert table_id in out

0 commit comments

Comments
 (0)
Please sign in to comment.