-
Notifications
You must be signed in to change notification settings - Fork 1
/
record_test.go
261 lines (217 loc) · 7.46 KB
/
record_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
// Copyright 2015 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.Copyright [201X] 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 (
"bytes"
"fmt"
"testing"
)
func TestRecordRequiresSchema(t *testing.T) {
_, err := NewRecord()
checkErrorFatal(t, err, "cannot build record: no schema defined")
}
func TestRecordFieldNames(t *testing.T) {
someJSONSchema := `{"type":"record","name":"org.foo.Y","fields":[{"type":"int","name":"X"},{"type":"string","name":"W"}]}`
someRecord, err := NewRecord(RecordSchema(someJSONSchema))
checkErrorFatal(t, err, nil)
if someRecord.Name != "org.foo.Y" {
t.Errorf("Actual: %#v; Expected: %#v", someRecord.Name, "org.foo.Y")
}
if someRecord.Fields[0].Name != "org.foo.X" {
t.Errorf("Actual: %#v; Expected: %#v", someRecord.Fields[0].Name, "org.foo.X")
}
if someRecord.Fields[1].Name != "org.foo.W" {
t.Errorf("Actual: %#v; Expected: %#v", someRecord.Fields[1].Name, "org.foo.W")
}
}
func TestRecordFieldBailsWithoutName(t *testing.T) {
schema := make(map[string]interface{})
schema["type"] = "int"
_, err := newRecordField(schema)
checkError(t, err, "ought to have name key")
schema["name"] = 5
_, err = newRecordField(schema)
checkError(t, err, "name ought to be non-empty string")
schema["name"] = ""
_, err = newRecordField(schema)
checkError(t, err, "name ought to be non-empty string")
}
func TestRecordFieldChecksSchema(t *testing.T) {
var err error
schema := make(map[string]interface{})
schema["name"] = ""
_, err = newRecordField(schema)
checkError(t, err, "name ought to be non-empty string")
schema["name"] = "someRecordField"
_, err = newRecordField(schema)
checkError(t, err, fmt.Errorf("ought to have type key"))
}
func TestRecordField(t *testing.T) {
schema := make(map[string]interface{})
schema["name"] = "someRecordField"
schema["type"] = "int"
schema["doc"] = "contans some integer"
someRecordField, err := newRecordField(schema)
checkError(t, err, nil)
if someRecordField.Name != "someRecordField" {
t.Errorf("Actual: %#v; Expected: %#v", someRecordField.Name, "someRecordField")
}
}
func TestRecordBailsWithoutName(t *testing.T) {
var recordFields []*recordField
{
schema := make(map[string]interface{})
schema["name"] = "someRecordField"
schema["type"] = "int"
schema["doc"] = "contans some integer"
someRecordField, err := newRecordField(schema)
checkErrorFatal(t, err, nil)
recordFields = append(recordFields, someRecordField)
}
schema := make(map[string]interface{})
schema["fields"] = recordFields
schema["name"] = 5
_, err := NewRecord(recordSchemaRaw(schema))
checkErrorFatal(t, err, "ought to be non-empty string")
schema["name"] = ""
_, err = NewRecord(recordSchemaRaw(schema))
checkError(t, err, "ought to be non-empty string")
}
func TestRecordBailsWithoutFields(t *testing.T) {
schema := make(map[string]interface{})
schema["name"] = "someRecord"
_, err := NewRecord(recordSchemaRaw(schema))
checkError(t, err, fmt.Errorf("record requires one or more fields"))
schema["fields"] = 5
_, err = NewRecord(recordSchemaRaw(schema))
checkError(t, err, fmt.Errorf("record fields ought to be non-empty array"))
schema["fields"] = make([]interface{}, 0)
// Empty unions are only checked if RecordPedantic is set
_, err = NewRecord(recordSchemaRaw(schema), RecordPedantic())
checkError(t, err, fmt.Errorf("record fields ought to be non-empty array"))
var fields []interface{}
fields = append(fields, "int")
schema["fields"] = fields
_, err = NewRecord(recordSchemaRaw(schema))
checkError(t, err, fmt.Errorf("expected: map[string]interface{}; received: string"))
}
func TestRecordFieldUnionNullOrStringCanBeNull(t *testing.T) {
someJSONSchema := `{"type":"record","name":"Foo","fields":[{"type":["null","string"],"name":"field1"}]}`
codec, err := NewCodec(someJSONSchema)
checkErrorFatal(t, err, nil)
record, err := NewRecord(RecordSchema(someJSONSchema))
checkErrorFatal(t, err, nil)
bb := new(bytes.Buffer)
err = codec.Encode(bb, record)
checkError(t, err, nil)
}
func TestRecordFieldUnionNullOrStringCanBeString(t *testing.T) {
someJSONSchema := `{"type":"record","name":"Foo","fields":[{"type":["null","string"],"name":"field1"}]}`
codec, err := NewCodec(someJSONSchema)
checkErrorFatal(t, err, nil)
record, err := NewRecord(RecordSchema(someJSONSchema))
checkErrorFatal(t, err, nil)
record.Set("field1", "something")
bb := new(bytes.Buffer)
err = codec.Encode(bb, record)
checkError(t, err, nil)
}
func TestRecordGetFieldSchema(t *testing.T) {
outerSchema := `
{
"type": "record",
"name": "TestRecord",
"fields": [
{
"name": "value",
"type": "int"
},
{
"name": "rec",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "TestRecord2",
"fields": [
{
"name": "stringValue",
"type": "string"
},
{
"name": "intValue",
"type": "int"
}
]
}
}
}
]
}
`
outerRecord, err := NewRecord(RecordSchema(outerSchema))
checkErrorFatal(t, err, nil)
// make sure it bails when no such schema
_, err = outerRecord.GetFieldSchema("no_such_field")
checkError(t, err, "no such field: \"no_such_field\"")
// get the inner schema
schema, err := outerRecord.GetFieldSchema("rec")
checkErrorFatal(t, err, nil)
_, ok := schema.(map[string]interface{})
if !ok {
t.Errorf("Actual: %#v; Expected: %#v", ok, true)
}
}
func TestNullField(t *testing.T) {
someJSONSchema := `{"type":"record","name":"Foo","fields":[{"type":"null","name":"field1"}]}`
codec, err := NewCodec(someJSONSchema)
checkError(t, err, nil)
bb := bytes.NewBufferString("")
rec, err := NewRecord(RecordSchema(someJSONSchema))
checkError(t, err, nil)
err = codec.Encode(bb, rec)
checkError(t, err, nil)
}
func TestNullableStringField(t *testing.T) {
schema := `
{
"name": "record",
"type": "record",
"fields": [
{ "type": "string", "name": "string" },
{ "type": ["null", "string"], "name": "nil_or_string" }
]
}
`
codec, err := NewCodec(schema)
checkErrorFatal(t, err, nil)
record, err := NewRecord(RecordSchema(schema))
checkErrorFatal(t, err, nil)
record.Set("nil_or_string", nil) // or a string
record.Set("string", "can't be empty")
buf := new(bytes.Buffer)
err = codec.Encode(buf, record) // "\x1ccan't be empty\x00"
checkErrorFatal(t, err, nil)
decode, err := codec.Decode(buf)
checkErrorFatal(t, err, nil)
nilOrString, err := decode.(*Record).Get("nil_or_string")
checkErrorFatal(t, err, nil)
if nilOrString != nil {
t.Fatalf("Expected nil, got (%T) - (%q)", nilOrString, nilOrString)
}
}