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 5f178e8

Browse filesBrowse files
authored
Merge pull request googleapis#2809 from tseaver/2118-bigquery-allow_override_dataset_project
Allow overriding dataset's project during construction.
2 parents 37fd8f8 + 1e60969 commit 5f178e8
Copy full SHA for 5f178e8

File tree

Expand file treeCollapse file tree

5 files changed

+71
-9
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+71
-9
lines changed

‎bigquery/google/cloud/bigquery/client.py

Copy file name to clipboardExpand all lines: bigquery/google/cloud/bigquery/client.py
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,20 @@ def list_datasets(self, include_all=False, max_results=None,
129129
items_key='datasets', page_token=page_token,
130130
max_results=max_results, extra_params=extra_params)
131131

132-
def dataset(self, dataset_name):
132+
def dataset(self, dataset_name, project=None):
133133
"""Construct a dataset bound to this client.
134134
135135
:type dataset_name: str
136136
:param dataset_name: Name of the dataset.
137137
138+
:type project: str
139+
:param project: (Optional) project ID for the dataset (defaults to
140+
the project of the client).
141+
138142
:rtype: :class:`google.cloud.bigquery.dataset.Dataset`
139143
:returns: a new ``Dataset`` instance
140144
"""
141-
return Dataset(dataset_name, client=self)
145+
return Dataset(dataset_name, client=self, project=project)
142146

143147
def job_from_resource(self, resource):
144148
"""Detect correct job type from resource and instantiate.

‎bigquery/google/cloud/bigquery/dataset.py

Copy file name to clipboardExpand all lines: bigquery/google/cloud/bigquery/dataset.py
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,21 @@ class Dataset(object):
101101
102102
:type access_grants: list of :class:`AccessGrant`
103103
:param access_grants: roles granted to entities for this dataset
104+
105+
:type project: str
106+
:param project: (Optional) project ID for the dataset (defaults to
107+
the project of the client).
104108
"""
105109

106110
_access_grants = None
107111

108-
def __init__(self, name, client, access_grants=()):
112+
def __init__(self, name, client, access_grants=(), project=None):
109113
self.name = name
110114
self._client = client
111115
self._properties = {}
112116
# Let the @property do validation.
113117
self.access_grants = access_grants
118+
self._project = project or client.project
114119

