Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding dataset's project during construction. #2809

Merged
merged 3 commits into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,20 @@ def list_datasets(self, include_all=False, max_results=None,
items_key='datasets', page_token=page_token,
max_results=max_results, extra_params=extra_params)

def dataset(self, dataset_name):
def dataset(self, dataset_name, project=None):
"""Construct a dataset bound to this client.

:type dataset_name: str
:param dataset_name: Name of the dataset.

:type project: str
:param project: (Optional) project ID for the dataset (defaults to
the project of the client).

:rtype: :class:`google.cloud.bigquery.dataset.Dataset`
:returns: a new ``Dataset`` instance
"""
return Dataset(dataset_name, client=self)
return Dataset(dataset_name, client=self, project=project)

def job_from_resource(self, resource):
"""Detect correct job type from resource and instantiate.
Expand Down
9 changes: 7 additions & 2 deletions bigquery/google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,21 @@ class Dataset(object):

:type access_grants: list of :class:`AccessGrant`
:param access_grants: roles granted to entities for this dataset

:type project: str
:param project: (Optional) project ID for the dataset (defaults to
the project of the client).
"""

_access_grants = None

def __init__(self, name, client, access_grants=()):
def __init__(self, name, client, access_grants=(), project=None):
self.name = name
self._client = client
self._properties = {}
# Let the @property do validation.
self.access_grants = access_grants
self._project = project

@property
def project(self):
Expand All @@ -119,7 +124,7 @@ def project(self):
:rtype: str
:returns: the project (derived from the client).
"""
return self._client.project
return self._project or self._client.project

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


@property
def path(self):
Expand Down
17 changes: 16 additions & 1 deletion bigquery/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_list_datasets_explicit_response_missing_datasets_key(self):
self.assertEqual(req['query_params'],
{'all': True, 'maxResults': 3, 'pageToken': TOKEN})

def test_dataset(self):
def test_dataset_defaults(self):
from google.cloud.bigquery.dataset import Dataset
PROJECT = 'PROJECT'
DATASET = 'dataset_name'
Expand All @@ -187,6 +187,21 @@ def test_dataset(self):
self.assertIsInstance(dataset, Dataset)
self.assertEqual(dataset.name, DATASET)
self.assertIs(dataset._client, client)
self.assertEqual(dataset.project, PROJECT)

def test_dataset_explicit(self):
from google.cloud.bigquery.dataset import Dataset
PROJECT = 'my-project-123'
OTHER_PROJECT = 'other-project-456'
DATASET = 'dataset_name'
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
dataset = client.dataset(DATASET, project=OTHER_PROJECT)
self.assertIsInstance(dataset, Dataset)
self.assertEqual(dataset.name, DATASET)
self.assertIs(dataset._client, client)
self.assertEqual(dataset.project, OTHER_PROJECT)

def test_job_from_resource_unknown_type(self):
PROJECT = 'PROJECT'
Expand Down
37 changes: 33 additions & 4 deletions bigquery/unit_tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _verifyResourceProperties(self, dataset, resource):
else:
self.assertEqual(dataset.access_grants, [])

def test_ctor(self):
def test_ctor_defaults(self):
client = _Client(self.PROJECT)
dataset = self._make_one(self.DS_NAME, client)
self.assertEqual(dataset.name, self.DS_NAME)
Expand All @@ -196,21 +196,50 @@ def test_ctor(self):
self.assertIsNone(dataset.friendly_name)
self.assertIsNone(dataset.location)

def test_access_roles_setter_non_list(self):
def test_ctor_explicit(self):
from google.cloud.bigquery.dataset import AccessGrant
phred = AccessGrant('OWNER', 'userByEmail', '[email protected]')
bharney = AccessGrant('OWNER', 'userByEmail', '[email protected]')
grants = [phred, bharney]
OTHER_PROJECT = 'foo-bar-123'
client = _Client(self.PROJECT)
dataset = self._make_one(self.DS_NAME, client,
access_grants=grants,
project=OTHER_PROJECT)
self.assertEqual(dataset.name, self.DS_NAME)
self.assertIs(dataset._client, client)
self.assertEqual(dataset.project, OTHER_PROJECT)
self.assertEqual(
dataset.path,
'/projects/%s/datasets/%s' % (OTHER_PROJECT, self.DS_NAME))
self.assertEqual(dataset.access_grants, grants)

self.assertIsNone(dataset.created)
self.assertIsNone(dataset.dataset_id)
self.assertIsNone(dataset.etag)
self.assertIsNone(dataset.modified)
self.assertIsNone(dataset.self_link)

self.assertIsNone(dataset.default_table_expiration_ms)
self.assertIsNone(dataset.description)
self.assertIsNone(dataset.friendly_name)
self.assertIsNone(dataset.location)

def test_access_grants_setter_non_list(self):
client = _Client(self.PROJECT)
dataset = self._make_one(self.DS_NAME, client)
with self.assertRaises(TypeError):
dataset.access_grants = object()

def test_access_roles_setter_invalid_field(self):
def test_access_grants_setter_invalid_field(self):
from google.cloud.bigquery.dataset import AccessGrant
client = _Client(self.PROJECT)
dataset = self._make_one(self.DS_NAME, client)
phred = AccessGrant('OWNER', 'userByEmail', '[email protected]')
with self.assertRaises(ValueError):
dataset.access_grants = [phred, object()]

def test_access_roles_setter(self):
def test_access_grants_setter(self):
from google.cloud.bigquery.dataset import AccessGrant
client = _Client(self.PROJECT)
dataset = self._make_one(self.DS_NAME, client)
Expand Down