Skip to content

Commit f14643f

Browse files
committed
Split out titles test cases from the others
1 parent 8e4ff34 commit f14643f

File tree

1 file changed

+141
-64
lines changed

1 file changed

+141
-64
lines changed

flattentool/tests/test_input_SpreadsheetInput_unflatten.py

Lines changed: 141 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,49 @@
1616
import copy
1717
from six import text_type
1818

19+
ROOT_ID_TITLES = {
20+
'ocid': 'Open Contracting ID',
21+
'custom': 'Custom'
22+
}
23+
1924
def inject_root_id(root_id, d):
2025
"""
2126
Insert the appropriate root id, with the given value, into the dictionary d and return.
2227
"""
2328
d = copy.copy(d)
24-
if root_id != '':
25-
d.update({root_id: d['ROOT_ID']})
26-
del d['ROOT_ID']
29+
if 'ROOT_ID' in d:
30+
if root_id != '':
31+
d.update({root_id: d['ROOT_ID']})
32+
del d['ROOT_ID']
33+
if 'ROOT_ID_TITLE' in d:
34+
if root_id != '':
35+
d.update({ROOT_ID_TITLES[root_id]: d['ROOT_ID_TITLE']})
36+
del d['ROOT_ID_TITLE']
2737
return d
2838

29-
UNICODE_TEST_STRING = 'éαГ😼𝒞人'
3039

3140
# TODO Actually create appropriate schema
3241
SCHEMA = {}
3342

