Skip to content

Commit

Permalink
BigQuery: add reference property to Table and Dataset (#4405)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
tswast authored Nov 21, 2017
1 parent d5141db commit 58b642e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
11 changes: 11 additions & 0 deletions bigquery/google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 16 additions & 12 deletions bigquery/google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions bigquery/tests/unit/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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' % (
Expand Down

0 comments on commit 58b642e

Please sign in to comment.