forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_marshaler_test.go
144 lines (106 loc) · 3.69 KB
/
codec_marshaler_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
package avro_test
import (
"bytes"
"errors"
"testing"
"time"
"github.com/hamba/avro/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDecoder_TextUnmarshalerPtr(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x28, 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x32, 0x54, 0x30, 0x33, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x35, 0x5a}
schema := "string"
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_TextUnmarshalerPtrPtr(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x28, 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x32, 0x54, 0x30, 0x33, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x35, 0x5a}
schema := "string"
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_TextUnmarshalerError(t *testing.T) {
defer ConfigTeardown()
data := []byte{0x28, 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x32, 0x54, 0x30, 0x33, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x35, 0x5a}
schema := "string"
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_TextMarshaler(t *testing.T) {
defer ConfigTeardown()
schema := "string"
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{0x28, 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x32, 0x54, 0x30, 0x33, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x35, 0x5a}, buf.Bytes())
}
func TestEncoder_TextMarshalerPtr(t *testing.T) {
defer ConfigTeardown()
schema := "string"
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{0x28, 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x31, 0x2d, 0x30, 0x32, 0x54, 0x30, 0x33, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x35, 0x5a}, buf.Bytes())
}
func TestEncoder_TextMarshalerPtrNil(t *testing.T) {
defer ConfigTeardown()
schema := "string"
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_TextMarshalerError(t *testing.T) {
defer ConfigTeardown()
schema := "string"
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)
ts := TestTimestampError{}
err = enc.Encode(&ts)
assert.Error(t, err)
}
type TestTimestamp time.Time
func (t TestTimestamp) MarshalText() ([]byte, error) {
return (time.Time)(t).MarshalText()
}
type TestTimestampPtr time.Time
func (t *TestTimestampPtr) UnmarshalText(data []byte) error {
return (*time.Time)(t).UnmarshalText(data)
}
func (t *TestTimestampPtr) MarshalText() ([]byte, error) {
return (*time.Time)(t).MarshalText()
}
type TestTimestampError time.Time
func (t *TestTimestampError) UnmarshalText(data []byte) error {
return errors.New("test")
}
func (t *TestTimestampError) MarshalText() ([]byte, error) {
return nil, errors.New("test")
}