forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_test.go
218 lines (202 loc) · 6.21 KB
/
result_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
package mock
import (
"bytes"
"errors"
"io"
"reflect"
"strings"
"testing"
"github.com/redis/rueidis"
)
func TestRedisString(t *testing.T) {
m := RedisString("s")
if v, err := m.ToString(); err != nil || v != "s" {
t.Fatalf("unexpected value %v", v)
}
}
func TestRedisError(t *testing.T) {
m := RedisError("s")
if err := m.Error(); err == nil || err.Error() != "s" {
t.Fatalf("unexpected value %v", err)
}
}
func TestRedisInt64(t *testing.T) {
m := RedisInt64(1)
if v, err := m.ToInt64(); err != nil || v != int64(1) {
t.Fatalf("unexpected value %v", v)
}
if v, err := m.AsInt64(); err != nil || v != int64(1) {
t.Fatalf("unexpected value %v", v)
}
}
func TestRedisFloat64(t *testing.T) {
m := RedisFloat64(1)
if v, err := m.ToFloat64(); err != nil || v != float64(1) {
t.Fatalf("unexpected value %v", v)
}
if v, err := m.AsFloat64(); err != nil || v != float64(1) {
t.Fatalf("unexpected value %v", v)
}
}
func TestRedisBool(t *testing.T) {
m := RedisBool(true)
if v, err := m.ToBool(); err != nil || v != true {
t.Fatalf("unexpected value %v", v)
}
if v, err := m.AsBool(); err != nil || v != true {
t.Fatalf("unexpected value %v", v)
}
}
func TestRedisNil(t *testing.T) {
m := RedisNil()
if err := m.Error(); err == nil {
t.Fatalf("unexpected value %v", err)
}
if v := m.IsNil(); v != true {
t.Fatalf("unexpected value %v", v)
}
}
func TestRedisArray(t *testing.T) {
m := RedisArray(RedisString("0"), RedisString("1"), RedisString("2"))
if arr, err := m.AsStrSlice(); err != nil || !reflect.DeepEqual(arr, []string{"0", "1", "2"}) {
t.Fatalf("unexpected value %v", err)
}
}
func TestRedisMap(t *testing.T) {
m := RedisMap(map[string]rueidis.RedisMessage{
"a": RedisString("0"),
"b": RedisString("1"),
})
if arr, err := m.AsStrMap(); err != nil || !reflect.DeepEqual(arr, map[string]string{
"a": "0",
"b": "1",
}) {
t.Fatalf("unexpected value %v", err)
}
if arr, err := m.ToMap(); err != nil || !reflect.DeepEqual(arr, map[string]rueidis.RedisMessage{
"a": RedisString("0"),
"b": RedisString("1"),
}) {
t.Fatalf("unexpected value %v", err)
}
}
func TestRedisResult(t *testing.T) {
r := Result(RedisNil())
if err := r.Error(); !rueidis.IsRedisNil(err) {
t.Fatalf("unexpected value %v", err)
}
}
func TestErrorResult(t *testing.T) {
r := ErrorResult(errors.New("any"))
if err := r.Error(); err.Error() != "any" {
t.Fatalf("unexpected value %v", err)
}
}
func TestErrorResultStream(t *testing.T) {
s := RedisResultStreamError(errors.New("any"))
if err := s.Error(); err.Error() != "any" {
t.Fatalf("unexpected value %v", err)
}
}
func TestErrorMultiResultStream(t *testing.T) {
s := MultiRedisResultStreamError(errors.New("any"))
if err := s.Error(); err.Error() != "any" {
t.Fatalf("unexpected value %v", err)
}
}
func TestResultStream(t *testing.T) {
type test struct {
msg []rueidis.RedisMessage
out []string
err []string
}
tests := []test{
{msg: []rueidis.RedisMessage{RedisString("")}, out: []string{""}, err: []string{""}},
{msg: []rueidis.RedisMessage{RedisString("0"), RedisBlobString("12345")}, out: []string{"0", "12345"}, err: []string{"", ""}},
{msg: []rueidis.RedisMessage{RedisInt64(123), RedisInt64(-456)}, out: []string{"123", "-456"}, err: []string{"", ""}},
{msg: []rueidis.RedisMessage{RedisString(""), RedisNil()}, out: []string{"", ""}, err: []string{"", "nil"}},
{msg: []rueidis.RedisMessage{RedisArray(RedisString("n")), RedisString("ok"), RedisNil(), RedisMap(map[string]rueidis.RedisMessage{"b": RedisBlobString("b")})}, out: []string{"", "ok", "", ""}, err: []string{"unsupported", "", "nil", "unsupported"}},
}
for _, tc := range tests {
s := RedisResultStream(tc.msg...)
if err := s.Error(); err != nil {
t.Fatalf("unexpected value %v", err)
}
if !s.HasNext() {
t.Fatalf("unexpected value %v", s.HasNext())
}
buf := bytes.NewBuffer(nil)
for i := 0; s.HasNext(); i++ {
n, err := s.WriteTo(buf)
if tc.err[i] != "" {
if err == nil {
t.Fatalf("unexpected value %v", err)
} else if !strings.Contains(err.Error(), tc.err[i]) {
t.Fatalf("unexpected value %v", err)
}
} else if err != nil {
t.Fatalf("unexpected value %v", err)
}
if n != int64(len(tc.out[i])) {
t.Fatalf("unexpected value %v", n)
}
}
if buf.String() != strings.Join(tc.out, "") {
t.Fatalf("unexpected value %v", buf.String())
}
if s.HasNext() {
t.Fatalf("unexpected value %v", s.HasNext())
}
if err := s.Error(); err != io.EOF {
t.Fatalf("unexpected value %v", err)
}
}
}
func TestMultiResultStream(t *testing.T) {
type test struct {
msg []rueidis.RedisMessage
out []string
err []string
}
tests := []test{
{msg: []rueidis.RedisMessage{RedisString("")}, out: []string{""}, err: []string{""}},
{msg: []rueidis.RedisMessage{RedisString("0"), RedisBlobString("12345")}, out: []string{"0", "12345"}, err: []string{"", ""}},
{msg: []rueidis.RedisMessage{RedisInt64(123), RedisInt64(-456)}, out: []string{"123", "-456"}, err: []string{"", ""}},
{msg: []rueidis.RedisMessage{RedisString(""), RedisNil()}, out: []string{"", ""}, err: []string{"", "nil"}},
{msg: []rueidis.RedisMessage{RedisArray(RedisString("n")), RedisString("ok"), RedisNil(), RedisMap(map[string]rueidis.RedisMessage{"b": RedisBlobString("b")})}, out: []string{"", "ok", "", ""}, err: []string{"unsupported", "", "nil", "unsupported"}},
}
for _, tc := range tests {
s := MultiRedisResultStream(tc.msg...)
if err := s.Error(); err != nil {
t.Fatalf("unexpected value %v", err)
}
if !s.HasNext() {
t.Fatalf("unexpected value %v", s.HasNext())
}
buf := bytes.NewBuffer(nil)
for i := 0; s.HasNext(); i++ {
n, err := s.WriteTo(buf)
if tc.err[i] != "" {
if err == nil {
t.Fatalf("unexpected value %v", err)
} else if !strings.Contains(err.Error(), tc.err[i]) {
t.Fatalf("unexpected value %v", err)
}
} else if err != nil {
t.Fatalf("unexpected value %v", err)
}
if n != int64(len(tc.out[i])) {
t.Fatalf("unexpected value %v", n)
}
}
if buf.String() != strings.Join(tc.out, "") {
t.Fatalf("unexpected value %v", buf.String())
}
if s.HasNext() {
t.Fatalf("unexpected value %v", s.HasNext())
}
if err := s.Error(); err != io.EOF {
t.Fatalf("unexpected value %v", err)
}
}
}