From 4e94c483d85dd8032143a3833242725bf8442116 Mon Sep 17 00:00:00 2001 From: Alix Hamilton Date: Fri, 15 Sep 2017 16:07:48 -0700 Subject: [PATCH] BigQuery: Adds client.get_dataset() and removes dataset.reload() (#3973) * BigQuery: Adds client.get_dataset() and removes dataset.reload() * BigQuery: changes dataset.name to dataset.dataset_id in test * fixes client.get_dataset() docstring and removes unnecessary test variable --- bigquery/google/cloud/bigquery/client.py | 14 ++++++++++ bigquery/google/cloud/bigquery/dataset.py | 26 +++++++----------- bigquery/tests/system.py | 17 ++++++------ bigquery/tests/unit/test_client.py | 25 +++++++++++++++++ bigquery/tests/unit/test_dataset.py | 33 ----------------------- 5 files changed, 57 insertions(+), 58 deletions(-) diff --git a/bigquery/google/cloud/bigquery/client.py b/bigquery/google/cloud/bigquery/client.py index 5ab8ff820764..f9a393c0bbb6 100644 --- a/bigquery/google/cloud/bigquery/client.py +++ b/bigquery/google/cloud/bigquery/client.py @@ -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. diff --git a/bigquery/google/cloud/bigquery/dataset.py b/bigquery/google/cloud/bigquery/dataset.py index cd31f737e693..8166e4fbec5e 100644 --- a/bigquery/google/cloud/bigquery/dataset.py +++ b/bigquery/google/cloud/bigquery/dataset.py @@ -127,6 +127,15 @@ def dataset_id(self): """ return self._dataset_id + @property + def path(self): + """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. @@ -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. diff --git a/bigquery/tests/system.py b/bigquery/tests/system.py index 48d66c7be2e0..432dfa18b84d 100644 --- a/bigquery/tests/system.py +++ b/bigquery/tests/system.py @@ -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) diff --git a/bigquery/tests/unit/test_client.py b/bigquery/tests/unit/test_client.py index cb2e476e3f99..a011c59fec2a 100644 --- a/bigquery/tests/unit/test_client.py +++ b/bigquery/tests/unit/test_client.py @@ -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() diff --git a/bigquery/tests/unit/test_dataset.py b/bigquery/tests/unit/test_dataset.py index 0689e93b0f20..673fa69731cd 100644 --- a/bigquery/tests/unit/test_dataset.py +++ b/bigquery/tests/unit/test_dataset.py @@ -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)