From 2616602e540aae99e985311a550a52fe0e19d714 Mon Sep 17 00:00:00 2001 From: Layla Bristol Date: Fri, 29 Mar 2019 11:14:28 -0700 Subject: [PATCH 1/7] Added client method (create table) for Tables API --- bigquery/samples/create_table.py | 40 ++++++++++++++++++++ bigquery/samples/tests/conftest.py | 16 ++++++++ bigquery/samples/tests/test_table_samples.py | 25 ++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 bigquery/samples/create_table.py create mode 100644 bigquery/samples/tests/test_table_samples.py diff --git a/bigquery/samples/create_table.py b/bigquery/samples/create_table.py new file mode 100644 index 000000000000..bf8bbdf1c965 --- /dev/null +++ b/bigquery/samples/create_table.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 create_table(): + + # [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 project, dataset, and table IDs + # project_id = "your-project" + # dataset_id = "your_dataset" + # table_id = "table_name" + + table = bigquery.Table( + "{}.{}.{}".format(project_id, dataset_id, table_id), schema=schema + ) + + table = client.create_table(table) # API request + print("Created table {}".format(table.full_table_id)) + # [END bigquery_create_table] diff --git a/bigquery/samples/tests/conftest.py b/bigquery/samples/tests/conftest.py index 1543e1fdcd0a..262e43854083 100644 --- a/bigquery/samples/tests/conftest.py +++ b/bigquery/samples/tests/conftest.py @@ -25,6 +25,22 @@ def client(): return bigquery.Client() +@pytest.fixture +def project_id(client): + return client.project + + +@pytest.fixture +def table_id(client, dataset_id): + now = datetime.datetime.now() + table_id = "example_table_{}_{}".format( + now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] + ) + table = client.create_table("{}.{}".format(dataset_id, table_id)) + yield "{}".format(table.full_table_id) + client.delete_table(table, delete_contents=True) + + @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..c4c03f05dc94 --- /dev/null +++ b/bigquery/samples/tests/test_table_samples.py @@ -0,0 +1,25 @@ +# 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_table_samples(capsys, client, project_id, dataset_id, table_id): + """Since creating a table is a long operation, test all table model samples + in the same test, following a typical end-to-end flow. + """ + create_table.create_table(client, project_id, dataset_id, table_id) + out, err = capsys.readouterr() + assert create_table in out + assert table.table_id == "my_table" From b3f61b9dd3be4cc9212aa1b3201e2cf4a0b46d83 Mon Sep 17 00:00:00 2001 From: Layla Bristol Date: Mon, 1 Apr 2019 21:21:03 -0700 Subject: [PATCH 2/7] Updated create table to address review feedback. --- bigquery/samples/create_table.py | 17 +++++++---------- bigquery/samples/tests/conftest.py | 8 +++----- bigquery/samples/tests/test_table_samples.py | 7 +++---- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/bigquery/samples/create_table.py b/bigquery/samples/create_table.py index bf8bbdf1c965..5e2e34d41d99 100644 --- a/bigquery/samples/create_table.py +++ b/bigquery/samples/create_table.py @@ -13,7 +13,7 @@ # limitations under the License. -def create_table(): +def create_table(client, table_id): # [START bigquery_create_table] from google.cloud import bigquery @@ -26,15 +26,12 @@ def create_table(): # TODO(developer): Construct a BigQuery client object. # client = bigquery.Client() - # TODO(developer): Set project, dataset, and table IDs - # project_id = "your-project" - # dataset_id = "your_dataset" - # table_id = "table_name" - - table = bigquery.Table( - "{}.{}.{}".format(project_id, dataset_id, table_id), schema=schema - ) + # 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.full_table_id)) + 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 262e43854083..abc238b97ea2 100644 --- a/bigquery/samples/tests/conftest.py +++ b/bigquery/samples/tests/conftest.py @@ -31,14 +31,12 @@ def project_id(client): @pytest.fixture -def table_id(client, dataset_id): +def random_table_id(client, dataset_id): now = datetime.datetime.now() - table_id = "example_table_{}_{}".format( + random_table_id = "example_table_{}_{}".format( now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] ) - table = client.create_table("{}.{}".format(dataset_id, table_id)) - yield "{}".format(table.full_table_id) - client.delete_table(table, delete_contents=True) + yield "{}.{}".format(dataset_id, random_table_id) @pytest.fixture diff --git a/bigquery/samples/tests/test_table_samples.py b/bigquery/samples/tests/test_table_samples.py index c4c03f05dc94..153384700d27 100644 --- a/bigquery/samples/tests/test_table_samples.py +++ b/bigquery/samples/tests/test_table_samples.py @@ -15,11 +15,10 @@ from .. import create_table -def test_table_samples(capsys, client, project_id, dataset_id, table_id): +def test_table_samples(capsys, client, dataset_id, random_table_id): """Since creating a table is a long operation, test all table model samples in the same test, following a typical end-to-end flow. """ - create_table.create_table(client, project_id, dataset_id, table_id) + create_table.create_table(client, random_table_id) out, err = capsys.readouterr() - assert create_table in out - assert table.table_id == "my_table" + assert "Created table {}".format(random_table_id) in out From eed701ff2a0ce491f3b58f359b11651ba5aaf242 Mon Sep 17 00:00:00 2001 From: Layla Bristol Date: Mon, 1 Apr 2019 21:23:41 -0700 Subject: [PATCH 3/7] Updated to fix typo in test docstring. --- bigquery/samples/tests/test_table_samples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigquery/samples/tests/test_table_samples.py b/bigquery/samples/tests/test_table_samples.py index 153384700d27..4b936c6b91f0 100644 --- a/bigquery/samples/tests/test_table_samples.py +++ b/bigquery/samples/tests/test_table_samples.py @@ -16,7 +16,7 @@ def test_table_samples(capsys, client, dataset_id, random_table_id): - """Since creating a table is a long operation, test all table model samples + """Since creating a table is a long operation, test all table samples in the same test, following a typical end-to-end flow. """ create_table.create_table(client, random_table_id) From 19124c00a1c9c6ade9435e083013026a24af30fb Mon Sep 17 00:00:00 2001 From: Layla Bristol Date: Mon, 1 Apr 2019 21:32:25 -0700 Subject: [PATCH 4/7] Removed unneeded parameter. --- bigquery/samples/tests/test_table_samples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigquery/samples/tests/test_table_samples.py b/bigquery/samples/tests/test_table_samples.py index 4b936c6b91f0..35b69cc39aa2 100644 --- a/bigquery/samples/tests/test_table_samples.py +++ b/bigquery/samples/tests/test_table_samples.py @@ -15,7 +15,7 @@ from .. import create_table -def test_table_samples(capsys, client, dataset_id, random_table_id): +def test_table_samples(capsys, client, random_table_id): """Since creating a table is a long operation, test all table samples in the same test, following a typical end-to-end flow. """ From 7b35b526c88bb9e7a67b0f13be8695ca9303461e Mon Sep 17 00:00:00 2001 From: Layla Bristol Date: Tue, 2 Apr 2019 13:19:41 -0700 Subject: [PATCH 5/7] Removed test_create_table from snippets collection --- bigquery/docs/samples | 1 + bigquery/docs/snippets.py | 25 ------------------------- bigquery/docs/usage/tables.rst | 2 +- 3 files changed, 2 insertions(+), 26 deletions(-) create mode 120000 bigquery/docs/samples 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] From 64592ba5f5858b1ba5e011afbd9967eca36edde4 Mon Sep 17 00:00:00 2001 From: Layla Bristol Date: Tue, 2 Apr 2019 13:25:34 -0700 Subject: [PATCH 6/7] Removed unnecessary fixture, docstring, and made changes as directed. --- bigquery/samples/tests/conftest.py | 9 ++------- bigquery/samples/tests/test_table_samples.py | 6 ++---- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/bigquery/samples/tests/conftest.py b/bigquery/samples/tests/conftest.py index abc238b97ea2..cdd2f68f5b96 100644 --- a/bigquery/samples/tests/conftest.py +++ b/bigquery/samples/tests/conftest.py @@ -25,18 +25,13 @@ def client(): return bigquery.Client() -@pytest.fixture -def project_id(client): - return client.project - - @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] ) - yield "{}.{}".format(dataset_id, random_table_id) + return "{}.{}".format(dataset_id, random_table_id) @pytest.fixture @@ -46,7 +41,7 @@ def dataset_id(client): now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] ) dataset = client.create_dataset(dataset_id) - yield "{}.{}".format(dataset.project, dataset.dataset_id) + return "{}.{}".format(dataset.project, dataset.dataset_id) client.delete_dataset(dataset, delete_contents=True) diff --git a/bigquery/samples/tests/test_table_samples.py b/bigquery/samples/tests/test_table_samples.py index 35b69cc39aa2..903f76b536ea 100644 --- a/bigquery/samples/tests/test_table_samples.py +++ b/bigquery/samples/tests/test_table_samples.py @@ -15,10 +15,8 @@ from .. import create_table -def test_table_samples(capsys, client, random_table_id): - """Since creating a table is a long operation, test all table samples - in the same test, following a typical end-to-end flow. - """ +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 cd66b9a58c391ecb12d386b45bce446c5faee4f1 Mon Sep 17 00:00:00 2001 From: Layla Bristol Date: Tue, 2 Apr 2019 13:43:31 -0700 Subject: [PATCH 7/7] Updated dataset fixture to yield instead of return. --- bigquery/samples/tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigquery/samples/tests/conftest.py b/bigquery/samples/tests/conftest.py index cdd2f68f5b96..ca7e9081a416 100644 --- a/bigquery/samples/tests/conftest.py +++ b/bigquery/samples/tests/conftest.py @@ -41,7 +41,7 @@ def dataset_id(client): now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] ) dataset = client.create_dataset(dataset_id) - return "{}.{}".format(dataset.project, dataset.dataset_id) + yield "{}.{}".format(dataset.project, dataset.dataset_id) client.delete_dataset(dataset, delete_contents=True)