-
Notifications
You must be signed in to change notification settings - Fork 18
/
convert_util_test.go
294 lines (240 loc) · 7.06 KB
/
convert_util_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package godis
import (
"fmt"
"github.com/stretchr/testify/assert"
"math"
"testing"
)
func TestBoolToByteArray(t *testing.T) {
arr := BoolToByteArr(true)
assert.Equal(t, []byte{0x31}, arr)
arr = BoolToByteArr(false)
assert.Equal(t, []byte{0x30}, arr)
}
func TestByteArrToStringReply(t *testing.T) {
arr := []byte("good")
s, e := ByteArrToStrReply(arr, nil)
assert.Nil(t, e)
assert.Equal(t, "good", s)
s, e = ByteArrToStrReply(arr, newClusterMaxAttemptsError("exceed max attempts"))
assert.NotNil(t, e, e.Error())
assert.Equal(t, "", s)
}
func TestByteArrayToFloat64(t *testing.T) {
arr := []byte("1.1")
f := ByteArrToFloat64(arr)
assert.Equal(t, 1.1, f)
}
func TestFloat64ToByteArray(t *testing.T) {
arr := []byte("1.1")
arr1 := Float64ToByteArr(1.1)
assert.Equal(t, arr1, arr)
arr1 = Float64ToByteArr(math.Inf(1))
assert.Equal(t, []byte("+inf"), arr1)
arr1 = Float64ToByteArr(math.Inf(-1))
assert.Equal(t, []byte("-inf"), arr1)
}
func TestInt64ToBoolReply(t *testing.T) {
b, e := Int64ToBoolReply(1, nil)
assert.Nil(t, e)
assert.Equal(t, true, b)
b, e = Int64ToBoolReply(0, nil)
assert.Nil(t, e)
assert.Equal(t, false, b)
b, e = Int64ToBoolReply(10, nil)
assert.Nil(t, e)
assert.Equal(t, false, b)
b, e = Int64ToBoolReply(0, newRedirectError("redirect too many times"))
assert.NotNil(t, e, e.Error())
assert.Equal(t, false, b)
}
func TestInt64ToByteArray(t *testing.T) {
}
func TestIntToByteArray(t *testing.T) {
}
func TestObjectArrToGeoCoordinateReply(t *testing.T) {
arr, e := ObjArrToGeoCoordinateReply(nil, newMovedDataError("move error", "localhost", 7000, 1000))
assert.NotNil(t, e, e.Error())
assert.Nil(t, arr)
arr0 := make([]interface{}, 0)
for i := 0; i < 3; i++ {
arr0 = append(arr0, nil)
}
arr, e = ObjArrToGeoCoordinateReply(arr0, nil)
assert.Nil(t, e)
assert.Len(t, arr, 3)
arr0 = make([]interface{}, 0)
item := make([]interface{}, 0)
item = append(item, []byte("1a"))
item = append(item, []byte("2"))
arr0 = append(arr0, item)
arr, e = ObjArrToGeoCoordinateReply(arr0, nil)
assert.NotNil(t, e)
assert.Nil(t, arr)
arr0 = make([]interface{}, 0)
item = make([]interface{}, 0)
item = append(item, []byte("1"))
item = append(item, []byte("2b"))
arr0 = append(arr0, item)
arr, e = ObjArrToGeoCoordinateReply(arr0, nil)
assert.NotNil(t, e)
assert.Nil(t, arr)
}
func TestObjectArrToGeoRadiusResponseReply(t *testing.T) {
arr, e := ObjArrToGeoRadiusResponseReply(nil, newAskDataError("move error", "localhost", 7000, 1000))
assert.NotNil(t, e, e.Error())
assert.Nil(t, arr)
arr0 := make([]interface{}, 0)
arr0 = append(arr0, []byte("a"))
arr0 = append(arr0, []byte("b"))
arr, e = ObjArrToGeoRadiusResponseReply(arr0, nil)
assert.Nil(t, e)
assert.Len(t, arr, 2)
}
func TestObjectArrToMapArrayReply(t *testing.T) {
objs := make([]interface{}, 0)
for i := 0; i < 4; i++ {
objs = append(objs, [][]byte{[]byte(fmt.Sprintf("good%d", i)), []byte(fmt.Sprintf("good%d", i+1))})
}
arr, e := ObjArrToMapArrayReply(objs, nil)
assert.Nil(t, e)
assert.Len(t, arr, 4)
}
func TestObjectArrToScanResultReply(t *testing.T) {
result, e := ObjArrToScanResultReply(nil, newNoReachableClusterNodeError("no reachable server"))
assert.NotNil(t, e, e.Error())
assert.Nil(t, result)
}
func TestObjectToEvalResult(t *testing.T) {
arr, e := ObjToEvalResult(nil, newClusterError("error"))
assert.NotNil(t, e, e.Error())
assert.Nil(t, arr)
arr0 := make([]interface{}, 0)
arr0 = append(arr0, []byte("a"))
arr0 = append(arr0, []byte("b"))
arr, e = ObjToEvalResult(arr0, nil)
assert.Nil(t, e)
assert.Len(t, arr, 2)
arr, e = ObjToEvalResult("a", nil)
assert.Nil(t, e)
assert.Equal(t, "a", arr)
}
func TestStringArrToTupleReply(t *testing.T) {
arr := []string{"a", "1", "b", "2"}
tuples, e := StrArrToTupleReply(arr, nil)
assert.Nil(t, e)
assert.Len(t, tuples, 2)
arr = []string{}
tuples, e = StrArrToTupleReply(arr, nil)
assert.Nil(t, e)
assert.Len(t, tuples, 0)
arr = []string{"a", "1a", "b", "2"}
tuples, e = StrArrToTupleReply(arr, nil)
assert.NotNil(t, e) //convert failed
assert.Nil(t, tuples)
}
func TestStringArrayToByteArray(t *testing.T) {
}
func TestStringArrayToMapReply(t *testing.T) {
m, e := StrArrToMapReply(nil, newRedisError("internal error"))
assert.NotNil(t, e, e.Error())
assert.Nil(t, m)
}
func TestStringStringArrayToByteArray(t *testing.T) {
}
func TestStringStringArrayToStringArray(t *testing.T) {
}
func TestStringToFloat64Reply(t *testing.T) {
f, e := StrToFloat64Reply("1.1", nil)
assert.Nil(t, e)
assert.Equal(t, 1.1, f)
f, e = StrToFloat64Reply("1.1a", nil)
assert.NotNil(t, e) //not a float
assert.Equal(t, float64(0), f)
f, e = StrToFloat64Reply("1.1", newDataError("error data format"))
assert.NotNil(t, e, e.Error())
assert.Equal(t, float64(0), f)
}
func TestToBoolArrayReply(t *testing.T) {
b, e := ToBoolArrReply(nil, newBusyError("is busy now"))
assert.NotNil(t, e, e.Error())
assert.Nil(t, b)
}
func TestToBoolReply(t *testing.T) {
b, e := ToBoolReply(nil, newBusyError("is busy now"))
assert.NotNil(t, e, e.Error())
assert.Equal(t, false, b)
}
func TestToFloat64Reply(t *testing.T) {
b, e := ToFloat64Reply(nil, newClusterMaxAttemptsError("internal error"))
assert.NotNil(t, e, e.Error())
assert.Equal(t, float64(0), b)
}
func TestToGeoArrayReply(t *testing.T) {
b, e := ToGeoCoordArrReply(nil, newClusterOperationError("is busy now"))
assert.NotNil(t, e, e.Error())
assert.Nil(t, b)
}
func TestToGeoRespArrayReply(t *testing.T) {
b, e := ToGeoRespArrReply(nil, newNoScriptError("is busy now"))
assert.NotNil(t, e, e.Error())
assert.Nil(t, b)
}
func TestToInt64ArrayReply(t *testing.T) {
var obj interface{}
obj = []int64{1, 2, 3}
arr, e := ToInt64ArrReply(obj, nil)
assert.Nil(t, e)
assert.Equal(t, []int64{1, 2, 3}, arr)
}
func TestToInt64Reply(t *testing.T) {
_, e := ToGeoCoordArrReply(nil, newBusyError("is busy now"))
assert.NotNil(t, e, e.Error())
}
func TestToMapReply(t *testing.T) {
_, e := ToMapReply(nil, newBusyError("is busy now"))
assert.NotNil(t, e, e.Error())
}
func TestToScanResultReply(t *testing.T) {
_, e := ToScanResultReply(nil, newBusyError("is busy now"))
assert.NotNil(t, e, e.Error())
}
func TestToStringArrayReply(t *testing.T) {
_, e := ToStrArrReply(nil, newBusyError("is busy now"))
assert.NotNil(t, e, e.Error())
}
func TestToStringReply(t *testing.T) {
_, e := ToStrReply(nil, newBusyError("is busy now"))
assert.NotNil(t, e, e.Error())
}
func TestToTupleArrayReply(t *testing.T) {
_, e := ToTupleArrReply(nil, newBusyError("is busy now"))
assert.NotNil(t, e, e.Error())
}
func Test_int64Builder_build(t *testing.T) {
b := newInt64Builder()
r, e := b.build(nil)
assert.Nil(t, e)
assert.Equal(t, 0, r)
r, e = b.build("a")
assert.NotNil(t, e)
assert.Equal(t, 0, r)
}
func Test_stringArrayBuilder_build(t *testing.T) {
b := newStringArrayBuilder()
r, e := b.build(nil)
assert.Nil(t, e)
assert.Empty(t, r)
r, e = b.build(1)
assert.NotNil(t, e)
assert.Equal(t, nil, r)
}
func Test_stringBuilder_build(t *testing.T) {
b := newStrBuilder()
r, e := b.build(nil)
assert.Nil(t, e)
assert.Equal(t, "", r)
r, e = b.build(1)
assert.NotNil(t, e)
assert.Equal(t, "", r)
}