diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index 3b0346f60cdf..fc17c5b9a009 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -473,8 +473,9 @@ def _build_resource(self): resource['schema'] = { 'fields': _build_schema_resource(self._schema) } - else: - raise ValueError("Set either 'view_query' or 'schema'.") + elif self.partitioning_type is None: + raise ValueError( + "Set either 'view_query' or 'schema' or 'partitioning_type'.") return resource diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index 6a496ba69e95..bd6a023614ce 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -395,7 +395,7 @@ 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): + def test_create_no_view_query_no_schema_no_partitioning(self): conn = _Connection() client = _Client(project=self.PROJECT, connection=conn) dataset = _Dataset(client) @@ -403,6 +403,30 @@ def test_create_no_view_query_no_schema(self): with self.assertRaises(ValueError): table.create() + + 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() + + 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