Skip to content

Commit 83a32fa

Browse files
committed
[#90] Merge subsheets even when we don't have an id for the sub object
1 parent 068997e commit 83a32fa

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

flattentool/input.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ def merge(base, mergee):
7575
if key in base:
7676
if isinstance(value, TemporaryDict):
7777
for temporarydict_key, temporarydict_value in value.items():
78-
assert isinstance(base[key], TemporaryDict)
7978
if temporarydict_key in base[key]:
8079
merge(base[key][temporarydict_key], temporarydict_value)
8180
else:
8281
base[key][temporarydict_key] = temporarydict_value
82+
for temporarydict_value in value.items_no_keyfield:
83+
base[key].items_no_keyfield.append(temporarydict_value)
8384
elif isinstance(value, dict) and isinstance(base[key], dict):
8485
merge(base[key], value)
8586
elif base[key] != mergee[key]:

flattentool/tests/test_input_SpreadsheetInput_unflatten_mulitplesheets.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,40 @@ def test_basic_sub_sheet(self):
2222
{
2323
'ocid': 1,
2424
'id': 2,
25+
},
26+
{
27+
'ocid': 1,
28+
'id': 3,
2529
}
2630
],
2731
'sub': [
2832
{
2933
'ocid': 1,
3034
'id': 2,
3135
'subField/0/testA': 3,
36+
},
37+
{
38+
'ocid': 1,
39+
'id': 2,
40+
'subField/0/testA': 4,
3241
}
3342
]
3443
},
3544
main_sheet_name='custom_main')
3645
spreadsheet_input.read_sheets()
3746
assert list(spreadsheet_input.unflatten()) == [
38-
{'ocid': 1, 'id': 2, 'subField': [{'testA': 3}]}
47+
{
48+
'ocid': 1,
49+
'id': 2,
50+
'subField': [
51+
{'testA': 3},
52+
{'testA': 4},
53+
]
54+
},
55+
{
56+
'ocid': 1,
57+
'id': 3
58+
}
3959
]
4060

4161
@pytest.mark.parametrize('nested_id_in_subsheet', [True, False])
@@ -82,6 +102,10 @@ def test_basic_two_sub_sheets(self):
82102
OrderedDict([
83103
('ocid', 1),
84104
('id', 2),
105+
]),
106+
OrderedDict([
107+
('ocid', 1),
108+
('id', 6),
85109
])
86110
],
87111
'sub1': [
@@ -104,7 +128,7 @@ def test_basic_two_sub_sheets(self):
104128
main_sheet_name='custom_main')
105129
spreadsheet_input.read_sheets()
106130
unflattened = list(spreadsheet_input.unflatten())
107-
assert len(unflattened) == 1
131+
assert len(unflattened) == 2
108132
assert set(unflattened[0]) == set(['ocid', 'id', 'sub1Field']) # FIXME should be ordered
109133
assert unflattened[0]['ocid'] == 1
110134
assert unflattened[0]['id'] == 2
@@ -119,6 +143,7 @@ def test_basic_two_sub_sheets(self):
119143
]
120144
}
121145
]
146+
assert unflattened[1] == {'ocid':1 , 'id':6}
122147

123148
def test_nested_id(self):
124149
spreadsheet_input = ListInput(
@@ -190,16 +215,28 @@ def test_same_rollup(self, recwarn):
190215
{
191216
'ocid': 1,
192217
'id': 2,
193-
'testA/0/id': 3,
194-
'testA/0/testB': 4
218+
'testC': 3,
219+
'testA/0/id': 4,
220+
'testA/0/testB': 5,
221+
},
222+
{
223+
'ocid': 6,
224+
'id': 7,
225+
'testC': 8,
226+
'testA/0/testB': 9,
195227
}
196228
],
197229
'testA': [
198230
{
199231
'ocid': 1,
200232
'id': 2,
201-
'testA/0/id': 3,
202-
'testA/0/testB': 4,
233+
'testA/0/id': 4,
234+
'testA/0/testB': 5,
235+
},
236+
{
237+
'ocid': 6,
238+
'id': 7,
239+
'testA/0/testB': 9,
203240
}
204241
]
205242
},
@@ -208,7 +245,8 @@ def test_same_rollup(self, recwarn):
208245
spreadsheet_input.read_sheets()
209246
unflattened = list(spreadsheet_input.unflatten())
210247
assert unflattened == [
211-
{'ocid': 1, 'id': 2, 'testA': [{'id': 3, 'testB': 4}]}
248+
{'ocid': 1, 'id': 2, 'testC':3, 'testA': [{'id': 4, 'testB': 5}]},
249+
{'ocid': 6, 'id': 7, 'testC':8, 'testA': [{'testB': 9}, {'testB': 9}]}, # FIXME rollup with ID causes duplicates
212250
]
213251
# We expect no warnings
214252
assert recwarn.list == []

0 commit comments

Comments
 (0)