Move code sample for create dataset to samples directory#5
Move code sample for create dataset to samples directory#5tswast merged 6 commits intotswast:bq-snippetsfrom
Conversation
| # TODO(developer): Set dataset_id to the ID of the dataset to create | ||
| # dataset_id = "your-project.your_dataset" | ||
|
|
||
| dataset = bigquery.Dataset(dataset_id) |
There was a problem hiding this comment.
Please demonstrate setting a location, as is done in the existing snippet.
| # dataset_id = "your-project.your_dataset" | ||
|
|
||
| dataset = bigquery.Dataset(dataset_id) | ||
| dataset = client.create_dataset(dataset) # API request |
There was a problem hiding this comment.
Please include the additional comment about the Conflict exception from the existing snippet.
There was a problem hiding this comment.
Added the original Conflict exception comment.
bigquery/samples/tests/conftest.py
Outdated
| random_dataset_id = "example_dataset_{}_{}".format( | ||
| now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] | ||
| ) | ||
| return "{}.{}".format(client.project, random_dataset_id) |
There was a problem hiding this comment.
You'll probably notice that a dataset is created but not deleted when you run these tests. Our tests should clean up after themselves. In this case, we should yield the ID, and then afterwards delete the dataset.
Set both delete_contents=True and not_found_ok=True when you call delete_dataset so that this fixture can be used in samples that create tables within the dataset or don't create a dataset at all (such as if there was a test failure).
There was a problem hiding this comment.
Changed return to yield and added the cleanup. Also added the delete_contents=True and not_found_ok=True as directed.
In addition, applied not_found_ok=True to the dataset_id fixture for consistency. Please let me know if it should be removed!
There was a problem hiding this comment.
Great, thank you. That will come in handy for the "delete dataset" snippets.
bigquery/samples/create_dataset.py
Outdated
| dataset = bigquery.Dataset(dataset_id) | ||
|
|
||
| # TODO(developer): Specify the geographic location where the dataset should reside. | ||
| # dataset.location = "US" |
There was a problem hiding this comment.
This doesn't need to be a comment. Specifying an actual location is better, since we can do it. Comments are a last resort (such as when we need to generate a random ID for testing)
bigquery/samples/tests/conftest.py
Outdated
| random_dataset_id = "example_dataset_{}_{}".format( | ||
| now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] | ||
| ) | ||
| return "{}.{}".format(client.project, random_dataset_id) |
There was a problem hiding this comment.
Great, thank you. That will come in handy for the "delete dataset" snippets.
bigquery/samples/tests/conftest.py
Outdated
| client.delete_dataset(random_dataset, delete_contents=True, not_found_ok=True) | ||
| dataset = client.create_dataset(random_dataset_id) | ||
| client.delete_dataset(dataset, delete_contents=True, not_found_ok=True) | ||
| yield "{}.{}".format(client.project, dataset.dataset_id) |
There was a problem hiding this comment.
Delete needs to come after yield.
Learn more about what this is doing here: https://docs.pytest.org/en/latest/fixture.html#fixture-finalization-executing-teardown-code
There was a problem hiding this comment.
I see you reverted this change, thanks. I do recommend reading that pytest documentation section to understand fixtures better.
tswast
left a comment
There was a problem hiding this comment.
LGTM, thanks!
I've verified that the docs build with nox -s docs.
@engelke