/
commands.go
124 lines (106 loc) · 3.63 KB
/
commands.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
package redimock
import (
"fmt"
"strings"
)
// TODO : I don't want to fall in `implement another redis` trap. so be careful :)
// == General Commands == //
// ExpectQuit try to return quit command
func (s *Server) ExpectQuit() *Command {
return s.Expect("Quit").CloseConnection().WillReturn("OK")
}
// ExpectPing is the ping command
func (s *Server) ExpectPing() *Command {
return s.Expect("PING").WithAnyArgs().WillReturn(func(args ...string) []interface{} {
if len(args) == 0 {
return []interface{}{"PONG"}
} else if len(args) == 1 {
return []interface{}{args[0]}
}
return []interface{}{Error("ERR wrong number of arguments for 'ping' command")}
})
}
// == String Commands == //
// ExpectGet return a redis GET command
func (s *Server) ExpectGet(key string, exists bool, result string) *Command {
c := s.Expect("GET").WithArgs(key)
if exists {
return c.WillReturn(BulkString(result))
}
return c.WillReturn(nil)
}
// ExpectSet return a redis set command. success could be false only for NX or XX option,
// otherwise it dose not make sense
func (s *Server) ExpectSet(key string, value string, success bool, extra ...string) *Command {
args := append([]string{key, value}, extra...)
c := s.Expect("SET").WithArgs(args...)
if success {
return c.WillReturn("OK")
}
return c.WillReturn(func(args ...string) []interface{} {
for _, i := range args {
x := strings.ToUpper(i)
if x == "NX" || x == "XX" {
return []interface{}{nil}
}
}
return []interface{}{"OK"}
})
}
// == Hash Commands == //
// ExpectHSet is the command HSET, if the update is true, then it means the key
// was there already
func (s *Server) ExpectHSet(key, field, value string, update bool) *Command {
ret := 1
if update {
ret = 0
}
return s.Expect("HSET").WithArgs(key, field, value).WillReturn(ret)
}
// ExpectHGetAll return the HGETALL command
func (s *Server) ExpectHGetAll(key string, ret map[string]string) *Command {
arr := make([]BulkString, 0, len(ret)*2)
for i := range ret {
arr = append(arr, BulkString(i), BulkString(ret[i]))
}
return s.Expect("HGETALL").WithArgs(key).WillReturn(arr)
}
// == List Commands == //
// ExpectRPush is a wrapper for both lpush and rpush
func (s *Server) expectLRPush(cmd string, result int, key string, values ...string) *Command {
return s.Expect(cmd).WithFnArgs(func(in ...string) (x bool) {
if len(values) == 0 {
if len(in) == 0 {
return false
}
return in[0] == key
}
return equalArgs(in, append([]string{key}, values...))
}).WillReturn(result)
}
// ExpectLPush is helper for lpush command
func (s *Server) ExpectLPush(result int, key string, values ...string) *Command {
return s.expectLRPush("lpush", result, key, values...)
}
// ExpectRPush is helper for lpush command
func (s *Server) ExpectRPush(result int, key string, values ...string) *Command {
return s.expectLRPush("rpush", result, key, values...)
}
func (s *Server) expectBLRPop(cmd string, delay int, topic, resp string, result bool, keys ...string) *Command {
return s.Expect(cmd).WithFnArgs(func(in ...string) bool {
return equalArgs(in, append(keys, fmt.Sprint(delay)))
}).WillReturnFn(func(...string) []interface{} {
if result {
return []interface{}{[]string{topic, resp}}
}
return []interface{}{nil}
})
}
// ExpectBLPop is the blpop command
func (s *Server) ExpectBLPop(delay int, topic, resp string, result bool, keys ...string) *Command {
return s.expectBLRPop("BLPOP", delay, topic, resp, result, keys...)
}
// ExpectBRPop is the brpop command
func (s *Server) ExpectBRPop(delay int, topic, resp string, result bool, keys ...string) *Command {
return s.expectBLRPop("BRPOP", delay, topic, resp, result, keys...)
}