Skip to content

Commit 363995d

Browse files
committed
[#76] Emit warning if field in rollUp doesn't exist in the schema
1 parent d294619 commit 363995d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

flattentool/schema.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ def parse_schema_dict(self, parent_name, schema_dict, parent_id_fields=None):
9999
fields = self.parse_schema_dict(parent_name+'/'+property_name+'[]',
100100
property_schema_dict['items'],
101101
parent_id_fields=id_fields)
102+
103+
rolledUp = set()
104+
102105
for field, child_title in fields:
103106
if self.use_titles:
104107
if not child_title:
@@ -110,7 +113,14 @@ def parse_schema_dict(self, parent_name, schema_dict, parent_id_fields=None):
110113
if child_title:
111114
self.sub_sheets[sub_sheet_name].titles[child_title] = field
112115
if self.rollup and 'rollUp' in property_schema_dict and field in property_schema_dict['rollUp']:
116+
rolledUp.add(field)
113117
yield property_name+'[]/'+field, (title+':'+child_title if title and child_title else None)
118+
119+
# Check that all items in rollUp are in the schema
120+
if self.rollup and 'rollUp' in property_schema_dict:
121+
missedRollUp = set(property_schema_dict['rollUp']) - rolledUp
122+
if missedRollUp:
123+
warn('{} in rollUp but not in schema'.format(', '.join(missedRollUp)))
114124
else:
115125
raise ValueError
116126
elif 'string' in property_type_set:

flattentool/tests/test_schema_parser.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,35 @@ def test_rollup():
341341
assert set(parser.sub_sheets) == set(['testA'])
342342
assert set(parser.sub_sheets['testA']) == set(['ocid', 'testB', 'testC'])
343343

344+
def test_bad_rollup(recwarn):
345+
'''
346+
When rollUp is specified, but the field is missing in the schema, we expect
347+
a warning.
348+
349+
'''
350+
parser = SchemaParser(root_schema_dict={
351+
'properties': {
352+
'testA': {
353+
'type': 'array',
354+
'rollUp': [ 'testB' ],
355+
'items': {
356+
'type': 'object',
357+
'properties': {
358+
'testC': type_string
359+
}
360+
}
361+
},
362+
}
363+
}, rollup=True)
364+
parser.parse()
365+
366+
w = recwarn.pop(UserWarning)
367+
assert 'testB in rollUp but not in schema' in text_type(w.message)
368+
369+
assert set(parser.main_sheet) == set()
370+
assert set(parser.sub_sheets) == set(['testA'])
371+
assert set(parser.sub_sheets['testA']) == set(['ocid', 'testC'])
372+
344373

345374
def test_sub_sheet_custom_id():
346375
parser = SchemaParser(root_schema_dict={

0 commit comments

Comments
 (0)