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

Add missing bigquery samples #622

Merged
merged 2 commits into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions bigquery/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,27 @@


def list_projects():
raise NotImplementedError(
'https://github.com/GoogleCloudPlatform/gcloud-python/issues/2143')
bigquery_client = bigquery.Client()

projects = []
page_token = None

while True:
results, page_token = bigquery_client.list_projects(
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this not using the new iterator thingy? I thought there was a .pages or something now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not yet released.

page_token=page_token)
projects.extend(results)

if not page_token:
break

for project in projects:
print(projects.name)


def list_datasets(project=None):
"""Lists all datasets in a given project.

If no project is specified, then the currently active project is used
If no project is specified, then the currently active project is used.
"""
bigquery_client = bigquery.Client(project=project)

Expand All @@ -59,6 +72,20 @@ def list_datasets(project=None):
print(dataset.name)


def create_dataset(dataset_name, project=None):
"""Craetes a dataset in a given project.

If no project is specified, then the currently active project is used.
"""
bigquery_client = bigquery.Client(project=project)

dataset = bigquery_client.dataset(dataset_name)

dataset.create()

print('Created dataset {}.'.format(dataset_name))


def list_tables(dataset_name, project=None):
"""Lists all of the tables in a given dataset.

Expand Down Expand Up @@ -221,9 +248,16 @@ def delete_table(dataset_name, table_name, project=None):

subparsers = parser.add_subparsers(dest='command')

list_projects_parser = subparsers.add_parser(
'list-projects', help=list_projects.__doc__)

list_datasets_parser = subparsers.add_parser(
'list-datasets', help=list_datasets.__doc__)

create_dataset_parser = subparsers.add_parser(
'list-datasets', help=list_datasets.__doc__)
create_dataset_parser.add_argument('dataset_name')

list_tables_parser = subparsers.add_parser(
'list-tables', help=list_tables.__doc__)
list_tables_parser.add_argument('dataset_name')
Expand Down Expand Up @@ -251,8 +285,12 @@ def delete_table(dataset_name, table_name, project=None):

args = parser.parse_args()

if args.command == 'list-datasets':
if args.command == 'list-projects':
list_projects()
elif args.command == 'list-datasets':
list_datasets(args.project)
elif args.command == 'create-dataset':
create_dataset(args.dataset_name, args.project)
elif args.command == 'list-tables':
list_tables(args.dataset_name, args.project)
elif args.command == 'create-table':
Expand Down
26 changes: 23 additions & 3 deletions bigquery/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
TABLE_ID = 'test_table'


@pytest.mark.xfail(
strict=True,
reason='https://github.com/GoogleCloudPlatform/gcloud-python/issues/2143')
def test_list_projects():
snippets.list_projects()
# No need to check the ouput, lack of exception is enough.
Expand All @@ -39,6 +36,29 @@ def test_list_datasets(capsys):
assert DATASET_ID in out


@pytest.fixture
def cleanup_dataset():
dataset_name = 'test_temporary_dataset'
bigquery_client = bigquery.Client()
dataset = bigquery_client.dataset(dataset_name)

if dataset.exists():
dataset.delete()

yield dataset_name

if dataset.exists():
dataset.delete()


def test_create_dataset(capsys, cleanup_dataset):
snippets.create_dataset(cleanup_dataset)

out, _ = capsys.readouterr()

assert cleanup_dataset in out


def test_list_tables(capsys):
# Requires the dataset and table to have been created in the test project.
snippets.list_tables(DATASET_ID)
Expand Down