From 46903ded6cb4fb4c836a876b72ddbf62bbbac5d8 Mon Sep 17 00:00:00 2001 From: lbristol88 Date: Tue, 2 Apr 2019 13:47:22 -0700 Subject: [PATCH 1/4] Move create table sample to samples directory * Add `samples/create_table.py` sample code. * Remove `test_create_table` from snippets collection. --- bigquery/docs/samples | 1 + bigquery/docs/snippets.py | 25 ------------- bigquery/docs/usage/tables.rst | 2 +- bigquery/samples/create_table.py | 37 ++++++++++++++++++++ bigquery/samples/tests/conftest.py | 9 +++++ bigquery/samples/tests/test_table_samples.py | 22 ++++++++++++ 6 files changed, 70 insertions(+), 26 deletions(-) create mode 120000 bigquery/docs/samples create mode 100644 bigquery/samples/create_table.py create mode 100644 bigquery/samples/tests/test_table_samples.py diff --git a/bigquery/docs/samples b/bigquery/docs/samples new file mode 120000 index 000000000000..18cd9a30aaff --- /dev/null +++ b/bigquery/docs/samples @@ -0,0 +1 @@ +../samples/ \ No newline at end of file diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 00569c40af18..8836c8790ee9 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -522,31 +522,6 @@ def test_list_tables(client, to_delete): # [END bigquery_list_tables] -def test_create_table(client, to_delete): - """Create a table.""" - dataset_id = "create_table_dataset_{}".format(_millis()) - dataset_ref = client.dataset(dataset_id) - dataset = bigquery.Dataset(dataset_ref) - client.create_dataset(dataset) - to_delete.append(dataset) - - # [START bigquery_create_table] - # from google.cloud import bigquery - # client = bigquery.Client() - # dataset_ref = client.dataset('my_dataset') - - schema = [ - bigquery.SchemaField("full_name", "STRING", mode="REQUIRED"), - bigquery.SchemaField("age", "INTEGER", mode="REQUIRED"), - ] - table_ref = dataset_ref.table("my_table") - table = bigquery.Table(table_ref, schema=schema) - table = client.create_table(table) # API request - - assert table.table_id == "my_table" - # [END bigquery_create_table] - - def test_create_table_nested_repeated_schema(client, to_delete): dataset_id = "create_table_nested_repeated_{}".format(_millis()) dataset_ref = client.dataset(dataset_id) diff --git a/bigquery/docs/usage/tables.rst b/bigquery/docs/usage/tables.rst index 555366fd2a4b..0da02a173828 100644 --- a/bigquery/docs/usage/tables.rst +++ b/bigquery/docs/usage/tables.rst @@ -43,7 +43,7 @@ Creating a Table Create an empty table with the :func:`~google.cloud.bigquery.client.Client.create_table` method: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/create_table.py :language: python :dedent: 4 :start-after: [START bigquery_create_table] diff --git a/bigquery/samples/create_table.py b/bigquery/samples/create_table.py new file mode 100644 index 000000000000..5e2e34d41d99 --- /dev/null +++ b/bigquery/samples/create_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 create_table(client, table_id): + + # [START bigquery_create_table] + from google.cloud import bigquery + + schema = [ + bigquery.SchemaField("full_name", "STRING", mode="REQUIRED"), + bigquery.SchemaField("age", "INTEGER", mode="REQUIRED"), + ] + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + # TODO(developer): Set table_id to the ID of the table to create + # table_id = "your-project.your_dataset.your_table_name" + + table = bigquery.Table(table_id, schema=schema) + table = client.create_table(table) # API request + print( + "Created table {}.{}.{}".format(table.project, table.dataset_id, table.table_id) + ) + # [END bigquery_create_table] diff --git a/bigquery/samples/tests/conftest.py b/bigquery/samples/tests/conftest.py index 1543e1fdcd0a..ca7e9081a416 100644 --- a/bigquery/samples/tests/conftest.py +++ b/bigquery/samples/tests/conftest.py @@ -25,6 +25,15 @@ def client(): return bigquery.Client() +@pytest.fixture +def random_table_id(client, dataset_id): + now = datetime.datetime.now() + random_table_id = "example_table_{}_{}".format( + now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] + ) + return "{}.{}".format(dataset_id, random_table_id) + + @pytest.fixture def dataset_id(client): now = datetime.datetime.now() diff --git a/bigquery/samples/tests/test_table_samples.py b/bigquery/samples/tests/test_table_samples.py new file mode 100644 index 000000000000..903f76b536ea --- /dev/null +++ b/bigquery/samples/tests/test_table_samples.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 create_table + + +def test_create_table(capsys, client, random_table_id): + + create_table.create_table(client, random_table_id) + out, err = capsys.readouterr() + assert "Created table {}".format(random_table_id) in out From 92fd8afb197d3af8f1dddfc5b2300ce020bd2327 Mon Sep 17 00:00:00 2001 From: lbristol88 Date: Thu, 4 Apr 2019 05:44:18 -0700 Subject: [PATCH 2/4] Move code sample for create dataset to samples directory (#5) --- bigquery/docs/snippets.py | 27 ------------- bigquery/docs/usage/datasets.rst | 2 +- bigquery/samples/create_dataset.py | 38 +++++++++++++++++++ bigquery/samples/tests/conftest.py | 12 +++++- .../samples/tests/test_dataset_samples.py | 22 +++++++++++ 5 files changed, 72 insertions(+), 29 deletions(-) create mode 100644 bigquery/samples/create_dataset.py create mode 100644 bigquery/samples/tests/test_dataset_samples.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 8836c8790ee9..05b81b00c152 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -192,33 +192,6 @@ def test_list_datasets_by_label(client, to_delete): assert dataset_id in found -def test_create_dataset(client, to_delete): - """Create a dataset.""" - dataset_id = "create_dataset_{}".format(_millis()) - - # [START bigquery_create_dataset] - # from google.cloud import bigquery - # client = bigquery.Client() - # dataset_id = 'my_dataset' - - # Create a DatasetReference using a chosen dataset ID. - # The project defaults to the Client's project if not specified. - dataset_ref = client.dataset(dataset_id) - - # Construct a full Dataset object to send to the API. - dataset = bigquery.Dataset(dataset_ref) - # Specify the geographic location where the dataset should reside. - dataset.location = "US" - - # Send the dataset to the API for creation. - # Raises google.api_core.exceptions.Conflict if the Dataset already - # exists within the project. - dataset = client.create_dataset(dataset) # API request - # [END bigquery_create_dataset] - - to_delete.append(dataset) - - def test_get_dataset_information(client, to_delete): """View information about a dataset.""" dataset_id = "get_dataset_{}".format(_millis()) diff --git a/bigquery/docs/usage/datasets.rst b/bigquery/docs/usage/datasets.rst index 09ae90767cdc..dbcd834d5cd3 100644 --- a/bigquery/docs/usage/datasets.rst +++ b/bigquery/docs/usage/datasets.rst @@ -43,7 +43,7 @@ Creating a Dataset Create a new dataset with the :func:`~google.cloud.bigquery.client.Client.create_dataset` method: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/create_dataset.py :language: python :dedent: 4 :start-after: [START bigquery_create_dataset] diff --git a/bigquery/samples/create_dataset.py b/bigquery/samples/create_dataset.py new file mode 100644 index 000000000000..89ca9d38f5f3 --- /dev/null +++ b/bigquery/samples/create_dataset.py @@ -0,0 +1,38 @@ +# 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 create_dataset(client, dataset_id): + + # [START bigquery_create_dataset] + 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 to create. + # dataset_id = "{}.your_dataset".format(client.project) + + # Construct a full Dataset object to send to the API. + dataset = bigquery.Dataset(dataset_id) + + # TODO(developer): Specify the geographic location where the dataset should reside. + dataset.location = "US" + + # Send the dataset to the API for creation. + # Raises google.api_core.exceptions.Conflict if the Dataset already + # exists within the project. + dataset = client.create_dataset(dataset) # API request + print("Created dataset {}.{}".format(client.project, dataset.dataset_id)) + # [END bigquery_create_dataset] diff --git a/bigquery/samples/tests/conftest.py b/bigquery/samples/tests/conftest.py index ca7e9081a416..ed797e56310b 100644 --- a/bigquery/samples/tests/conftest.py +++ b/bigquery/samples/tests/conftest.py @@ -34,6 +34,16 @@ def random_table_id(client, dataset_id): return "{}.{}".format(dataset_id, random_table_id) +@pytest.fixture +def random_dataset_id(client): + now = datetime.datetime.now() + random_dataset_id = "example_dataset_{}_{}".format( + now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] + ) + yield "{}.{}".format(client.project, random_dataset_id) + client.delete_dataset(random_dataset_id, delete_contents=True, not_found_ok=True) + + @pytest.fixture def dataset_id(client): now = datetime.datetime.now() @@ -42,7 +52,7 @@ def dataset_id(client): ) dataset = client.create_dataset(dataset_id) yield "{}.{}".format(dataset.project, dataset.dataset_id) - client.delete_dataset(dataset, delete_contents=True) + client.delete_dataset(dataset, delete_contents=True, not_found_ok=True) @pytest.fixture diff --git a/bigquery/samples/tests/test_dataset_samples.py b/bigquery/samples/tests/test_dataset_samples.py new file mode 100644 index 000000000000..dfadc67d8468 --- /dev/null +++ b/bigquery/samples/tests/test_dataset_samples.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 create_dataset + + +def test_create_dataset(capsys, client, random_dataset_id): + + create_dataset.create_dataset(client, random_dataset_id) + out, err = capsys.readouterr() + assert "Created dataset {}".format(random_dataset_id) in out From 32e43a976389717b9e0a30f60dbdf8e6fb2d433a Mon Sep 17 00:00:00 2001 From: lbristol88 Date: Mon, 8 Apr 2019 15:53:32 -0700 Subject: [PATCH 3/4] Added collection of Managing Dataset samples (#6) * Added delete dataset function * Added get dataset function * Added list dataset function * Added update dataset description sample * Added update dataset access sample * Added update dataset table expiration sample * Added tests for dataset samples and updated docs * Removing original update dataset access from snippets file. * Moved all dataset tests into own file. Made changes based on feedback. * Made changes based on feedback * Removed unnecessary use of random_dataset_id in tests and removed one_day_ms fixture * Removed unnecessary constant * Stored the math as a constant to make it look cleaner. --- bigquery/docs/snippets.py | 196 ------------------ bigquery/docs/usage/datasets.rst | 10 +- bigquery/samples/delete_dataset.py | 32 +++ bigquery/samples/get_dataset.py | 56 +++++ bigquery/samples/list_datasets.py | 33 +++ ...aset_samples.py => test_create_dataset.py} | 0 bigquery/samples/tests/test_delete_dataset.py | 22 ++ bigquery/samples/tests/test_get_dataset.py | 22 ++ bigquery/samples/tests/test_list_datasets.py | 22 ++ .../tests/test_update_dataset_access.py | 24 +++ ...update_dataset_default_table_expiration.py | 29 +++ .../tests/test_update_dataset_description.py | 22 ++ bigquery/samples/update_dataset_access.py | 45 ++++ ...update_dataset_default_table_expiration.py | 40 ++++ .../samples/update_dataset_description.py | 37 ++++ 15 files changed, 389 insertions(+), 201 deletions(-) create mode 100644 bigquery/samples/delete_dataset.py create mode 100644 bigquery/samples/get_dataset.py create mode 100644 bigquery/samples/list_datasets.py rename bigquery/samples/tests/{test_dataset_samples.py => test_create_dataset.py} (100%) create mode 100644 bigquery/samples/tests/test_delete_dataset.py create mode 100644 bigquery/samples/tests/test_get_dataset.py create mode 100644 bigquery/samples/tests/test_list_datasets.py create mode 100644 bigquery/samples/tests/test_update_dataset_access.py create mode 100644 bigquery/samples/tests/test_update_dataset_default_table_expiration.py create mode 100644 bigquery/samples/tests/test_update_dataset_description.py create mode 100644 bigquery/samples/update_dataset_access.py create mode 100644 bigquery/samples/update_dataset_default_table_expiration.py create mode 100644 bigquery/samples/update_dataset_description.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 05b81b00c152..58bd6689c80f 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -147,24 +147,6 @@ def test_create_client_json_credentials(): assert client is not None -def test_list_datasets(client): - """List datasets for a project.""" - # [START bigquery_list_datasets] - # from google.cloud import bigquery - # client = bigquery.Client() - - datasets = list(client.list_datasets()) - project = client.project - - if datasets: - print("Datasets in project {}:".format(project)) - for dataset in datasets: # API request(s) - print("\t{}".format(dataset.dataset_id)) - else: - print("{} project does not contain any datasets.".format(project)) - # [END bigquery_list_datasets] - - def test_list_datasets_by_label(client, to_delete): dataset_id = "list_datasets_by_label_{}".format(_millis()) dataset = bigquery.Dataset(client.dataset(dataset_id)) @@ -192,51 +174,6 @@ def test_list_datasets_by_label(client, to_delete): assert dataset_id in found -def test_get_dataset_information(client, to_delete): - """View information about a dataset.""" - dataset_id = "get_dataset_{}".format(_millis()) - dataset_labels = {"color": "green"} - dataset_ref = client.dataset(dataset_id) - dataset = bigquery.Dataset(dataset_ref) - dataset.description = ORIGINAL_DESCRIPTION - dataset.labels = dataset_labels - dataset = client.create_dataset(dataset) # API request - to_delete.append(dataset) - - # [START bigquery_get_dataset] - # from google.cloud import bigquery - # client = bigquery.Client() - # dataset_id = 'my_dataset' - - dataset_ref = client.dataset(dataset_id) - dataset = client.get_dataset(dataset_ref) # API request - - # View dataset properties - print("Dataset ID: {}".format(dataset_id)) - print("Description: {}".format(dataset.description)) - print("Labels:") - labels = dataset.labels - if labels: - for label, value in labels.items(): - print("\t{}: {}".format(label, value)) - else: - print("\tDataset has no labels defined.") - - # View tables in dataset - print("Tables:") - tables = list(client.list_tables(dataset_ref)) # API request(s) - if tables: - for table in tables: - print("\t{}".format(table.table_id)) - else: - print("\tThis dataset does not contain any tables.") - # [END bigquery_get_dataset] - - assert dataset.description == ORIGINAL_DESCRIPTION - assert dataset.labels == dataset_labels - assert tables == [] - - # [START bigquery_dataset_exists] def dataset_exists(client, dataset_reference): """Return if a dataset exists. @@ -274,66 +211,6 @@ def test_dataset_exists(client, to_delete): assert not dataset_exists(client, client.dataset("i_dont_exist")) -@pytest.mark.skip( - reason=( - "update_dataset() is flaky " - "https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5588" - ) -) -def test_update_dataset_description(client, to_delete): - """Update a dataset's description.""" - dataset_id = "update_dataset_description_{}".format(_millis()) - dataset = bigquery.Dataset(client.dataset(dataset_id)) - dataset.description = "Original description." - client.create_dataset(dataset) - to_delete.append(dataset) - - # [START bigquery_update_dataset_description] - # from google.cloud import bigquery - # client = bigquery.Client() - # dataset_ref = client.dataset('my_dataset') - # dataset = client.get_dataset(dataset_ref) # API request - - assert dataset.description == "Original description." - dataset.description = "Updated description." - - dataset = client.update_dataset(dataset, ["description"]) # API request - - assert dataset.description == "Updated description." - # [END bigquery_update_dataset_description] - - -@pytest.mark.skip( - reason=( - "update_dataset() is flaky " - "https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5588" - ) -) -def test_update_dataset_default_table_expiration(client, to_delete): - """Update a dataset's default table expiration.""" - dataset_id = "update_dataset_default_expiration_{}".format(_millis()) - dataset = bigquery.Dataset(client.dataset(dataset_id)) - dataset = client.create_dataset(dataset) - to_delete.append(dataset) - - # [START bigquery_update_dataset_expiration] - # from google.cloud import bigquery - # client = bigquery.Client() - # dataset_ref = client.dataset('my_dataset') - # dataset = client.get_dataset(dataset_ref) # API request - - assert dataset.default_table_expiration_ms is None - one_day_ms = 24 * 60 * 60 * 1000 # in milliseconds - dataset.default_table_expiration_ms = one_day_ms - - dataset = client.update_dataset( - dataset, ["default_table_expiration_ms"] - ) # API request - - assert dataset.default_table_expiration_ms == one_day_ms - # [END bigquery_update_dataset_expiration] - - @pytest.mark.skip( reason=( "update_dataset() is flaky " @@ -397,79 +274,6 @@ def test_manage_dataset_labels(client, to_delete): # [END bigquery_delete_label_dataset] -@pytest.mark.skip( - reason=( - "update_dataset() is flaky " - "https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5588" - ) -) -def test_update_dataset_access(client, to_delete): - """Update a dataset's access controls.""" - dataset_id = "update_dataset_access_{}".format(_millis()) - dataset = bigquery.Dataset(client.dataset(dataset_id)) - dataset = client.create_dataset(dataset) - to_delete.append(dataset) - - # [START bigquery_update_dataset_access] - # from google.cloud import bigquery - # client = bigquery.Client() - # dataset = client.get_dataset(client.dataset('my_dataset')) - - entry = bigquery.AccessEntry( - role="READER", - entity_type="userByEmail", - entity_id="sample.bigquery.dev@gmail.com", - ) - assert entry not in dataset.access_entries - entries = list(dataset.access_entries) - entries.append(entry) - dataset.access_entries = entries - - dataset = client.update_dataset(dataset, ["access_entries"]) # API request - - assert entry in dataset.access_entries - # [END bigquery_update_dataset_access] - - -def test_delete_dataset(client): - """Delete a dataset.""" - from google.cloud.exceptions import NotFound - - dataset1_id = "delete_dataset_{}".format(_millis()) - dataset1 = bigquery.Dataset(client.dataset(dataset1_id)) - client.create_dataset(dataset1) - - dataset2_id = "delete_dataset_with_tables{}".format(_millis()) - dataset2 = bigquery.Dataset(client.dataset(dataset2_id)) - client.create_dataset(dataset2) - - table = bigquery.Table(dataset2.table("new_table")) - client.create_table(table) - - # [START bigquery_delete_dataset] - # from google.cloud import bigquery - # client = bigquery.Client() - - # Delete a dataset that does not contain any tables - # dataset1_id = 'my_empty_dataset' - dataset1_ref = client.dataset(dataset1_id) - client.delete_dataset(dataset1_ref) # API request - - print("Dataset {} deleted.".format(dataset1_id)) - - # Use the delete_contents parameter to delete a dataset and its contents - # dataset2_id = 'my_dataset_with_tables' - dataset2_ref = client.dataset(dataset2_id) - client.delete_dataset(dataset2_ref, delete_contents=True) # API request - - print("Dataset {} deleted.".format(dataset2_id)) - # [END bigquery_delete_dataset] - - for dataset in [dataset1, dataset2]: - with pytest.raises(NotFound): - client.get_dataset(dataset) # API request - - def test_list_tables(client, to_delete): """List tables within a dataset.""" dataset_id = "list_tables_dataset_{}".format(_millis()) diff --git a/bigquery/docs/usage/datasets.rst b/bigquery/docs/usage/datasets.rst index dbcd834d5cd3..d5646355c00d 100644 --- a/bigquery/docs/usage/datasets.rst +++ b/bigquery/docs/usage/datasets.rst @@ -19,7 +19,7 @@ Listing Datasets List datasets for a project with the :func:`~google.cloud.bigquery.client.Client.list_datasets` method: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/list_datasets.py :language: python :dedent: 4 :start-after: [START bigquery_list_datasets] @@ -31,7 +31,7 @@ Getting a Dataset Get a dataset resource (to pick up changes made by another client) with the :func:`~google.cloud.bigquery.client.Client.get_dataset` method: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/get_dataset.py :language: python :dedent: 4 :start-after: [START bigquery_get_dataset] @@ -55,7 +55,7 @@ Updating a Dataset Update a property in a dataset's metadata with the :func:`~google.cloud.bigquery.client.Client.update_dataset` method: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/update_dataset_description.py :language: python :dedent: 4 :start-after: [START bigquery_update_dataset_description] @@ -64,7 +64,7 @@ Update a property in a dataset's metadata with the Modify user permissions on a dataset with the :func:`~google.cloud.bigquery.client.Client.update_dataset` method: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/update_dataset_access.py :language: python :dedent: 4 :start-after: [START bigquery_update_dataset_access] @@ -76,7 +76,7 @@ Deleting a Dataset Delete a dataset with the :func:`~google.cloud.bigquery.client.Client.delete_dataset` method: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/delete_dataset.py :language: python :dedent: 4 :start-after: [START bigquery_delete_dataset] diff --git a/bigquery/samples/delete_dataset.py b/bigquery/samples/delete_dataset.py new file mode 100644 index 000000000000..ad04c3fb3664 --- /dev/null +++ b/bigquery/samples/delete_dataset.py @@ -0,0 +1,32 @@ +# 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_dataset(client, dataset_id): + + # [START bigquery_delete_dataset] + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + # TODO(developer): Set model_id to the ID of the model to fetch. + # dataset_id = 'your-project.your_dataset' + + # 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)) diff --git a/bigquery/samples/get_dataset.py b/bigquery/samples/get_dataset.py new file mode 100644 index 000000000000..eeab2e088d2f --- /dev/null +++ b/bigquery/samples/get_dataset.py @@ -0,0 +1,56 @@ +# 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_dataset(client, dataset_id): + + # [START bigquery_get_dataset] + 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 to fetch. + # dataset_id = 'your-project.your_dataset' + + dataset = client.get_dataset(dataset_id) + + full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id) + friendly_name = dataset.friendly_name + print( + "Got dataset '{}' with friendly_name '{}'.".format( + full_dataset_id, friendly_name + ) + ) + + # View dataset properties + print("Description: {}".format(dataset.description)) + print("Labels:") + labels = dataset.labels + if labels: + for label, value in labels.items(): + print("\t{}: {}".format(label, value)) + else: + print("\tDataset has no labels defined.") + + # View tables in dataset + print("Tables:") + tables = list(client.list_tables(dataset)) # API request(s) + if tables: + for table in tables: + print("\t{}".format(table.table_id)) + else: + print("\tThis dataset does not contain any tables.") + + # [END bigquery_get_dataset] diff --git a/bigquery/samples/list_datasets.py b/bigquery/samples/list_datasets.py new file mode 100644 index 000000000000..c9ddf4f2523c --- /dev/null +++ b/bigquery/samples/list_datasets.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_datasets(client): + + # [START bigquery_list_datasets] + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + datasets = list(client.list_datasets()) + project = client.project + + if datasets: + print("Datasets in project {}:".format(project)) + for dataset in datasets: # API request(s) + print("\t{}".format(dataset.dataset_id)) + else: + print("{} project does not contain any datasets.".format(project)) + # [END bigquery_list_datasets] diff --git a/bigquery/samples/tests/test_dataset_samples.py b/bigquery/samples/tests/test_create_dataset.py similarity index 100% rename from bigquery/samples/tests/test_dataset_samples.py rename to bigquery/samples/tests/test_create_dataset.py diff --git a/bigquery/samples/tests/test_delete_dataset.py b/bigquery/samples/tests/test_delete_dataset.py new file mode 100644 index 000000000000..2b1b6ad06195 --- /dev/null +++ b/bigquery/samples/tests/test_delete_dataset.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_dataset + + +def test_delete_dataset(capsys, client, dataset_id): + + delete_dataset.delete_dataset(client, dataset_id) + out, err = capsys.readouterr() + assert "Deleted dataset '{}'.".format(dataset_id) in out diff --git a/bigquery/samples/tests/test_get_dataset.py b/bigquery/samples/tests/test_get_dataset.py new file mode 100644 index 000000000000..374f8835211a --- /dev/null +++ b/bigquery/samples/tests/test_get_dataset.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 get_dataset + + +def test_get_dataset(capsys, client, dataset_id): + + get_dataset.get_dataset(client, dataset_id) + out, err = capsys.readouterr() + assert "{}".format(dataset_id) in out diff --git a/bigquery/samples/tests/test_list_datasets.py b/bigquery/samples/tests/test_list_datasets.py new file mode 100644 index 000000000000..4c66a24f9b1a --- /dev/null +++ b/bigquery/samples/tests/test_list_datasets.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 list_datasets + + +def test_list_datasets(capsys, client, dataset_id): + + list_datasets.list_datasets(client) + out, err = capsys.readouterr() + assert "Datasets in project {}:".format(client.project) in out diff --git a/bigquery/samples/tests/test_update_dataset_access.py b/bigquery/samples/tests/test_update_dataset_access.py new file mode 100644 index 000000000000..ae33dbfe4a4c --- /dev/null +++ b/bigquery/samples/tests/test_update_dataset_access.py @@ -0,0 +1,24 @@ +# 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 update_dataset_access + + +def test_update_dataset_access(capsys, client, dataset_id): + + update_dataset_access.update_dataset_access(client, dataset_id) + out, err = capsys.readouterr() + assert ( + "Updated dataset '{}' with modified user permissions.".format(dataset_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 new file mode 100644 index 000000000000..0366b767fbe8 --- /dev/null +++ b/bigquery/samples/tests/test_update_dataset_default_table_expiration.py @@ -0,0 +1,29 @@ +# 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 update_dataset_default_table_expiration + + +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 + ) + out, err = capsys.readouterr() + assert ( + "Updated dataset {} with new expiration {}".format(dataset_id, one_day_ms) + in out + ) diff --git a/bigquery/samples/tests/test_update_dataset_description.py b/bigquery/samples/tests/test_update_dataset_description.py new file mode 100644 index 000000000000..c6f8889f50da --- /dev/null +++ b/bigquery/samples/tests/test_update_dataset_description.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 update_dataset_description + + +def test_update_dataset_description(capsys, client, dataset_id): + + update_dataset_description.update_dataset_description(client, dataset_id) + out, err = capsys.readouterr() + assert "Updated description." in out diff --git a/bigquery/samples/update_dataset_access.py b/bigquery/samples/update_dataset_access.py new file mode 100644 index 000000000000..aa316a38dff9 --- /dev/null +++ b/bigquery/samples/update_dataset_access.py @@ -0,0 +1,45 @@ +# 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 update_dataset_access(client, dataset_id): + + # [START bigquery_update_dataset_access] + 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 to fetch. + # dataset_id = 'your-project.your_dataset' + + dataset = client.get_dataset(dataset_id) + + entry = bigquery.AccessEntry( + role="READER", + entity_type="userByEmail", + entity_id="sample.bigquery.dev@gmail.com", + ) + + entries = list(dataset.access_entries) + entries.append(entry) + dataset.access_entries = entries + + dataset = client.update_dataset(dataset, ["access_entries"]) # API request + + full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id) + print( + "Updated dataset '{}' with modified user permissions.".format(full_dataset_id) + ) + # [END bigquery_update_dataset_access] diff --git a/bigquery/samples/update_dataset_default_table_expiration.py b/bigquery/samples/update_dataset_default_table_expiration.py new file mode 100644 index 000000000000..a5ac38c01a99 --- /dev/null +++ b/bigquery/samples/update_dataset_default_table_expiration.py @@ -0,0 +1,40 @@ +# 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 update_dataset_default_table_expiration(client, dataset_id): + + # [START bigquery_update_dataset_expiration] + 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 to fetch. + # dataset_id = 'your-project.your_dataset' + + dataset = client.get_dataset(dataset_id) + dataset.default_table_expiration_ms = 24 * 60 * 60 * 1000 # in milliseconds + + dataset = client.update_dataset( + dataset, ["default_table_expiration_ms"] + ) # API request + + full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id) + print( + "Updated dataset {} with new expiration {}".format( + full_dataset_id, dataset.default_table_expiration_ms + ) + ) + # [END bigquery_update_dataset_expiration] diff --git a/bigquery/samples/update_dataset_description.py b/bigquery/samples/update_dataset_description.py new file mode 100644 index 000000000000..70be80b7507e --- /dev/null +++ b/bigquery/samples/update_dataset_description.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 update_dataset_description(client, dataset_id): + + # [START bigquery_update_dataset_description] + 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 to fetch. + # dataset_id = 'your-project.your_dataset' + + dataset = client.get_dataset(dataset_id) + dataset.description = "Updated description." + dataset = client.update_dataset(dataset, ["description"]) + + full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id) + print( + "Updated dataset '{}' with description '{}'.".format( + full_dataset_id, dataset.description + ) + ) + # [END bigquery_update_dataset_description] From 8724e8779d5019d9411d88955b59f48759208fb5 Mon Sep 17 00:00:00 2001 From: lbristol88 Date: Wed, 10 Apr 2019 10:09:45 -0700 Subject: [PATCH 4/4] 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 --- bigquery/docs/snippets.py | 90 ------------------- bigquery/docs/usage/tables.rst | 6 +- bigquery/samples/delete_dataset.py | 2 +- bigquery/samples/delete_model.py | 3 +- bigquery/samples/delete_table.py | 31 +++++++ bigquery/samples/get_table.py | 37 ++++++++ bigquery/samples/list_tables.py | 33 +++++++ bigquery/samples/tests/conftest.py | 13 +++ ..._table_samples.py => test_create_table.py} | 0 bigquery/samples/tests/test_delete_table.py | 22 +++++ bigquery/samples/tests/test_get_table.py | 35 ++++++++ bigquery/samples/tests/test_list_tables.py | 23 +++++ ...update_dataset_default_table_expiration.py | 2 +- 13 files changed, 200 insertions(+), 97 deletions(-) create mode 100644 bigquery/samples/delete_table.py create mode 100644 bigquery/samples/get_table.py create mode 100644 bigquery/samples/list_tables.py rename bigquery/samples/tests/{test_table_samples.py => test_create_table.py} (100%) create mode 100644 bigquery/samples/tests/test_delete_table.py create mode 100644 bigquery/samples/tests/test_get_table.py create mode 100644 bigquery/samples/tests/test_list_tables.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 58bd6689c80f..01ad3e014608 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 0da02a173828..4aede9545cd8 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 ad04c3fb3664..58851f1e2120 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 dfe23cd7ef29..371f9003576b 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 000000000000..3eb7dc918da7 --- /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 000000000000..e6a5c502e2b3 --- /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 000000000000..33ed408906b0 --- /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 ed797e56310b..629b23473b01 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 000000000000..8f4796623a83 --- /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 000000000000..debf1b63a3fc --- /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 000000000000..ec1621ac7579 --- /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 0366b767fbe8..46e9654209ed 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 (