-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshal_test.go
151 lines (120 loc) · 4.32 KB
/
marshal_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
145
146
147
148
149
150
151
package clickhouse
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func BenchmarkMarshalString(b *testing.B) {
for i := 0; i < b.N; i++ {
marshal("test")
}
}
func TestUnmarshal(t *testing.T) {
var (
err error
valInt int
valInt8 int8
valInt16 int16
valInt32 int32
valInt64 int64
valString string
valTime time.Time
valUnsupported testing.T
valFloat32 float32
valFloat64 float64
valArrayString []string
valArrayInt []int
valArrayFloat32 []float32
valArray Array
)
err = unmarshal(&valInt, "10")
assert.Equal(t, int(10), valInt)
assert.NoError(t, err)
err = unmarshal(&valInt8, "10")
assert.Equal(t, int8(10), valInt8)
assert.NoError(t, err)
err = unmarshal(&valInt16, "10")
assert.Equal(t, int16(10), valInt16)
assert.NoError(t, err)
err = unmarshal(&valInt32, "10")
assert.Equal(t, int32(10), valInt32)
assert.NoError(t, err)
err = unmarshal(&valInt64, "10")
assert.Equal(t, int64(10), valInt64)
assert.NoError(t, err)
err = unmarshal(&valString, "10")
assert.Equal(t, "10", valString)
assert.NoError(t, err)
err = unmarshal(&valString, "String1\\'")
assert.Equal(t, "String1'", valString)
assert.NoError(t, err)
err = unmarshal(&valTime, "2016-10-07 19:21:17")
assert.Equal(t, time.Date(2016, 10, 7, 19, 21, 17, 0, time.UTC), valTime)
assert.NoError(t, err)
err = unmarshal(&valUnsupported, "10")
assert.Error(t, err)
err = unmarshal(&valFloat32, "3.141592")
assert.Equal(t, float32(3.141592), valFloat32)
assert.NoError(t, err)
err = unmarshal(&valFloat64, "3.1415926535")
assert.Equal(t, float64(3.1415926535), valFloat64)
assert.NoError(t, err)
err = unmarshal(&valArrayString, "['k10','20']")
assert.Equal(t, []string{"k10", "20"}, valArrayString)
assert.NoError(t, err)
err = unmarshal(&valArrayString, "")
assert.Error(t, err, "Column data is not of type []string")
err = unmarshal(&valArrayString, "[]")
assert.Equal(t, []string{}, valArrayString)
assert.NoError(t, err)
err = unmarshal(&valArrayInt, "[10,20]")
assert.Equal(t, []int{10, 20}, valArrayInt)
assert.NoError(t, err)
err = unmarshal(&valArrayInt, "[10,20]")
assert.Equal(t, []int{10, 20}, valArrayInt)
assert.NoError(t, err)
err = unmarshal(&valArrayInt, "")
assert.Error(t, err, "Column data is not of type []int")
err = unmarshal(&valArrayFloat32, "[3.141592]")
assert.Equal(t, []float32{3.141592}, valArrayFloat32)
assert.NoError(t, err)
err = unmarshal(&valArray, "['k10','20']")
assert.Equal(t, Array{"k10", "20"}, valArray)
assert.NoError(t, err)
err = unmarshal(&valArray, "[10,20]")
assert.Equal(t, Array{10, 20}, valArray)
assert.NoError(t, err)
err = unmarshal(&valArray, "[3.14,5.25]")
assert.Equal(t, Array{3.14, 5.25}, valArray)
assert.NoError(t, err)
err = unmarshal(&valArray, "")
assert.Error(t, err, "Column data is not of type Array")
err = unmarshal(&valArray, "[]")
assert.Equal(t, Array{}, valArray)
assert.NoError(t, err)
}
func TestMarshal(t *testing.T) {
assert.Equal(t, "10", marshal(10))
assert.Equal(t, "10", marshal(int8(10)))
assert.Equal(t, "10", marshal(int16(10)))
assert.Equal(t, "10", marshal(int32(10)))
assert.Equal(t, "10", marshal(int64(10)))
assert.Equal(t, "1", marshal(true))
assert.Equal(t, "0", marshal(false))
assert.Equal(t, "3.141592", marshal(float32(3.141592)))
assert.Equal(t, "3.1415926535", marshal(float64(3.1415926535)))
assert.Equal(t, "'10'", marshal("10"))
assert.Equal(t, "'String1\\''", marshal("String1'"))
assert.Equal(t, "'String\r'", marshal("String\r"))
assert.Equal(t, "'String\r'", marshal("String\r"))
assert.Equal(t, `'String\\'`, marshal(`String\`))
assert.Equal(t, "[10,20,30]", marshal(Array{10, 20, 30}))
assert.Equal(t, "['k10','20','30val']", marshal(Array{"k10", "20", "30val"}))
assert.Equal(t, "['k10','20','30val']", marshal([]string{"k10", "20", "30val"}))
assert.Equal(t, "['k10','20','30val\\\\']", marshal([]string{"k10", "20", "30val\\"}))
assert.Equal(t, "[10,20,30]", marshal([]int{10, 20, 30}))
assert.Equal(t, "IPv4StringToNum('192.0.2.128')", marshal(Func{"IPv4StringToNum", "192.0.2.128"}))
assert.Equal(t, "IPv4NumToString(3221225985)", marshal(Func{"IPv4NumToString", 3221225985}))
assert.Equal(t, "''", marshal(t))
assert.Equal(t, "2017-04-10", marshal(time.Date(2017, 04, 10, 0, 0, 0, 0, time.UTC)))
}