forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_test.go
311 lines (284 loc) · 6.2 KB
/
convert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package parquet_test
import (
"reflect"
"testing"
"github.com/segmentio/parquet-go"
)
type AddressBook2 struct {
Owner string `parquet:"owner,zstd"`
OwnerPhoneNumbers []string `parquet:"ownerPhoneNumbers,gzip"`
Contacts []Contact `parquet:"contacts"`
Extra string `parquet:"extra"`
}
type AddressBook3 struct {
Owner string `parquet:"owner,zstd"`
Contacts []Contact2 `parquet:"contacts"`
}
type Contact2 struct {
Name string `parquet:"name"`
PhoneNumbers []string `parquet:"phoneNumbers,zstd"`
Addresses []string `parquet:"addresses,zstd"`
}
type AddressBook4 struct {
Owner string `parquet:"owner,zstd"`
Contacts []Contact2 `parquet:"contacts"`
Extra string `parquet:"extra"`
}
type SimpleNumber struct {
Number *int64 `parquet:"number,optional"`
}
type SimpleContact struct {
Numbers []SimpleNumber `parquet:"numbers"`
}
type SimpleAddressBook struct {
Name string
Contact SimpleContact
}
type SimpleAddressBook2 struct {
Name string
Contact SimpleContact
Extra string
}
var conversionTests = [...]struct {
scenario string
from interface{}
to interface{}
}{
{
scenario: "convert between rows which have the same schema",
from: AddressBook{
Owner: "Julien Le Dem",
OwnerPhoneNumbers: []string{
"555 123 4567",
"555 666 1337",
},
Contacts: []Contact{
{
Name: "Dmitriy Ryaboy",
PhoneNumber: "555 987 6543",
},
{
Name: "Chris Aniszczyk",
},
},
},
to: AddressBook{
Owner: "Julien Le Dem",
OwnerPhoneNumbers: []string{
"555 123 4567",
"555 666 1337",
},
Contacts: []Contact{
{
Name: "Dmitriy Ryaboy",
PhoneNumber: "555 987 6543",
},
{
Name: "Chris Aniszczyk",
},
},
},
},
{
scenario: "missing column",
from: struct{ FirstName, LastName string }{FirstName: "Luke", LastName: "Skywalker"},
to: struct{ LastName string }{LastName: "Skywalker"},
},
{
scenario: "missing optional column",
from: struct {
FirstName *string
LastName string
}{FirstName: newString("Luke"), LastName: "Skywalker"},
to: struct{ LastName string }{LastName: "Skywalker"},
},
{
scenario: "missing repeated column",
from: struct {
ID uint64
Names []string
}{ID: 42, Names: []string{"me", "myself", "I"}},
to: struct{ ID uint64 }{ID: 42},
},
{
scenario: "extra column",
from: struct{ LastName string }{LastName: "Skywalker"},
to: struct{ FirstName, LastName string }{LastName: "Skywalker"},
},
{
scenario: "extra optional column",
from: struct{ ID uint64 }{ID: 2},
to: struct {
ID uint64
Details *struct{ FirstName, LastName string }
}{ID: 2, Details: nil},
},
{
scenario: "extra repeated column",
from: struct{ ID uint64 }{ID: 1},
to: struct {
ID uint64
Names []string
}{ID: 1, Names: []string{}},
},
{
scenario: "extra column on complex struct",
from: AddressBook{
Owner: "Julien Le Dem",
OwnerPhoneNumbers: []string{},
Contacts: []Contact{
{
Name: "Dmitriy Ryaboy",
PhoneNumber: "555 987 6543",
},
{
Name: "Chris Aniszczyk",
},
},
},
to: AddressBook2{
Owner: "Julien Le Dem",
OwnerPhoneNumbers: []string{},
Contacts: []Contact{
{
Name: "Dmitriy Ryaboy",
PhoneNumber: "555 987 6543",
},
{
Name: "Chris Aniszczyk",
},
},
},
},
{
scenario: "handle nested repeated elements during conversion",
from: AddressBook3{
Owner: "Julien Le Dem",
Contacts: []Contact2{
{
Name: "Dmitriy Ryaboy",
PhoneNumbers: []string{
"555 987 6543",
"555 123 4567",
},
Addresses: []string{},
},
{
Name: "Chris Aniszczyk",
PhoneNumbers: []string{
"555 345 8129",
},
Addresses: []string{
"42 Wallaby Way Sydney",
"1 White House Way",
},
},
{
Name: "Bob Ross",
PhoneNumbers: []string{
"555 198 3628",
},
Addresses: []string{
"::1",
},
},
},
},
to: AddressBook4{
Owner: "Julien Le Dem",
Contacts: []Contact2{
{
Name: "Dmitriy Ryaboy",
PhoneNumbers: []string{
"555 987 6543",
"555 123 4567",
},
Addresses: []string{},
},
{
Name: "Chris Aniszczyk",
PhoneNumbers: []string{
"555 345 8129",
},
Addresses: []string{
"42 Wallaby Way Sydney",
"1 White House Way",
},
},
{
Name: "Bob Ross",
PhoneNumbers: []string{
"555 198 3628",
},
Addresses: []string{
"::1",
},
},
},
Extra: "",
},
},
{
scenario: "handle nested repeated elements during conversion",
from: SimpleAddressBook{
Name: "New Contact",
Contact: SimpleContact{
Numbers: []SimpleNumber{
{
Number: nil,
},
{
Number: newInt64(1329),
},
},
},
},
to: SimpleAddressBook2{
Name: "New Contact",
Contact: SimpleContact{
Numbers: []SimpleNumber{
{
Number: nil,
},
{
Number: newInt64(1329),
},
},
},
Extra: "",
},
},
}
func TestConvert(t *testing.T) {
for _, test := range conversionTests {
t.Run(test.scenario, func(t *testing.T) {
to := parquet.SchemaOf(test.to)
from := parquet.SchemaOf(test.from)
conv, err := parquet.Convert(to, from)
if err != nil {
t.Fatal(err)
}
oldRow := from.Deconstruct(nil, test.from)
row, err := conv.Convert(nil, oldRow)
if err != nil {
t.Fatal(err)
}
// Helpful debugging info
// newRow := to.Deconstruct(nil, test.to)
// fmt.Printf("conv: %+v\n", conv)
// fmt.Printf("old row: %+v\n", oldRow)
// fmt.Printf("new row (desired state): %+v\n", newRow)
// fmt.Printf("new row (converted from old): %+v\n", row)
value := reflect.New(reflect.TypeOf(test.to))
if err := to.Reconstruct(value.Interface(), row); err != nil {
t.Fatal(err)
}
value = value.Elem()
if !reflect.DeepEqual(value.Interface(), test.to) {
t.Errorf("converted value mismatch:\nwant = %+v\ngot = %+v", test.to, value.Interface())
}
})
}
}
func newInt64(i int64) *int64 { return &i }
func newString(s string) *string { return &s }