16
16
import copy
17
17
from six import text_type
18
18
19
+ ROOT_ID_TITLES = {
20
+ 'ocid' : 'Open Contracting ID' ,
21
+ 'custom' : 'Custom'
22
+ }
23
+
19
24
def inject_root_id (root_id , d ):
20
25
"""
21
26
Insert the appropriate root id, with the given value, into the dictionary d and return.
22
27
"""
23
28
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' ]
27
37
return d
28
38
29
- UNICODE_TEST_STRING = 'éαГ😼𝒞人'
30
39
31
40
# TODO Actually create appropriate schema
32
41
SCHEMA = {}
33
42
43
+ UNICODE_TEST_STRING = 'éαГ😼𝒞人'
34
44
# ROOT_ID will be replace by the appropirate root_id name in the test (e.g. ocid)
45
+
35
46
testdata = [
36
- # Flat
47
+ # Basic flat
37
48
(
38
- [{
39
- 'ROOT_ID' : 1 ,
40
- 'Identifier' : 2 ,
41
- 'testA' : 3
42
- }],
43
49
[{
44
50
'ROOT_ID' : 1 ,
45
51
'id' : 2 ,
46
52
'testA' : 3
47
53
}],
48
- [{'ROOT_ID' : 1 , 'id' : 2 , 'testA' : 3 }]
54
+ [{
55
+ 'ROOT_ID' : 1 ,
56
+ 'id' : 2 ,
57
+ 'testA' : 3
58
+ }]
49
59
),
50
60
# Nested
51
61
(
52
- [{
53
- 'ROOT_ID' : 1 ,
54
- 'Identifier' : 2 ,
55
- 'testA/testB' : 3 ,
56
- 'testA/testC' : 4 ,
57
- }],
58
62
[{
59
63
'ROOT_ID' : 1 ,
60
64
'id' : 2 ,
@@ -69,10 +73,6 @@ def inject_root_id(root_id, d):
69
73
),
70
74
# Unicode
71
75
(
72
- [{
73
- 'ROOT_ID' : UNICODE_TEST_STRING ,
74
- 'A title' : UNICODE_TEST_STRING
75
- }],
76
76
[{
77
77
'ROOT_ID' : UNICODE_TEST_STRING ,
78
78
'testA' : UNICODE_TEST_STRING
@@ -84,12 +84,6 @@ def inject_root_id(root_id, d):
84
84
),
85
85
# Rollup
86
86
(
87
- [{
88
- 'ROOT_ID' : 1 ,
89
- 'Identifier' : 2 ,
90
- 'testA[]/id' : 3 ,
91
- 'testA[]/testB' : 4
92
- }],
93
87
[{
94
88
'ROOT_ID' : 1 ,
95
89
'id' : 2 ,
@@ -104,11 +98,6 @@ def inject_root_id(root_id, d):
104
98
),
105
99
# Rollup without an ID
106
100
(
107
- [{
108
- 'ROOT_ID' : '1' ,
109
- 'Identifier' : '2' ,
110
- 'testA[]/testB' : '3' ,
111
- }],
112
101
[{
113
102
'ROOT_ID' : '1' ,
114
103
'testA[]/id' : '2' ,
@@ -124,15 +113,6 @@ def inject_root_id(root_id, d):
124
113
),
125
114
# Empty
126
115
(
127
- [{
128
- 'ROOT_ID' : '' ,
129
- 'Identifier' : '' ,
130
- 'testA:number' : '' ,
131
- 'testB:boolean' : '' ,
132
- 'testC:array' : '' ,
133
- 'testD:string' : '' ,
134
- 'testE' : '' ,
135
- }],
136
116
[{
137
117
'ROOT_ID' : '' ,
138
118
'id:integer' : '' ,
@@ -148,47 +128,132 @@ def inject_root_id(root_id, d):
148
128
(
149
129
[{
150
130
'ROOT_ID' : 1 ,
151
- 'Identifier ' : '' ,
131
+ 'id:integer ' : '' ,
152
132
'testA:number' : '' ,
153
133
'testB:boolean' : '' ,
154
134
'testC:array' : '' ,
155
135
'testD:string' : '' ,
156
136
'testE' : '' ,
157
137
}],
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 ((
158
160
[{
159
161
'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' : '' ,
166
235
}],
167
236
[{
168
237
'ROOT_ID' : 1
169
238
}]
170
- )
239
+ ))
171
240
]
172
241
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 = [
183
243
('ocid' , {}), # If not root_id kwarg is passed, then a root_id of ocid is assumed
184
244
('ocid' , {'root_id' : 'ocid' }),
185
245
('custom' , {'root_id' : 'custom' }),
186
246
('' , {'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 ):
192
257
extra_kwargs = {'convert_titles' : convert_titles }
193
258
extra_kwargs .update (root_id_kwargs )
194
259
spreadsheet_input = ListInput (
@@ -218,3 +283,15 @@ def test_unflatten(convert_titles, use_schema, use_input_titles, root_id, root_i
218
283
if not convert_titles : # TODO what are the warnings here
219
284
assert recwarn .list == []
220
285
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