-
Notifications
You must be signed in to change notification settings - Fork 5
/
sets_test.go
179 lines (134 loc) · 4.64 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
package redimo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBasicSets(t *testing.T) {
c := newClient(t)
addedMembers, err := c.SADD("s1", "m1", "m2")
assert.NoError(t, err)
assert.Equal(t, 2, len(addedMembers))
assert.ElementsMatch(t, []string{"m1", "m2"}, addedMembers)
addedMembers, err = c.SADD("s1", "m1", "m2", "m3")
assert.NoError(t, err)
assert.Equal(t, 1, len(addedMembers))
assert.ElementsMatch(t, []string{"m3"}, addedMembers)
ok, err := c.SISMEMBER("s1", "m1")
assert.NoError(t, err)
assert.True(t, ok)
ok, err = c.SISMEMBER("s1", "nonexistentmember")
assert.NoError(t, err)
assert.False(t, ok)
members, err := c.SMEMBERS("s1")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m1", "m2", "m3"}, members)
count, err := c.SCARD("s1")
assert.NoError(t, err)
assert.Equal(t, int64(3), count)
members, err = c.SMEMBERS("nosuchset")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{}, members)
removedMembers, err := c.SREM("s1", "m1", "m2")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m1", "m2"}, removedMembers)
removedMembers, err = c.SREM("s1", "m1", "m2")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{}, removedMembers)
members, err = c.SMEMBERS("s1")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m3"}, members)
ok, err = c.SISMEMBER("s1", "m1")
assert.NoError(t, err)
assert.False(t, ok)
count, err = c.SCARD("s1")
assert.NoError(t, err)
assert.Equal(t, int64(1), count)
count, err = c.SCARD("nosuchkey")
assert.NoError(t, err)
assert.Equal(t, int64(0), count)
}
func TestSetOperations(t *testing.T) {
c := newClient(t)
_, err := c.SADD("s1", "m1", "m2", "m3")
assert.NoError(t, err)
_, err = c.SADD("s2", "m3", "m4", "m5")
assert.NoError(t, err)
_, err = c.SADD("s3", "m5", "m6", "m7")
assert.NoError(t, err)
union, err := c.SUNION("s1", "s2", "s3")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m1", "m2", "m3", "m4", "m5", "m6", "m7"}, union)
unionCount, err := c.SUNIONSTORE("union", "s1", "s2", "s3")
assert.NoError(t, err)
assert.Equal(t, int64(7), unionCount)
unionMembers, err := c.SMEMBERS("union")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m1", "m2", "m3", "m4", "m5", "m6", "m7"}, unionMembers)
intersection, err := c.SINTER("s1", "s2")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m3"}, intersection)
intersection, err = c.SINTER("s1", "s3")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{}, intersection)
intersectionCount, err := c.SINTERSTORE("inter", "s1", "s2")
assert.NoError(t, err)
assert.Equal(t, int64(1), intersectionCount)
intersectionMembers, err := c.SMEMBERS("inter")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m3"}, intersectionMembers)
diff, err := c.SDIFF("s1", "s2", "s3")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m1", "m2"}, diff)
diff, err = c.SDIFF("s1", "s3")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m1", "m2", "m3"}, diff)
diffCount, err := c.SDIFFSTORE("diff", "s1", "s2", "s3")
assert.NoError(t, err)
assert.Equal(t, int64(2), diffCount)
diffMembers, err := c.SMEMBERS("diff")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m1", "m2"}, diffMembers)
union, err = c.SUNION()
assert.NoError(t, err)
assert.ElementsMatch(t, []string{}, union)
}
func TestSetModifiers(t *testing.T) {
c := newClient(t)
_, err := c.SADD("s1", "m1", "m2", "m3")
assert.NoError(t, err)
members, err := c.SRANDMEMBER("s1", 1)
assert.NoError(t, err)
assert.Equal(t, 1, len(members))
assert.Subset(t, []string{"m1", "m2", "m3"}, members)
members, err = c.SRANDMEMBER("s1", -2)
assert.NoError(t, err)
assert.Equal(t, 2, len(members))
assert.Subset(t, []string{"m1", "m2", "m3"}, members)
members, err = c.SPOP("s1", 2)
assert.NoError(t, err)
assert.Equal(t, 2, len(members))
assert.Subset(t, []string{"m1", "m2", "m3"}, members)
newCount, err := c.SCARD("s1")
assert.NoError(t, err)
assert.Equal(t, int64(1), newCount)
_, err = c.SADD("s1", "m1", "m2", "m3")
assert.NoError(t, err)
ok, err := c.SMOVE("s1", "s2", "m1")
assert.NoError(t, err)
assert.True(t, ok)
members, err = c.SMEMBERS("s1")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m2", "m3"}, members)
members, err = c.SMEMBERS("s2")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m1"}, members)
ok, err = c.SMOVE("s1", "s2", "nosuchmember")
assert.NoError(t, err)
assert.False(t, ok)
members, err = c.SMEMBERS("s1")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m2", "m3"}, members)
members, err = c.SMEMBERS("s2")
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"m1"}, members)
}