115120
@property
116121
def project(self):
@@ -119,7 +124,7 @@ def project(self):
119124
:rtype: str
120125
:returns: the project (derived from the client).
121126
"""
122-
return self._client.project
127+
return self._project
123128

124129
@property
125130
def path(self):

‎bigquery/unit_tests/test_client.py

Copy file name to clipboardExpand all lines: bigquery/unit_tests/test_client.py
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_list_datasets_explicit_response_missing_datasets_key(self):
176176
self.assertEqual(req['query_params'],
177177
{'all': True, 'maxResults': 3, 'pageToken': TOKEN})
178178

179-
def test_dataset(self):
179+
def test_dataset_defaults(self):
180180
from google.cloud.bigquery.dataset import Dataset
181181
PROJECT = 'PROJECT'
182182
DATASET = 'dataset_name'
@@ -187,6 +187,21 @@ def test_dataset(self):
187187
self.assertIsInstance(dataset, Dataset)
188188
self.assertEqual(dataset.name, DATASET)
189189
self.assertIs(dataset._client, client)
190+
self.assertEqual(dataset.project, PROJECT)
191+
192+
def test_dataset_explicit(self):
193+
from google.cloud.bigquery.dataset import Dataset
194+
PROJECT = 'my-project-123'
195+
OTHER_PROJECT = 'other-project-456'
196+
DATASET = 'dataset_name'
197+
creds = object()
198+
http = object()
199+
client = self._make_one(project=PROJECT, credentials=creds, http=http)
200+
dataset = client.dataset(DATASET, project=OTHER_PROJECT)
201+
self.assertIsInstance(dataset, Dataset)
202+
self.assertEqual(dataset.name, DATASET)
203+
self.assertIs(dataset._client, client)
204+
self.assertEqual(dataset.project, OTHER_PROJECT)
190205

191206
def test_job_from_resource_unknown_type(self):
192207
PROJECT = 'PROJECT'

‎bigquery/unit_tests/test_dataset.py

Copy file name to clipboardExpand all lines: bigquery/unit_tests/test_dataset.py
+33-4Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def _verifyResourceProperties(self, dataset, resource):
174174
else:
175175
self.assertEqual(dataset.access_grants, [])
176176

177-
def test_ctor(self):
177+
def test_ctor_defaults(self):
178178
client = _Client(self.PROJECT)
179179
dataset = self._make_one(self.DS_NAME, client)
180180
self.assertEqual(dataset.name, self.DS_NAME)
@@ -196,21 +196,50 @@ def test_ctor(self):
196196
self.assertIsNone(dataset.friendly_name)
197197
self.assertIsNone(dataset.location)
198198

199-
def test_access_roles_setter_non_list(self):
199+
def test_ctor_explicit(self):
200+
from google.cloud.bigquery.dataset import AccessGrant
201+
phred = AccessGrant('OWNER', 'userByEmail', 'phred@example.com')
202+
bharney = AccessGrant('OWNER', 'userByEmail', 'bharney@example.com')
203+
grants = [phred, bharney]
204+
OTHER_PROJECT = 'foo-bar-123'
205+
client = _Client(self.PROJECT)
206+
dataset = self._make_one(self.DS_NAME, client,
207+
access_grants=grants,
208+
project=OTHER_PROJECT)
209+
self.assertEqual(dataset.name, self.DS_NAME)
210+
self.assertIs(dataset._client, client)
211+
self.assertEqual(dataset.project, OTHER_PROJECT)
212+
self.assertEqual(
213+
dataset.path,
214+
'/projects/%s/datasets/%s' % (OTHER_PROJECT, self.DS_NAME))
215+
self.assertEqual(dataset.access_grants, grants)
216+
217+
self.assertIsNone(dataset.created)
218+
self.assertIsNone(dataset.dataset_id)
219+
self.assertIsNone(dataset.etag)
220+
self.assertIsNone(dataset.modified)
221+
self.assertIsNone(dataset.self_link)
222+
223+
self.assertIsNone(dataset.default_table_expiration_ms)
224+
self.assertIsNone(dataset.description)
225+
self.assertIsNone(dataset.friendly_name)
226+
self.assertIsNone(dataset.location)
227+
228+
def test_access_grants_setter_non_list(self):
200229
client = _Client(self.PROJECT)
201230
dataset = self._make_one(self.DS_NAME, client)
202231
with self.assertRaises(TypeError):
203232
dataset.access_grants = object()
204233

205-
def test_access_roles_setter_invalid_field(self):
234+
def test_access_grants_setter_invalid_field(self):
206235
from google.cloud.bigquery.dataset import AccessGrant
207236
client = _Client(self.PROJECT)
208237
dataset = self._make_one(self.DS_NAME, client)
209238
phred = AccessGrant('OWNER', 'userByEmail', 'phred@example.com')
210239
with self.assertRaises(ValueError):
211240
dataset.access_grants = [phred, object()]
212241

213-
def test_access_roles_setter(self):
242+
def test_access_grants_setter(self):
214243
from google.cloud.bigquery.dataset import AccessGrant
215244
client = _Client(self.PROJECT)
216245
dataset = self._make_one(self.DS_NAME, client)

‎system_tests/bigquery.py

Copy file name to clipboardExpand all lines: system_tests/bigquery.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,12 @@ def test_sync_query_w_standard_sql_types(self):
561561
self.assertEqual(len(query.rows), 1)
562562
self.assertEqual(len(query.rows[0]), 1)
563563
self.assertEqual(query.rows[0][0], example['expected'])
564+
565+
def test_dump_table_w_public_data(self):
566+
PUBLIC = 'bigquery-public-data'
567+
DATASET_NAME = 'samples'
568+
TABLE_NAME = 'natality'
569+
570+
dataset = Config.CLIENT.dataset(DATASET_NAME, project=PUBLIC)
571+
table = dataset.table(TABLE_NAME)
572+
self._fetch_single_page(table)

0 commit comments

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