From 4a1f061429fa7e3379bb45b3d165fb31968903e6 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Thu, 27 Oct 2016 10:52:34 -0700 Subject: [PATCH 1/2] Add missing bigquery samples Change-Id: Ibb5125cad43385cec2b08fc9512e7ded32c24855 --- bigquery/cloud-client/snippets.py | 46 +++++++++++++++++++++++--- bigquery/cloud-client/snippets_test.py | 26 +++++++++++++-- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/bigquery/cloud-client/snippets.py b/bigquery/cloud-client/snippets.py index 0e1f5d4b5a87..93065069d776 100644 --- a/bigquery/cloud-client/snippets.py +++ b/bigquery/cloud-client/snippets.py @@ -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(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) @@ -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. @@ -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') @@ -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': diff --git a/bigquery/cloud-client/snippets_test.py b/bigquery/cloud-client/snippets_test.py index 35f79af7b57b..af368d9a4a2a 100644 --- a/bigquery/cloud-client/snippets_test.py +++ b/bigquery/cloud-client/snippets_test.py @@ -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. @@ -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) From aacbd49519e33ea36226ad22ffa1024bc1809722 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Thu, 27 Oct 2016 12:15:46 -0700 Subject: [PATCH 2/2] Fix typo Change-Id: Ic2b0beac2b32f28353cc143b47d45932b0fdaf02 --- bigquery/cloud-client/snippets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigquery/cloud-client/snippets.py b/bigquery/cloud-client/snippets.py index 93065069d776..f7ab33731058 100644 --- a/bigquery/cloud-client/snippets.py +++ b/bigquery/cloud-client/snippets.py @@ -47,7 +47,7 @@ def list_projects(): break for project in projects: - print(projects.name) + print(project.project_id) def list_datasets(project=None):