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: add reference property to Table and Dataset #4405

Merged
merged 2 commits into from
Nov 21, 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
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