forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_fixed_test.go
93 lines (67 loc) · 2.36 KB
/
encoder_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
package avro_test
import (
"bytes"
"math/big"
"testing"
"github.com/hamba/avro/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncoder_FixedInvalidType(t *testing.T) {
defer ConfigTeardown()
schema := `{"type":"fixed", "name": "test", "size": 6}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
err = enc.Encode([6]int{})
assert.Error(t, err)
}
func TestEncoder_Fixed(t *testing.T) {
defer ConfigTeardown()
schema := `{"type":"fixed", "name": "test", "size": 6}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
err = enc.Encode([6]byte{'f', 'o', 'o', 'f', 'o', 'o'})
require.NoError(t, err)
assert.Equal(t, []byte{0x66, 0x6F, 0x6F, 0x66, 0x6F, 0x6F}, buf.Bytes())
}
func TestEncoder_FixedRat_Positive(t *testing.T) {
defer ConfigTeardown()
schema := `{"type":"fixed", "name": "test", "size": 6,"logicalType":"decimal","precision":4,"scale":2}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
err = enc.Encode(big.NewRat(1734, 5))
require.NoError(t, err)
assert.Equal(t, []byte{0x00, 0x00, 0x00, 0x00, 0x87, 0x78}, buf.Bytes())
}
func TestEncoder_FixedRat_Negative(t *testing.T) {
defer ConfigTeardown()
schema := `{"type":"fixed", "name": "test", "size": 6, "logicalType":"decimal","precision":4,"scale":2}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
err = enc.Encode(big.NewRat(-1734, 5))
require.NoError(t, err)
assert.Equal(t, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x78, 0x88}, buf.Bytes())
}
func TestEncoder_FixedRat_Zero(t *testing.T) {
defer ConfigTeardown()
schema := `{"type":"fixed", "name": "test", "size": 6,"logicalType":"decimal","precision":4,"scale":2}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
err = enc.Encode(big.NewRat(0, 1))
require.NoError(t, err)
assert.Equal(t, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, buf.Bytes())
}
func TestEncoder_FixedRatInvalidLogicalSchema(t *testing.T) {
defer ConfigTeardown()
schema := `{"type":"fixed", "name": "test", "size": 6}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
err = enc.Encode(big.NewRat(1734, 5))
assert.Error(t, err)
}