forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_convertible_test.go
117 lines (87 loc) · 3.02 KB
/
codec_convertible_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
package avro_test
import (
"bytes"
"testing"
"time"
"github.com/nikplxjt/avro/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDecoder_ConvertiblePtr(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x90, 0xb2, 0xae, 0xc3, 0xec, 0x5b}
schema := `{"type": "long", "logicalType": "timestamp-millis"}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
require.NoError(t, err)
var ts TestTimestampPtr
err = dec.Decode(&ts)
require.NoError(t, err)
want := TestTimestampPtr(time.Date(2020, 01, 02, 03, 04, 05, 00, time.UTC))
assert.Equal(t, want, ts)
}
func TestDecoder_ConvertiblePtrPtr(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x2, 0x90, 0xb2, 0xae, 0xc3, 0xec, 0x5b}
schema := `{"type" : ["null", {"type": "long", "logicalType": "timestamp-millis"}]}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
require.NoError(t, err)
var ts *TestTimestampPtr
err = dec.Decode(&ts)
require.NoError(t, err)
assert.NotNil(t, ts)
want := TestTimestampPtr(time.Date(2020, 01, 02, 03, 04, 05, 00, time.UTC))
assert.Equal(t, want, *ts)
}
func TestDecoder_ConvertibleError(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x90, 0xb2, 0xae, 0xc3, 0xec, 0x5b}
schema := `{"type": "long", "logicalType": "timestamp-millis"}`
dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
require.NoError(t, err)
var ts *TestTimestampError
err = dec.Decode(&ts)
assert.Error(t, err)
}
func TestEncoder_Convertible(t *testing.T) {
defer ConfigTeardown()
schema := `{"type": "long", "logicalType": "timestamp-millis"}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
ts := TestTimestamp(time.Date(2020, 01, 02, 03, 04, 05, 00, time.UTC))
err = enc.Encode(ts)
require.NoError(t, err)
assert.Equal(t, []byte{0x90, 0xb2, 0xae, 0xc3, 0xec, 0x5b}, buf.Bytes())
}
func TestEncoder_ConvertiblePtr(t *testing.T) {
defer ConfigTeardown()
schema := `{"type" : ["null", {"type": "long", "logicalType": "timestamp-millis"}]}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
ts := TestTimestampPtr(time.Date(2020, 01, 02, 03, 04, 05, 00, time.UTC))
err = enc.Encode(&ts)
require.NoError(t, err)
assert.Equal(t, []byte{0x2, 0x90, 0xb2, 0xae, 0xc3, 0xec, 0x5b}, buf.Bytes())
}
func TestEncoder_ConvertiblePtrNil(t *testing.T) {
defer ConfigTeardown()
schema := `{"type" : ["null", {"type": "long", "logicalType": "timestamp-millis"}]}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
var ts *TestTimestampPtr
err = enc.Encode(ts)
require.NoError(t, err)
assert.Equal(t, []byte{0x00}, buf.Bytes())
}
func TestEncoder_ConvertibleError(t *testing.T) {
defer ConfigTeardown()
schema := `{"type": "long", "logicalType": "timestamp-millis"}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
ts := TestTimestampError{}
err = enc.Encode(&ts)
assert.Error(t, err)
}