Skip to content

Commit 5bc1e38

Browse files
authored
Merge pull request googleapis#3381 from richkadel/fix-new-partitioned-table
Fix new partitioned table
2 parents 2d63cda + e4b7c91 commit 5bc1e38

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

bigquery/google/cloud/bigquery/table.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,11 @@ def _build_resource(self):
469469
if self.view_query is not None:
470470
view = resource['view'] = {}
471471
view['query'] = self.view_query
472-
elif self._schema:
472+
473+
if self._schema:
473474
resource['schema'] = {
474475
'fields': _build_schema_resource(self._schema)
475476
}
476-
else:
477-
raise ValueError("Set either 'view_query' or 'schema'.")
478477

479478
return resource
480479

bigquery/tests/unit/test_table.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,29 @@ def test_from_api_repr_w_properties(self):
395395
self.assertIs(table._dataset._client, client)
396396
self._verifyResourceProperties(table, RESOURCE)
397397

398-
def test_create_no_view_query_no_schema(self):
399-
conn = _Connection()
398+
def test_create_new_day_partitioned_table(self):
399+
PATH = 'projects/%s/datasets/%s/tables' % (self.PROJECT, self.DS_NAME)
400+
RESOURCE = self._makeResource()
401+
conn = _Connection(RESOURCE)
400402
client = _Client(project=self.PROJECT, connection=conn)
401403
dataset = _Dataset(client)
402404
table = self._make_one(self.TABLE_NAME, dataset)
405+
table.partitioning_type = 'DAY'
406+
table.create()
403407

404-
with self.assertRaises(ValueError):
405-
table.create()
408+
self.assertEqual(len(conn._requested), 1)
409+
req = conn._requested[0]
410+
self.assertEqual(req['method'], 'POST')
411+
self.assertEqual(req['path'], '/%s' % PATH)
412+
SENT = {
413+
'tableReference': {
414+
'projectId': self.PROJECT,
415+
'datasetId': self.DS_NAME,
416+
'tableId': self.TABLE_NAME},
417+
'timePartitioning': {'type': 'DAY'},
418+
}
419+
self.assertEqual(req['data'], SENT)
420+
self._verifyResourceProperties(table, RESOURCE)
406421

407422
def test_create_w_bound_client(self):
408423
from google.cloud.bigquery.table import SchemaField
@@ -647,7 +662,6 @@ def test_create_w_alternate_client(self):
647662
import datetime
648663
from google.cloud._helpers import UTC
649664
from google.cloud._helpers import _millis
650-
from google.cloud.bigquery.table import SchemaField
651665

652666
PATH = 'projects/%s/datasets/%s/tables' % (self.PROJECT, self.DS_NAME)
653667
DESCRIPTION = 'DESCRIPTION'
@@ -667,10 +681,7 @@ def test_create_w_alternate_client(self):
667681
conn2 = _Connection(RESOURCE)
668682
client2 = _Client(project=self.PROJECT, connection=conn2)
669683
dataset = _Dataset(client=client1)
670-
full_name = SchemaField('full_name', 'STRING', mode='REQUIRED')
671-
age = SchemaField('age', 'INTEGER', mode='REQUIRED')
672-
table = self._make_one(self.TABLE_NAME, dataset=dataset,
673-
schema=[full_name, age])
684+
table = self._make_one(self.TABLE_NAME, dataset=dataset)
674685
table.friendly_name = TITLE
675686
table.description = DESCRIPTION
676687
table.view_query = QUERY

0 commit comments

Comments
 (0)