12
12
import pytest
13
13
import openpyxl
14
14
import datetime
15
+ import copy
15
16
from six import text_type
16
17
17
- def inject_root_id (root_id , d , value = 1 ):
18
+ def inject_root_id (root_id , d ):
18
19
"""
19
20
Insert the appropriate root id, with the given value, into the dictionary d and return.
20
21
"""
21
- d .update ({root_id : value })
22
+ d = copy .copy (d )
23
+ if root_id != '' :
24
+ d .update ({root_id : d ['ROOT_ID' ]})
25
+ del d ['ROOT_ID' ]
22
26
return d
23
27
28
+ UNICODE_TEST_STRING = 'éαГ😼𝒞人'
29
+
30
+ # ROOT_ID will be replace by the appropirate root_id name in the test (e.g. ocid)
31
+ testdata = [
32
+ # Flat
33
+ (
34
+ [{
35
+ 'ROOT_ID' : 1 ,
36
+ 'id' : 2 ,
37
+ 'testA' : 3
38
+ }],
39
+ [{'ROOT_ID' : 1 , 'id' : 2 , 'testA' : 3 }]
40
+ ),
41
+ # Nested
42
+ (
43
+ [{
44
+ 'ROOT_ID' : 1 ,
45
+ 'id' : 2 ,
46
+ 'testA/testB' : 3 ,
47
+ 'testA/testC' : 4 ,
48
+ }],
49
+ [{'ROOT_ID' : 1 , 'id' : 2 , 'testA' : {'testB' : 3 , 'testC' : 4 }}]
50
+ ),
51
+ # Unicode
52
+ (
53
+ [{
54
+ 'ROOT_ID' : UNICODE_TEST_STRING ,
55
+ 'testA' : UNICODE_TEST_STRING
56
+ }],
57
+ [{'ROOT_ID' : UNICODE_TEST_STRING , 'testA' : UNICODE_TEST_STRING }]
58
+ ),
59
+ # Rollup
60
+ (
61
+ [{
62
+ 'ROOT_ID' : 1 ,
63
+ 'id' : 2 ,
64
+ 'testA[]/id' : 3 ,
65
+ 'testA[]/testB' : 4
66
+ }],
67
+ [{'ROOT_ID' : 1 , 'id' : 2 , 'testA' : [{'id' : 3 , 'testB' : 4 }]}]
68
+ ),
69
+ # Rollup without an ID
70
+ (
71
+ [{
72
+ 'ROOT_ID' : '1' ,
73
+ 'testA[]/id' : '2' ,
74
+ 'testA[]/testB' : '3' ,
75
+ }],
76
+ [{
77
+ 'ROOT_ID' : '1' ,
78
+ 'testA' : [{'id' : '2' , 'testB' : '3' }]
79
+ }]
80
+ ),
81
+ # Empty
82
+ (
83
+ [{
84
+ 'ROOT_ID' : '' ,
85
+ 'id:integer' : '' ,
86
+ 'testA:number' : '' ,
87
+ 'testB:boolean' : '' ,
88
+ 'testC:array' : '' ,
89
+ 'testD:string' : '' ,
90
+ 'testE' : '' ,
91
+ }],
92
+ []
93
+ ),
94
+ # Empty except for root id
95
+ (
96
+ [{
97
+ 'ROOT_ID' : 1 ,
98
+ 'id:integer' : '' ,
99
+ 'testA:number' : '' ,
100
+ 'testB:boolean' : '' ,
101
+ 'testC:array' : '' ,
102
+ 'testD:string' : '' ,
103
+ 'testE' : '' ,
104
+ }],
105
+ [{'ROOT_ID' : 1 }]
106
+ )
107
+ ]
24
108
25
109
@pytest .mark .parametrize ('root_id,root_id_kwargs' ,
26
110
[
@@ -29,141 +113,24 @@ def inject_root_id(root_id, d, value=1):
29
113
('custom' , {'root_id' : 'custom' }),
30
114
('' , {'root_id' : '' })
31
115
])
32
- class TestUnflatten (object ):
33
- def test_main_sheet_flat (self , root_id , root_id_kwargs ):
34
- spreadsheet_input = ListInput (
35
- sheets = {
36
- 'custom_main' : [
37
- inject_root_id (root_id , {
38
- 'id' : 2 ,
39
- 'testA' : 3 ,
40
- })
41
- ]
42
- },
43
- main_sheet_name = 'custom_main' ,
44
- ** root_id_kwargs )
45
- spreadsheet_input .read_sheets ()
46
- assert list (spreadsheet_input .unflatten ()) == [
47
- inject_root_id (root_id , {'id' : 2 , 'testA' : 3 })
48
- ]
49
-
50
- def test_main_sheet_nonflat (self , root_id , root_id_kwargs ):
51
- spreadsheet_input = ListInput (
52
- sheets = {
53
- 'custom_main' : [
54
- inject_root_id (root_id , {
55
- 'id' : 2 ,
56
- 'testA/testB' : 3 ,
57
- 'testA/testC' : 4 ,
58
- })
59
- ]
60
- },
61
- main_sheet_name = 'custom_main' ,
62
- ** root_id_kwargs )
63
- spreadsheet_input .read_sheets ()
64
- assert list (spreadsheet_input .unflatten ()) == [
65
- inject_root_id (root_id , {'id' : 2 , 'testA' : {'testB' : 3 , 'testC' : 4 }})
66
- ]
67
-
68
- def test_unicode (self , root_id , root_id_kwargs ):
69
- unicode_string = 'éαГ😼𝒞人'
70
- spreadsheet_input = ListInput (
71
- sheets = {
72
- 'custom_main' : [
73
- inject_root_id (root_id , {
74
- 'testA' : unicode_string ,
75
- })
76
- ]
77
- },
78
- main_sheet_name = 'custom_main' ,
79
- ** root_id_kwargs )
80
- spreadsheet_input .read_sheets ()
81
- assert list (spreadsheet_input .unflatten ()) == [
82
- inject_root_id (root_id , {'testA' : unicode_string })
83
- ]
84
-
85
- def test_rollup (self , recwarn , root_id , root_id_kwargs ):
86
- spreadsheet_input = ListInput (
87
- sheets = {
88
- 'main' : [
89
- inject_root_id (root_id , {
90
- 'id' : 2 ,
91
- 'testA[]/id' : 3 ,
92
- 'testA[]/testB' : 4
93
- })
94
- ]
95
- },
96
- main_sheet_name = 'main' ,
97
- ** root_id_kwargs
98
- )
99
- spreadsheet_input .read_sheets ()
100
- unflattened = list (spreadsheet_input .unflatten ())
101
- assert len (unflattened ) == 1
102
- assert unflattened == [
103
- inject_root_id (root_id , {'id' : 2 , 'testA' : [{'id' : 3 , 'testB' : 4 }]})
104
- ]
105
- # We expect no warnings
106
- assert recwarn .list == []
107
-
108
- def test_rollup_no_id (self , recwarn , root_id , root_id_kwargs ):
109
- spreadsheet_input = ListInput (
110
- sheets = {
111
- 'custom_main' : [
112
- inject_root_id (root_id , {
113
- 'testA[]/id' : '2' ,
114
- 'testA[]/testB' : '3' ,
115
- })
116
- ]
117
- },
118
- main_sheet_name = 'custom_main' ,
119
- ** root_id_kwargs )
120
- spreadsheet_input .read_sheets ()
121
- unflattened = list (spreadsheet_input .unflatten ())
122
- assert len (unflattened ) == 1
123
- assert unflattened == [ inject_root_id (root_id , {
124
- 'testA' : [{'id' : '2' , 'testB' : '3' }]
125
- }) ]
126
- # We expect no warnings
127
- assert recwarn .list == []
128
-
129
- def test_all_empty (self , root_id , root_id_kwargs ):
130
- spreadsheet_input = ListInput (
131
- sheets = {
132
- 'custom_main' : [
133
- inject_root_id (root_id , {
134
- 'id:integer' : '' ,
135
- 'testA:number' : '' ,
136
- 'testB:boolean' : '' ,
137
- 'testC:array' : '' ,
138
- 'testD:string' : '' ,
139
- }, '' )
140
- ]
141
- },
142
- main_sheet_name = 'custom_main' ,
143
- ** root_id_kwargs )
144
- spreadsheet_input .read_sheets ()
145
- output = list (spreadsheet_input .unflatten ())
146
- assert len (output ) == 0
147
-
148
- def test_types_empty (self , root_id , root_id_kwargs ):
149
- spreadsheet_input = ListInput (
150
- sheets = {
151
- 'custom_main' : [
152
- inject_root_id (root_id , {
153
- 'id:integer' : '' ,
154
- 'testA:number' : '' ,
155
- 'testB:boolean' : '' ,
156
- 'testC:array' : '' ,
157
- 'testD:string' : '' ,
158
- 'testE' : '' ,
159
- })
160
- ]
161
- },
162
- main_sheet_name = 'custom_main' ,
163
- ** root_id_kwargs )
164
- spreadsheet_input .read_sheets ()
165
- output = list (spreadsheet_input .unflatten ())
166
- assert len (output ) == 1
167
- assert output [0 ] == inject_root_id (root_id , {})
168
-
116
+ @pytest .mark .parametrize ('input_list,expected_output_list' , testdata )
117
+ def test_unflatten (root_id , root_id_kwargs , input_list , expected_output_list , recwarn ):
118
+ spreadsheet_input = ListInput (
119
+ sheets = {
120
+ 'custom_main' : [
121
+ inject_root_id (root_id , input_row ) for input_row in input_list
122
+ ]
123
+ },
124
+ main_sheet_name = 'custom_main' ,
125
+ ** root_id_kwargs )
126
+ spreadsheet_input .read_sheets ()
127
+ expected_output_list = [
128
+ inject_root_id (root_id , expected_output_dict ) for expected_output_dict in expected_output_list
129
+ ]
130
+ if expected_output_list == [{}]:
131
+ # We don't expect an empty dictionary
132
+ expected_output_list = []
133
+ assert list (spreadsheet_input .unflatten ()) == expected_output_list
134
+ # We expect no warnings
135
+ assert recwarn .list == []
169
136
0 commit comments