diff --git a/samples/snippets/snippets.py b/samples/snippets/snippets.py index 0e1f5d4b5..f7ab33731 100644 --- a/samples/snippets/snippets.py +++ b/samples/snippets/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(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) @@ -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/samples/snippets/snippets_test.py b/samples/snippets/snippets_test.py index 35f79af7b..af368d9a4 100644 --- a/samples/snippets/snippets_test.py +++ b/samples/snippets/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)