-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_goredis_test.go
126 lines (91 loc) · 2.61 KB
/
server_goredis_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
package redimock
import (
"context"
"testing"
"time"
"github.com/go-redis/redis"
"github.com/stretchr/testify/require"
)
func TestGoRedisStringKey(t *testing.T) {
ctx, cnl := context.WithCancel(context.Background())
defer cnl()
s, err := NewServer(ctx, "")
require.NoError(t, err)
s.ExpectSet("test_key", "Hello", true).Once()
s.ExpectSet("test_key", "Hello2", false, "ex", "1", "nx").Once()
s.ExpectGet("test_key", true, "Hello").Once()
s.ExpectGet("test_key_2", false, "").Once()
cl := redis.NewClient(&redis.Options{
Addr: s.Addr().String(),
})
x, e := cl.Set("test_key", "Hello", 0).Result()
require.NoError(t, e)
require.Equal(t, "OK", x)
b, e := cl.SetNX("test_key", "Hello2", time.Second).Result()
require.NoError(t, e)
require.False(t, b)
str := cl.Get("test_key")
x, e = str.Result()
require.NoError(t, e)
require.Equal(t, "Hello", x)
str = cl.Get("test_key_2")
_, e = str.Result()
require.Error(t, e)
require.NoError(t, s.ExpectationsWereMet())
}
func TestGoRedisExtraCall(t *testing.T) {
ctx, cnl := context.WithCancel(context.Background())
defer cnl()
s, err := NewServer(ctx, "")
require.NoError(t, err)
cl := redis.NewClient(&redis.Options{
Addr: s.Addr().String(),
})
err = cl.Ping().Err()
require.Error(t, err)
require.Error(t, s.ExpectationsWereMet())
}
func TestGoRedisPing(t *testing.T) {
ctx, cnl := context.WithCancel(context.Background())
defer cnl()
s, err := NewServer(ctx, "")
require.NoError(t, err)
s.ExpectPing().Times(1)
cl := redis.NewClient(&redis.Options{
Addr: s.Addr().String(),
})
// this library does not support Ping argument
st, e := cl.Ping().Result()
require.NoError(t, e)
require.Equal(t, "PONG", st)
require.NoError(t, s.ExpectationsWereMet())
}
func TestGoRedisList(t *testing.T) {
ctx, cnl := context.WithCancel(context.Background())
defer cnl()
s, err := NewServer(ctx, "")
require.NoError(t, err)
v := map[string]string{"v1": "v2", "v3": "v4"}
s.ExpectHGetAll("mykey", v).Once()
cl := redis.NewClient(&redis.Options{
Addr: s.Addr().String(),
})
ret, err := cl.HGetAll("mykey").Result()
require.NoError(t, err)
require.Equal(t, v, ret)
require.NoError(t, s.ExpectationsWereMet())
}
func TestGoRedisListHSet(t *testing.T) {
ctx, cnl := context.WithCancel(context.Background())
defer cnl()
s, err := NewServer(ctx, "")
require.NoError(t, err)
s.ExpectHSet("mykey", "fld", "value", true).Once()
cl := redis.NewClient(&redis.Options{
Addr: s.Addr().String(),
})
ret, err := cl.HSet("mykey", "fld", "value").Result()
require.NoError(t, err)
require.False(t, ret)
require.NoError(t, s.ExpectationsWereMet())
}