Skip to content

Commit

Permalink
Add handling of missing properties in SchemaField.from_api_repr() (#4754
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alanbato authored and Jon Wayne Parrott committed Jan 18, 2018
1 parent 95aaa50 commit 321e1f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions bigquery/google/cloud/bigquery/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ def from_api_repr(cls, api_repr):
google.cloud.biquery.schema.SchemaField:
The ``SchemaField`` object.
"""
# Handle optional properties with default values
mode = api_repr.get('mode', 'NULLABLE')
fields = api_repr.get('fields', ())
return cls(
field_type=api_repr['type'].upper(),
fields=[cls.from_api_repr(f) for f in api_repr.get('fields', ())],
mode=api_repr['mode'].upper(),
fields=[cls.from_api_repr(f) for f in fields],
mode=mode.upper(),
name=api_repr['name'],
)

Expand Down
10 changes: 10 additions & 0 deletions bigquery/tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ def test_from_api_repr(self):
self.assertEqual(field.fields[0].field_type, 'INTEGER')
self.assertEqual(field.fields[0].mode, 'NULLABLE')

def test_from_api_repr_defaults(self):
field = self._get_target_class().from_api_repr({
'name': 'foo',
'type': 'record',
})
self.assertEqual(field.name, 'foo')
self.assertEqual(field.field_type, 'RECORD')
self.assertEqual(field.mode, 'NULLABLE')
self.assertEqual(len(field.fields), 0)

def test_name_property(self):
name = 'lemon-ness'
schema_field = self._make_one(name, 'INTEGER')
Expand Down

0 comments on commit 321e1f3

Please sign in to comment.