-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_test.go
59 lines (53 loc) · 1.43 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
package avrogo
import (
"bytes"
"os"
"testing"
)
func load(t *testing.T, fn string) Type {
loaded, err := loadErr(t, fn)
if err != nil {
t.Fatalf("Schema error: %s", err)
}
return loaded
}
func loadErr(t *testing.T, fn string) (Type, os.Error) {
reader, err := os.Open(fn, os.O_RDONLY, 0444)
if err != nil {
t.Fatalf("Unable to open file %s", fn)
}
return LoadJsonSchema(reader)
}
func SkipTestDecode(t *testing.T) {
m := load(t, "test_schema.json")
if m.(Record).Id() != "org.apache.avro.Interop" {
t.Fatalf("Read json incorrectly? parsed=%v", m)
}
}
func TestPrimitiveDecode(t *testing.T) {
m := load(t, "primitive_record_schema.json")
if m.(Record).Id() != "test.AllPrimitives" {
t.Fatalf("Read record id incorrectly?%v", m)
}
r := m.(Record)
if len(r.fields) != 3 {
t.Fatalf("Got incorrect number of fields?%v", m)
}
encoded := []byte{0x01, 0x81, 0x01}
decoded, _ := r.Read(bytes.NewBuffer(encoded))
if decoded.(map[string]interface{})["nullField"] != nil {
t.Fatalf("Got wrong type for nullField")
}
if decoded.(map[string]interface{})["boolField"] != true {
t.Fatalf("Got wrong value for boolField")
}
if decoded.(map[string]interface{})["intField"] != int32(-65) {
t.Fatalf("Got wrong value for intField")
}
}
func TestNamelessRecord(t *testing.T) {
_, err := loadErr(t, "nameless_record_schema.json")
if err.String() != "Missing required field 'name'" {
t.Fatalf("Incorrect SchemaError: %s", err)
}
}