43+
UNICODE_TEST_STRING = 'éαГ😼𝒞人'
3444
# ROOT_ID will be replace by the appropirate root_id name in the test (e.g. ocid)
45+
3546
testdata = [
36-
# Flat
47+
# Basic flat
3748
(
38-
[{
39-
'ROOT_ID': 1,
40-
'Identifier': 2,
41-
'testA': 3
42-
}],
4349
[{
4450
'ROOT_ID': 1,
4551
'id': 2,
4652
'testA': 3
4753
}],
48-
[{'ROOT_ID': 1, 'id': 2, 'testA': 3}]
54+
[{
55+
'ROOT_ID': 1,
56+
'id': 2,
57+
'testA': 3
58+
}]
4959
),
5060
# Nested
5161
(
52-
[{
53-
'ROOT_ID': 1,
54-
'Identifier': 2,
55-
'testA/testB': 3,
56-
'testA/testC': 4,
57-
}],
5862
[{
5963
'ROOT_ID': 1,
6064
'id': 2,
@@ -69,10 +73,6 @@ def inject_root_id(root_id, d):
6973
),
7074
# Unicode
7175
(
72-
[{
73-
'ROOT_ID': UNICODE_TEST_STRING,
74-
'A title': UNICODE_TEST_STRING
75-
}],
7676
[{
7777
'ROOT_ID': UNICODE_TEST_STRING,
7878
'testA': UNICODE_TEST_STRING
@@ -84,12 +84,6 @@ def inject_root_id(root_id, d):
8484
),
8585
# Rollup
8686
(
87-
[{
88-
'ROOT_ID': 1,
89-
'Identifier': 2,
90-
'testA[]/id': 3,
91-
'testA[]/testB': 4
92-
}],
9387
[{
9488
'ROOT_ID': 1,
9589
'id': 2,
@@ -104,11 +98,6 @@ def inject_root_id(root_id, d):
10498
),
10599
# Rollup without an ID
106100
(
107-
[{
108-
'ROOT_ID': '1',
109-
'Identifier': '2',
110-
'testA[]/testB': '3',
111-
}],
112101
[{
113102
'ROOT_ID': '1',
114103
'testA[]/id': '2',
@@ -124,15 +113,6 @@ def inject_root_id(root_id, d):
124113
),
125114
# Empty
126115
(
127-
[{
128-
'ROOT_ID': '',
129-
'Identifier': '',
130-
'testA:number': '',
131-
'testB:boolean': '',
132-
'testC:array': '',
133-
'testD:string': '',
134-
'testE': '',
135-
}],
136116
[{
137117
'ROOT_ID': '',
138118
'id:integer': '',
@@ -148,47 +128,132 @@ def inject_root_id(root_id, d):
148128
(
149129
[{
150130
'ROOT_ID': 1,
151-
'Identifier': '',
131+
'id:integer': '',
152132
'testA:number': '',
153133
'testB:boolean': '',
154134
'testC:array': '',
155135
'testD:string': '',
156136
'testE': '',
157137
}],
138+
[{
139+
'ROOT_ID': 1
140+
}]
141+
)
142+
]
143+
144+
testdata_titles = [
145+
# Basic flat
146+
pytest.mark.xfail((
147+
[{
148+
'ROOT_ID': 1,
149+
'Identifier': 2,
150+
'testA': 3
151+
}],
152+
[{
153+
'ROOT_ID': 1,
154+
'id': 2,
155+
'testA': 3
156+
}]
157+
)),
158+
# Nested
159+
pytest.mark.xfail((
158160
[{
159161
'ROOT_ID': 1,
160-
'id:integer': '',
161-
'testA:number': '',
162-
'testB:boolean': '',
163-
'testC:array': '',
164-
'testD:string': '',
165-
'testE': '',
162+
'id': 2,
163+
'testA/testB': 3,
164+
'testA/testC': 4,
165+
}],
166+
[{
167+
'ROOT_ID': 1,
168+
'id': 2,
169+
'testA': {'testB': 3, 'testC': 4}
170+
}]
171+
)),
172+
# Unicode
173+
pytest.mark.xfail((
174+
[{
175+
'ROOT_ID': UNICODE_TEST_STRING,
176+
'testA': UNICODE_TEST_STRING
177+
}],
178+
[{
179+
'ROOT_ID': UNICODE_TEST_STRING,
180+
'testA': UNICODE_TEST_STRING
181+
}]
182+
)),
183+
# Rollup
184+
pytest.mark.xfail((
185+
[{
186+
'ROOT_ID': 1,
187+
'id': 2,
188+
'testA[]/id': 3,
189+
'testA[]/testB': 4
190+
}],
191+
[{
192+
'ROOT_ID': 1, 'id': 2, 'testA': [{
193+
'id': 3, 'testB': 4
194+
}]
195+
}]
196+
)),
197+
# Rollup without an ID
198+
pytest.mark.xfail((
199+
[{
200+
'ROOT_ID': '1',
201+
'testA[]/id': '2',
202+
'testA[]/testB': '3',
203+
}],
204+
[{
205+
'ROOT_ID': '1',
206+
'testA': [{
207+
'id': '2',
208+
'testB': '3'
209+
}]
210+
}]
211+
)),
212+
# Empty
213+
(
214+
[{
215+
'ROOT_ID_TITLE': '',
216+
'Identifier': '',
217+
'A title': '',
218+
'B title': '',
219+
'C title': '',
220+
'D title': '',
221+
'E title': '',
222+
}],
223+
[]
224+
),
225+
# Empty except for root id
226+
pytest.mark.xfail((
227+
[{
228+
'ROOT_ID_TITLE': 1,
229+
'Identifier': '',
230+
'A title': '',
231+
'B title': '',
232+
'C title': '',
233+
'D title': '',
234+
'E title': '',
166235
}],
167236
[{
168237
'ROOT_ID': 1
169238
}]
170-
)
239+
))
171240
]
172241

173-
# Convert titles modes: with appropirate schema, without, off
174-
@pytest.mark.parametrize('convert_titles,use_schema,use_input_titles', [
175-
(False, False, False), # Test without titles support at all
176-
(True, False, False), # Test that non-titles convert properly with convert_titles on
177-
(True, True, False), # Test that non-titles convert properly with
178-
# convert_titles on, and an appropriate schema
179-
pytest.mark.xfail((True, True, True)), # Test that actual titles convert
180-
])
181-
@pytest.mark.parametrize('root_id,root_id_kwargs',
182-
[
242+
ROOT_ID_PARAMS = [
183243
('ocid', {}), # If not root_id kwarg is passed, then a root_id of ocid is assumed
184244
('ocid', {'root_id': 'ocid'}),
185245
('custom', {'root_id': 'custom'}),
186246
('', {'root_id': ''})
187-
])
188-
@pytest.mark.parametrize('input_list_titles,input_list,,expected_output_list', testdata)
189-
def test_unflatten(convert_titles, use_schema, use_input_titles, root_id, root_id_kwargs, input_list, input_list_titles, expected_output_list, recwarn):
190-
if use_input_titles:
191-
input_list = input_list_titles
247+
]
248+
249+
# Since we're not using titles, and titles mode should fall back to assuming
250+
# we've supplied a fieldname, we should be able to run this test with
251+
# convert_titles and use_schema as True or False
252+
@pytest.mark.parametrize('convert_titles', [True, False])
253+
@pytest.mark.parametrize('use_schema', [True, False])
254+
@pytest.mark.parametrize('root_id,root_id_kwargs', ROOT_ID_PARAMS)
255+
@pytest.mark.parametrize('input_list,expected_output_list', testdata)
256+
def test_unflatten(convert_titles, use_schema, root_id, root_id_kwargs, input_list, expected_output_list, recwarn):
192257
extra_kwargs = {'convert_titles': convert_titles}
193258
extra_kwargs.update(root_id_kwargs)
194259
spreadsheet_input = ListInput(
@@ -218,3 +283,15 @@ def test_unflatten(convert_titles, use_schema, use_input_titles, root_id, root_i
218283
if not convert_titles: # TODO what are the warnings here
219284
assert recwarn.list == []
220285

286+
287+
@pytest.mark.parametrize('root_id,root_id_kwargs', ROOT_ID_PARAMS)
288+
@pytest.mark.parametrize('input_list,expected_output_list', testdata_titles)
289+
def test_unflatten_titles(root_id, root_id_kwargs, input_list, expected_output_list, recwarn):
290+
"""
291+
Essentially the same as test unflatten, except that convert_titles and
292+
use_schema are always true, as both of these are needed to convert titles
293+
properly. (and runs with different test data).
294+
"""
295+
return test_unflatten(convert_titles=True, 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)
296+
297+

0 commit comments

Comments
 (0)