forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder_test.go
111 lines (103 loc) · 2.99 KB
/
encoder_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
package clickhouse
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTextEncoder(t *testing.T) {
dt := time.Date(2011, 3, 6, 6, 20, 0, 0, time.UTC)
d := time.Date(2012, 5, 31, 0, 0, 0, 0, time.UTC)
testCases := []struct {
value interface{}
expected string
}{
{true, "1"},
{int8(1), "1"},
{int16(1), "1"},
{int32(1), "1"},
{int64(1), "1"},
{int(-1), "-1"},
{uint8(1), "1"},
{uint16(1), "1"},
{uint32(1), "1"},
{uint64(1), "1"},
{uint(1), "1"},
{float32(1), "1"},
{float64(1), "1"},
{dt, "'2011-03-06 06:20:00'"},
{d, "'2012-05-31 00:00:00'"},
{"hello", "'hello'"},
{[]byte("hello"), "hello"},
{`\\'hello`, `'\\\\\'hello'`},
{[]byte(`\\'hello`), `\\'hello`},
{[]int32{1, 2}, "[1,2]"},
{[]int32{}, "[]"},
{Array([]int8{1}), "[1]"},
{Array([]interface{}{Array([]int8{1})}), "[[1]]"},
{[][]int16{{1}}, "[[1]]"},
{[]int16(nil), "[]"},
{(*int16)(nil), "NULL"},
}
enc := new(textEncoder)
for _, tc := range testCases {
v, err := enc.Encode(tc.value)
if assert.NoError(t, err) {
assert.Equal(t, tc.expected, string(v))
}
}
}
func TestTextDecoder(t *testing.T) {
dt := time.Date(2011, 3, 6, 6, 20, 0, 0, time.UTC)
d := time.Date(2012, 5, 31, 0, 0, 0, 0, time.UTC)
zerodt := time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
testCases := []struct {
tt string
value string
expected interface{}
}{
{"Int8", "1", int8(1)},
{"Int16", "1", int16(1)},
{"Int32", "1", int32(1)},
{"Int64", "1", int64(1)},
{"UInt8", "1", uint8(1)},
{"UInt16", "1", uint16(1)},
{"UInt32", "1", uint32(1)},
{"UInt64", "1", uint64(1)},
{"Float32", "1", float32(1)},
{"Float64", "1", float64(1)},
{"Date", "'2012-05-31'", d},
{"Date", "'0000-00-00'", zerodt},
{"DateTime", "'2011-03-06 06:20:00'", dt},
{"DateTime", "'0000-00-00 00:00:00'", zerodt},
{"DateTime(\\'Europe/Moscow\\')", "'2011-03-06 06:20:00'", dt},
{"String", "'hello'", "hello"},
{"String", `'\\\\\'hello'`, `\\'hello`},
{"FixedString(5)", "'hello'", "hello"},
{"FixedString(7)", `'\\\\\'hello'`, `\\'hello`},
{"Enum8('one'=1)", "'one'", "one"},
{"Enum16('one'=1)", "'one'", "one"},
{"Array(UInt32)", "[1,2]", []uint32{1, 2}},
{"Array(UInt32)", "[]", []uint32{}},
{"Array(String)", "['one, two','one\\'']", []string{"one, two", "one'"}},
{"Array(String)", "['']", []string{""}},
{"Array(String)", "[]", []string{}},
{"Array(FixedString(3)", "['1,2','2,3']", []string{"1,2", "2,3"}},
}
dec := &textDecoder{location: time.UTC, useDBLocation: false}
for i, tc := range testCases {
v, err := dec.Decode(tc.tt, []byte(tc.value))
if assert.NoError(t, err, "%d", i) {
assert.Equal(t, tc.expected, v)
}
}
}
func TestDecodeTimeWithLocation(t *testing.T) {
dt := time.Date(2011, 3, 6, 3, 20, 0, 0, time.UTC)
dataType := "DateTime(\\'Europe/Moscow\\')"
dtStr := "'2011-03-06 06:20:00'"
dec := &textDecoder{location: time.UTC, useDBLocation: true}
v, err := dec.Decode(dataType, []byte(dtStr))
if assert.NoError(t, err) {
assert.Equal(t, dt, v)
}
}