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

BigQuery: Adds client.get_dataset() and removes dataset.reload() #3973

Merged
merged 6 commits into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ def dataset(self, dataset_name, project=None):

return DatasetReference(project, dataset_name)

def get_dataset(self, dataset_ref):
"""Fetch the dataset referenced by ``dataset_ref``

:type dataset_ref:
:class:`google.cloud.bigquery.dataset.DatasetReference`
:param dataset_ref: the dataset to use.

:rtype: :class:`google.cloud.bigquery.dataset.Dataset`
:returns: a ``Dataset`` instance
"""
api_response = self._connection.api_request(
method='GET', path=dataset_ref.path)
return Dataset.from_api_repr(api_response, self)

def _get_query_results(self, job_id, project=None, timeout_ms=None):
"""Get the query results object for a query job.

Expand Down
26 changes: 9 additions & 17 deletions bigquery/google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ def dataset_id(self):
"""
return self._dataset_id

@property
def path(self):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

"""URL path for the dataset's APIs.

:rtype: str
:returns: the path based on project and dataset name.
"""
return '/projects/%s/datasets/%s' % (self.project_id, self.dataset_id)

def table(self, table_id):
"""Constructs a TableReference.

Expand Down Expand Up @@ -505,23 +514,6 @@ def exists(self, client=None):
else:
return True

def reload(self, client=None):
"""API call: refresh dataset properties via a GET request.

See
https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/get

:type client: :class:`~google.cloud.bigquery.client.Client` or
``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current dataset.
"""
client = self._require_client(client)

api_response = client._connection.api_request(
method='GET', path=self.path)
self._set_properties(api_response)

def patch(self, client=None, **kw):
"""API call: update individual dataset properties via a PATCH request.

Expand Down
17 changes: 9 additions & 8 deletions bigquery/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,20 @@ def test_create_dataset(self):
self.assertTrue(dataset.exists())
self.assertEqual(dataset.dataset_id, DATASET_ID)

def test_reload_dataset(self):
DATASET_ID = _make_dataset_id('reload_dataset')
dataset = Dataset(DATASET_ID, Config.CLIENT)
def test_get_dataset(self):
DATASET_ID = _make_dataset_id('get_dataset')
client = Config.CLIENT
dataset = Dataset(DATASET_ID, client)
dataset.friendly_name = 'Friendly'
dataset.description = 'Description'

retry_403(dataset.create)()
self.to_delete.append(dataset)
dataset_ref = client.dataset(DATASET_ID)

got = client.get_dataset(dataset_ref)

other = Dataset(DATASET_ID, Config.CLIENT)
other.reload()
self.assertEqual(other.friendly_name, 'Friendly')
self.assertEqual(other.description, 'Description')
self.assertEqual(got.friendly_name, 'Friendly')
self.assertEqual(got.description, 'Description')

def test_patch_dataset(self):
dataset = Dataset(_make_dataset_id('patch_dataset'), Config.CLIENT)
Expand Down
25 changes: 25 additions & 0 deletions bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,31 @@ def test_dataset_with_default_project(self):
self.assertEqual(dataset.dataset_id, DATASET)
self.assertEqual(dataset.project_id, PROJECT)

def test_get_dataset(self):
project = 'PROJECT'
dataset_id = 'dataset_id'
path = 'projects/%s/datasets/%s' % (project, dataset_id)
creds = _make_credentials()
http = object()
client = self._make_one(project=project, credentials=creds, _http=http)
resource = {
'id': '%s:%s' % (project, dataset_id),
'datasetReference': {
'projectId': project,
'datasetId': dataset_id,
},
}
conn = client._connection = _Connection(resource)
dataset_ref = client.dataset(dataset_id)

dataset = client.get_dataset(dataset_ref)

self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % path)
self.assertEqual(dataset.dataset_id, dataset_id)

def test_job_from_resource_unknown_type(self):
PROJECT = 'PROJECT'
creds = _make_credentials()
Expand Down
33 changes: 0 additions & 33 deletions bigquery/tests/unit/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,39 +520,6 @@ def test_exists_hit_w_alternate_client(self):
self.assertEqual(req['path'], '/%s' % PATH)
self.assertEqual(req['query_params'], {'fields': 'id'})

def test_reload_w_bound_client(self):
PATH = 'projects/%s/datasets/%s' % (self.PROJECT, self.DS_ID)
RESOURCE = self._makeResource()
conn = _Connection(RESOURCE)
client = _Client(project=self.PROJECT, connection=conn)
dataset = self._make_one(self.DS_ID, client=client)

dataset.reload()

self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % PATH)
self._verify_resource_properties(dataset, RESOURCE)

def test_reload_w_alternate_client(self):
PATH = 'projects/%s/datasets/%s' % (self.PROJECT, self.DS_ID)
RESOURCE = self._makeResource()
conn1 = _Connection()
CLIENT1 = _Client(project=self.PROJECT, connection=conn1)
conn2 = _Connection(RESOURCE)
CLIENT2 = _Client(project=self.PROJECT, connection=conn2)
dataset = self._make_one(self.DS_ID, client=CLIENT1)

dataset.reload(client=CLIENT2)

self.assertEqual(len(conn1._requested), 0)
self.assertEqual(len(conn2._requested), 1)
req = conn2._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % PATH)
self._verify_resource_properties(dataset, RESOURCE)

def test_patch_w_invalid_expiration(self):
RESOURCE = self._makeResource()
conn = _Connection(RESOURCE)
Expand Down