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

Fix new partitioned table #3381

Merged
merged 3 commits into from
May 15, 2017
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
5 changes: 2 additions & 3 deletions bigquery/google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,11 @@ def _build_resource(self):
if self.view_query is not None:
view = resource['view'] = {}
view['query'] = self.view_query
elif self._schema:

if self._schema:
resource['schema'] = {
'fields': _build_schema_resource(self._schema)
}
else:
raise ValueError("Set either 'view_query' or 'schema'.")

return resource

Expand Down
29 changes: 20 additions & 9 deletions bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,29 @@ def test_from_api_repr_w_properties(self):
self.assertIs(table._dataset._client, client)
self._verifyResourceProperties(table, RESOURCE)

def test_create_no_view_query_no_schema(self):
conn = _Connection()
def test_create_new_day_partitioned_table(self):
PATH = 'projects/%s/datasets/%s/tables' % (self.PROJECT, self.DS_NAME)
RESOURCE = self._makeResource()
conn = _Connection(RESOURCE)
client = _Client(project=self.PROJECT, connection=conn)
dataset = _Dataset(client)
table = self._make_one(self.TABLE_NAME, dataset)
table.partitioning_type = 'DAY'
table.create()

with self.assertRaises(ValueError):
table.create()
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], '/%s' % PATH)
SENT = {
'tableReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_NAME,
'tableId': self.TABLE_NAME},
'timePartitioning': {'type': 'DAY'},
}
self.assertEqual(req['data'], SENT)
self._verifyResourceProperties(table, RESOURCE)

def test_create_w_bound_client(self):
from google.cloud.bigquery.table import SchemaField
Expand Down Expand Up @@ -647,7 +662,6 @@ def test_create_w_alternate_client(self):
import datetime
from google.cloud._helpers import UTC
from google.cloud._helpers import _millis
from google.cloud.bigquery.table import SchemaField

PATH = 'projects/%s/datasets/%s/tables' % (self.PROJECT, self.DS_NAME)
DESCRIPTION = 'DESCRIPTION'
Expand All @@ -667,10 +681,7 @@ def test_create_w_alternate_client(self):
conn2 = _Connection(RESOURCE)
client2 = _Client(project=self.PROJECT, connection=conn2)
dataset = _Dataset(client=client1)
full_name = SchemaField('full_name', 'STRING', mode='REQUIRED')
age = SchemaField('age', 'INTEGER', mode='REQUIRED')
table = self._make_one(self.TABLE_NAME, dataset=dataset,
schema=[full_name, age])
table = self._make_one(self.TABLE_NAME, dataset=dataset)
table.friendly_name = TITLE
table.description = DESCRIPTION
table.view_query = QUERY
Expand Down