Skip to content

Commit

Permalink
Merge pull request googleapis#3118 from tseaver/3029-bigquery-systest…
Browse files Browse the repository at this point in the history
…-create-w-complex-schema

Add system test for 'Table.create' using nested schema
  • Loading branch information
tseaver authored Mar 15, 2017
2 parents 50138d6 + 2530919 commit 0fa8823
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
28 changes: 28 additions & 0 deletions system_tests/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import operator
import os

import unittest

Expand All @@ -25,6 +27,9 @@
from system_test_utils import unique_resource_id


WHERE = os.path.abspath(os.path.dirname(__file__))


def _has_rows(result):
return len(result) > 0

Expand All @@ -33,6 +38,14 @@ def _make_dataset_name(prefix):
return '%s%s' % (prefix, unique_resource_id())


def _load_json_schema(filename='bigquery_test_schema.json'):
from google.cloud.bigquery.table import _parse_schema_resource
json_filename = os.path.join(WHERE, filename)

with open(json_filename, 'r') as schema_file:
return _parse_schema_resource(json.load(schema_file))


def _rate_limit_exceeded(forbidden):
"""Predicate: pass only exceptions with 'rateLimitExceeded' as reason."""
return any(error['reason'] == 'rateLimitExceeded'
Expand Down Expand Up @@ -650,3 +663,18 @@ def test_dump_table_w_public_data(self):
dataset = Config.CLIENT.dataset(DATASET_NAME, project=PUBLIC)
table = dataset.table(TABLE_NAME)
self._fetch_single_page(table)

def test_create_table_nested_schema(self):
table_name = 'test_table'
dataset = Config.CLIENT.dataset(
_make_dataset_name('create_table_nested_schema'))
self.assertFalse(dataset.exists())

retry_403(dataset.create)()
self.to_delete.append(dataset)

table = dataset.table(table_name, schema=_load_json_schema())
table.create()
self.to_delete.insert(0, table)
self.assertTrue(table.exists())
self.assertEqual(table.name, table_name)
83 changes: 83 additions & 0 deletions system_tests/bigquery_test_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"fields" : [
{
"type" : "STRING",
"name" : "Name",
"mode" : "NULLABLE"
},
{
"name" : "Age",
"mode" : "NULLABLE",
"type" : "INTEGER"
},
{
"type" : "FLOAT",
"name" : "Weight",
"mode" : "NULLABLE"
},
{
"mode" : "NULLABLE",
"name" : "IsMagic",
"type" : "BOOLEAN"
},
{
"name" : "Spells",
"fields" : [
{
"mode" : "NULLABLE",
"name" : "Name",
"type" : "STRING"
},
{
"mode" : "NULLABLE",
"name" : "LastUsed",
"type" : "TIMESTAMP"
},
{
"type" : "STRING",
"mode" : "NULLABLE",
"name" : "DiscoveredBy"
},
{
"name" : "Properties",
"fields" : [
{
"name" : "Name",
"mode" : "NULLABLE",
"type" : "STRING"
},
{
"type" : "FLOAT",
"name" : "Power",
"mode" : "NULLABLE"
}
],
"mode" : "REPEATED",
"type" : "RECORD"
},
{
"mode" : "NULLABLE",
"name" : "Icon",
"type" : "BYTES"
}
],
"mode" : "REPEATED",
"type" : "RECORD"
},
{
"type" : "TIME",
"mode" : "NULLABLE",
"name" : "TeaTime"
},
{
"type" : "DATE",
"name" : "NextVacation",
"mode" : "NULLABLE"
},
{
"mode" : "NULLABLE",
"name" : "FavoriteTime",
"type" : "DATETIME"
}
]
}

0 comments on commit 0fa8823

Please sign in to comment.