forked from karrick/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_test.go
189 lines (174 loc) · 5.87 KB
/
text_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
package goavro_test
import (
"bytes"
"fmt"
"math"
"testing"
"github.com/karrick/goavro"
)
func testTextDecodeFail(t *testing.T, schema string, buf []byte, errorMessage string) {
c, err := goavro.NewCodec(schema)
if err != nil {
t.Fatal(err)
}
value, newBuffer, err := c.NativeFromTextual(buf)
ensureError(t, err, errorMessage)
if value != nil {
t.Errorf("Actual: %v; Expected: %v", value, nil)
}
if !bytes.Equal(buf, newBuffer) {
t.Errorf("Actual: %v; Expected: %v", newBuffer, buf)
}
}
func testTextEncodeFail(t *testing.T, schema string, datum interface{}, errorMessage string) {
c, err := goavro.NewCodec(schema)
if err != nil {
t.Fatal(err)
}
buf, err := c.TextualFromNative(nil, datum)
ensureError(t, err, errorMessage)
if buf != nil {
t.Errorf("Actual: %v; Expected: %v", buf, nil)
}
}
func testTextEncodeFailBadDatumType(t *testing.T, schema string, datum interface{}) {
testTextEncodeFail(t, schema, datum, "received: ")
}
func testTextDecodeFailShortBuffer(t *testing.T, schema string, buf []byte) {
testTextDecodeFail(t, schema, buf, "short buffer")
}
func testTextDecodePass(t *testing.T, schema string, datum interface{}, encoded []byte) {
codec, err := goavro.NewCodec(schema)
if err != nil {
t.Fatalf("schema: %s; %s", schema, err)
}
decoded, remaining, err := codec.NativeFromTextual(encoded)
if err != nil {
t.Fatalf("schema: %s; %s", schema, err)
}
// remaining ought to be empty because there is nothing remaining to be
// decoded
if actual, expected := len(remaining), 0; actual != expected {
t.Errorf("schema: %s; Datum: %#v; Actual: %v; Expected: %v", schema, datum, actual, expected)
}
var datumIsMap, datumIsNumerical, datumIsSlice, datumIsString bool
var datumFloat float64
var datumMap map[string]interface{}
var datumSlice []interface{}
var datumString string
switch v := datum.(type) {
case float64:
datumFloat = v
datumIsNumerical = true
case float32:
datumFloat = float64(v)
datumIsNumerical = true
case int:
datumFloat = float64(v)
datumIsNumerical = true
case int32:
datumFloat = float64(v)
datumIsNumerical = true
case int64:
datumFloat = float64(v)
datumIsNumerical = true
case string:
datumString = v
datumIsString = true
case []interface{}:
datumIsSlice = true
datumSlice = v
case map[string]interface{}:
datumIsMap = true
datumMap = v
}
var decodedIsMap, decodedIsNumerical, decodedIsSlice, decodedIsString bool
var decodedMap map[string]interface{}
var decodedFloat float64
var decodedSlice []interface{}
var decodedString string
switch v := decoded.(type) {
case float64:
decodedFloat = v
decodedIsNumerical = true
case float32:
decodedFloat = float64(v)
decodedIsNumerical = true
case int:
decodedFloat = float64(v)
decodedIsNumerical = true
case int32:
decodedFloat = float64(v)
decodedIsNumerical = true
case int64:
decodedFloat = float64(v)
decodedIsNumerical = true
case string:
decodedString = v
decodedIsString = true
case []interface{}:
decodedIsSlice = true
decodedSlice = v
case map[string]interface{}:
decodedIsMap = true
decodedMap = v
}
// NOTE: Special handling when both datum and decoded values are floating
// point to test whether both are NaN, -Inf, or +Inf.
if datumIsNumerical && decodedIsNumerical {
if (math.IsNaN(datumFloat) != math.IsNaN(decodedFloat)) &&
(math.IsInf(datumFloat, 1) != math.IsInf(decodedFloat, 1)) &&
(math.IsInf(datumFloat, -1) != math.IsInf(decodedFloat, -1)) &&
datumFloat != decodedFloat {
t.Errorf("numerical comparison: schema: %s; Datum: %v; Actual: %v; Expected: %v", schema, datum, decodedFloat, datumFloat)
}
} else if datumIsMap && decodedIsMap {
if actual, expected := len(decodedMap), len(datumMap); actual != expected {
t.Fatalf("map comparison: length mismatch; Actual: %v; Expected: %v", actual, expected)
}
for key, datumValue := range datumMap {
decodedValue, ok := decodedMap[key]
if !ok {
t.Fatalf("map comparison: decoded missing key: %q: Actual: %v; Expected: %v", key, decodedMap, datumMap)
}
if actual, expected := fmt.Sprintf("%v", decodedValue), fmt.Sprintf("%v", datumValue); actual != expected {
t.Errorf("map comparison: values differ for key: %q; Actual: %v; Expected: %v", key, actual, expected)
}
}
} else if datumIsSlice && decodedIsSlice {
if actual, expected := len(decodedMap), len(datumMap); actual != expected {
t.Fatalf("slice comparison: length mismatch; Actual: %v; Expected: %v", actual, expected)
}
for i, datumValue := range datumSlice {
decodedValue := decodedSlice[i]
if actual, expected := fmt.Sprintf("%v", decodedValue), fmt.Sprintf("%v", datumValue); actual != expected {
t.Errorf("slice comparison: values differ for index: %d: Actual: %v; Expected: %v", i+1, actual, expected)
}
}
} else if datumIsString && decodedIsString {
if actual, expected := decodedString, datumString; actual != expected {
t.Errorf("string comparison: Actual: %v; Expected: %v", actual, expected)
}
} else if actual, expected := fmt.Sprintf("%v", decoded), fmt.Sprintf("%v", datum); actual != expected {
t.Errorf("schema: %s; Datum: %v; Actual: %s; Expected: %s", schema, datum, actual, expected)
}
}
func testTextEncodePass(t *testing.T, schema string, datum interface{}, expected []byte) {
codec, err := goavro.NewCodec(schema)
if err != nil {
t.Fatalf("Schma: %q %s", schema, err)
}
actual, err := codec.TextualFromNative(nil, datum)
if err != nil {
t.Fatalf("schema: %s; Datum: %v; %s", schema, datum, err)
}
if !bytes.Equal(actual, expected) {
t.Errorf("schema: %s; Datum: %v; Actual: %+q; Expected: %+q", schema, datum, actual, expected)
}
}
// testTextCodecPass does a bi-directional codec check, by encoding datum to
// bytes, then decoding bytes back to datum.
func testTextCodecPass(t *testing.T, schema string, datum interface{}, buf []byte) {
testTextDecodePass(t, schema, datum, buf)
testTextEncodePass(t, schema, datum, buf)
}