Skip to content

Commit

Permalink
Add missing bigquery samples [(googleapis#622)](GoogleCloudPlatform/p…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott authored and shollyman committed Jul 22, 2020
1 parent 7bb4ca4 commit 1e1afe1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
46 changes: 42 additions & 4 deletions samples/snippets/snippets.py
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(
page_token=page_token)
projects.extend(results)

if not page_token:
break

for project in projects:
print(project.project_id)


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 samples/snippets/snippets_test.py
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

0 comments on commit 1e1afe1

Please sign in to comment.