Skip to content

Commit

Permalink
Wrapping up a few Managing Table samples (#7)
Browse files Browse the repository at this point in the history
* Updated list tables snippet along with test.

* Removed old list table snippet and upated location of sample
  • Loading branch information
lbristol88 authored and tswast committed Apr 10, 2019
1 parent fa82c54 commit 094602b
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 97 deletions.
90 changes: 0 additions & 90 deletions bigquery/docs/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,31 +274,6 @@ def test_manage_dataset_labels(client, to_delete):
# [END bigquery_delete_label_dataset]


def test_list_tables(client, to_delete):
"""List tables within a dataset."""
dataset_id = "list_tables_dataset_{}".format(_millis())
dataset_ref = client.dataset(dataset_id)
dataset = client.create_dataset(bigquery.Dataset(dataset_ref))
to_delete.append(dataset)

# [START bigquery_list_tables]
# from google.cloud import bigquery
# client = bigquery.Client()
# dataset_ref = client.dataset('my_dataset')

tables = list(client.list_tables(dataset_ref)) # API request(s)
assert len(tables) == 0

table_ref = dataset.table("my_table")
table = bigquery.Table(table_ref)
client.create_table(table) # API request
tables = list(client.list_tables(dataset)) # API request(s)

assert len(tables) == 1
assert tables[0].table_id == "my_table"
# [END bigquery_list_tables]


def test_create_table_nested_repeated_schema(client, to_delete):
dataset_id = "create_table_nested_repeated_{}".format(_millis())
dataset_ref = client.dataset(dataset_id)
Expand Down Expand Up @@ -481,40 +456,6 @@ def test_load_and_query_partitioned_table(client, to_delete):
assert len(rows) == 29


def test_get_table_information(client, to_delete):
"""Show a table's properties."""
dataset_id = "show_table_dataset_{}".format(_millis())
table_id = "show_table_table_{}".format(_millis())
dataset_ref = client.dataset(dataset_id)
dataset = bigquery.Dataset(dataset_ref)
client.create_dataset(dataset)
to_delete.append(dataset)

table = bigquery.Table(dataset.table(table_id), schema=SCHEMA)
table.description = ORIGINAL_DESCRIPTION
table = client.create_table(table)

# [START bigquery_get_table]
# from google.cloud import bigquery
# client = bigquery.Client()
# dataset_id = 'my_dataset'
# table_id = 'my_table'

dataset_ref = client.dataset(dataset_id)
table_ref = dataset_ref.table(table_id)
table = client.get_table(table_ref) # API Request

# View table properties
print(table.schema)
print(table.description)
print(table.num_rows)
# [END bigquery_get_table]

assert table.schema == SCHEMA
assert table.description == ORIGINAL_DESCRIPTION
assert table.num_rows == 0


# [START bigquery_table_exists]
def table_exists(client, table_reference):
"""Return if a table exists.
Expand Down Expand Up @@ -1833,37 +1774,6 @@ def test_extract_table_compressed(client, to_delete):
to_delete.insert(0, blob)


def test_delete_table(client, to_delete):
"""Delete a table."""
from google.cloud.exceptions import NotFound

dataset_id = "delete_table_dataset_{}".format(_millis())
table_id = "delete_table_table_{}".format(_millis())
dataset_ref = client.dataset(dataset_id)
dataset = bigquery.Dataset(dataset_ref)
dataset.location = "US"
dataset = client.create_dataset(dataset)
to_delete.append(dataset)

table_ref = dataset.table(table_id)
table = bigquery.Table(table_ref, schema=SCHEMA)
client.create_table(table)
# [START bigquery_delete_table]
# from google.cloud import bigquery
# client = bigquery.Client()
# dataset_id = 'my_dataset'
# table_id = 'my_table'

table_ref = client.dataset(dataset_id).table(table_id)
client.delete_table(table_ref) # API request

print("Table {}:{} deleted.".format(dataset_id, table_id))
# [END bigquery_delete_table]

with pytest.raises(NotFound):
client.get_table(table) # API request


def test_undelete_table(client, to_delete):
dataset_id = "undelete_table_dataset_{}".format(_millis())
table_id = "undelete_table_table_{}".format(_millis())
Expand Down
6 changes: 3 additions & 3 deletions bigquery/docs/usage/tables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Listing Tables
List the tables belonging to a dataset with the
:func:`~google.cloud.bigquery.client.Client.list_tables` method:

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

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

.. literalinclude:: ../snippets.py
.. literalinclude:: ../samples/delete_table.py
:language: python
:dedent: 4
:start-after: [START bigquery_delete_table]
Expand Down
2 changes: 1 addition & 1 deletion bigquery/samples/delete_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ def delete_dataset(client, dataset_id):
# Use the delete_contents parameter to delete a dataset and its contents
# Use the not_found_ok parameter to not receive an error if the dataset has already been deleted.
client.delete_dataset(dataset_id, delete_contents=True, not_found_ok=True)
# [END bigquery_delete_dataset]

print("Deleted dataset '{}'.".format(dataset_id))
# [END bigquery_delete_dataset]
3 changes: 1 addition & 2 deletions bigquery/samples/delete_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ def delete_model(client, model_id):
# model_id = 'your-project.your_dataset.your_model'

client.delete_model(model_id)
# [END bigquery_delete_model]

print("Deleted model '{}'.".format(model_id))
# [END bigquery_delete_model]
31 changes: 31 additions & 0 deletions bigquery/samples/delete_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2019 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 delete_table(client, table_id):

# [START bigquery_delete_table]
from google.cloud import bigquery

# TODO(developer): Construct a BigQuery client object.
# client = bigquery.Client()

# TODO(developer): Set table_id to the ID of the table to fetch.
# table_id = 'your-project.your_dataset.your_table'

# If the table does not exist, delete_table raises
# google.api_core.exceptions.NotFound unless not_found_ok is True
client.delete_table(table_id, not_found_ok=True)
print("Deleted table '{}'.".format(table_id))
# [END bigquery_delete_table]
37 changes: 37 additions & 0 deletions bigquery/samples/get_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2019 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 get_table(client, table_id):

# [START bigquery_get_table]
from google.cloud import bigquery

# TODO(developer): Construct a BigQuery client object.
# client = bigquery.Client()

# TODO(developer): Set table_id to the ID of the model to fetch.
# table_id = 'your-project.your_dataset.your_table'

table = client.get_table(table_id)

print(
"Got table '{}.{}.{}'.".format(table.project, table.dataset_id, table.table_id)
)

# View table properties
print("Table schema: {}".format(table.schema))
print("Table description: {}".format(table.description))
print("Table has {} rows".format(table.num_rows))
# [END bigquery_get_table]
33 changes: 33 additions & 0 deletions bigquery/samples/list_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2019 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 list_tables(client, dataset_id):

# [START bigquery_list_tables]
from google.cloud import bigquery

# TODO(developer): Construct a BigQuery client object.
# client = bigquery.Client()

# TODO(developer): Set dataset_id to the ID of the dataset that contains
# the tables you are listing.
# dataset_id = 'your-project.your_dataset'

tables = client.list_tables(dataset_id)

print("Tables contained in '{}':".format(dataset_id))
for table in tables:
print("{}.{}.{}".format(table.project, table.dataset_id, table.table_id))
# [END bigquery_list_tables]
13 changes: 13 additions & 0 deletions bigquery/samples/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ def dataset_id(client):
client.delete_dataset(dataset, delete_contents=True, not_found_ok=True)


@pytest.fixture
def table_id(client, dataset_id):
now = datetime.datetime.now()
table_id = "python_samples_{}_{}".format(
now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
)

table = bigquery.Table("{}.{}".format(dataset_id, table_id))
table = client.create_table(table)
yield "{}.{}.{}".format(table.project, table.dataset_id, table.table_id)
client.delete_table(table, not_found_ok=True)


@pytest.fixture
def model_id(client, dataset_id):
model_id = "{}.{}".format(dataset_id, uuid.uuid4().hex)
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions bigquery/samples/tests/test_delete_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2019 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.

from .. import delete_table


def test_delete_table(capsys, client, table_id):

delete_table.delete_table(client, table_id)
out, err = capsys.readouterr()
assert "Deleted table '{}'.".format(table_id) in out
35 changes: 35 additions & 0 deletions bigquery/samples/tests/test_get_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2019 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.

from google.cloud import bigquery
from .. import get_table


def test_get_table(capsys, client, random_table_id):
schema = [
bigquery.SchemaField("full_name", "STRING", mode="REQUIRED"),
bigquery.SchemaField("age", "INTEGER", mode="REQUIRED"),
]

table = bigquery.Table(random_table_id, schema)
table.description = "Sample Table"
table = client.create_table(table)

get_table.get_table(client, random_table_id)
out, err = capsys.readouterr()
assert "Got table '{}'.".format(random_table_id) in out
assert "full_name" in out # test that schema is printed
assert "Table description: Sample Table" in out
assert "Table has 0 rows" in out
client.delete_table(table, not_found_ok=True)
23 changes: 23 additions & 0 deletions bigquery/samples/tests/test_list_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2019 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.

from .. import list_tables


def test_list_tables(capsys, client, dataset_id, table_id):

list_tables.list_tables(client, dataset_id)
out, err = capsys.readouterr()
assert "Tables contained in '{}':".format(dataset_id) in out
assert table_id in out
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_update_dataset_default_table_expiration(capsys, client, dataset_id):
one_day_ms = 24 * 60 * 60 * 1000 # in milliseconds

update_dataset_default_table_expiration.update_dataset_default_table_expiration(
client, dataset_id, one_day_ms
client, dataset_id
)
out, err = capsys.readouterr()
assert (
Expand Down

0 comments on commit 094602b

Please sign in to comment.