From b4fd4bf0bc8600a75c22e358ca3d1c2358793474 Mon Sep 17 00:00:00 2001 From: Jared Chapman Date: Tue, 20 Dec 2022 13:50:14 -0600 Subject: [PATCH 1/2] docs: revised create_partitioned_table sample --- samples/snippets/create_partitioned_table.py | 40 +++++++++++++++++++ .../snippets/create_partitioned_table_test.py | 36 +++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 samples/snippets/create_partitioned_table.py create mode 100644 samples/snippets/create_partitioned_table_test.py diff --git a/samples/snippets/create_partitioned_table.py b/samples/snippets/create_partitioned_table.py new file mode 100644 index 000000000..2ad3bc3f2 --- /dev/null +++ b/samples/snippets/create_partitioned_table.py @@ -0,0 +1,40 @@ +# Copyright 2022 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_partitioned_table(client, to_delete): + + #[START bigquery_create_table_partitioned] + from google.cloud import bigquery + + client = bigquery.Client() + + table_id = "your-project.your_dataset.your_table_name" + schema = [ + bigquery.SchemaField("name", "STRING"), + bigquery.SchemaField("post_abbr", "STRING"), + bigquery.SchemaField("date", "DATE"), + ] + table = bigquery.Table(table_id, schema=schema) + table.time_partitioning = bigquery.TimePartitioning( + type_=bigquery.TimePartitioningType.DAY, + field="date", # name of column to use for partitioning + expiration_ms=1000 * 60 * 60 * 24 * 90, + ) # 90 days + + table = client.create_table(table) + + print(f"Created table {table.table_id}, partitioned on column {table.time_partitioning.field}.") + # [END bigquery_create_table_partitioned] + + \ No newline at end of file diff --git a/samples/snippets/create_partitioned_table_test.py b/samples/snippets/create_partitioned_table_test.py new file mode 100644 index 000000000..3c75aed75 --- /dev/null +++ b/samples/snippets/create_partitioned_table_test.py @@ -0,0 +1,36 @@ +# Copyright 2022 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. + +import typing + +import create_partitioned_table + +if typing.TYPE_CHECKING: + import pathlib + + import pytest + + +def test_create_partitioned_table( + capsys: "pytest.CaptureFixture[str]", + table_id: str, + tmp_path: "pathlib.Path", +) -> None: + schema_path = str(tmp_path / "test_schema.json") + + create_partitioned_table.create_partitioned_table(client, to_delete) + + assert table.time_partitioning.type_ == "DAY" + assert table.time_partitioning.field == "date" + assert table.time_partitioning.expiration_ms == 7776000000 From ead525adf2bc5b8e286d6098cae9a6c68f782bd5 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Thu, 5 Oct 2023 12:29:09 -0500 Subject: [PATCH 2/2] update sample tests to use correct fixture --- docs/snippets.py | 2 ++ samples/snippets/create_partitioned_table.py | 21 ++++++++++++------- .../snippets/create_partitioned_table_test.py | 12 +++++------ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/docs/snippets.py b/docs/snippets.py index 3a46cd36c..7f9b4f59e 100644 --- a/docs/snippets.py +++ b/docs/snippets.py @@ -125,6 +125,8 @@ def test_create_partitioned_table(client, to_delete): dataset = client.create_dataset(dataset_ref) to_delete.append(dataset) + # TODO(tswast): remove this snippet once cloud.google.com is updated to use + # samples/snippets/create_partitioned_table.py # [START bigquery_create_table_partitioned] # from google.cloud import bigquery # client = bigquery.Client() diff --git a/samples/snippets/create_partitioned_table.py b/samples/snippets/create_partitioned_table.py index 2ad3bc3f2..0277d7d0f 100644 --- a/samples/snippets/create_partitioned_table.py +++ b/samples/snippets/create_partitioned_table.py @@ -12,14 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -def create_partitioned_table(client, to_delete): - - #[START bigquery_create_table_partitioned] + +def create_partitioned_table(table_id): + your_fully_qualified_table_id = table_id + + # [START bigquery_create_table_partitioned] from google.cloud import bigquery - + client = bigquery.Client() - table_id = "your-project.your_dataset.your_table_name" + # Use format "your-project.your_dataset.your_table_name" for table_id + table_id = your_fully_qualified_table_id schema = [ bigquery.SchemaField("name", "STRING"), bigquery.SchemaField("post_abbr", "STRING"), @@ -34,7 +37,9 @@ def create_partitioned_table(client, to_delete): table = client.create_table(table) - print(f"Created table {table.table_id}, partitioned on column {table.time_partitioning.field}.") + print( + f"Created table {table.project}.{table.dataset_id}.{table.table_id}, " + f"partitioned on column {table.time_partitioning.field}." + ) # [END bigquery_create_table_partitioned] - - \ No newline at end of file + return table diff --git a/samples/snippets/create_partitioned_table_test.py b/samples/snippets/create_partitioned_table_test.py index 3c75aed75..0f684fcb0 100644 --- a/samples/snippets/create_partitioned_table_test.py +++ b/samples/snippets/create_partitioned_table_test.py @@ -17,20 +17,18 @@ import create_partitioned_table if typing.TYPE_CHECKING: - import pathlib - import pytest def test_create_partitioned_table( capsys: "pytest.CaptureFixture[str]", - table_id: str, - tmp_path: "pathlib.Path", + random_table_id: str, ) -> None: - schema_path = str(tmp_path / "test_schema.json") + table = create_partitioned_table.create_partitioned_table(random_table_id) - create_partitioned_table.create_partitioned_table(client, to_delete) + out, _ = capsys.readouterr() + assert "Created" in out + assert random_table_id in out assert table.time_partitioning.type_ == "DAY" assert table.time_partitioning.field == "date" - assert table.time_partitioning.expiration_ms == 7776000000