Skip to content

Commit 9b15771

Browse files
committed
[#90] When ids are missing or don't match, simply append to JSON output
There may be valid reasons people want to do this with flatten-tool. For the standards we support errors will be flagged by validation.
1 parent c0efb5b commit 9b15771

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

flattentool/input.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,18 @@ def unflatten(self):
161161
main_sheet_by_ocid[root_id_or_none].append(unflatten_main_with_parser(self.parser, line, self.timezone))
162162

163163
for sheet_name, lines in self.get_sub_sheets_lines():
164-
for line in lines:
164+
for i, line in enumerate(lines):
165+
lineno = i+1
165166
if all(x == '' for x in line.values()):
166167
continue
167168
root_id_or_none = line[self.root_id] if self.root_id else None
168169
unflattened = unflatten_main_with_parser(self.parser, line, self.timezone)
169-
try:
170+
if root_id_or_none not in main_sheet_by_ocid:
171+
main_sheet_by_ocid[root_id_or_none] = TemporaryDict('id')
172+
if 'id' in unflattened and unflattened['id'] in main_sheet_by_ocid[root_id_or_none]:
170173
merge(main_sheet_by_ocid[root_id_or_none][unflattened.get('id')], unflattened)
171-
except KeyError:
172-
pass
173-
# FIXME add an appropriate warning here
174+
else:
175+
main_sheet_by_ocid[root_id_or_none].append(unflattened)
174176

175177
temporarydicts_to_lists(main_sheet_by_ocid)
176178

flattentool/tests/test_input_SpreadsheetInput_unflatten_mulitplesheets.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ def test_nested_id(self):
169169
{'ocid': 1, 'id': 2, 'subField': [{'id': 3, 'testA': {'id': 4}}]}
170170
]
171171

172-
@pytest.mark.xfail
173172
def test_missing_columns(self, recwarn):
174173
spreadsheet_input = ListInput(
175174
sheets={
@@ -184,7 +183,7 @@ def test_missing_columns(self, recwarn):
184183
'ocid': 1,
185184
'id': '',
186185
'subField/0/id': 3,
187-
'subField/0/testA/id': 4,
186+
'subField/0/testA': 4,
188187
},
189188
{
190189
'ocid': 1,
@@ -197,13 +196,42 @@ def test_missing_columns(self, recwarn):
197196
main_sheet_name='custom_main')
198197
spreadsheet_input.read_sheets()
199198
unflattened = list(spreadsheet_input.unflatten())
200-
# We should have a warning about conflicting ID fields
201-
w = recwarn.pop(UserWarning)
202-
assert 'no parent id fields populated' in text_type(w.message)
203-
assert 'Line 2 of sheet sub' in text_type(w.message)
204199
# Check that following lines are parsed correctly
205200
assert unflattened == [
206-
{'ocid': 1, 'id': 2, 'subField': [{'id': 3, 'testA': 5}]}
201+
{'ocid': 1, 'id': 2, 'subField': [{'id': 3, 'testA': 5}]},
202+
{'ocid': 1, 'subField': [{'id': 3, 'testA': 4}]},
203+
]
204+
205+
def test_unmatched_id(self, recwarn):
206+
spreadsheet_input = ListInput(
207+
sheets={
208+
'custom_main': [
209+
{
210+
'ocid': 1,
211+
'id': 2,
212+
}
213+
],
214+
'sub': [
215+
{
216+
'ocid': 1,
217+
'id': 100,
218+
'subField/0/id': 3,
219+
'subField/0/testA': 4,
220+
},
221+
{
222+
'ocid': 1,
223+
'id': 2,
224+
'subField/0/id': 3,
225+
'subField/0/testA': 5,
226+
}
227+
]
228+
},
229+
main_sheet_name='custom_main')
230+
spreadsheet_input.read_sheets()
231+
unflattened = list(spreadsheet_input.unflatten())
232+
assert unflattened == [
233+
{'ocid': 1, 'id': 2, 'subField': [{'id': 3, 'testA': 5}]},
234+
{'ocid': 1, 'id': 100, 'subField': [{'id': 3, 'testA': 4}]},
207235
]
208236

209237

0 commit comments

Comments
 (0)