Skip to content

Commit 5d8047e

Browse files
committed
[#90] Fix tests of flattening using the schema
1 parent 4bcf260 commit 5d8047e

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

flattentool/json_input.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,16 @@ def parse_json_dict(self, json_dict, sheet, json_key=None, parent_name='', flatt
143143
if self.rollup and parent_name == '': # Rollup only currently possible to main sheet
144144
if len(value) == 1:
145145
for k, v in value[0].items():
146-
if parent_name+key+'[]/'+k in self.schema_parser.main_sheet:
146+
if parent_name+key+'/0/'+k in self.schema_parser.main_sheet:
147147
if type(v) in BASIC_TYPES:
148-
flattened_dict[sheet_key(sheet, parent_name+key+'[]/'+k)] = v
148+
flattened_dict[sheet_key(sheet, parent_name+key+'/0/'+k)] = v
149149
else:
150150
raise ValueError('Rolled up values must be basic types')
151151
elif len(value) > 1:
152152
for k in set(sum((list(x.keys()) for x in value), [])):
153153
warn('More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.'.format(parent_name+key))
154-
if parent_name+key+'[]/'+k in self.schema_parser.main_sheet:
155-
flattened_dict[sheet_key(sheet, parent_name+key+'[]/'+k)] = 'WARNING: More than one value supplied, consult the relevant sub-sheet for the data.'
154+
if parent_name+key+'/0/'+k in self.schema_parser.main_sheet:
155+
flattened_dict[sheet_key(sheet, parent_name+key+'/0/'+k)] = 'WARNING: More than one value supplied, consult the relevant sub-sheet for the data.'
156156

157157
sub_sheet_name = key
158158
if sub_sheet_name not in self.sub_sheets:

flattentool/schema.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ def parse_schema_dict(self, parent_path, schema_dict, parent_id_fields=None, tit
193193
else:
194194
raise ValueError
195195
elif 'string' in property_type_set:
196-
self.flattened[parent_path+property_name] = "string"
196+
self.flattened[parent_path.replace('/0/', '/')+property_name] = "string"
197197
yield property_name, title
198198
elif 'number' in property_type_set:
199-
self.flattened[parent_path+property_name] = "number"
199+
self.flattened[parent_path.replace('/0/', '/')+property_name] = "number"
200200
yield property_name, title
201201
elif 'integer' in property_type_set:
202-
self.flattened[parent_path+property_name] = "integer"
202+
self.flattened[parent_path.replace('/0/', '/')+property_name] = "integer"
203203
yield property_name, title
204204
elif 'boolean' in property_type_set:
205-
self.flattened[parent_path+property_name] = "boolean"
205+
self.flattened[parent_path.replace('/0/', '/')+property_name] = "boolean"
206206
yield property_name, title
207207
else:
208208
warn('Unrecognised types {} for property "{}" with context "{}",'

flattentool/tests/test_json_input.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ def test_parse_ids_nested(self):
263263

264264

265265
class TestParseUsingSchema(object):
266-
@pytest.mark.xfail
267266
def test_sub_sheet_names(self, tmpdir):
268267
test_schema = tmpdir.join('test.json')
269268
test_schema.write('''{
@@ -298,8 +297,8 @@ def test_sub_sheet_names(self, tmpdir):
298297
{'a': 'b'}
299298
]
300299
assert len(parser.sub_sheets) == 1
301-
assert list(parser.sub_sheets['testB']) == list(['ocid', 'd', 'f'])
302-
assert parser.sub_sheets['testB'].lines == [{'d':'e'}]
300+
assert list(parser.sub_sheets['c']) == list(['ocid', 'c/0/d', 'c/0/f'])
301+
assert parser.sub_sheets['c'].lines == [{'c/0/d':'e'}]
303302

304303
def test_column_matching(self, tmpdir):
305304
test_schema = tmpdir.join('test.json')
@@ -328,7 +327,6 @@ def test_column_matching(self, tmpdir):
328327
]
329328
assert len(parser.sub_sheets) == 0
330329

331-
@pytest.mark.xfail
332330
def test_rollup(self):
333331
schema_parser = SchemaParser(root_schema_dict={
334332
'properties': {
@@ -353,15 +351,14 @@ def test_rollup(self):
353351
schema_parser=schema_parser
354352
)
355353
parser.parse()
356-
assert list(parser.main_sheet) == [ 'testA[]/testB' ]
354+
assert list(parser.main_sheet) == [ 'testA/0/testB' ]
357355
assert parser.main_sheet.lines == [
358-
{'testA[]/testB': '1'}
356+
{'testA/0/testB': '1'}
359357
]
360358
assert len(parser.sub_sheets) == 1
361-
assert set(parser.sub_sheets['testA']) == set(['ocid', 'testB', 'testC'])
362-
assert parser.sub_sheets['testA'].lines == [{'testB':'1', 'testC': '2'}]
359+
assert set(parser.sub_sheets['testA']) == set(['ocid', 'testA/0/testB', 'testA/0/testC'])
360+
assert parser.sub_sheets['testA'].lines == [{'testA/0/testB':'1', 'testA/0/testC': '2'}]
363361

364-
@pytest.mark.xfail
365362
def test_rollup_multiple_values(self, recwarn):
366363
schema_parser = SchemaParser(root_schema_dict={
367364
'properties': {
@@ -389,17 +386,17 @@ def test_rollup_multiple_values(self, recwarn):
389386
schema_parser=schema_parser
390387
)
391388
parser.parse()
392-
assert list(parser.main_sheet) == [ 'testA[]/testB' ]
389+
assert list(parser.main_sheet) == [ 'testA/0/testB' ]
393390
assert parser.main_sheet.lines == [
394391
{
395-
'testA[]/testB': 'WARNING: More than one value supplied, consult the relevant sub-sheet for the data.'
392+
'testA/0/testB': 'WARNING: More than one value supplied, consult the relevant sub-sheet for the data.'
396393
}
397394
]
398395
assert len(parser.sub_sheets) == 1
399-
assert set(parser.sub_sheets['testA']) == set(['ocid', 'testB', 'testC'])
396+
assert set(parser.sub_sheets['testA']) == set(['ocid', 'testA/0/testB', 'testA/0/testC'])
400397
assert parser.sub_sheets['testA'].lines == [
401-
{'testB':'1', 'testC': '2'},
402-
{'testB':'3', 'testC': '4'}
398+
{'testA/0/testB':'1', 'testA/0/testC': '2'},
399+
{'testA/0/testB':'3', 'testA/0/testC': '4'}
403400
]
404401
w = recwarn.pop(UserWarning)
405402
assert 'Could not provide rollup' in text_type(w.message)

0 commit comments

Comments
 (0)