fakeredis 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +4 -0
- data/.travis.yml +16 -5
- data/LICENSE +1 -1
- data/README.md +18 -2
- data/fakeredis.gemspec +1 -1
- data/gemfiles/redisrb-master.gemfile +14 -0
- data/lib/fakeredis/bitop_command.rb +56 -0
- data/lib/fakeredis/expiring_hash.rb +1 -1
- data/lib/fakeredis/minitest.rb +24 -0
- data/lib/fakeredis/rspec.rb +1 -0
- data/lib/fakeredis/sort_method.rb +3 -2
- data/lib/fakeredis/transaction_commands.rb +1 -1
- data/lib/fakeredis/version.rb +1 -1
- data/lib/fakeredis/zset.rb +7 -1
- data/lib/redis/connection/memory.rb +424 -38
- data/spec/bitop_command_spec.rb +209 -0
- data/spec/compatibility_spec.rb +1 -1
- data/spec/connection_spec.rb +20 -20
- data/spec/hashes_spec.rb +123 -57
- data/spec/keys_spec.rb +197 -80
- data/spec/lists_spec.rb +61 -34
- data/spec/memory_spec.rb +60 -7
- data/spec/server_spec.rb +24 -24
- data/spec/sets_spec.rb +95 -46
- data/spec/sort_method_spec.rb +6 -0
- data/spec/sorted_sets_spec.rb +288 -150
- data/spec/spec_helper.rb +1 -0
- data/spec/strings_spec.rb +83 -78
- data/spec/subscription_spec.rb +107 -0
- data/spec/support/shared_examples/bitwise_operation.rb +59 -0
- data/spec/support/shared_examples/sortable.rb +20 -16
- data/spec/transactions_spec.rb +20 -12
- data/spec/upcase_method_name_spec.rb +2 -2
- metadata +14 -5
data/spec/lists_spec.rb
CHANGED
@@ -10,41 +10,51 @@ module FakeRedis
|
|
10
10
|
@client.lpush("key1", "val1")
|
11
11
|
@client.lpush("key1", "val2")
|
12
12
|
|
13
|
-
@client.lindex("key1", 0).
|
14
|
-
@client.lindex("key1", -1).
|
15
|
-
@client.lindex("key1", 3).
|
13
|
+
expect(@client.lindex("key1", 0)).to eq("val2")
|
14
|
+
expect(@client.lindex("key1", -1)).to eq("val1")
|
15
|
+
expect(@client.lindex("key1", 3)).to eq(nil)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should insert an element before or after another element in a list" do
|
19
19
|
@client.rpush("key1", "v1")
|
20
20
|
@client.rpush("key1", "v3")
|
21
21
|
@client.linsert("key1", :before, "v3", "v2")
|
22
|
+
@client.linsert("key1", :after, "v3", 100)
|
23
|
+
@client.linsert("key1", :before, 100, 99)
|
22
24
|
|
23
|
-
@client.lrange("key1", 0, -1).
|
25
|
+
expect(@client.lrange("key1", 0, -1)).to eq(["v1", "v2", "v3", "99", "100"])
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should not insert if after/before index not found" do
|
29
|
+
@client.rpush("key", "v1")
|
30
|
+
expect(@client.linsert("key", :before, "unknown", "v2")).to eq(-1)
|
31
|
+
expect(@client.linsert("key", :after, "unknown", "v3")).to eq(-1)
|
32
|
+
|
33
|
+
expect(@client.lrange("key", 0, -1)).to eq(["v1"])
|
24
34
|
end
|
25
35
|
|
26
36
|
it 'should allow multiple values to be added to a list in a single rpush' do
|
27
37
|
@client.rpush('key1', [1, 2, 3])
|
28
|
-
@client.lrange('key1', 0, -1).
|
38
|
+
expect(@client.lrange('key1', 0, -1)).to eq(['1', '2', '3'])
|
29
39
|
end
|
30
40
|
|
31
41
|
it 'should allow multiple values to be added to a list in a single lpush' do
|
32
42
|
@client.lpush('key1', [1, 2, 3])
|
33
|
-
@client.lrange('key1', 0, -1).
|
43
|
+
expect(@client.lrange('key1', 0, -1)).to eq(['3', '2', '1'])
|
34
44
|
end
|
35
45
|
|
36
46
|
it "should error if an invalid where argument is given" do
|
37
47
|
@client.rpush("key1", "v1")
|
38
48
|
@client.rpush("key1", "v3")
|
39
|
-
|
49
|
+
expect { @client.linsert("key1", :invalid, "v3", "v2") }.to raise_error(Redis::CommandError, "ERR syntax error")
|
40
50
|
end
|
41
51
|
|
42
52
|
it "should get the length of a list" do
|
43
53
|
@client.rpush("key1", "v1")
|
44
54
|
@client.rpush("key1", "v2")
|
45
55
|
|
46
|
-
@client.llen("key1").
|
47
|
-
@client.llen("key2").
|
56
|
+
expect(@client.llen("key1")).to eq(2)
|
57
|
+
expect(@client.llen("key2")).to eq(0)
|
48
58
|
end
|
49
59
|
|
50
60
|
it "should remove and get the first element in a list" do
|
@@ -52,15 +62,15 @@ module FakeRedis
|
|
52
62
|
@client.rpush("key1", "v2")
|
53
63
|
@client.rpush("key1", "v3")
|
54
64
|
|
55
|
-
@client.lpop("key1").
|
56
|
-
@client.lrange("key1", 0, -1).
|
65
|
+
expect(@client.lpop("key1")).to eq("v1")
|
66
|
+
expect(@client.lrange("key1", 0, -1)).to eq(["v2", "v3"])
|
57
67
|
end
|
58
68
|
|
59
69
|
it "should prepend a value to a list" do
|
60
70
|
@client.rpush("key1", "v1")
|
61
71
|
@client.rpush("key1", "v2")
|
62
72
|
|
63
|
-
@client.lrange("key1", 0, -1).
|
73
|
+
expect(@client.lrange("key1", 0, -1)).to eq(["v1", "v2"])
|
64
74
|
end
|
65
75
|
|
66
76
|
it "should prepend a value to a list, only if the list exists" do
|
@@ -69,8 +79,8 @@ module FakeRedis
|
|
69
79
|
@client.lpushx("key1", "v2")
|
70
80
|
@client.lpushx("key2", "v3")
|
71
81
|
|
72
|
-
@client.lrange("key1", 0, -1).
|
73
|
-
@client.llen("key2").
|
82
|
+
expect(@client.lrange("key1", 0, -1)).to eq(["v2", "v1"])
|
83
|
+
expect(@client.llen("key2")).to eq(0)
|
74
84
|
end
|
75
85
|
|
76
86
|
it "should get a range of elements from a list" do
|
@@ -78,7 +88,8 @@ module FakeRedis
|
|
78
88
|
@client.rpush("key1", "v2")
|
79
89
|
@client.rpush("key1", "v3")
|
80
90
|
|
81
|
-
@client.lrange("key1", 1, -1).
|
91
|
+
expect(@client.lrange("key1", 1, -1)).to eq(["v2", "v3"])
|
92
|
+
expect(@client.lrange("key1", -999, -1)).to eq(["v1", "v2", "v3"])
|
82
93
|
end
|
83
94
|
|
84
95
|
it "should remove elements from a list" do
|
@@ -87,10 +98,17 @@ module FakeRedis
|
|
87
98
|
@client.rpush("key1", "v2")
|
88
99
|
@client.rpush("key1", "v2")
|
89
100
|
@client.rpush("key1", "v1")
|
101
|
+
@client.rpush("key1", 42)
|
102
|
+
|
103
|
+
expect(@client.lrem("key1", 1, "v1")).to eq(1)
|
104
|
+
expect(@client.lrem("key1", -2, "v2")).to eq(2)
|
105
|
+
expect(@client.lrem("key1", 0, 42)).to eq(1)
|
106
|
+
expect(@client.llen("key1")).to eq(2)
|
107
|
+
end
|
90
108
|
|
91
|
-
|
92
|
-
@client.
|
93
|
-
@client.
|
109
|
+
it "should return 0 if key does not map to a list" do
|
110
|
+
expect(@client.exists("nonexistant")).to eq(false)
|
111
|
+
expect(@client.lrem("nonexistant", 0, "value")).to eq(0)
|
94
112
|
end
|
95
113
|
|
96
114
|
it "should remove list's key when list is empty" do
|
@@ -99,7 +117,7 @@ module FakeRedis
|
|
99
117
|
@client.lrem("key1", 1, "v1")
|
100
118
|
@client.lrem("key1", 1, "v2")
|
101
119
|
|
102
|
-
@client.exists("key1").
|
120
|
+
expect(@client.exists("key1")).to eq(false)
|
103
121
|
end
|
104
122
|
|
105
123
|
it "should set the value of an element in a list by its index" do
|
@@ -109,9 +127,10 @@ module FakeRedis
|
|
109
127
|
|
110
128
|
@client.lset("key1", 0, "four")
|
111
129
|
@client.lset("key1", -2, "five")
|
112
|
-
@client.
|
130
|
+
@client.lset("key1", 2, 6)
|
113
131
|
|
114
|
-
|
132
|
+
expect(@client.lrange("key1", 0, -1)).to eq(["four", "five", "6"])
|
133
|
+
expect { @client.lset("key1", 4, "seven") }.to raise_error(Redis::CommandError, "ERR index out of range")
|
115
134
|
end
|
116
135
|
|
117
136
|
it "should trim a list to the specified range" do
|
@@ -119,8 +138,8 @@ module FakeRedis
|
|
119
138
|
@client.rpush("key1", "two")
|
120
139
|
@client.rpush("key1", "three")
|
121
140
|
|
122
|
-
@client.ltrim("key1", 1, -1).
|
123
|
-
@client.lrange("key1", 0, -1).
|
141
|
+
expect(@client.ltrim("key1", 1, -1)).to eq("OK")
|
142
|
+
expect(@client.lrange("key1", 0, -1)).to eq(["two", "three"])
|
124
143
|
end
|
125
144
|
|
126
145
|
|
@@ -131,7 +150,7 @@ module FakeRedis
|
|
131
150
|
before { @client.ltrim("listOfOne", -5, -1) }
|
132
151
|
|
133
152
|
it "returns the unmodified list" do
|
134
|
-
@client.lrange("listOfOne", 0, -1).
|
153
|
+
expect(@client.lrange("listOfOne", 0, -1)).to eq(["one"])
|
135
154
|
end
|
136
155
|
end
|
137
156
|
end
|
@@ -150,7 +169,7 @@ module FakeRedis
|
|
150
169
|
before { @client.ltrim("maxTest", -5, -1) }
|
151
170
|
|
152
171
|
it "should trim a list to the specified maximum size" do
|
153
|
-
@client.lrange("maxTest", 0, -1).
|
172
|
+
expect(@client.lrange("maxTest", 0, -1)).to eq(["two","three", "four", "five", "six"])
|
154
173
|
end
|
155
174
|
end
|
156
175
|
end
|
@@ -161,8 +180,8 @@ module FakeRedis
|
|
161
180
|
@client.rpush("key1", "two")
|
162
181
|
@client.rpush("key1", "three")
|
163
182
|
|
164
|
-
@client.rpop("key1").
|
165
|
-
@client.lrange("key1", 0, -1).
|
183
|
+
expect(@client.rpop("key1")).to eq("three")
|
184
|
+
expect(@client.lrange("key1", 0, -1)).to eq(["one", "two"])
|
166
185
|
end
|
167
186
|
|
168
187
|
it "should remove the last element in a list, append it to another list and return it" do
|
@@ -170,17 +189,17 @@ module FakeRedis
|
|
170
189
|
@client.rpush("key1", "two")
|
171
190
|
@client.rpush("key1", "three")
|
172
191
|
|
173
|
-
@client.rpoplpush("key1", "key2").
|
192
|
+
expect(@client.rpoplpush("key1", "key2")).to eq("three")
|
174
193
|
|
175
|
-
@client.lrange("key1", 0, -1).
|
176
|
-
@client.lrange("key2", 0, -1).
|
194
|
+
expect(@client.lrange("key1", 0, -1)).to eq(["one", "two"])
|
195
|
+
expect(@client.lrange("key2", 0, -1)).to eq(["three"])
|
177
196
|
end
|
178
197
|
|
179
198
|
context 'when the source list is empty' do
|
180
199
|
it 'rpoplpush does not add anything to the destination list' do
|
181
200
|
@client.rpoplpush("source", "destination")
|
182
201
|
|
183
|
-
@client.lrange("destination", 0, -1).
|
202
|
+
expect(@client.lrange("destination", 0, -1)).to eq([])
|
184
203
|
end
|
185
204
|
end
|
186
205
|
|
@@ -188,7 +207,7 @@ module FakeRedis
|
|
188
207
|
@client.rpush("key1", "one")
|
189
208
|
@client.rpush("key1", "two")
|
190
209
|
|
191
|
-
@client.lrange("key1", 0, -1).
|
210
|
+
expect(@client.lrange("key1", 0, -1)).to eq(["one", "two"])
|
192
211
|
end
|
193
212
|
|
194
213
|
it "should append a value to a list, only if the list exists" do
|
@@ -196,8 +215,16 @@ module FakeRedis
|
|
196
215
|
@client.rpushx("key1", "two")
|
197
216
|
@client.rpushx("key2", "two")
|
198
217
|
|
199
|
-
@client.lrange("key1", 0, -1).
|
200
|
-
@client.lrange("key2", 0, -1).
|
218
|
+
expect(@client.lrange("key1", 0, -1)).to eq(["one", "two"])
|
219
|
+
expect(@client.lrange("key2", 0, -1)).to eq([])
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'should not allow pushing empty list of objects' do
|
223
|
+
expect { @client.lpush("key1", []) }.to raise_error(Redis::CommandError, /lpush[^x]/)
|
224
|
+
expect { @client.lpush("key1", 1); @client.lpushx("key1", []) }.to raise_error(Redis::CommandError, /lpushx/)
|
225
|
+
|
226
|
+
expect { @client.rpush("key1", []) }.to raise_error(Redis::CommandError, /rpush[^x]/)
|
227
|
+
expect { @client.rpush("key1", 1); @client.rpushx("key1", []) }.to raise_error(Redis::CommandError, /rpushx/)
|
201
228
|
end
|
202
229
|
end
|
203
230
|
end
|
data/spec/memory_spec.rb
CHANGED
@@ -1,27 +1,80 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
3
|
+
RSpec.describe FakeRedis do
|
4
|
+
let(:redis) { Redis.new }
|
5
|
+
|
6
|
+
def populate_keys_in_redis(num)
|
7
|
+
num.times do |count|
|
8
|
+
redis.set("key#{count}", count)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#scan' do
|
13
|
+
def result
|
14
|
+
returned_keys = []
|
15
|
+
cursor = 0
|
16
|
+
|
17
|
+
loop do
|
18
|
+
cursor, keys = redis.scan(cursor, match_arguments)
|
19
|
+
returned_keys += keys
|
20
|
+
break if cursor == '0'
|
21
|
+
end
|
22
|
+
returned_keys
|
23
|
+
end
|
24
|
+
|
25
|
+
before do
|
26
|
+
populate_keys_in_redis(11)
|
27
|
+
end
|
28
|
+
|
29
|
+
context('when deleting') do
|
30
|
+
it('preverves cursor') do
|
31
|
+
cursor, keys = redis.scan('0')
|
32
|
+
keys.each { |key| redis.del(key) }
|
33
|
+
_, keys = redis.scan(cursor)
|
34
|
+
expect(keys).to eq(%w(key10))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'with one namespace' do
|
39
|
+
let(:match_arguments) { {} }
|
40
|
+
|
41
|
+
it 'returns the expected array of keys' do
|
42
|
+
expect(result).to match_array(redis.keys)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'with multiple namespaces' do
|
47
|
+
let(:namespaced_key) { 'test' }
|
48
|
+
let(:match_arguments) { { match: namespaced_key } }
|
49
|
+
|
50
|
+
before { redis.set(namespaced_key, 12) }
|
51
|
+
|
52
|
+
it 'returns the expected array of keys' do
|
53
|
+
expect(result).to match_array([namespaced_key])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
4
58
|
describe 'time' do
|
5
59
|
before(:each) do
|
6
|
-
|
7
|
-
Time.stub_chain(:now, :to_f).and_return(1397845595.5139461)
|
60
|
+
allow(Time).to receive_message_chain(:now, :to_f).and_return(1397845595.5139461)
|
8
61
|
end
|
9
62
|
|
10
63
|
it 'is an array' do
|
11
|
-
expect(
|
64
|
+
expect(redis.time).to be_an_instance_of(Array)
|
12
65
|
end
|
13
66
|
|
14
67
|
it 'has two elements' do
|
15
|
-
expect(
|
68
|
+
expect(redis.time.count).to eql 2
|
16
69
|
end
|
17
70
|
|
18
71
|
if fakeredis?
|
19
72
|
it 'has the current time in seconds' do
|
20
|
-
expect(
|
73
|
+
expect(redis.time.first).to eql 1397845595
|
21
74
|
end
|
22
75
|
|
23
76
|
it 'has the current leftover microseconds' do
|
24
|
-
expect(
|
77
|
+
expect(redis.time.last).to eql 513946
|
25
78
|
end
|
26
79
|
end
|
27
80
|
end
|
data/spec/server_spec.rb
CHANGED
@@ -12,29 +12,29 @@ module FakeRedis
|
|
12
12
|
@client.set("key2", "2")
|
13
13
|
@client.set("key2", "two")
|
14
14
|
|
15
|
-
@client.dbsize.
|
15
|
+
expect(@client.dbsize).to eq(2)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should get information and statistics about the server" do
|
19
|
-
@client.info.key?("redis_version").
|
19
|
+
expect(@client.info.key?("redis_version")).to eq(true)
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should handle non-existent methods" do
|
23
|
-
|
23
|
+
expect { @client.idontexist }.to raise_error(Redis::CommandError, "ERR unknown command 'idontexist'")
|
24
24
|
end
|
25
25
|
|
26
26
|
describe "multiple databases" do
|
27
27
|
it "should default to database 0" do
|
28
|
-
@client.inspect.
|
28
|
+
expect(@client.inspect).to match(%r#/0>$#)
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should select another database" do
|
32
32
|
@client.select(1)
|
33
|
-
@client.inspect.
|
33
|
+
expect(@client.inspect).to match(%r#/1>$#)
|
34
34
|
end
|
35
35
|
|
36
36
|
it "should store keys separately in each database" do
|
37
|
-
@client.select(0).
|
37
|
+
expect(@client.select(0)).to eq("OK")
|
38
38
|
@client.set("key1", "1")
|
39
39
|
@client.set("key2", "2")
|
40
40
|
|
@@ -44,56 +44,56 @@ module FakeRedis
|
|
44
44
|
@client.set("key5", "5")
|
45
45
|
|
46
46
|
@client.select(0)
|
47
|
-
@client.dbsize.
|
48
|
-
@client.exists("key1").
|
49
|
-
@client.exists("key3").
|
47
|
+
expect(@client.dbsize).to eq(2)
|
48
|
+
expect(@client.exists("key1")).to be true
|
49
|
+
expect(@client.exists("key3")).to be false
|
50
50
|
|
51
51
|
@client.select(1)
|
52
|
-
@client.dbsize.
|
53
|
-
@client.exists("key4").
|
54
|
-
@client.exists("key2").
|
52
|
+
expect(@client.dbsize).to eq(3)
|
53
|
+
expect(@client.exists("key4")).to be true
|
54
|
+
expect(@client.exists("key2")).to be false
|
55
55
|
|
56
56
|
@client.flushall
|
57
|
-
@client.dbsize.
|
57
|
+
expect(@client.dbsize).to eq(0)
|
58
58
|
|
59
59
|
@client.select(0)
|
60
|
-
@client.dbsize.
|
60
|
+
expect(@client.dbsize).to eq(0)
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should flush a database" do
|
64
64
|
@client.select(0)
|
65
65
|
@client.set("key1", "1")
|
66
66
|
@client.set("key2", "2")
|
67
|
-
@client.dbsize.
|
67
|
+
expect(@client.dbsize).to eq(2)
|
68
68
|
|
69
69
|
@client.select(1)
|
70
70
|
@client.set("key3", "3")
|
71
71
|
@client.set("key4", "4")
|
72
|
-
@client.dbsize.
|
72
|
+
expect(@client.dbsize).to eq(2)
|
73
73
|
|
74
|
-
@client.flushdb.
|
74
|
+
expect(@client.flushdb).to eq("OK")
|
75
75
|
|
76
|
-
@client.dbsize.
|
76
|
+
expect(@client.dbsize).to eq(0)
|
77
77
|
@client.select(0)
|
78
|
-
@client.dbsize.
|
78
|
+
expect(@client.dbsize).to eq(2)
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should flush all databases" do
|
82
82
|
@client.select(0)
|
83
83
|
@client.set("key3", "3")
|
84
84
|
@client.set("key4", "4")
|
85
|
-
@client.dbsize.
|
85
|
+
expect(@client.dbsize).to eq(2)
|
86
86
|
|
87
87
|
@client.select(1)
|
88
88
|
@client.set("key3", "3")
|
89
89
|
@client.set("key4", "4")
|
90
|
-
@client.dbsize.
|
90
|
+
expect(@client.dbsize).to eq(2)
|
91
91
|
|
92
|
-
@client.flushall.
|
92
|
+
expect(@client.flushall).to eq("OK")
|
93
93
|
|
94
|
-
@client.dbsize.
|
94
|
+
expect(@client.dbsize).to eq(0)
|
95
95
|
@client.select(0)
|
96
|
-
@client.dbsize.
|
96
|
+
expect(@client.dbsize).to eq(0)
|
97
97
|
end
|
98
98
|
end
|
99
99
|
end
|
data/spec/sets_spec.rb
CHANGED
@@ -7,30 +7,33 @@ module FakeRedis
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should add a member to a set" do
|
10
|
-
@client.sadd("key", "value").
|
11
|
-
@client.sadd("key", "value").
|
10
|
+
expect(@client.sadd("key", "value")).to eq(true)
|
11
|
+
expect(@client.sadd("key", "value")).to eq(false)
|
12
12
|
|
13
|
-
@client.smembers("key").
|
13
|
+
expect(@client.smembers("key")).to eq(["value"])
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should raise error if command arguments count is not enough" do
|
17
17
|
expect { @client.sadd("key", []) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'sadd' command")
|
18
18
|
expect { @client.sinter(*[]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'sinter' command")
|
19
|
+
expect { @client.sinter([]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'sinter' command")
|
20
|
+
expect { @client.sunion(*[]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'sunion' command")
|
21
|
+
expect { @client.sunion([]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'sunion' command")
|
19
22
|
|
20
|
-
@client.smembers("key").
|
23
|
+
expect(@client.smembers("key")).to be_empty
|
21
24
|
end
|
22
25
|
|
23
26
|
it "should add multiple members to a set" do
|
24
|
-
@client.sadd("key", %w(value other something more)).
|
25
|
-
@client.sadd("key", %w(and additional values)).
|
26
|
-
@client.smembers("key").
|
27
|
+
expect(@client.sadd("key", %w(value other something more))).to eq(4)
|
28
|
+
expect(@client.sadd("key", %w(and additional values))).to eq(3)
|
29
|
+
expect(@client.smembers("key")).to match_array(["value", "other", "something", "more", "and", "additional", "values"])
|
27
30
|
end
|
28
31
|
|
29
32
|
it "should get the number of members in a set" do
|
30
33
|
@client.sadd("key", "val1")
|
31
34
|
@client.sadd("key", "val2")
|
32
35
|
|
33
|
-
@client.scard("key").
|
36
|
+
expect(@client.scard("key")).to eq(2)
|
34
37
|
end
|
35
38
|
|
36
39
|
it "should subtract multiple sets" do
|
@@ -43,14 +46,16 @@ module FakeRedis
|
|
43
46
|
@client.sadd("key3", "c")
|
44
47
|
@client.sadd("key3", "e")
|
45
48
|
|
46
|
-
@client.sdiff("key1", "key2", "key3").
|
49
|
+
expect(@client.sdiff("key1", "key2", "key3")).to match_array(["b", "d"])
|
50
|
+
expect(@client.sdiff("key1", ["key2", "key3"])).to match_array(["b", "d"])
|
47
51
|
end
|
48
52
|
|
49
53
|
it "should subtract from a nonexistent set" do
|
50
54
|
@client.sadd("key2", "a")
|
51
55
|
@client.sadd("key2", "b")
|
52
56
|
|
53
|
-
@client.sdiff("key1", "key2").
|
57
|
+
expect(@client.sdiff("key1", "key2")).to eq([])
|
58
|
+
expect(@client.sdiff(["key1", "key2"])).to eq([])
|
54
59
|
end
|
55
60
|
|
56
61
|
it "should subtract multiple sets and store the resulting set in a key" do
|
@@ -63,8 +68,10 @@ module FakeRedis
|
|
63
68
|
@client.sadd("key3", "c")
|
64
69
|
@client.sadd("key3", "e")
|
65
70
|
@client.sdiffstore("key", "key1", "key2", "key3")
|
71
|
+
@client.sdiffstore("new_key", "key1", ["key2", "key3"])
|
66
72
|
|
67
|
-
@client.smembers("key").
|
73
|
+
expect(@client.smembers("key")).to match_array(["b", "d"])
|
74
|
+
expect(@client.smembers("new_key")).to match_array(["b", "d"])
|
68
75
|
end
|
69
76
|
|
70
77
|
it "should intersect multiple sets" do
|
@@ -77,7 +84,8 @@ module FakeRedis
|
|
77
84
|
@client.sadd("key3", "c")
|
78
85
|
@client.sadd("key3", "e")
|
79
86
|
|
80
|
-
@client.sinter("key1", "key2", "key3").
|
87
|
+
expect(@client.sinter("key1", "key2", "key3")).to eq(["c"])
|
88
|
+
expect(@client.sinter(["key1", "key2", "key3"])).to eq(["c"])
|
81
89
|
end
|
82
90
|
|
83
91
|
it "should intersect multiple sets and store the resulting set in a key" do
|
@@ -90,15 +98,18 @@ module FakeRedis
|
|
90
98
|
@client.sadd("key3", "c")
|
91
99
|
@client.sadd("key3", "e")
|
92
100
|
@client.sinterstore("key", "key1", "key2", "key3")
|
93
|
-
@client.
|
101
|
+
@client.sinterstore("new_key", ["key1", "key2", "key3"])
|
102
|
+
|
103
|
+
expect(@client.smembers("key")).to eq(["c"])
|
104
|
+
expect(@client.smembers("new_key")).to eq(["c"])
|
94
105
|
end
|
95
106
|
|
96
107
|
it "should determine if a given value is a member of a set" do
|
97
108
|
@client.sadd("key1", "a")
|
98
109
|
|
99
|
-
@client.sismember("key1", "a").
|
100
|
-
@client.sismember("key1", "b").
|
101
|
-
@client.sismember("key2", "a").
|
110
|
+
expect(@client.sismember("key1", "a")).to eq(true)
|
111
|
+
expect(@client.sismember("key1", "b")).to eq(false)
|
112
|
+
expect(@client.sismember("key2", "a")).to eq(false)
|
102
113
|
end
|
103
114
|
|
104
115
|
it "should get all the members in a set" do
|
@@ -107,51 +118,51 @@ module FakeRedis
|
|
107
118
|
@client.sadd("key", "c")
|
108
119
|
@client.sadd("key", "d")
|
109
120
|
|
110
|
-
@client.smembers("key").
|
121
|
+
expect(@client.smembers("key")).to match_array(["a", "b", "c", "d"])
|
111
122
|
end
|
112
123
|
|
113
124
|
it "should move a member from one set to another" do
|
114
125
|
@client.sadd("key1", "a")
|
115
126
|
@client.sadd("key1", "b")
|
116
127
|
@client.sadd("key2", "c")
|
117
|
-
@client.smove("key1", "key2", "a").
|
118
|
-
@client.smove("key1", "key2", "a").
|
128
|
+
expect(@client.smove("key1", "key2", "a")).to eq(true)
|
129
|
+
expect(@client.smove("key1", "key2", "a")).to eq(false)
|
119
130
|
|
120
|
-
@client.smembers("key1").
|
121
|
-
@client.smembers("key2").
|
131
|
+
expect(@client.smembers("key1")).to eq(["b"])
|
132
|
+
expect(@client.smembers("key2")).to match_array(["c", "a"])
|
122
133
|
end
|
123
134
|
|
124
135
|
it "should remove and return a random member from a set" do
|
125
136
|
@client.sadd("key1", "a")
|
126
137
|
@client.sadd("key1", "b")
|
127
138
|
|
128
|
-
["a", "b"].include?(@client.spop("key1")).
|
129
|
-
["a", "b"].include?(@client.spop("key1")).
|
130
|
-
@client.spop("key1").
|
139
|
+
expect(["a", "b"].include?(@client.spop("key1"))).to be true
|
140
|
+
expect(["a", "b"].include?(@client.spop("key1"))).to be true
|
141
|
+
expect(@client.spop("key1")).to be_nil
|
131
142
|
end
|
132
143
|
|
133
144
|
it "should get a random member from a set" do
|
134
145
|
@client.sadd("key1", "a")
|
135
146
|
@client.sadd("key1", "b")
|
136
147
|
|
137
|
-
["a", "b"].include?(@client.spop("key1")).
|
148
|
+
expect(["a", "b"].include?(@client.spop("key1"))).to be true
|
138
149
|
end
|
139
150
|
|
140
151
|
it "should remove a member from a set" do
|
141
152
|
@client.sadd("key1", "a")
|
142
153
|
@client.sadd("key1", "b")
|
143
|
-
@client.srem("key1", "a").
|
144
|
-
@client.srem("key1", "a").
|
154
|
+
expect(@client.srem("key1", "a")).to eq(true)
|
155
|
+
expect(@client.srem("key1", "a")).to eq(false)
|
145
156
|
|
146
|
-
@client.smembers("key1").
|
157
|
+
expect(@client.smembers("key1")).to eq(["b"])
|
147
158
|
end
|
148
159
|
|
149
160
|
it "should remove multiple members from a set" do
|
150
161
|
@client.sadd("key1", "a")
|
151
162
|
@client.sadd("key1", "b")
|
152
163
|
|
153
|
-
@client.srem("key1", [ "a", "b"]).
|
154
|
-
@client.smembers("key1").
|
164
|
+
expect(@client.srem("key1", [ "a", "b"])).to eq(2)
|
165
|
+
expect(@client.smembers("key1")).to be_empty
|
155
166
|
end
|
156
167
|
|
157
168
|
it "should remove the set's key once it's empty" do
|
@@ -160,7 +171,7 @@ module FakeRedis
|
|
160
171
|
@client.srem("key1", "b")
|
161
172
|
@client.srem("key1", "a")
|
162
173
|
|
163
|
-
@client.exists("key1").
|
174
|
+
expect(@client.exists("key1")).to eq(false)
|
164
175
|
end
|
165
176
|
|
166
177
|
it "should add multiple sets" do
|
@@ -173,7 +184,7 @@ module FakeRedis
|
|
173
184
|
@client.sadd("key3", "c")
|
174
185
|
@client.sadd("key3", "e")
|
175
186
|
|
176
|
-
@client.sunion("key1", "key2", "key3").
|
187
|
+
expect(@client.sunion("key1", "key2", "key3")).to match_array(["a", "b", "c", "d", "e"])
|
177
188
|
end
|
178
189
|
|
179
190
|
it "should add multiple sets and store the resulting set in a key" do
|
@@ -187,7 +198,7 @@ module FakeRedis
|
|
187
198
|
@client.sadd("key3", "e")
|
188
199
|
@client.sunionstore("key", "key1", "key2", "key3")
|
189
200
|
|
190
|
-
@client.smembers("key").
|
201
|
+
expect(@client.smembers("key")).to match_array(["a", "b", "c", "d", "e"])
|
191
202
|
end
|
192
203
|
end
|
193
204
|
|
@@ -207,7 +218,7 @@ module FakeRedis
|
|
207
218
|
it 'is a random element from the set' do
|
208
219
|
random_element = @client.srandmember("key1")
|
209
220
|
|
210
|
-
['a', 'b', 'c'].include?(random_element).
|
221
|
+
expect(['a', 'b', 'c'].include?(random_element)).to be true
|
211
222
|
end
|
212
223
|
end
|
213
224
|
|
@@ -215,7 +226,7 @@ module FakeRedis
|
|
215
226
|
it 'is an array of one random element from the set' do
|
216
227
|
random_elements = @client.srandmember("key1", 1)
|
217
228
|
|
218
|
-
[['a'], ['b'], ['c']].include?(@client.srandmember("key1", 1)).
|
229
|
+
expect([['a'], ['b'], ['c']].include?(@client.srandmember("key1", 1))).to be true
|
219
230
|
end
|
220
231
|
end
|
221
232
|
|
@@ -223,10 +234,10 @@ module FakeRedis
|
|
223
234
|
it 'is an array of two unique, random elements from the set' do
|
224
235
|
random_elements = @client.srandmember("key1", 2)
|
225
236
|
|
226
|
-
random_elements.count.
|
227
|
-
random_elements.uniq.count.
|
237
|
+
expect(random_elements.count).to eq(2)
|
238
|
+
expect(random_elements.uniq.count).to eq(2)
|
228
239
|
random_elements.all? do |element|
|
229
|
-
['a', 'b', 'c'].include?(element).
|
240
|
+
expect(['a', 'b', 'c'].include?(element)).to be true
|
230
241
|
end
|
231
242
|
end
|
232
243
|
end
|
@@ -235,10 +246,10 @@ module FakeRedis
|
|
235
246
|
it 'is an array of 100 random elements from the set, some of which are repeated' do
|
236
247
|
random_elements = @client.srandmember("key1", -100)
|
237
248
|
|
238
|
-
random_elements.count.
|
239
|
-
random_elements.uniq.count.
|
249
|
+
expect(random_elements.count).to eq(100)
|
250
|
+
expect(random_elements.uniq.count).to be <= 3
|
240
251
|
random_elements.all? do |element|
|
241
|
-
['a', 'b', 'c'].include?(element).
|
252
|
+
expect(['a', 'b', 'c'].include?(element)).to be true
|
242
253
|
end
|
243
254
|
end
|
244
255
|
end
|
@@ -247,9 +258,9 @@ module FakeRedis
|
|
247
258
|
it 'is an array of all of the elements from the set, none of which are repeated' do
|
248
259
|
random_elements = @client.srandmember("key1", 100)
|
249
260
|
|
250
|
-
random_elements.count.
|
251
|
-
random_elements.uniq.count.
|
252
|
-
random_elements.
|
261
|
+
expect(random_elements.count).to eq(3)
|
262
|
+
expect(random_elements.uniq.count).to eq(3)
|
263
|
+
expect(random_elements).to match_array(['a', 'b', 'c'])
|
253
264
|
end
|
254
265
|
end
|
255
266
|
end
|
@@ -258,11 +269,49 @@ module FakeRedis
|
|
258
269
|
before { @client.del("key1") }
|
259
270
|
|
260
271
|
it 'is nil without the extra parameter' do
|
261
|
-
@client.srandmember("key1").
|
272
|
+
expect(@client.srandmember("key1")).to be_nil
|
262
273
|
end
|
263
274
|
|
264
275
|
it 'is an empty array with an extra parameter' do
|
265
|
-
@client.srandmember("key1", 1).
|
276
|
+
expect(@client.srandmember("key1", 1)).to eq([])
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
describe "#sscan" do
|
281
|
+
it 'with no arguments and few items, returns all items' do
|
282
|
+
@client.sadd('set', ['name', 'Jack', 'age', '33'])
|
283
|
+
result = @client.sscan('set', 0)
|
284
|
+
|
285
|
+
expect(result[0]).to eq('0')
|
286
|
+
expect(result[1]).to eq(['name', 'Jack', 'age', '33'])
|
287
|
+
end
|
288
|
+
|
289
|
+
it 'with a count should return that number of members or more' do
|
290
|
+
@client.sadd('set', ['a', '1', 'b', '2', 'c', '3', 'd', '4', 'e', '5', 'f', '6', 'g', '7'])
|
291
|
+
result = @client.sscan('set', 0, count: 3)
|
292
|
+
expect(result[0]).to eq('3')
|
293
|
+
expect(result[1]).to eq([ 'a', '1', 'b'])
|
294
|
+
end
|
295
|
+
|
296
|
+
it 'returns items starting at the provided cursor' do
|
297
|
+
@client.sadd('set', ['a', '1', 'b', '2', 'c', '3', 'd', '4', 'e', '5', 'f', '6', 'g', '7'])
|
298
|
+
result = @client.sscan('set', 2, count: 3)
|
299
|
+
expect(result[0]).to eq('5')
|
300
|
+
expect(result[1]).to eq(['b', '2', 'c'])
|
301
|
+
end
|
302
|
+
|
303
|
+
it 'with match, returns items matching the given pattern' do
|
304
|
+
@client.sadd('set', ['aa', '1', 'b', '2', 'cc', '3', 'd', '4', 'ee', '5', 'f', '6', 'gg', '7'])
|
305
|
+
result = @client.sscan('set', 2, count: 7, match: '??')
|
306
|
+
expect(result[0]).to eq('9')
|
307
|
+
expect(result[1]).to eq(['cc','ee'])
|
308
|
+
end
|
309
|
+
|
310
|
+
it 'returns an empty result if the key is not found' do
|
311
|
+
result = @client.sscan('set', 0)
|
312
|
+
|
313
|
+
expect(result[0]).to eq('0')
|
314
|
+
expect(result[1]).to eq([])
|
266
315
|
end
|
267
316
|
end
|
268
317
|
end
|