-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
203 lines (191 loc) · 3.79 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
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
package jcache
import (
"context"
"github.com/redis/go-redis/v9"
"reflect"
"testing"
"time"
"github.com/jerbe/jcache/v2/driver"
)
/**
@author : Jerbe - The porter from Earth
@time : 2023/9/16 22:24
@describe :
*/
func newClient() *Client {
return NewClient(driver.NewMemory())
}
func Test_Client_Del(t *testing.T) {
cli := newClient()
type fields struct {
drivers []driver.Common
}
type args struct {
ctx context.Context
keys []string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "删除成功",
args: args{
ctx: context.Background(),
keys: []string{"key", "key1", "key2"},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := cli.Del(tt.args.ctx, tt.args.keys...).Err(); (err != nil) != tt.wantErr {
t.Errorf("Del() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_Client_Exists(t *testing.T) {
cli := newClient()
cli.Set(context.Background(), "key1", "value1", 0)
type args struct {
ctx context.Context
keys []string
}
tests := []struct {
name string
args args
want int64
wantErr bool
}{
{
name: "判断失败",
args: args{
ctx: context.Background(),
keys: []string{"key"},
},
want: 0,
},
{
name: "判断成功,1",
args: args{
ctx: context.Background(),
keys: []string{"key1"},
},
want: 1,
},
{
name: "判断成功,2",
args: args{
ctx: context.Background(),
keys: []string{"key1", "key"},
},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := cli.Exists(tt.args.ctx, tt.args.keys...).Result()
if (err != nil) != tt.wantErr {
t.Errorf("Exists() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Exists() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_Client_Expire(t *testing.T) {
cli := newClient()
cli.Set(context.Background(), "key1", "value1", 0)
type args struct {
ctx context.Context
key string
expiration time.Duration
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "设置失败",
args: args{
ctx: context.Background(),
key: "key",
expiration: 0,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := cli.Expire(tt.args.ctx, tt.args.key, tt.args.expiration).Err(); (err != nil) != tt.wantErr {
t.Errorf("Expire() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_Client_ExpireAt(t *testing.T) {
type fields struct {
drivers []driver.Common
}
type args struct {
ctx context.Context
key string
at time.Time
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cli := &BaseClient{
drivers: tt.fields.drivers,
}
if err := cli.ExpireAt(tt.args.ctx, tt.args.key, tt.args.at); (err != nil) != tt.wantErr {
t.Errorf("ExpireAt() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestBaseClient_Persist(t *testing.T) {
cli := newClient()
cli.Set(context.Background(), "ABC", "abc", 0)
type args struct {
ctx context.Context
key string
}
tests := []struct {
name string
args args
want driver.BoolValuer
}{
{
name: "设置成功",
args: args{
ctx: context.Background(),
key: "ABC",
},
want: func() driver.BoolValuer {
cmd := &redis.BoolCmd{}
cmd.SetVal(true)
return cmd
}(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := cli.Persist(tt.args.ctx, tt.args.key); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Persist() = %v, want %v", got, tt.want)
}
})
}
}