forked from go-avro/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_test.go
339 lines (306 loc) · 11.9 KB
/
schema_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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package avro
import (
"testing"
)
func TestPrimitiveSchema(t *testing.T) {
primitiveSchemaAssert(t, "string", String, "STRING")
primitiveSchemaAssert(t, "int", Int, "INT")
primitiveSchemaAssert(t, "long", Long, "LONG")
primitiveSchemaAssert(t, "boolean", Boolean, "BOOLEAN")
primitiveSchemaAssert(t, "float", Float, "FLOAT")
primitiveSchemaAssert(t, "double", Double, "DOUBLE")
primitiveSchemaAssert(t, "bytes", Bytes, "BYTES")
primitiveSchemaAssert(t, "null", Null, "NULL")
}
func primitiveSchemaAssert(t *testing.T, raw string, expected int, typeName string) {
s, err := ParseSchema(raw)
assert(t, err, nil)
if s.Type() != expected {
t.Errorf("\n%s \n===\n Should parse into Type() = %s", raw, typeName)
}
}
func TestArraySchema(t *testing.T) {
//array of strings
raw := `{"type":"array", "items": "string"}`
s, err := ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Array {
t.Errorf("\n%s \n===\n Should parse into Type() = %s", raw, "ARRAY")
}
if s.(*ArraySchema).Items.Type() != String {
t.Errorf("\n%s \n===\n Array item type should be STRING", raw)
}
//array of longs
raw = `{"type":"array", "items": "long"}`
s, err = ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Array {
t.Errorf("\n%s \n===\n Should parse into Type() = %s", raw, "ARRAY")
}
if s.(*ArraySchema).Items.Type() != Long {
t.Errorf("\n%s \n===\n Array item type should be LONG", raw)
}
//array of arrays of strings
raw = `{"type":"array", "items": {"type":"array", "items": "string"}}`
s, err = ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Array {
t.Errorf("\n%s \n===\n Should parse into Type() = %s", raw, "ARRAY")
}
if s.(*ArraySchema).Items.Type() != Array {
t.Errorf("\n%s \n===\n Array item type should be ARRAY", raw)
}
if s.(*ArraySchema).Items.(*ArraySchema).Items.Type() != String {
t.Errorf("\n%s \n===\n Array's nested item type should be STRING", raw)
}
raw = `{"type":"array", "items": {"type": "record", "name": "TestRecord", "fields": [
{"name": "longRecordField", "type": "long"},
{"name": "floatRecordField", "type": "float"}
]}}`
s, err = ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Array {
t.Errorf("\n%s \n===\n Should parse into Type() = %s", raw, "ARRAY")
}
if s.(*ArraySchema).Items.Type() != Record {
t.Errorf("\n%s \n===\n Array item type should be RECORD", raw)
}
if s.(*ArraySchema).Items.(*RecordSchema).Fields[0].Type.Type() != Long {
t.Errorf("\n%s \n===\n Array's nested record first field type should be LONG", raw)
}
if s.(*ArraySchema).Items.(*RecordSchema).Fields[1].Type.Type() != Float {
t.Errorf("\n%s \n===\n Array's nested record first field type should be FLOAT", raw)
}
}
func TestMapSchema(t *testing.T) {
//map[string, int]
raw := `{"type":"map", "values": "int"}`
s, err := ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Map {
t.Errorf("\n%s \n===\n Should parse into MapSchema. Actual %#v", raw, s)
}
if s.(*MapSchema).Values.Type() != Int {
t.Errorf("\n%s \n===\n Map value type should be Int. Actual %#v", raw, s.(*MapSchema).Values)
}
//map[string, []string]
raw = `{"type":"map", "values": {"type":"array", "items": "string"}}`
s, err = ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Map {
t.Errorf("\n%s \n===\n Should parse into MapSchema. Actual %#v", raw, s)
}
if s.(*MapSchema).Values.Type() != Array {
t.Errorf("\n%s \n===\n Map value type should be Array. Actual %#v", raw, s.(*MapSchema).Values)
}
if s.(*MapSchema).Values.(*ArraySchema).Items.Type() != String {
t.Errorf("\n%s \n===\n Map nested array item type should be String. Actual %#v", raw, s.(*MapSchema).Values.(*ArraySchema).Items)
}
//map[string, [int, string]]
raw = `{"type":"map", "values": ["int", "string"]}`
s, err = ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Map {
t.Errorf("\n%s \n===\n Should parse into MapSchema. Actual %#v", raw, s)
}
if s.(*MapSchema).Values.Type() != Union {
t.Errorf("\n%s \n===\n Map value type should be Union. Actual %#v", raw, s.(*MapSchema).Values)
}
if s.(*MapSchema).Values.(*UnionSchema).Types[0].Type() != Int {
t.Errorf("\n%s \n===\n Map nested union's first type should be Int. Actual %#v", raw, s.(*MapSchema).Values.(*UnionSchema).Types[0])
}
if s.(*MapSchema).Values.(*UnionSchema).Types[1].Type() != String {
t.Errorf("\n%s \n===\n Map nested union's second type should be String. Actual %#v", raw, s.(*MapSchema).Values.(*UnionSchema).Types[1])
}
//map[string, record]
raw = `{"type":"map", "values": {"type": "record", "name": "TestRecord2", "fields": [
{"name": "doubleRecordField", "type": "double"},
{"name": "fixedRecordField", "type": {"type": "fixed", "size": 4, "name": "bytez"}}
]}}`
s, err = ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Map {
t.Errorf("\n%s \n===\n Should parse into MapSchema. Actual %#v", raw, s)
}
if s.(*MapSchema).Values.Type() != Record {
t.Errorf("\n%s \n===\n Map value type should be Record. Actual %#v", raw, s.(*MapSchema).Values)
}
if s.(*MapSchema).Values.(*RecordSchema).Fields[0].Type.Type() != Double {
t.Errorf("\n%s \n===\n Map value's record first field should be Double. Actual %#v", raw, s.(*MapSchema).Values.(*RecordSchema).Fields[0].Type)
}
if s.(*MapSchema).Values.(*RecordSchema).Fields[1].Type.Type() != Fixed {
t.Errorf("\n%s \n===\n Map value's record first field should be Fixed. Actual %#v", raw, s.(*MapSchema).Values.(*RecordSchema).Fields[1].Type)
}
}
func TestRecordSchema(t *testing.T) {
raw := `{"type": "record", "name": "TestRecord", "fields": [
{"name": "longRecordField", "type": "long"},
{"name": "stringRecordField", "type": "string"},
{"name": "intRecordField", "type": "int"},
{"name": "floatRecordField", "type": "float"}
]}`
s, err := ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Record {
t.Errorf("\n%s \n===\n Should parse into RecordSchema. Actual %#v", raw, s)
}
if s.(*RecordSchema).Fields[0].Type.Type() != Long {
t.Errorf("\n%s \n===\n Record's first field type should parse into LongSchema. Actual %#v", raw, s.(*RecordSchema).Fields[0].Type)
}
if s.(*RecordSchema).Fields[1].Type.Type() != String {
t.Errorf("\n%s \n===\n Record's second field type should parse into StringSchema. Actual %#v", raw, s.(*RecordSchema).Fields[1].Type)
}
if s.(*RecordSchema).Fields[2].Type.Type() != Int {
t.Errorf("\n%s \n===\n Record's third field type should parse into IntSchema. Actual %#v", raw, s.(*RecordSchema).Fields[2].Type)
}
if s.(*RecordSchema).Fields[3].Type.Type() != Float {
t.Errorf("\n%s \n===\n Record's fourth field type should parse into FloatSchema. Actual %#v", raw, s.(*RecordSchema).Fields[3].Type)
}
raw = `{"namespace": "scalago",
"type": "record",
"name": "PingPong",
"fields": [
{"name": "counter", "type": "long"},
{"name": "name", "type": "string"}
]}`
s, err = ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Record {
t.Errorf("\n%s \n===\n Should parse into RecordSchema. Actual %#v", raw, s)
}
if s.(*RecordSchema).Name != "PingPong" {
t.Errorf("\n%s \n===\n Record's name should be PingPong. Actual %#v", raw, s.(*RecordSchema).Name)
}
f0 := s.(*RecordSchema).Fields[0]
if f0.Name != "counter" {
t.Errorf("\n%s \n===\n Record's first field name should be 'counter'. Actual %#v", raw, f0.Name)
}
if f0.Type.Type() != Long {
t.Errorf("\n%s \n===\n Record's first field type should parse into LongSchema. Actual %#v", raw, f0.Type)
}
f1 := s.(*RecordSchema).Fields[1]
if f1.Name != "name" {
t.Errorf("\n%s \n===\n Record's first field name should be 'counter'. Actual %#v", raw, f0.Name)
}
if f1.Type.Type() != String {
t.Errorf("\n%s \n===\n Record's second field type should parse into StringSchema. Actual %#v", raw, f1.Type)
}
}
func TestEnumSchema(t *testing.T) {
raw := `{"type":"enum", "name":"foo", "symbols":["A", "B", "C", "D"]}`
s, err := ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Enum {
t.Errorf("\n%s \n===\n Should parse into EnumSchema. Actual %#v", raw, s)
}
if s.(*EnumSchema).Name != "foo" {
t.Errorf("\n%s \n===\n Enum name should be 'foo'. Actual %#v", raw, s.(*EnumSchema).Name)
}
if !arrayEqual(s.(*EnumSchema).Symbols, []string{"A", "B", "C", "D"}) {
t.Errorf("\n%s \n===\n Enum symbols should be [\"A\", \"B\", \"C\", \"D\"]. Actual %#v", raw, s.(*EnumSchema).Symbols)
}
}
func TestUnionSchema(t *testing.T) {
raw := `["null", "string"]`
s, err := ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Union {
t.Errorf("\n%s \n===\n Should parse into UnionSchema. Actual %#v", raw, s)
}
if s.(*UnionSchema).Types[0].Type() != Null {
t.Errorf("\n%s \n===\n Union's first type should be Null. Actual %#v", raw, s.(*UnionSchema).Types[0])
}
if s.(*UnionSchema).Types[1].Type() != String {
t.Errorf("\n%s \n===\n Union's second type should be String. Actual %#v", raw, s.(*UnionSchema).Types[1])
}
raw = `["string", "null"]`
s, err = ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Union {
t.Errorf("\n%s \n===\n Should parse into UnionSchema. Actual %#v", raw, s)
}
if s.(*UnionSchema).Types[0].Type() != String {
t.Errorf("\n%s \n===\n Union's first type should be String. Actual %#v", raw, s.(*UnionSchema).Types[0])
}
if s.(*UnionSchema).Types[1].Type() != Null {
t.Errorf("\n%s \n===\n Union's second type should be Null. Actual %#v", raw, s.(*UnionSchema).Types[1])
}
}
func TestFixedSchema(t *testing.T) {
raw := `{"type": "fixed", "size": 16, "name": "md5"}`
s, err := ParseSchema(raw)
assert(t, err, nil)
if s.Type() != Fixed {
t.Errorf("\n%s \n===\n Should parse into FixedSchema. Actual %#v", raw, s)
}
if s.(*FixedSchema).Size != 16 {
t.Errorf("\n%s \n===\n Fixed size should be 16. Actual %#v", raw, s.(*FixedSchema).Size)
}
if s.(*FixedSchema).Name != "md5" {
t.Errorf("\n%s \n===\n Fixed name should be md5. Actual %#v", raw, s.(*FixedSchema).Name)
}
}
func TestSchemaRegistryMap(t *testing.T) {
rawSchema1 := `{"type": "record", "name": "TestRecord", "namespace": "com.github.elodina", "fields": [
{"name": "longRecordField", "type": "long"}
]}`
rawSchema2 := `{"type": "record", "name": "TestRecord2", "namespace": "com.github.elodina", "fields": [
{"name": "record", "type": ["null", "TestRecord"]}
]}`
rawSchema3 := `{"type": "record", "name": "TestRecord3", "namespace": "com.github.other", "fields": [
{"name": "record", "type": ["null", "com.github.elodina.TestRecord2"]}
]}`
rawSchema4 := `{"type": "record", "name": "TestRecord3", "namespace": "com.github.elodina", "fields": [
{"name": "record", "type": ["null", {"type": "TestRecord2"}, "com.github.other.TestRecord3"]}
]}`
registry := make(map[string]Schema)
s1, err := ParseSchemaWithRegistry(rawSchema1, registry)
assert(t, err, nil)
assert(t, s1.Type(), Record)
assert(t, len(registry), 1)
s2, err := ParseSchemaWithRegistry(rawSchema2, registry)
assert(t, err, nil)
assert(t, s2.Type(), Record)
assert(t, len(registry), 2)
s3, err := ParseSchemaWithRegistry(rawSchema3, registry)
assert(t, err, nil)
assert(t, s3.Type(), Record)
assert(t, len(registry), 3)
s4, err := ParseSchemaWithRegistry(rawSchema4, registry)
assert(t, err, nil)
assert(t, s4.Type(), Record)
assert(t, len(registry), 4)
}
func TestRecordCustomProps(t *testing.T) {
raw := `{"type": "record", "name": "TestRecord", "hello": "world", "fields": [
{"name": "longRecordField", "type": "long"},
{"name": "stringRecordField", "type": "string"},
{"name": "intRecordField", "type": "int"},
{"name": "floatRecordField", "type": "float"}
]}`
s, err := ParseSchema(raw)
assert(t, err, nil)
assert(t, len(s.(*RecordSchema).Properties), 1)
value, exists := s.Prop("hello")
assert(t, exists, true)
assert(t, value, "world")
}
func TestLoadSchemas(t *testing.T) {
schemas := LoadSchemas("test/schemas/")
assert(t, len(schemas), 4)
_, exists := schemas["example.avro.Complex"]
assert(t, exists, true)
_, exists = schemas["example.avro.foo"]
assert(t, exists, true)
}
func arrayEqual(arr1 []string, arr2 []string) bool {
if len(arr1) != len(arr2) {
return false
}
for i := range arr1 {
if arr1[i] != arr2[i] {
return false
}
}
return true
}