Skip to content

Commit b29bccb

Browse files
committed
fix tests
1 parent 9f52bc1 commit b29bccb

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

elasticgit/commands/avro.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@ def field_class_for(self, field, field_mapping):
214214
return field_mapping[field_name].__name__
215215

216216
if isinstance(field_type, dict):
217+
print 'field dict', field
217218
return self.field_class_for_complex_type(field)
218219
if isinstance(field_type, list):
220+
print 'field list', field
219221
return self.field_class_for_core_type(field_type)
220222

221223
return self.core_mapping[field_type].__name__
@@ -227,9 +229,13 @@ def field_class_for_core_type(self, core_types):
227229
return self.core_mapping[not_null_type].__name__
228230

229231
def field_class_for_complex_type(self, field):
230-
field_type = field['type']
232+
field_type = field['type']['type']
233+
if isinstance(field_type, list):
234+
[field_type] = [ft
235+
for ft in field_type
236+
if ft != "null"]
231237
handler = getattr(
232-
self, 'field_class_for_complex_%(type)s_type' % field_type)
238+
self, 'field_class_for_complex_%s_type' % (field_type,))
233239
return handler(field)
234240

235241
def field_class_for_complex_record_type(self, field):
@@ -376,10 +382,10 @@ def map_ListField_type(self, field):
376382
'type': 'array',
377383
'name': field.name,
378384
'namespace': field.__class__.__module__,
379-
'items': reduce(
385+
'items': list(set(reduce(
380386
lambda a, b: a + b,
381387
[self.map_field_to_type(fld) for fld in field.fields],
382-
[]),
388+
[]))),
383389
}
384390

385391
def map_DictField_type(self, field):

elasticgit/commands/tests/test_avro.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class TestModel(models.Model):
5959
tags = self.get_field(schema, 'tags')
6060
field_type = tags['type']
6161
self.assertEqual(field_type['type'], 'array')
62-
self.assertEqual(field_type['items'], ['int'])
62+
self.assertEqual(set(field_type['items']), set(['null', 'int']))
6363

6464

6565
class TestLoadSchemaTool(ToolBaseTest):
@@ -139,7 +139,7 @@ def test_array_field(self):
139139
self.assertFieldCreation({
140140
'name': 'array',
141141
'type': {
142-
'type': 'array',
142+
'type': ['null', 'array'],
143143
'items': ['string'],
144144
},
145145
'doc': 'The Array',
@@ -154,7 +154,7 @@ def test_dict_field(self):
154154
'items': ['string'],
155155
'fields': [{
156156
'name': 'hello',
157-
'type': 'string',
157+
'type': ['null', 'string'],
158158
}]
159159
},
160160
'doc': 'The Object',
@@ -171,7 +171,7 @@ def test_complex_field(self):
171171
'items': ['string'],
172172
'fields': [{
173173
'name': 'foo',
174-
'type': 'string',
174+
'type': ['null', 'string'],
175175
}]
176176
},
177177
'doc': 'Super Complex',
@@ -223,6 +223,7 @@ def test_two_way(self):
223223
schema_loader = self.mk_schema_loader()
224224

225225
schema = schema_dumper.dump_schema(DumpAndLoadModel)
226+
print 'schema', schema
226227

227228
generated_code = schema_loader.generate_model(json.loads(schema))
228229

elasticgit/templates/model_generator.py.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class {{model_class_for(schema.name)}}(models.Model):
2525
{%- if is_complex(field) -%}, fields=(
2626
{%- set sub_type = field['type'] %}
2727
{%- if sub_type['type'] == 'array' %}
28-
{% for item in sub_type['items'] %}
29-
models.{{field_class_for_core_type(item)}}('{{item}}'),{%- endfor %}
28+
{% for item in sub_type['items'] %}{% if item != 'null' %}
29+
models.{{field_class_for_core_type([item])}}('{{item}}'),{% endif %}{%- endfor %}
3030
{%- elif sub_type['type'] == 'record' %}
3131
{% for field in sub_type['fields'] %}
3232
models.{{field_class_for_core_type(field.type)}}('{{field.name}}', name='{{field.name}}'),{%- endfor %}

0 commit comments

Comments
 (0)