-
Notifications
You must be signed in to change notification settings - Fork 5
/
schema_test.go
190 lines (168 loc) · 5.97 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
package goavro_test
import (
"fmt"
"testing"
"github.com/karrick/goavro"
)
func testSchemaPrimativeCodec(t *testing.T, primitiveTypeName string) {
if _, err := goavro.NewCodec(primitiveTypeName); err != nil {
t.Errorf("Bare primitive type: Schema: %q; Actual: %#v; Expected: %#v", primitiveTypeName, err, nil)
}
quoted := `"` + primitiveTypeName + `"`
if _, err := goavro.NewCodec(quoted); err != nil {
t.Errorf("Bare primitive type: Schema: %q; Actual: %#v; Expected: %#v", quoted, err, nil)
}
full := fmt.Sprintf(`{"type":"%s"}`, primitiveTypeName)
if _, err := goavro.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 := goavro.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) {
_, err := goavro.NewCodec(schema)
ensureError(t, err, errorMessage)
}
func testSchemaValid(t *testing.T, schema string) {
_, err := goavro.NewCodec(schema)
if err != nil {
t.Errorf("Actual: %v; Expected: %v", err, nil)
}
}
func TestSchemaFailInvalidType(t *testing.T) {
testSchemaInvalid(t, `{"type":"flubber"}`, "unknown type name")
}
func TestCodecSchema(t *testing.T) {
codec, err := goavro.NewCodec(` { "type" : "string" } `)
if err != nil {
t.Fatal(err)
}
if actual, expected := codec.Schema(), `{"type":"string"}`; actual != expected {
t.Errorf("Actual: %v; Expected: %v", actual, expected)
}
}
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 := goavro.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)
}