diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 58bd6689c80fc..01ad3e014608b 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -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) @@ -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. @@ -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()) diff --git a/bigquery/docs/usage/tables.rst b/bigquery/docs/usage/tables.rst index 0da02a1738287..4aede9545cd8e 100644 --- a/bigquery/docs/usage/tables.rst +++ b/bigquery/docs/usage/tables.rst @@ -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] @@ -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] @@ -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] diff --git a/bigquery/samples/delete_dataset.py b/bigquery/samples/delete_dataset.py index ad04c3fb36647..58851f1e21208 100644 --- a/bigquery/samples/delete_dataset.py +++ b/bigquery/samples/delete_dataset.py @@ -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] diff --git a/bigquery/samples/delete_model.py b/bigquery/samples/delete_model.py index dfe23cd7ef297..371f9003576b5 100644 --- a/bigquery/samples/delete_model.py +++ b/bigquery/samples/delete_model.py @@ -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] diff --git a/bigquery/samples/delete_table.py b/bigquery/samples/delete_table.py new file mode 100644 index 0000000000000..3eb7dc918da74 --- /dev/null +++ b/bigquery/samples/delete_table.py @@ -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] diff --git a/bigquery/samples/get_table.py b/bigquery/samples/get_table.py new file mode 100644 index 0000000000000..e6a5c502e2b33 --- /dev/null +++ b/bigquery/samples/get_table.py @@ -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] diff --git a/bigquery/samples/list_tables.py b/bigquery/samples/list_tables.py new file mode 100644 index 0000000000000..33ed408906b02 --- /dev/null +++ b/bigquery/samples/list_tables.py @@ -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] diff --git a/bigquery/samples/tests/conftest.py b/bigquery/samples/tests/conftest.py index ed797e56310b6..629b23473b016 100644 --- a/bigquery/samples/tests/conftest.py +++ b/bigquery/samples/tests/conftest.py @@ -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) diff --git a/bigquery/samples/tests/test_table_samples.py b/bigquery/samples/tests/test_create_table.py similarity index 100% rename from bigquery/samples/tests/test_table_samples.py rename to bigquery/samples/tests/test_create_table.py diff --git a/bigquery/samples/tests/test_delete_table.py b/bigquery/samples/tests/test_delete_table.py new file mode 100644 index 0000000000000..8f4796623a830 --- /dev/null +++ b/bigquery/samples/tests/test_delete_table.py @@ -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 diff --git a/bigquery/samples/tests/test_get_table.py b/bigquery/samples/tests/test_get_table.py new file mode 100644 index 0000000000000..debf1b63a3fc7 --- /dev/null +++ b/bigquery/samples/tests/test_get_table.py @@ -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) diff --git a/bigquery/samples/tests/test_list_tables.py b/bigquery/samples/tests/test_list_tables.py new file mode 100644 index 0000000000000..ec1621ac75799 --- /dev/null +++ b/bigquery/samples/tests/test_list_tables.py @@ -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 diff --git a/bigquery/samples/tests/test_update_dataset_default_table_expiration.py b/bigquery/samples/tests/test_update_dataset_default_table_expiration.py index 0366b767fbe85..46e9654209edf 100644 --- a/bigquery/samples/tests/test_update_dataset_default_table_expiration.py +++ b/bigquery/samples/tests/test_update_dataset_default_table_expiration.py @@ -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 (