-
Notifications
You must be signed in to change notification settings - Fork 221
/
schema_test.go
271 lines (244 loc) · 7.33 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
// Copyright [2019] LinkedIn Corp. Licensed under the Apache License, Version
// 2.0 (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
package goavro
import (
"fmt"
"testing"
)
func testSchemaPrimativeCodec(t *testing.T, primitiveTypeName string) {
t.Helper()
if _, err := NewCodec(primitiveTypeName); err != nil {
t.Errorf("Bare primitive type: Schema: %q; Actual: %#v; Expected: %#v", primitiveTypeName, err, nil)
}
full := fmt.Sprintf(`{"type":%s}`, primitiveTypeName)
if _, err := NewCodec(full); err != nil {
t.Errorf("Full primitive type: Schema: %q; Actual: %#v; Expected: %#v", full, err, nil)
}
extra := fmt.Sprintf(`{"type":%s,"ignoredKey":"ignoredValue"}`, primitiveTypeName)
if _, err := NewCodec(extra); err != nil {
t.Errorf("Full primitive type with extra attributes: Schema: %q; Actual: %#v; Expected: %#v", extra, err, nil)
}
}
func testSchemaInvalid(t *testing.T, schema, errorMessage string) {
t.Helper()
_, err := NewCodec(schema)
ensureError(t, err, errorMessage)
}
func testSchemaValid(t *testing.T, schema string) {
t.Helper()
_, err := NewCodec(schema)
if err != nil {
t.Errorf("GOT: %v; WANT: %v", err, nil)
}
}
func TestSchemaFailInvalidType(t *testing.T) {
testSchemaInvalid(t, `{"type":"flubber"}`, "unknown type name")
}
func TestSchemaWeather(t *testing.T) {
testSchemaValid(t, `
{"type": "record", "name": "test.Weather",
"doc": "A weather reading.",
"fields": [
{"name": "station", "type": "string", "order": "ignore"},
{"name": "time", "type": "long"},
{"name": "temp", "type": "int"}
]
}
`)
}
func TestSchemaFooBarSpecificRecord(t *testing.T) {
testSchemaValid(t, `
{
"type": "record",
"name": "FooBarSpecificRecord",
"namespace": "org.apache.avro",
"fields": [
{"name": "id", "type": "int"},
{"name": "name", "type": "string"},
{"name": "nicknames", "type":
{"type": "array", "items": "string"}},
{"name": "relatedids", "type":
{"type": "array", "items": "int"}},
{"name": "typeEnum", "type":
["null", {
"type": "enum",
"name": "TypeEnum",
"namespace": "org.apache.avro",
"symbols" : ["a","b", "c"]
}],
"default": null
}
]
}
`)
}
func TestSchemaInterop(t *testing.T) {
testSchemaValid(t, `
{"type": "record", "name":"Interop", "namespace": "org.apache.avro",
"fields": [
{"name": "intField", "type": "int"},
{"name": "longField", "type": "long"},
{"name": "stringField", "type": "string"},
{"name": "boolField", "type": "boolean"},
{"name": "floatField", "type": "float"},
{"name": "doubleField", "type": "double"},
{"name": "bytesField", "type": "bytes"},
{"name": "nullField", "type": "null"},
{"name": "arrayField", "type": {"type": "array", "items": "double"}},
{"name": "mapField", "type":
{"type": "map", "values":
{"type": "record", "name": "Foo",
"fields": [{"name": "label", "type": "string"}]}}},
{"name": "unionField", "type":
["boolean", "double", {"type": "array", "items": "bytes"}]},
{"name": "enumField", "type":
{"type": "enum", "name": "Kind", "symbols": ["A","B","C"]}},
{"name": "fixedField", "type":
{"type": "fixed", "name": "MD5", "size": 16}},
{"name": "recordField", "type":
{"type": "record", "name": "Node",
"fields": [
{"name": "label", "type": "string"},
{"name": "children", "type": {"type": "array", "items": "Node"}}]}}
]
}
`)
}
func TestSchemaFixedNameCanBeUsedLater(t *testing.T) {
schema := `{"type":"record","name":"record1","fields":[
{"name":"field1","type":{"type":"fixed","name":"fixed_4","size":4}},
{"name":"field2","type":"fixed_4"}]}`
datum := map[string]interface{}{
"field1": []byte("abcd"),
"field2": []byte("efgh"),
}
testBinaryEncodePass(t, schema, datum, []byte("abcdefgh"))
}
// func ExampleCodecSchema() {
// schema := `{"type":"map","values":{"type":"enum","name":"foo","symbols":["alpha","bravo"]}}`
// codec, err := NewCodec(schema)
// if err != nil {
// fmt.Println(err)
// }
// fmt.Println(codec.Schema())
// // Output: {"type":"map","values":{"name":"foo","type":"enum","symbols":["alpha","bravo"]}}
// }
func TestMapValueTypeEnum(t *testing.T) {
schema := `{"type":"map","values":{"type":"enum","name":"foo","symbols":["alpha","bravo"]}}`
datum := map[string]interface{}{"someKey": "bravo"}
expected := []byte{
0x2, // blockCount = 1 pair
0xe, // key size = 7
's', 'o', 'm', 'e', 'K', 'e', 'y',
0x2, // value = index 1 ("bravo")
0, // blockCount = 0 pairs
}
testBinaryCodecPass(t, schema, datum, expected)
}
func TestMapValueTypeRecord(t *testing.T) {
schema := `{"type":"map","values":{"type":"record","name":"foo","fields":[{"name":"field1","type":"string"},{"name":"field2","type":"int"}]}}`
datum := map[string]interface{}{
"map-key": map[string]interface{}{
"field1": "unlucky",
"field2": 13,
},
}
expected := []byte{
0x2, // blockCount = 1 key-value pair in top level map
0xe, // first key size = 7
'm', 'a', 'p', '-', 'k', 'e', 'y', // first key = "map-key"
// this key's value is a record, which is encoded by concatenated its field values
0x0e, // field one string size = 7
'u', 'n', 'l', 'u', 'c', 'k', 'y',
0x1a, // 13
0, // map has no more blocks
}
// cannot decode because order of map key enumeration random, and records
// are returned as a Go map
testBinaryEncodePass(t, schema, datum, expected)
}
func TestDefaultValueOughtToEncodeUsingFieldSchemaOK(t *testing.T) {
testSchemaValid(t, `
{
"namespace": "universe.of.things",
"type": "record",
"name": "Thing",
"fields": [
{
"name": "attributes",
"type": [
"null",
{
"type": "array",
"items": {
"namespace": "universe.of.things",
"type": "record",
"name": "attribute",
"fields": [
{
"name": "name",
"type": "string"
},
{
"name": "value",
"type": "string"
}
]
}
}
],
"default": "null"
}
]
}`)
}
func TestUnionOfRecordsDefaultValueOughtToEncodeUsingFieldSchemaOK(t *testing.T) {
testSchemaValid(t, `
{
"type": "record",
"name": "Thing",
"namespace": "universe.of.things",
"fields": [
{
"name": "layout",
"type": [
{
"type": "record",
"name": "AnotherThing",
"namespace": "another.universe.of.things",
"fields": [
{
"name": "text",
"type": "string",
"default": "someText"
}
]
},
{
"type": "record",
"name": "AnotherThing2",
"namespace": "another.universe.of.things",
"fields": [
{
"name": "text",
"type": "string",
"default": "someOtherText"
}
]
}
],
"default": {
"another.universe.of.things.AnotherThing": {
"text": "someDefaultText"
}
}
}
]
}`)
}