forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_fixed_test.go
97 lines (71 loc) · 2.4 KB
/
decoder_fixed_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
package avro_test
import (
"bytes"
"math/big"
"testing"
"github.com/hamba/avro"
"github.com/stretchr/testify/assert"
)
func TestDecoder_FixedInvalidType(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x66, 0x6F, 0x6F, 0x66, 0x6F, 0x6F}
schema := `{"type":"fixed", "name": "test", "size": 6}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
assert.NoError(t, err)
var i [6]int
err = dec.Decode(&i)
assert.Error(t, err)
}
func TestDecoder_Fixed(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x66, 0x6F, 0x6F, 0x66, 0x6F, 0x6F}
schema := `{"type":"fixed", "name": "test", "size": 6}`
dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))
var got [6]byte
err := dec.Decode(&got)
assert.NoError(t, err)
assert.Equal(t, [6]byte{'f', 'o', 'o', 'f', 'o', 'o'}, got)
}
func TestDecoder_FixedRat_Positive(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x00, 0x00, 0x00, 0x00, 0x87, 0x78}
schema := `{"type":"fixed", "name": "test", "size": 6,"logicalType":"decimal","precision":4,"scale":2}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
assert.NoError(t, err)
got := &big.Rat{}
err = dec.Decode(got)
assert.NoError(t, err)
assert.Equal(t, big.NewRat(1734, 5), got)
}
func TestDecoder_FixedRat_Negative(t *testing.T) {
defer ConfigTeardown()
data := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x78, 0x88}
schema := `{"type":"fixed", "name": "test", "size": 6, "logicalType":"decimal","precision":4,"scale":2}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
assert.NoError(t, err)
got := &big.Rat{}
err = dec.Decode(got)
assert.NoError(t, err)
assert.Equal(t, big.NewRat(-1734, 5), got)
}
func TestDecoder_FixedRat_Zero(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
schema := `{"type":"fixed", "name": "test", "size": 6,"logicalType":"decimal","precision":4,"scale":2}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
assert.NoError(t, err)
got := &big.Rat{}
err = dec.Decode(got)
assert.NoError(t, err)
assert.Equal(t, big.NewRat(0, 1), got)
}
func TestDecoder_FixedRatInvalidLogicalSchema(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
schema := `{"type":"fixed", "name": "test", "size": 6}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
assert.NoError(t, err)
got := &big.Rat{}
err = dec.Decode(got)
assert.Error(t, err)
}