From 9dad69c16cf835f6a821caf93860d45dff6d3910 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Thu, 16 Nov 2017 16:44:57 -0800 Subject: [PATCH] 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. --- bigquery/google/cloud/bigquery/dataset.py | 11 +++++++++++ bigquery/google/cloud/bigquery/table.py | 14 ++++++++++++++ bigquery/tests/unit/test_dataset.py | 3 +++ bigquery/tests/unit/test_table.py | 3 +++ 4 files changed, 31 insertions(+) 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..086679a3e7a8 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -223,6 +223,20 @@ def table_id(self): """ return self._table_id + @property + def reference(self): + """A :class:`~google.cloud.bigquery.table.TableReference` pointing to + this table. + + Returns: + google.cloud.bigquery.table.TableReference: + A 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) + @property def path(self): """URL path for the table's APIs. 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' % (