From 58b642efe58c8609be1c8d9ae99a1ae8d69c87fb Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Tue, 21 Nov 2017 13:29:28 -0800 Subject: [PATCH] BigQuery: add reference property to Table and Dataset (#4405) * BigQuery: add reference property to Table and Dataset This makes it easier to call `get_table`, `get_dataset`, or other functions that want just a reference, not a full resource. * Use shared reference property between Table and TableListItem. --- bigquery/google/cloud/bigquery/dataset.py | 11 +++++++++ bigquery/google/cloud/bigquery/table.py | 28 +++++++++++++---------- bigquery/tests/unit/test_dataset.py | 3 +++ bigquery/tests/unit/test_table.py | 3 +++ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/bigquery/google/cloud/bigquery/dataset.py b/bigquery/google/cloud/bigquery/dataset.py index eb7372876c1f..25e00405e2c8 100644 --- a/bigquery/google/cloud/bigquery/dataset.py +++ b/bigquery/google/cloud/bigquery/dataset.py @@ -279,6 +279,17 @@ def full_dataset_id(self): """ return self._properties.get('id') + @property + def reference(self): + """A :class:`~google.cloud.bigquery.dataset.DatasetReference` pointing to + this dataset. + + Returns: + google.cloud.bigquery.dataset.DatasetReference: + A pointer to this dataset + """ + return DatasetReference(self.project, self.dataset_id) + @property def etag(self): """ETag for the dataset resource. diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index eda14a409b3e..1433842ef61a 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -34,6 +34,19 @@ _MARKER = object() +def _reference_getter(table): + """A :class:`~google.cloud.bigquery.table.TableReference` pointing to + this table. + + Returns: + google.cloud.bigquery.table.TableReference: pointer to this table + """ + from google.cloud.bigquery import dataset + + dataset_ref = dataset.DatasetReference(table.project, table.dataset_id) + return TableReference(dataset_ref, table.table_id) + + def _view_use_legacy_sql_getter(table): """Specifies whether to execute the view with Legacy or Standard SQL. @@ -223,6 +236,8 @@ def table_id(self): """ return self._table_id + reference = property(_reference_getter) + @property def path(self): """URL path for the table's APIs. @@ -764,18 +779,7 @@ def table_id(self): """ return self._properties.get('tableReference', {}).get('tableId') - @property - def reference(self): - """A :class:`~google.cloud.bigquery.table.TableReference` pointing to - this table. - - Returns: - google.cloud.bigquery.table.TableReference: pointer to this table - """ - from google.cloud.bigquery import dataset - - dataset_ref = dataset.DatasetReference(self.project, self.dataset_id) - return TableReference(dataset_ref, self.table_id) + reference = property(_reference_getter) @property def labels(self): diff --git a/bigquery/tests/unit/test_dataset.py b/bigquery/tests/unit/test_dataset.py index 8a34b82a460d..e9e2f0dec813 100644 --- a/bigquery/tests/unit/test_dataset.py +++ b/bigquery/tests/unit/test_dataset.py @@ -245,7 +245,10 @@ def _verify_access_entry(self, access_entries, resource): def _verify_readonly_resource_properties(self, dataset, resource): + self.assertEqual(dataset.project, self.PROJECT) self.assertEqual(dataset.dataset_id, self.DS_ID) + self.assertEqual(dataset.reference.project, self.PROJECT) + self.assertEqual(dataset.reference.dataset_id, self.DS_ID) if 'creationTime' in resource: self.assertEqual(dataset.created, self.WHEN) diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index 5f6c5d58561c..752239b7276b 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -293,6 +293,9 @@ def test_ctor(self): self.assertEqual(table.table_id, self.TABLE_NAME) self.assertEqual(table.project, self.PROJECT) self.assertEqual(table.dataset_id, self.DS_ID) + self.assertEqual(table.reference.table_id, self.TABLE_NAME) + self.assertEqual(table.reference.project, self.PROJECT) + self.assertEqual(table.reference.dataset_id, self.DS_ID) self.assertEqual( table.path, '/projects/%s/datasets/%s/tables/%s' % (