Skip to content

Commit

Permalink
Add nested Avro record schema inference test.
Browse files Browse the repository at this point in the history
Also sort inferred fields by name and add Python 3.5 to Travis matrix.
  • Loading branch information
mtth committed Aug 27, 2016
1 parent 3a06dde commit 4f08ae8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python:
- 2.7
- 3.3
- 3.4
- 3.5
install:
- pip install . fastavro coverage mock
before_script:
Expand Down
2 changes: 1 addition & 1 deletion hdfs/ext/avro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def infer(self, obj):
'type': 'record',
'fields': [
{'name': k, 'type': self.infer(v)}
for k, v in obj.items()
for k, v in sorted(obj.items()) # Sort fields by name.
]
}
raise ValueError('Cannot infer type from %s: %r' % (type(obj), obj))
Expand Down
37 changes: 32 additions & 5 deletions test/test_ext_avro.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,55 @@ def test_buffered_read(self):

class TestInferSchema(object):

def test_array(self):
eq_(
_SchemaInferrer().infer({'foo': 1, 'bar': ['hello']}),
{
'type': 'record',
'name': '__Record1',
'fields': [
{'type': {'type': 'array', 'items': 'string'}, 'name': 'bar'},
{'type': 'int', 'name': 'foo'},
]
}
)

def test_flat_record(self):
eq_(
_SchemaInferrer().infer({'foo': 1, 'bar': 'hello'}),
{
'type': 'record',
'name': '__Record1',
'fields': [
{'type': 'int', 'name': 'foo'},
{'type': 'string', 'name': 'bar'},
{'type': 'int', 'name': 'foo'},
]
}
)

def test_array(self):
def test_nested_record(self):
eq_(
_SchemaInferrer().infer({'foo': 1, 'bar': ['hello']}),
_SchemaInferrer().infer({'foo': {'bax': 2}, 'bar': {'baz': 3}}),
{
'type': 'record',
'name': '__Record1',
'fields': [
{'type': 'int', 'name': 'foo'},
{'type': {'type': 'array', 'items': 'string'}, 'name': 'bar'},
{
'type': {
'type': 'record',
'name': '__Record2',
'fields': [{'type': 'int', 'name': 'baz'}]
},
'name': 'bar',
},
{
'type': {
'type': 'record',
'name': '__Record3',
'fields': [{'type': 'int', 'name': 'bax'}]
},
'name': 'foo',
},
]
}
)
Expand Down

0 comments on commit 4f08ae8

Please sign in to comment.