Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d7dc578

Browse filesBrowse files
author
Jon Wayne Parrott
authored
Add missing bigquery samples (GoogleCloudPlatform#622)
1 parent aac0e02 commit d7dc578
Copy full SHA for d7dc578

File tree

Expand file treeCollapse file tree

2 files changed

+65
-7
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+65
-7
lines changed

‎bigquery/cloud-client/snippets.py

Copy file name to clipboardExpand all lines: bigquery/cloud-client/snippets.py
+42-4Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,27 @@
3333

3434

3535
def list_projects():
36-
raise NotImplementedError(
37-
'https://github.com/GoogleCloudPlatform/gcloud-python/issues/2143')
36+
bigquery_client = bigquery.Client()
37+
38+
projects = []
39+
page_token = None
40+
41+
while True:
42+
results, page_token = bigquery_client.list_projects(
43+
page_token=page_token)
44+
projects.extend(results)
45+
46+
if not page_token:
47+
break
48+
49+
for project in projects:
50+
print(project.project_id)
3851

3952

4053
def list_datasets(project=None):
4154
"""Lists all datasets in a given project.
4255
43-
If no project is specified, then the currently active project is used
56+
If no project is specified, then the currently active project is used.
4457
"""
4558
bigquery_client = bigquery.Client(project=project)
4659

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

6174

75+
def create_dataset(dataset_name, project=None):
76+
"""Craetes a dataset in a given project.
77+
78+
If no project is specified, then the currently active project is used.
79+
"""
80+
bigquery_client = bigquery.Client(project=project)
81+
82+
dataset = bigquery_client.dataset(dataset_name)
83+
84+
dataset.create()
85+
86+
print('Created dataset {}.'.format(dataset_name))
87+
88+
6289
def list_tables(dataset_name, project=None):
6390
"""Lists all of the tables in a given dataset.
6491
@@ -221,9 +248,16 @@ def delete_table(dataset_name, table_name, project=None):
221248

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

251+
list_projects_parser = subparsers.add_parser(
252+
'list-projects', help=list_projects.__doc__)
253+
224254
list_datasets_parser = subparsers.add_parser(
225255
'list-datasets', help=list_datasets.__doc__)
226256

257+
create_dataset_parser = subparsers.add_parser(
258+
'list-datasets', help=list_datasets.__doc__)
259+
create_dataset_parser.add_argument('dataset_name')
260+
227261
list_tables_parser = subparsers.add_parser(
228262
'list-tables', help=list_tables.__doc__)
229263
list_tables_parser.add_argument('dataset_name')
@@ -251,8 +285,12 @@ def delete_table(dataset_name, table_name, project=None):
251285

252286
args = parser.parse_args()
253287

254-
if args.command == 'list-datasets':
288+
if args.command == 'list-projects':
289+
list_projects()
290+
elif args.command == 'list-datasets':
255291
list_datasets(args.project)
292+
elif args.command == 'create-dataset':
293+
create_dataset(args.dataset_name, args.project)
256294
elif args.command == 'list-tables':
257295
list_tables(args.dataset_name, args.project)
258296
elif args.command == 'create-table':

‎bigquery/cloud-client/snippets_test.py

Copy file name to clipboardExpand all lines: bigquery/cloud-client/snippets_test.py
+23-3Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
TABLE_ID = 'test_table'
2323

2424

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

4138

39+
@pytest.fixture
40+
def cleanup_dataset():
41+
dataset_name = 'test_temporary_dataset'
42+
bigquery_client = bigquery.Client()
43+
dataset = bigquery_client.dataset(dataset_name)
44+
45+
if dataset.exists():
46+
dataset.delete()
47+
48+
yield dataset_name
49+
50+
if dataset.exists():
51+
dataset.delete()
52+
53+
54+
def test_create_dataset(capsys, cleanup_dataset):
55+
snippets.create_dataset(cleanup_dataset)
56+
57+
out, _ = capsys.readouterr()
58+
59+
assert cleanup_dataset in out
60+
61+
4262
def test_list_tables(capsys):
4363
# Requires the dataset and table to have been created in the test project.
4464
snippets.list_tables(DATASET_ID)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.