Skip to content

Commit b773078

Browse files
committed
[#85] Add some more tests, fix header lookup.
Some tests to show json pointer numbering. Fixes to title lookup with multiple arrays.
1 parent 17c1bc0 commit b773078

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

flattentool/schema.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ def lookup_header(self, title_header):
3030
def lookup_header_list(self, title_header_list):
3131
first_title = title_header_list[0]
3232
remaining_titles = title_header_list[1:]
33-
if isinstance(first_title, int):
34-
return first_title + '/' + self[first_title].lookup_header_list(remaining_titles)
33+
try:
34+
int(first_title)
35+
return first_title + '/' + self.lookup_header_list(remaining_titles)
36+
except ValueError:
37+
pass
38+
3539
if first_title in self:
3640
if remaining_titles:
3741
return self[first_title].property_name + '/' + self[first_title].lookup_header_list(remaining_titles)
@@ -51,7 +55,7 @@ def __getitem__(self, key):
5155
raise KeyError
5256
else:
5357
return self.data[key.replace(' ', '').lower()]
54-
58+
5559
def __contains__(self, key):
5660
if key is None:
5761
return False
@@ -129,7 +133,7 @@ def parse_schema_dict(self, parent_name, parent_path, schema_dict, parent_id_fie
129133
yield (
130134
property_name+'/'+field,
131135
# TODO ambiguous use of "title"
132-
(title+':'+child_title if title and child_title else None)
136+
(title+':'+child_title if title and child_title else None)
133137
)
134138

135139
elif 'array' in property_type_set:

flattentool/tests/test_input_SpreadsheetInput_unflatten.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,60 @@ def inject_root_id(root_id, d):
138138
)
139139
]
140140

141+
testdata_pointer = [
142+
# Single item array without json numbering
143+
(
144+
[{
145+
'ROOT_ID': '1',
146+
'testR/id': '2',
147+
'testR/testB': '3',
148+
'testR/testX': '3',
149+
}],
150+
[{
151+
'ROOT_ID': '1',
152+
'testR': [{
153+
'id': '2',
154+
'testB': '3',
155+
'testX': '3'
156+
}]
157+
}]
158+
),
159+
# Multi item array one with varied numbering
160+
(
161+
[{
162+
'ROOT_ID': '1',
163+
'testR/id': '-1',
164+
'testR/testB': '-1',
165+
'testR/testX': '-2',
166+
'testR/0/id': '0',
167+
'testR/0/testB': '1',
168+
'testR/0/testX': '1',
169+
'testR/5/id': '5',
170+
'testR/5/testB': '5',
171+
'testR/5/testX': '6',
172+
}],
173+
[{
174+
'ROOT_ID': '1',
175+
'testR': [{
176+
'id': '-1',
177+
'testB': '-1',
178+
'testX': '-2'
179+
},
180+
{
181+
'id': '0',
182+
'testB': '1',
183+
'testX': '1'
184+
},
185+
{
186+
'id': '5',
187+
'testB': '5',
188+
'testX': '6'
189+
}
190+
]
191+
}]
192+
),
193+
]
194+
141195
def create_schema(root_id):
142196
schema = {
143197
'properties': {
@@ -336,6 +390,36 @@ def create_schema(root_id):
336390
}]
337391
}]
338392
),
393+
# Multi item array, allow numbering
394+
(
395+
[{
396+
'ROOT_ID_TITLE': 1,
397+
'Identifier': 2,
398+
'R title:C title': 3,
399+
'R title:Not in schema': 4,
400+
'R title:0:C title': 5,
401+
'R title:0:Not in schema': 6,
402+
'R title:5:C title': 7,
403+
'R title:5:Not in schema': 8,
404+
}],
405+
[{
406+
'ROOT_ID': 1,
407+
'id': 2,
408+
'testR': [{
409+
'testC': '3',
410+
'Not in schema': 4
411+
},
412+
{
413+
'testC': '5',
414+
'Not in schema': 6
415+
},
416+
{
417+
'testC': '7',
418+
'Not in schema': 8
419+
}
420+
]
421+
}]
422+
),
339423
# Empty
340424
(
341425
[{
@@ -414,6 +498,13 @@ def test_unflatten(convert_titles, use_schema, root_id, root_id_kwargs, input_li
414498
assert recwarn.list == []
415499

416500

501+
@pytest.mark.parametrize('convert_titles', [True, False])
502+
@pytest.mark.parametrize('root_id,root_id_kwargs', ROOT_ID_PARAMS)
503+
@pytest.mark.parametrize('input_list,expected_output_list', testdata_pointer)
504+
def test_unflatten_pointer(convert_titles, root_id, root_id_kwargs, input_list, expected_output_list, recwarn):
505+
return test_unflatten(convert_titles=convert_titles, use_schema=True, root_id=root_id, root_id_kwargs=root_id_kwargs, input_list=input_list, expected_output_list=expected_output_list, recwarn=recwarn)
506+
507+
417508
@pytest.mark.parametrize('input_list,expected_output_list', testdata_titles)
418509
@pytest.mark.parametrize('root_id,root_id_kwargs', ROOT_ID_PARAMS)
419510
def test_unflatten_titles(root_id, root_id_kwargs, input_list, expected_output_list, recwarn):

0 commit comments

Comments
 (0)