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: Allow using TableListItem to create a Table #8738

Merged
merged 4 commits into from
Jul 26, 2019
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
3 changes: 1 addition & 2 deletions bigquery/google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +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)
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:
Expand Down
41 changes: 41 additions & 0 deletions bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,47 @@ 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, TableListItem

import datetime
from google.cloud._helpers import _millis, UTC

self.WHEN_TS = 1437767599.125
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.
Expand Down