Content-Length: 1134320 | pFad | http://github.com/googleapis/google-cloud-python/commit/8724e8779d5019d9411d88955b59f48759208fb5

D5 Wrapping up a few Managing Table samples (#7) · googleapis/google-cloud-python@8724e87 · GitHub
Skip to content

Commit 8724e87

Browse files
lbristol88tswast
authored andcommitted
Wrapping up a few Managing Table samples (#7)
* Updated list tables snippet along with test. * Removed old list table snippet and upated location of sample
1 parent 32e43a9 commit 8724e87

13 files changed

+200
-97
lines changed

bigquery/docs/snippets.py

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -274,31 +274,6 @@ def test_manage_dataset_labels(client, to_delete):
274274
# [END bigquery_delete_label_dataset]
275275

276276

277-
def test_list_tables(client, to_delete):
278-
"""List tables within a dataset."""
279-
dataset_id = "list_tables_dataset_{}".format(_millis())
280-
dataset_ref = client.dataset(dataset_id)
281-
dataset = client.create_dataset(bigquery.Dataset(dataset_ref))
282-
to_delete.append(dataset)
283-
284-
# [START bigquery_list_tables]
285-
# from google.cloud import bigquery
286-
# client = bigquery.Client()
287-
# dataset_ref = client.dataset('my_dataset')
288-
289-
tables = list(client.list_tables(dataset_ref)) # API request(s)
290-
assert len(tables) == 0
291-
292-
table_ref = dataset.table("my_table")
293-
table = bigquery.Table(table_ref)
294-
client.create_table(table) # API request
295-
tables = list(client.list_tables(dataset)) # API request(s)
296-
297-
assert len(tables) == 1
298-
assert tables[0].table_id == "my_table"
299-
# [END bigquery_list_tables]
300-
301-
302277
def test_create_table_nested_repeated_schema(client, to_delete):
303278
dataset_id = "create_table_nested_repeated_{}".format(_millis())
304279
dataset_ref = client.dataset(dataset_id)
@@ -481,40 +456,6 @@ def test_load_and_query_partitioned_table(client, to_delete):
481456
assert len(rows) == 29
482457

483458

484-
def test_get_table_information(client, to_delete):
485-
"""Show a table's properties."""
486-
dataset_id = "show_table_dataset_{}".format(_millis())
487-
table_id = "show_table_table_{}".format(_millis())
488-
dataset_ref = client.dataset(dataset_id)
489-
dataset = bigquery.Dataset(dataset_ref)
490-
client.create_dataset(dataset)
491-
to_delete.append(dataset)
492-
493-
table = bigquery.Table(dataset.table(table_id), schema=SCHEMA)
494-
table.description = ORIGINAL_DESCRIPTION
495-
table = client.create_table(table)
496-
497-
# [START bigquery_get_table]
498-
# from google.cloud import bigquery
499-
# client = bigquery.Client()
500-
# dataset_id = 'my_dataset'
501-
# table_id = 'my_table'
502-
503-
dataset_ref = client.dataset(dataset_id)
504-
table_ref = dataset_ref.table(table_id)
505-
table = client.get_table(table_ref) # API Request
506-
507-
# View table properties
508-
print(table.schema)
509-
print(table.description)
510-
print(table.num_rows)
511-
# [END bigquery_get_table]
512-
513-
assert table.schema == SCHEMA
514-
assert table.description == ORIGINAL_DESCRIPTION
515-
assert table.num_rows == 0
516-
517-
518459
# [START bigquery_table_exists]
519460
def table_exists(client, table_reference):
520461
"""Return if a table exists.
@@ -1833,37 +1774,6 @@ def test_extract_table_compressed(client, to_delete):
18331774
to_delete.insert(0, blob)
18341775

18351776

1836-
def test_delete_table(client, to_delete):
1837-
"""Delete a table."""
1838-
from google.cloud.exceptions import NotFound
1839-
1840-
dataset_id = "delete_table_dataset_{}".format(_millis())
1841-
table_id = "delete_table_table_{}".format(_millis())
1842-
dataset_ref = client.dataset(dataset_id)
1843-
dataset = bigquery.Dataset(dataset_ref)
1844-
dataset.location = "US"
1845-
dataset = client.create_dataset(dataset)
1846-
to_delete.append(dataset)
1847-
1848-
table_ref = dataset.table(table_id)
1849-
table = bigquery.Table(table_ref, schema=SCHEMA)
1850-
client.create_table(table)
1851-
# [START bigquery_delete_table]
1852-
# from google.cloud import bigquery
1853-
# client = bigquery.Client()
1854-
# dataset_id = 'my_dataset'
1855-
# table_id = 'my_table'
1856-
1857-
table_ref = client.dataset(dataset_id).table(table_id)
1858-
client.delete_table(table_ref) # API request
1859-
1860-
print("Table {}:{} deleted.".format(dataset_id, table_id))
1861-
# [END bigquery_delete_table]
1862-
1863-
with pytest.raises(NotFound):
1864-
client.get_table(table) # API request
1865-
1866-
18671777
def test_undelete_table(client, to_delete):
18681778
dataset_id = "undelete_table_dataset_{}".format(_millis())
18691779
table_id = "undelete_table_table_{}".format(_millis())

bigquery/docs/usage/tables.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Listing Tables
1010
List the tables belonging to a dataset with the
1111
:func:`~google.cloud.bigquery.client.Client.list_tables` method:
1212

13-
.. literalinclude:: ../snippets.py
13+
.. literalinclude:: ../samples/list_tables.py
1414
:language: python
1515
:dedent: 4
1616
:start-after: [START bigquery_list_tables]
@@ -22,7 +22,7 @@ Getting a Table
2222
Get a table resource with the
2323
:func:`~google.cloud.bigquery.client.Client.get_table` method:
2424

25-
.. literalinclude:: ../snippets.py
25+
.. literalinclude:: ../samples/get_table.py
2626
:language: python
2727
:dedent: 4
2828
:start-after: [START bigquery_get_table]
@@ -140,7 +140,7 @@ Deleting a Table
140140
Delete a table with the
141141
:func:`~google.cloud.bigquery.client.Client.delete_table` method:
142142

143-
.. literalinclude:: ../snippets.py
143+
.. literalinclude:: ../samples/delete_table.py
144144
:language: python
145145
:dedent: 4
146146
:start-after: [START bigquery_delete_table]

bigquery/samples/delete_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ def delete_dataset(client, dataset_id):
2727
# Use the delete_contents parameter to delete a dataset and its contents
2828
# Use the not_found_ok parameter to not receive an error if the dataset has already been deleted.
2929
client.delete_dataset(dataset_id, delete_contents=True, not_found_ok=True)
30-
# [END bigquery_delete_dataset]
3130

3231
print("Deleted dataset '{}'.".format(dataset_id))
32+
# [END bigquery_delete_dataset]

bigquery/samples/delete_model.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ def delete_model(client, model_id):
2626
# model_id = 'your-project.your_dataset.your_model'
2727

2828
client.delete_model(model_id)
29-
# [END bigquery_delete_model]
30-
3129
print("Deleted model '{}'.".format(model_id))
30+
# [END bigquery_delete_model]

bigquery/samples/delete_table.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2019 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 delete_table(client, table_id):
17+
18+
# [START bigquery_delete_table]
19+
from google.cloud import bigquery
20+
21+
# TODO(developer): Construct a BigQuery client object.
22+
# client = bigquery.Client()
23+
24+
# TODO(developer): Set table_id to the ID of the table to fetch.
25+
# table_id = 'your-project.your_dataset.your_table'
26+
27+
# If the table does not exist, delete_table raises
28+
# google.api_core.exceptions.NotFound unless not_found_ok is True
29+
client.delete_table(table_id, not_found_ok=True)
30+
print("Deleted table '{}'.".format(table_id))
31+
# [END bigquery_delete_table]

bigquery/samples/get_table.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2019 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(client, table_id):
17+
18+
# [START bigquery_get_table]
19+
from google.cloud import bigquery
20+
21+
# TODO(developer): Construct a BigQuery client object.
22+
# client = bigquery.Client()
23+
24+
# TODO(developer): Set table_id to the ID of the model to fetch.
25+
# table_id = 'your-project.your_dataset.your_table'
26+
27+
table = client.get_table(table_id)
28+
29+
print(
30+
"Got table '{}.{}.{}'.".format(table.project, table.dataset_id, table.table_id)
31+
)
32+
33+
# View table properties
34+
print("Table schema: {}".format(table.schema))
35+
print("Table description: {}".format(table.description))
36+
print("Table has {} rows".format(table.num_rows))
37+
# [END bigquery_get_table]

bigquery/samples/list_tables.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2019 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 list_tables(client, dataset_id):
17+
18+
# [START bigquery_list_tables]
19+
from google.cloud import bigquery
20+
21+
# TODO(developer): Construct a BigQuery client object.
22+
# client = bigquery.Client()
23+
24+
# TODO(developer): Set dataset_id to the ID of the dataset that contains
25+
# the tables you are listing.
26+
# dataset_id = 'your-project.your_dataset'
27+
28+
tables = client.list_tables(dataset_id)
29+
30+
print("Tables contained in '{}':".format(dataset_id))
31+
for table in tables:
32+
print("{}.{}.{}".format(table.project, table.dataset_id, table.table_id))
33+
# [END bigquery_list_tables]

bigquery/samples/tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ def dataset_id(client):
5555
client.delete_dataset(dataset, delete_contents=True, not_found_ok=True)
5656

5757

58+
@pytest.fixture
59+
def table_id(client, dataset_id):
60+
now = datetime.datetime.now()
61+
table_id = "python_samples_{}_{}".format(
62+
now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
63+
)
64+
65+
table = bigquery.Table("{}.{}".format(dataset_id, table_id))
66+
table = client.create_table(table)
67+
yield "{}.{}.{}".format(table.project, table.dataset_id, table.table_id)
68+
client.delete_table(table, not_found_ok=True)
69+
70+
5871
@pytest.fixture
5972
def model_id(client, dataset_id):
6073
model_id = "{}.{}".format(dataset_id, uuid.uuid4().hex)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2019 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+
from .. import delete_table
16+
17+
18+
def test_delete_table(capsys, client, table_id):
19+
20+
delete_table.delete_table(client, table_id)
21+
out, err = capsys.readouterr()
22+
assert "Deleted table '{}'.".format(table_id) in out
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2019 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+
from google.cloud import bigquery
16+
from .. import get_table
17+
18+
19+
def test_get_table(capsys, client, random_table_id):
20+
schema = [
21+
bigquery.SchemaField("full_name", "STRING", mode="REQUIRED"),
22+
bigquery.SchemaField("age", "INTEGER", mode="REQUIRED"),
23+
]
24+
25+
table = bigquery.Table(random_table_id, schema)
26+
table.description = "Sample Table"
27+
table = client.create_table(table)
28+
29+
get_table.get_table(client, random_table_id)
30+
out, err = capsys.readouterr()
31+
assert "Got table '{}'.".format(random_table_id) in out
32+
assert "full_name" in out # test that schema is printed
33+
assert "Table description: Sample Table" in out
34+
assert "Table has 0 rows" in out
35+
client.delete_table(table, not_found_ok=True)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2019 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+
from .. import list_tables
16+
17+
18+
def test_list_tables(capsys, client, dataset_id, table_id):
19+
20+
list_tables.list_tables(client, dataset_id)
21+
out, err = capsys.readouterr()
22+
assert "Tables contained in '{}':".format(dataset_id) in out
23+
assert table_id in out

bigquery/samples/tests/test_update_dataset_default_table_expiration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_update_dataset_default_table_expiration(capsys, client, dataset_id):
2020
one_day_ms = 24 * 60 * 60 * 1000 # in milliseconds
2121

2222
update_dataset_default_table_expiration.update_dataset_default_table_expiration(
23-
client, dataset_id, one_day_ms
23+
client, dataset_id
2424
)
2525
out, err = capsys.readouterr()
2626
assert (

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/googleapis/google-cloud-python/commit/8724e8779d5019d9411d88955b59f48759208fb5

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy