From 2ff0aa5801dafb163b7a3622a159b82e7d47e3a4 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Mon, 30 Nov 2020 13:54:24 -0600 Subject: [PATCH 1/2] feat: add `TableReference.__str__` to get table ID in standard SQL format This is the natural inverse of the `TableReference.from_string` method. --- google/cloud/bigquery/table.py | 8 ++++++-- tests/unit/test_table.py | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/google/cloud/bigquery/table.py b/google/cloud/bigquery/table.py index 4bfedd758..f30c05773 100644 --- a/google/cloud/bigquery/table.py +++ b/google/cloud/bigquery/table.py @@ -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 @@ -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") @@ -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") diff --git a/tests/unit/test_table.py b/tests/unit/test_table.py index 1dd5fab46..e3653a948 100644 --- a/tests/unit/test_table.py +++ b/tests/unit/test_table.py @@ -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): @@ -813,6 +818,7 @@ 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() From 6305d9dc8f9be38d24701d6ad53a34a5765c5fd0 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Mon, 30 Nov 2020 15:30:02 -0600 Subject: [PATCH 2/2] blacken --- tests/unit/test_table.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_table.py b/tests/unit/test_table.py index e3653a948..67874ff91 100644 --- a/tests/unit/test_table.py +++ b/tests/unit/test_table.py @@ -818,7 +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") + self.assertEqual( + str(got.reference), "string-project.string_dataset.string_table" + ) def test_from_string_legacy_string(self): cls = self._get_target_class()