/
sets_test.go
427 lines (343 loc) · 11.3 KB
/
sets_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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package cache
import (
"context"
"fmt"
"testing"
"github.com/rafaeljusto/redigomock"
"github.com/stretchr/testify/assert"
)
// TestSetAdd test the method SetAdd()
func TestSetAdd(t *testing.T) {
t.Run("set add command using mocked redis", func(t *testing.T) {
t.Parallel()
// Load redis
client, conn := loadMockRedis()
assert.NotNil(t, client)
defer client.CloseAll(conn)
var tests = []struct {
testCase string
setName string
member interface{}
dependencies []string
}{
{"set with dep", testKey, testStringValue, []string{testDependantKey}},
{"set multiple strings", testKey, []string{"one", "two", "three"}, []string{testDependantKey}},
{"set multiple integers", testKey, []int{1, 2, 3}, []string{testDependantKey}},
{"empty value", testKey, "", []string{testDependantKey}},
{"no value, no dep", testKey, "", []string{}},
}
for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
conn.Clear()
var commands []*redigomock.Cmd
// The main command to test
commands = append(commands, conn.Command(AddToSetCommand, test.setName, test.member))
// Loop for each dependency
if len(test.dependencies) > 0 {
commands = append(commands, conn.Command(MultiCommand))
for _, dep := range test.dependencies {
commands = append(commands, conn.Command(AddToSetCommand, DependencyPrefix+dep, test.setName))
}
commands = append(commands, conn.Command(ExecuteCommand))
err := SetAddRaw(conn, test.setName, test.member, test.dependencies...)
assert.NoError(t, err)
} else {
err := SetAddRaw(conn, test.setName, test.member, test.dependencies...)
assert.NoError(t, err)
}
for _, c := range commands {
assert.Equal(t, true, c.Called)
}
})
}
})
t.Run("set add command using real redis", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
// Load redis
client, conn, err := loadRealRedis()
assert.NotNil(t, client)
assert.NoError(t, err)
defer client.CloseAll(conn)
// Start with a fresh db
err = clearRealRedis(conn)
assert.NoError(t, err)
// Fire the command
err = SetAddRaw(conn, testKey, testStringValue)
assert.NoError(t, err)
// Check that the command worked
var found bool
found, err = SetIsMemberRaw(conn, testKey, testStringValue)
assert.NoError(t, err)
assert.Equal(t, true, found)
})
}
// ExampleSetAdd is an example of the method SetAdd()
func ExampleSetAdd() {
// Load a mocked redis for testing/examples
client, _ := loadMockRedis()
// Close connections at end of request
defer client.Close()
// Set the key/value
_ = SetAdd(context.Background(), client, testKey, testStringValue, testDependantKey)
// Fire the command
_, _ = SetIsMember(context.Background(), client, testKey, testStringValue)
fmt.Printf("found member: %v", testStringValue)
// Output:found member: test-string-value
}
// TestSetAddMany test the method SetAddMany()
func TestSetAddMany(t *testing.T) {
t.Run("set add many command using mocked redis", func(t *testing.T) {
t.Parallel()
// Load redis
client, conn := loadMockRedis()
assert.NotNil(t, client)
defer client.CloseAll(conn)
var tests = []struct {
testCase string
setName string
members []interface{}
}{
{"set one", testKey, []interface{}{testStringValue}},
{"set multiple strings", testKey, []interface{}{"one", "two", "three"}},
{"empty value", testKey, []interface{}{}},
}
for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
conn.Clear()
var commands []*redigomock.Cmd
// Create the arguments
args := make([]interface{}, len(test.members)+1)
args[0] = test.setName
// Loop members
for i, key := range test.members {
args[i+1] = key
}
// The main command to test
commands = append(commands, conn.Command(AddToSetCommand, args...))
err := SetAddManyRaw(conn, test.setName, test.members...)
assert.NoError(t, err)
for _, c := range commands {
assert.Equal(t, true, c.Called)
}
})
}
})
t.Run("set add many command using real redis", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
// Load redis
client, conn, err := loadRealRedis()
assert.NotNil(t, client)
assert.NoError(t, err)
defer client.CloseAll(conn)
// Start with a fresh db
err = clearRealRedis(conn)
assert.NoError(t, err)
// Fire the command
err = SetAddMany(context.Background(), client, testKey, testStringValue, testStringValue+"2")
assert.NoError(t, err)
// Check that the command worked
var found bool
found, err = SetIsMember(context.Background(), client, testKey, testStringValue+"2")
assert.NoError(t, err)
assert.Equal(t, true, found)
})
}
// ExampleSetAddMany is an example of the method SetAddMany()
func ExampleSetAddMany() {
// Load a mocked redis for testing/examples
client, _ := loadMockRedis()
// Close connections at end of request
defer client.Close()
// Set the key/value
_ = SetAddMany(context.Background(), client, testKey, testStringValue, testStringValue+"2")
// Fire the command
_, _ = SetIsMember(context.Background(), client, testKey, testStringValue+"2")
fmt.Printf("found member: %v", testStringValue+"2")
// Output:found member: test-string-value2
}
// TestSetRemoveMember test the method SetRemoveMember()
func TestSetRemoveMember(t *testing.T) {
t.Run("set remove member command using mocked redis", func(t *testing.T) {
t.Parallel()
// Load redis
client, conn := loadMockRedis()
assert.NotNil(t, client)
defer client.CloseAll(conn)
var tests = []struct {
testCase string
setName string
member interface{}
}{
{"set with dep", testKey, testStringValue},
{"set multiple strings", testKey, testStringValue + "2"},
{"set multiple integers", testKey, 1},
{"empty value", testKey, ""},
}
for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
conn.Clear()
var commands []*redigomock.Cmd
// The main command to test
commands = append(commands, conn.Command(RemoveMemberCommand, test.setName, test.member))
err := SetRemoveMember(context.Background(), client, test.setName, test.member)
assert.NoError(t, err)
for _, c := range commands {
assert.Equal(t, true, c.Called)
}
})
}
})
t.Run("set remove member command using real redis", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
// Load redis
client, conn, err := loadRealRedis()
assert.NotNil(t, client)
assert.NoError(t, err)
defer client.CloseAll(conn)
// Start with a fresh db
err = clearRealRedis(conn)
assert.NoError(t, err)
// Fire the command
err = SetAddRaw(conn, testKey, testStringValue)
assert.NoError(t, err)
// Check that the command worked
var found bool
found, err = SetIsMemberRaw(conn, testKey, testStringValue)
assert.NoError(t, err)
assert.Equal(t, true, found)
// Fire the command
err = SetRemoveMemberRaw(conn, testKey, testStringValue)
assert.NoError(t, err)
// Check that the command worked
found, err = SetIsMemberRaw(conn, testKey, testStringValue)
assert.NoError(t, err)
assert.Equal(t, false, found)
})
}
// ExampleSetRemoveMember is an example of the method SetRemoveMember()
func ExampleSetRemoveMember() {
// Load a mocked redis for testing/examples
client, _ := loadMockRedis()
// Close connections at end of request
defer client.Close()
// Set the key/value
_ = SetAddMany(context.Background(), client, testKey, testStringValue, testStringValue+"2")
// Fire the command
_ = SetRemoveMember(context.Background(), client, testKey, testStringValue+"2")
fmt.Printf("removed member: %v", testStringValue+"2")
// Output:removed member: test-string-value2
}
// TestSetIsMember test the method SetIsMember()
func TestSetIsMember(t *testing.T) {
t.Run("set is member command using mocked redis", func(t *testing.T) {
t.Parallel()
// Load redis
client, conn := loadMockRedis()
assert.NotNil(t, client)
defer client.CloseAll(conn)
var tests = []struct {
testCase string
setName interface{}
member interface{}
expectedFound int64
}{
{"valid set and member", testKey, testStringValue, 1},
{"no set name", "", testStringValue, 0},
{"no member", testKey, "", 0},
}
for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
conn.Clear()
// The main command to test
isCmd := conn.Command(IsMemberCommand, test.setName, test.member).Expect(interface{}(test.expectedFound))
found, err := SetIsMemberRaw(conn, test.setName, test.member)
assert.NoError(t, err)
assert.Equal(t, test.expectedFound > 0, found)
assert.Equal(t, true, isCmd.Called)
})
}
})
t.Run("set is member command using real redis", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
// Load redis
client, conn, err := loadRealRedis()
assert.NotNil(t, client)
assert.NoError(t, err)
defer client.CloseAll(conn)
// Start with a fresh db
err = clearRealRedis(conn)
assert.NoError(t, err)
// Fire the command
err = SetAdd(context.Background(), client, testKey, testStringValue)
assert.NoError(t, err)
// Check that the command worked
var found bool
found, err = SetIsMember(context.Background(), client, testKey, testStringValue)
assert.NoError(t, err)
assert.Equal(t, true, found)
})
}
// ExampleSetIsMember is an example of the method SetIsMember()
func ExampleSetIsMember() {
// Load a mocked redis for testing/examples
client, _ := loadMockRedis()
// Close connections at end of request
defer client.Close()
// Set the key/value
_ = SetAddMany(context.Background(), client, testKey, testStringValue, testStringValue+"2")
// Fire the command
_, _ = SetIsMember(context.Background(), client, testKey, testStringValue+"2")
fmt.Printf("found member: %v", testStringValue+"2")
// Output:found member: test-string-value2
}
// TestSetMembers will test the method SetMembers()
func TestSetMembers(t *testing.T) {
t.Run("get members using mocked redis", func(t *testing.T) {
t.Parallel()
// Load redis
client, conn := loadMockRedis()
assert.NotNil(t, client)
defer client.CloseAll(conn)
var tests = []struct {
testCase string
setName interface{}
expectedFound []interface{}
}{
{"valid set and members", testKey, []interface{}{"one", "two"}},
}
for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
conn.Clear()
// The main command to test
cmd := conn.Command(MembersCommand, test.setName).Expect(test.expectedFound)
found, err := SetMembersRaw(conn, test.setName)
assert.NoError(t, err)
assert.Equal(t, 2, len(found))
assert.Equal(t, "one", found[0])
assert.Equal(t, "two", found[1])
assert.Equal(t, true, cmd.Called)
})
}
})
}
// ExampleSetMembers is an example of the method SetMembers()
func ExampleSetMembers() {
// Load a mocked redis for testing/examples
client, _ := loadMockRedis()
// Close connections at end of request
defer client.Close()
// Set the key/value
_ = SetAddMany(context.Background(), client, testKey, testStringValue, testStringValue)
// Fire the command
_, _ = SetMembers(context.Background(), client, testKey)
fmt.Printf("found members: [%v]", testStringValue)
// Output:found members: [test-string-value]
}