Skip to content

Commit

Permalink
Move code sample for create dataset to samples directory (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbristol88 authored and tswast committed Apr 9, 2019
1 parent 5f0094c commit 510a68b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 29 deletions.
27 changes: 0 additions & 27 deletions bigquery/docs/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion bigquery/docs/usage/datasets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
38 changes: 38 additions & 0 deletions bigquery/samples/create_dataset.py
Original file line number Diff line number Diff line change
@@ -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]
12 changes: 11 additions & 1 deletion bigquery/samples/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions bigquery/samples/tests/test_dataset_samples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .. import 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

0 comments on commit 510a68b

Please sign in to comment.