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

feat: add TableReference.__str__ to get table ID in standard SQL #405

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions google/cloud/bigquery/table.py
Expand Up @@ -262,6 +262,9 @@ def __ne__(self, other):
def __hash__(self):
return hash(self._key())

def __str__(self):
return f"{self.project}.{self.dataset_id}.{self.table_id}"

def __repr__(self):
from google.cloud.bigquery.dataset import DatasetReference

Expand Down Expand Up @@ -475,7 +478,7 @@ def full_table_id(self):
"""Union[str, None]: ID for the table (:data:`None` until set from the
server).

In the format ``project_id:dataset_id.table_id``.
In the format ``project-id:dataset_id.table_id``.
"""
return self._properties.get("id")

Expand All @@ -484,7 +487,8 @@ def table_type(self):
"""Union[str, None]: The type of the table (:data:`None` until set from
the server).

Possible values are ``'TABLE'``, ``'VIEW'``, or ``'EXTERNAL'``.
Possible values are ``'TABLE'``, ``'VIEW'``, ``'MATERIALIZED_VIEW'`` or
``'EXTERNAL'``.
"""
return self._properties.get("type")

Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_table.py
Expand Up @@ -272,6 +272,11 @@ def test___repr__(self):
)
self.assertEqual(repr(table1), expected)

def test___str__(self):
dataset = DatasetReference("project1", "dataset1")
table1 = self._make_one(dataset, "table1")
self.assertEqual(str(table1), "project1.dataset1.table1")


class TestTable(unittest.TestCase, _SchemaBase):

Expand Down Expand Up @@ -813,6 +818,9 @@ def test_from_string(self):
self.assertEqual(got.project, "string-project")
self.assertEqual(got.dataset_id, "string_dataset")
self.assertEqual(got.table_id, "string_table")
self.assertEqual(
str(got.reference), "string-project.string_dataset.string_table"
)

def test_from_string_legacy_string(self):
cls = self._get_target_class()
Expand Down