From 1ac5a19f51159e1c9def00d5cd89864f554f5af0 Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Fri, 19 Jul 2019 11:27:42 +0300 Subject: [PATCH 1/3] Bigquery: Table creation using the TableListItem Provided the opportunity to create a Table with TableListItem avoiding the AttributeError. --- bigquery/google/cloud/bigquery/table.py | 2 ++ bigquery/tests/unit/test_table.py | 44 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index 5b1de345c068..e018f510fced 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -390,6 +390,8 @@ class Table(object): def __init__(self, table_ref, schema=None): if isinstance(table_ref, six.string_types): table_ref = TableReference.from_string(table_ref) + elif isinstance(table_ref, TableListItem): + table_ref = table_ref.reference self._properties = {"tableReference": table_ref.to_api_repr(), "labels": {}} # Let the @property do validation. if schema is not None: diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index a141d8f38abf..8b945154c755 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -526,6 +526,50 @@ def test_ctor_string(self): self.assertEqual(table.dataset_id, "some_dset") self.assertEqual(table.table_id, "some_tbl") + def test_ctor_tablelistitem(self): + from google.cloud.bigquery.table import Table + from google.cloud.bigquery.table import TableListItem + + import datetime + from google.cloud._helpers import _millis + from google.cloud._helpers import UTC + + self.WHEN_TS = 1437767599.125 + self.WHEN = datetime.datetime.utcfromtimestamp(self.WHEN_TS).replace(tzinfo=UTC) + self.EXP_TIME = datetime.datetime(2015, 8, 1, 23, 59, 59, tzinfo=UTC) + + project = "test-project" + dataset_id = "test_dataset" + table_id = "coffee_table" + resource = { + "creationTime": self.WHEN_TS * 1000, + "expirationTime": _millis(self.EXP_TIME), + "kind": "bigquery#table", + "id": "{}:{}.{}".format(project, dataset_id, table_id), + "tableReference": { + "projectId": project, + "datasetId": dataset_id, + "tableId": table_id, + }, + "friendlyName": "Mahogany Coffee Table", + "type": "TABLE", + "timePartitioning": { + "type": "DAY", + "field": "mycolumn", + "expirationMs": "10000", + }, + "labels": {"some-stuff": "this-is-a-label"}, + "clustering": {"fields": ["string"]}, + } + + table_list_item = TableListItem(resource) + table = Table(table_list_item) + + self.assertIsNone(table.created) + self.assertEqual(table.reference.project, project) + self.assertEqual(table.reference.dataset_id, dataset_id) + self.assertEqual(table.reference.table_id, table_id) + def test_ctor_string_wo_project_id(self): with pytest.raises(ValueError): # Project ID is missing. From ab10e6e639b31def1db2127258c99e1d077faa7d Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Fri, 19 Jul 2019 15:21:32 +0300 Subject: [PATCH 2/3] Update test_table.py cosmetic chgs --- bigquery/tests/unit/test_table.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index 8b945154c755..e14420846846 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -527,15 +527,12 @@ def test_ctor_string(self): self.assertEqual(table.table_id, "some_tbl") def test_ctor_tablelistitem(self): - from google.cloud.bigquery.table import Table - from google.cloud.bigquery.table import TableListItem + from google.cloud.bigquery.table import Table, TableListItem import datetime - from google.cloud._helpers import _millis - from google.cloud._helpers import UTC + from google.cloud._helpers import _millis, UTC self.WHEN_TS = 1437767599.125 - self.WHEN = datetime.datetime.utcfromtimestamp(self.WHEN_TS).replace(tzinfo=UTC) self.EXP_TIME = datetime.datetime(2015, 8, 1, 23, 59, 59, tzinfo=UTC) project = "test-project" From 3dc1c2dc4fa592176e105f90a26eb8e17125475c Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Tue, 23 Jul 2019 12:03:57 +0300 Subject: [PATCH 3/3] Update table.py Functionality transfered to helper --- bigquery/google/cloud/bigquery/table.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index e018f510fced..b1fe36c3dbed 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -388,10 +388,7 @@ class Table(object): } def __init__(self, table_ref, schema=None): - if isinstance(table_ref, six.string_types): - table_ref = TableReference.from_string(table_ref) - elif isinstance(table_ref, TableListItem): - table_ref = table_ref.reference + table_ref = _table_arg_to_table_ref(table_ref) self._properties = {"tableReference": table_ref.to_api_repr(), "labels": {}} # Let the @property do validation. if schema is not None: