-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
175 lines (155 loc) · 3.78 KB
/
client_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
package resp
import (
"context"
"testing"
)
// Mock objects and helpers
var (
AuthFunc func(password string) error
PingFunc func(ctx context.Context) error
SendFunc func(command string) error
ReceiveFunc func() (string, error)
CloseFunc func() error
)
type mockConnection struct {
// fields to simulate the Redis connection state
}
func (m *mockConnection) Ping(ctx context.Context) error {
return PingFunc(ctx)
}
func (m *mockConnection) Auth(ctx context.Context, password string) error {
return AuthFunc(password)
}
func (m *mockConnection) Send(ctx context.Context, command string) error {
return SendFunc(command)
}
func (m *mockConnection) Receive(ctx context.Context) (string, error) {
return ReceiveFunc()
}
func (m *mockConnection) Close() error {
return CloseFunc()
}
// Helper function to create a Client with a mock dialer and connection
func newMockClient(poolSize int, auth string) *Client {
client := &Client{
address: "localhost:6379",
auth: auth,
dialer: &MockDialer{},
}
client.conn = &mockConnection{}
return client
}
// TestDo tests sending a command to the Redis server
func TestClient_Do(t *testing.T) {
SendFunc = func(command string) error {
return nil
}
ReceiveFunc = func() (string, error) {
return "OK", nil
}
client := newMockClient(2, "password")
response, err := client.Do(context.Background(), "PING")
if err != nil {
t.Errorf("Do returned error: %s", err)
}
if response != "OK" {
t.Errorf("Do did not return +OK, got: %s", response)
}
}
func TestClient_Set(t *testing.T) {
SendFunc = func(command string) error {
return nil
}
ReceiveFunc = func() (string, error) {
return "OK", nil
}
client := newMockClient(2, "password")
err := client.Set(context.Background(), "key", "value")
if err != nil {
t.Errorf("Set returned error: %s", err)
}
}
func TestClient_Incr(t *testing.T) {
SendFunc = func(command string) error {
return nil
}
ReceiveFunc = func() (string, error) {
return ":1\r\n", nil
}
client := newMockClient(2, "password")
resp, err := client.Incr(context.Background(), "key")
if err != nil {
t.Errorf("Incr returned error: %s", err)
}
if resp != 1 {
t.Errorf("Do did not return valid reponse, got: %d", resp)
}
}
func TestClient_Expire(t *testing.T) {
SendFunc = func(command string) error {
return nil
}
ReceiveFunc = func() (string, error) {
return ":1", nil
}
client := newMockClient(2, "password")
success, err := client.Expire(context.Background(), "key", 1)
if err != nil {
t.Errorf("Expire returned error: %s", err)
}
if !success {
t.Errorf("invalid expire reponse")
}
}
func TestClient_SetWithTTL(t *testing.T) {
SendFunc = func(command string) error {
return nil
}
ReceiveFunc = func() (string, error) {
return "OK", nil
}
client := newMockClient(2, "password")
err := client.SetWithTTL(context.Background(), "key", "value", 1)
if err != nil {
t.Errorf("Set returned error: %s", err)
}
}
func TestClient_Get(t *testing.T) {
SendFunc = func(command string) error {
return nil
}
ReceiveFunc = func() (string, error) {
return "value", nil
}
client := newMockClient(2, "password")
resp, err := client.Get(context.Background(), "key")
if err != nil {
t.Errorf("Get returned error: %s", err)
}
if resp != "value" {
t.Errorf("invalid Get reponse")
}
}
func TestClient_Delete(t *testing.T) {
SendFunc = func(command string) error {
return nil
}
ReceiveFunc = func() (string, error) {
return ":1", nil
}
client := newMockClient(2, "password")
err := client.Delete(context.Background(), "key")
if err != nil {
t.Errorf("Delete returned error: %s", err)
}
}
func TestClose(t *testing.T) {
CloseFunc = func() error {
return nil
}
client := newMockClient(2, "password")
err := client.Close()
if err != nil {
t.Errorf("Close did not close the channel")
}
}