Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added BigQuery client method (create table) for Tables API #3

Merged
merged 8 commits into from
Apr 2, 2019
17 changes: 7 additions & 10 deletions bigquery/samples/create_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
8 changes: 3 additions & 5 deletions bigquery/samples/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, since you don't have to do any cleanup, this can now be return instead of yield.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced both table and dataset fixtures to return instead of yield.



@pytest.fixture
Expand Down
9 changes: 4 additions & 5 deletions bigquery/samples/tests/test_table_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
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
def test_table_samples(capsys, client, random_table_id):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can be renamed to test_create_table.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the name as directed.

"""Since creating a table is a long operation, test all table samples
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a table isn't all that long of an operation. You can test each sample separately. This docstring comment can be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this docstring.

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