redis-namespace 1.6.0 → 1.11.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 +5 -5
- data/LICENSE +17 -16
- data/README.md +22 -18
- data/lib/redis/namespace/version.rb +1 -1
- data/lib/redis/namespace.rb +144 -35
- data/spec/deprecation_spec.rb +5 -5
- data/spec/redis_spec.rb +466 -186
- data/spec/spec_helper.rb +5 -0
- metadata +51 -18
data/spec/redis_spec.rb
CHANGED
@@ -1,56 +1,63 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require File.dirname(__FILE__) + '/spec_helper'
|
4
|
+
require 'connection_pool'
|
4
5
|
|
5
6
|
describe "redis" do
|
6
|
-
@redis_version = Gem::Version.new(Redis.
|
7
|
+
@redis_version = Gem::Version.new(Redis.new.info["redis_version"])
|
7
8
|
let(:redis_client) { @redis.respond_to?(:_client) ? @redis._client : @redis.client}
|
8
9
|
|
9
|
-
before(:
|
10
|
+
before(:each) do
|
10
11
|
# use database 15 for testing so we dont accidentally step on your real data
|
11
12
|
@redis = Redis.new :db => 15
|
12
|
-
end
|
13
|
-
|
14
|
-
before(:each) do
|
15
|
-
@namespaced = Redis::Namespace.new(:ns, :redis => @redis)
|
16
13
|
@redis.flushdb
|
14
|
+
@namespaced = Redis::Namespace.new(:ns, :redis => @redis)
|
17
15
|
@redis.set('foo', 'bar')
|
18
16
|
end
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
after(:all) do
|
25
|
-
@redis.quit
|
18
|
+
# redis-rb 3.3.4+
|
19
|
+
it "should inject :namespace into connection info" do
|
20
|
+
info = @redis.connection.merge(:namespace => :ns)
|
21
|
+
expect(@namespaced.connection).to eq(info)
|
26
22
|
end
|
27
23
|
|
28
24
|
it "proxies `client` to the _client and deprecated" do
|
29
|
-
@namespaced.client.
|
25
|
+
expect(@namespaced.client).to eq(redis_client)
|
30
26
|
end
|
31
27
|
|
32
28
|
it "proxies `_client` to the _client" do
|
33
|
-
@namespaced._client.
|
29
|
+
expect(@namespaced._client).to eq(redis_client)
|
34
30
|
end
|
35
31
|
|
36
32
|
it "should be able to use a namespace" do
|
37
|
-
@namespaced.get('foo').
|
33
|
+
expect(@namespaced.get('foo')).to eq(nil)
|
38
34
|
@namespaced.set('foo', 'chris')
|
39
|
-
@namespaced.get('foo').
|
35
|
+
expect(@namespaced.get('foo')).to eq('chris')
|
40
36
|
@redis.set('foo', 'bob')
|
41
|
-
@redis.get('foo').
|
37
|
+
expect(@redis.get('foo')).to eq('bob')
|
42
38
|
|
43
39
|
@namespaced.incrby('counter', 2)
|
44
|
-
@namespaced.get('counter').to_i.
|
45
|
-
@redis.get('counter').
|
46
|
-
@namespaced.type('counter').
|
40
|
+
expect(@namespaced.get('counter').to_i).to eq(2)
|
41
|
+
expect(@redis.get('counter')).to eq(nil)
|
42
|
+
expect(@namespaced.type('counter')).to eq('string')
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should work with Proc namespaces" do
|
46
|
+
namespace = Proc.new { :dynamic_ns }
|
47
|
+
namespaced = Redis::Namespace.new(namespace, redis: @redis)
|
48
|
+
|
49
|
+
expect(namespaced.get('foo')).to eq(nil)
|
50
|
+
namespaced.set('foo', 'chris')
|
51
|
+
expect(namespaced.get('foo')).to eq('chris')
|
52
|
+
@redis.set('foo', 'bob')
|
53
|
+
expect(@redis.get('foo')).to eq('bob')
|
47
54
|
end
|
48
55
|
|
49
56
|
context 'when sending capital commands (issue 68)' do
|
50
57
|
it 'should be able to use a namespace' do
|
51
58
|
@namespaced.send('SET', 'fubar', 'quux')
|
52
|
-
@redis.get('fubar').
|
53
|
-
@namespaced.get('fubar').
|
59
|
+
expect(@redis.get('fubar')).to be_nil
|
60
|
+
expect(@namespaced.get('fubar')).to eq 'quux'
|
54
61
|
end
|
55
62
|
end
|
56
63
|
|
@@ -58,10 +65,10 @@ describe "redis" do
|
|
58
65
|
@namespaced.rpush "foo", "string"
|
59
66
|
@namespaced.rpush "foo", "ns:string"
|
60
67
|
@namespaced.rpush "foo", "string_no_timeout"
|
61
|
-
@namespaced.blpop("foo", 1).
|
62
|
-
@namespaced.blpop("foo", 1).
|
63
|
-
@namespaced.blpop("foo").
|
64
|
-
@namespaced.blpop("foo", 1).
|
68
|
+
expect(@namespaced.blpop("foo", 1)).to eq(["foo", "string"])
|
69
|
+
expect(@namespaced.blpop("foo", 1)).to eq(["foo", "ns:string"])
|
70
|
+
expect(@namespaced.blpop("foo")).to eq(["foo", "string_no_timeout"])
|
71
|
+
expect(@namespaced.blpop("foo", 1)).to eq(nil)
|
65
72
|
end
|
66
73
|
|
67
74
|
it "should be able to use a namespace with del" do
|
@@ -69,125 +76,186 @@ describe "redis" do
|
|
69
76
|
@namespaced.set('bar', 2000)
|
70
77
|
@namespaced.set('baz', 3000)
|
71
78
|
@namespaced.del 'foo'
|
72
|
-
@namespaced.get('foo').
|
79
|
+
expect(@namespaced.get('foo')).to eq(nil)
|
73
80
|
@namespaced.del 'bar', 'baz'
|
74
|
-
@namespaced.get('bar').
|
75
|
-
@namespaced.get('baz').
|
81
|
+
expect(@namespaced.get('bar')).to eq(nil)
|
82
|
+
expect(@namespaced.get('baz')).to eq(nil)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should be able to use a namespace with unlink" do
|
86
|
+
@namespaced.set('foo', 1000)
|
87
|
+
@namespaced.set('bar', 2000)
|
88
|
+
@namespaced.set('baz', 3000)
|
89
|
+
@namespaced.unlink 'foo'
|
90
|
+
expect(@namespaced.get('foo')).to eq(nil)
|
91
|
+
@namespaced.unlink 'bar', 'baz'
|
92
|
+
expect(@namespaced.get('bar')).to eq(nil)
|
93
|
+
expect(@namespaced.get('baz')).to eq(nil)
|
76
94
|
end
|
77
95
|
|
78
96
|
it 'should be able to use a namespace with append' do
|
79
97
|
@namespaced.set('foo', 'bar')
|
80
|
-
@namespaced.append('foo','n').
|
81
|
-
@namespaced.get('foo').
|
82
|
-
@redis.get('foo').
|
98
|
+
expect(@namespaced.append('foo','n')).to eq(4)
|
99
|
+
expect(@namespaced.get('foo')).to eq('barn')
|
100
|
+
expect(@redis.get('foo')).to eq('bar')
|
83
101
|
end
|
84
102
|
|
85
103
|
it 'should be able to use a namespace with brpoplpush' do
|
86
104
|
@namespaced.lpush('foo','bar')
|
87
|
-
@namespaced.brpoplpush('foo','bar',0).
|
88
|
-
@namespaced.lrange('foo',0,-1).
|
89
|
-
@namespaced.lrange('bar',0,-1).
|
105
|
+
expect(@namespaced.brpoplpush('foo','bar',0)).to eq('bar')
|
106
|
+
expect(@namespaced.lrange('foo',0,-1)).to eq([])
|
107
|
+
expect(@namespaced.lrange('bar',0,-1)).to eq(['bar'])
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should be able to use a namespace with getex" do
|
111
|
+
expect(@namespaced.set('mykey', 'Hello')).to eq('OK')
|
112
|
+
expect(@namespaced.getex('mykey', ex: 50)).to eq('Hello')
|
113
|
+
expect(@namespaced.get('mykey')).to eq('Hello')
|
114
|
+
expect(@namespaced.ttl('mykey')).to eq(50)
|
90
115
|
end
|
91
116
|
|
92
117
|
it 'should be able to use a namespace with getbit' do
|
93
118
|
@namespaced.set('foo','bar')
|
94
|
-
@namespaced.getbit('foo',1).
|
119
|
+
expect(@namespaced.getbit('foo',1)).to eq(1)
|
95
120
|
end
|
96
121
|
|
97
122
|
it 'should be able to use a namespace with getrange' do
|
98
123
|
@namespaced.set('foo','bar')
|
99
|
-
@namespaced.getrange('foo',0,-1).
|
124
|
+
expect(@namespaced.getrange('foo',0,-1)).to eq('bar')
|
100
125
|
end
|
101
126
|
|
102
127
|
it 'should be able to use a namespace with linsert' do
|
103
128
|
@namespaced.rpush('foo','bar')
|
104
129
|
@namespaced.rpush('foo','barn')
|
105
130
|
@namespaced.rpush('foo','bart')
|
106
|
-
@namespaced.linsert('foo','BEFORE','barn','barf').
|
107
|
-
@namespaced.lrange('foo',0,-1).
|
131
|
+
expect(@namespaced.linsert('foo','BEFORE','barn','barf')).to eq(4)
|
132
|
+
expect(@namespaced.lrange('foo',0,-1)).to eq(['bar','barf','barn','bart'])
|
108
133
|
end
|
109
134
|
|
110
135
|
it 'should be able to use a namespace with lpushx' do
|
111
|
-
@namespaced.lpushx('foo','bar').
|
136
|
+
expect(@namespaced.lpushx('foo','bar')).to eq(0)
|
112
137
|
@namespaced.lpush('foo','boo')
|
113
|
-
@namespaced.lpushx('foo','bar').
|
114
|
-
@namespaced.lrange('foo',0,-1).
|
138
|
+
expect(@namespaced.lpushx('foo','bar')).to eq(2)
|
139
|
+
expect(@namespaced.lrange('foo',0,-1)).to eq(['bar','boo'])
|
115
140
|
end
|
116
141
|
|
117
142
|
it 'should be able to use a namespace with rpushx' do
|
118
|
-
@namespaced.rpushx('foo','bar').
|
143
|
+
expect(@namespaced.rpushx('foo','bar')).to eq(0)
|
119
144
|
@namespaced.lpush('foo','boo')
|
120
|
-
@namespaced.rpushx('foo','bar').
|
121
|
-
@namespaced.lrange('foo',0,-1).
|
145
|
+
expect(@namespaced.rpushx('foo','bar')).to eq(2)
|
146
|
+
expect(@namespaced.lrange('foo',0,-1)).to eq(['boo','bar'])
|
122
147
|
end
|
123
148
|
|
124
149
|
it 'should be able to use a namespace with setbit' do
|
125
150
|
@namespaced.setbit('virgin_key', 1, 1)
|
126
|
-
@namespaced.exists('virgin_key').
|
127
|
-
@namespaced.get('virgin_key').
|
151
|
+
expect(@namespaced.exists?('virgin_key')).to be true
|
152
|
+
expect(@namespaced.get('virgin_key')).to eq(@namespaced.getrange('virgin_key',0,-1))
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'should be able to use a namespace with exists' do
|
156
|
+
@namespaced.set('foo', 1000)
|
157
|
+
@namespaced.set('bar', 2000)
|
158
|
+
expect(@namespaced.exists('foo', 'bar')).to eq(2)
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'should be able to use a namespace with exists?' do
|
162
|
+
@namespaced.set('foo', 1000)
|
163
|
+
@namespaced.set('bar', 2000)
|
164
|
+
expect(@namespaced.exists?('does_not_exist', 'bar')).to eq(true)
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'should be able to use a namespace with bitpos' do
|
168
|
+
@namespaced.setbit('bit_map', 42, 1)
|
169
|
+
expect(@namespaced.bitpos('bit_map', 0)).to eq(0)
|
170
|
+
expect(@namespaced.bitpos('bit_map', 1)).to eq(42)
|
128
171
|
end
|
129
172
|
|
130
173
|
it 'should be able to use a namespace with setrange' do
|
131
174
|
@namespaced.setrange('foo', 0, 'bar')
|
132
|
-
@namespaced.get('foo').
|
175
|
+
expect(@namespaced.get('foo')).to eq('bar')
|
133
176
|
|
134
177
|
@namespaced.setrange('bar', 2, 'foo')
|
135
|
-
@namespaced.get('bar').
|
178
|
+
expect(@namespaced.get('bar')).to eq("\000\000foo")
|
136
179
|
end
|
137
180
|
|
138
181
|
it "should be able to use a namespace with mget" do
|
139
182
|
@namespaced.set('foo', 1000)
|
140
183
|
@namespaced.set('bar', 2000)
|
141
|
-
@namespaced.mapped_mget('foo', 'bar').
|
142
|
-
@namespaced.mapped_mget('foo', 'baz', 'bar').
|
184
|
+
expect(@namespaced.mapped_mget('foo', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000' })
|
185
|
+
expect(@namespaced.mapped_mget('foo', 'baz', 'bar')).to eq({'foo'=>'1000', 'bar'=>'2000', 'baz' => nil})
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should utilize connection_pool while using a namespace with mget" do
|
189
|
+
memo = @namespaced
|
190
|
+
connection_pool = ConnectionPool.new(size: 2, timeout: 2) { Redis.new db: 15 }
|
191
|
+
@namespaced = Redis::Namespace.new(:ns, redis: connection_pool)
|
192
|
+
|
193
|
+
expect(connection_pool).to receive(:with).and_call_original do |arg|
|
194
|
+
expect(arg).to be(an_instance_of(Redis))
|
195
|
+
end.at_least(:once)
|
196
|
+
|
197
|
+
@namespaced.set('foo', 1000)
|
198
|
+
@namespaced.set('bar', 2000)
|
199
|
+
expect(@namespaced.mapped_mget('foo', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000' })
|
200
|
+
expect(@namespaced.mapped_mget('foo', 'baz', 'bar')).to eq({'foo'=>'1000', 'bar'=>'2000', 'baz' => nil})
|
201
|
+
@redis.get('foo').should eq('bar')
|
202
|
+
|
203
|
+
@namespaced = memo
|
143
204
|
end
|
144
205
|
|
145
206
|
it "should be able to use a namespace with mset" do
|
146
207
|
@namespaced.mset('foo', '1000', 'bar', '2000')
|
147
|
-
@namespaced.mapped_mget('foo', 'bar').
|
148
|
-
@namespaced.mapped_mget('foo', 'baz', 'bar').
|
208
|
+
expect(@namespaced.mapped_mget('foo', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000' })
|
209
|
+
expect(@namespaced.mapped_mget('foo', 'baz', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000', 'baz' => nil})
|
210
|
+
|
149
211
|
@namespaced.mapped_mset('foo' => '3000', 'bar' => '5000')
|
150
|
-
@namespaced.mapped_mget('foo', 'bar').
|
151
|
-
@namespaced.mapped_mget('foo', 'baz', 'bar').
|
212
|
+
expect(@namespaced.mapped_mget('foo', 'bar')).to eq({ 'foo' => '3000', 'bar' => '5000' })
|
213
|
+
expect(@namespaced.mapped_mget('foo', 'baz', 'bar')).to eq({ 'foo' => '3000', 'bar' => '5000', 'baz' => nil})
|
214
|
+
|
215
|
+
@namespaced.mset(['foo', '4000'], ['baz', '6000'])
|
216
|
+
expect(@namespaced.mapped_mget('foo', 'bar', 'baz')).to eq({ 'foo' => '4000', 'bar' => '5000', 'baz' => '6000' })
|
152
217
|
end
|
153
218
|
|
154
219
|
it "should be able to use a namespace with msetnx" do
|
155
220
|
@namespaced.msetnx('foo', '1000', 'bar', '2000')
|
156
|
-
@namespaced.mapped_mget('foo', 'bar').
|
157
|
-
@namespaced.mapped_mget('foo', 'baz', 'bar').
|
221
|
+
expect(@namespaced.mapped_mget('foo', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000' })
|
222
|
+
expect(@namespaced.mapped_mget('foo', 'baz', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000', 'baz' => nil})
|
223
|
+
|
224
|
+
@namespaced.msetnx(['baz', '4000'])
|
225
|
+
expect(@namespaced.mapped_mget('foo', 'baz', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000', 'baz' => '4000'})
|
158
226
|
end
|
159
227
|
|
160
228
|
it "should be able to use a namespace with mapped_msetnx" do
|
161
229
|
@namespaced.set('foo','1')
|
162
|
-
@namespaced.mapped_msetnx('foo'=>'1000', 'bar'=>'2000').
|
163
|
-
@namespaced.mapped_mget('foo', 'bar').
|
164
|
-
@namespaced.mapped_msetnx('bar'=>'2000', 'baz'=>'1000').
|
165
|
-
@namespaced.mapped_mget('foo', 'bar').
|
230
|
+
expect(@namespaced.mapped_msetnx('foo'=>'1000', 'bar'=>'2000')).to be false
|
231
|
+
expect(@namespaced.mapped_mget('foo', 'bar')).to eq({ 'foo' => '1', 'bar' => nil })
|
232
|
+
expect(@namespaced.mapped_msetnx('bar'=>'2000', 'baz'=>'1000')).to be true
|
233
|
+
expect(@namespaced.mapped_mget('foo', 'bar')).to eq({ 'foo' => '1', 'bar' => '2000' })
|
166
234
|
end
|
167
235
|
|
168
236
|
it "should be able to use a namespace with hashes" do
|
169
237
|
@namespaced.hset('foo', 'key', 'value')
|
170
238
|
@namespaced.hset('foo', 'key1', 'value1')
|
171
|
-
@namespaced.hget('foo', 'key').
|
172
|
-
@namespaced.hgetall('foo').
|
173
|
-
@namespaced.hlen('foo').
|
174
|
-
@namespaced.hkeys('foo').
|
239
|
+
expect(@namespaced.hget('foo', 'key')).to eq('value')
|
240
|
+
expect(@namespaced.hgetall('foo')).to eq({'key' => 'value', 'key1' => 'value1'})
|
241
|
+
expect(@namespaced.hlen('foo')).to eq(2)
|
242
|
+
expect(@namespaced.hkeys('foo')).to eq(['key', 'key1'])
|
175
243
|
@namespaced.hmset('bar', 'key', 'value', 'key1', 'value1')
|
176
244
|
@namespaced.hmget('bar', 'key', 'key1')
|
177
245
|
@namespaced.hmset('bar', 'a_number', 1)
|
178
|
-
@namespaced.hmget('bar', 'a_number').
|
246
|
+
expect(@namespaced.hmget('bar', 'a_number')).to eq(['1'])
|
179
247
|
@namespaced.hincrby('bar', 'a_number', 3)
|
180
|
-
@namespaced.hmget('bar', 'a_number').
|
181
|
-
@namespaced.hgetall('bar').
|
182
|
-
|
183
|
-
@namespaced.hsetnx('foonx','nx',10).
|
184
|
-
@namespaced.hsetnx('foonx','nx',12).
|
185
|
-
@namespaced.hget('foonx','nx').
|
186
|
-
@namespaced.hkeys('foonx').
|
187
|
-
@namespaced.hvals('foonx').
|
248
|
+
expect(@namespaced.hmget('bar', 'a_number')).to eq(['4'])
|
249
|
+
expect(@namespaced.hgetall('bar')).to eq({'key' => 'value', 'key1' => 'value1', 'a_number' => '4'})
|
250
|
+
|
251
|
+
expect(@namespaced.hsetnx('foonx','nx',10)).to be true
|
252
|
+
expect(@namespaced.hsetnx('foonx','nx',12)).to be false
|
253
|
+
expect(@namespaced.hget('foonx','nx')).to eq("10")
|
254
|
+
expect(@namespaced.hkeys('foonx')).to eq(%w{ nx })
|
255
|
+
expect(@namespaced.hvals('foonx')).to eq(%w{ 10 })
|
188
256
|
@namespaced.mapped_hmset('baz', {'key' => 'value', 'key1' => 'value1', 'a_number' => 4})
|
189
|
-
@namespaced.mapped_hmget('baz', 'key', 'key1', 'a_number').
|
190
|
-
@namespaced.hgetall('baz').
|
257
|
+
expect(@namespaced.mapped_hmget('baz', 'key', 'key1', 'a_number')).to eq({'key' => 'value', 'key1' => 'value1', 'a_number' => '4'})
|
258
|
+
expect(@namespaced.hgetall('baz')).to eq({'key' => 'value', 'key1' => 'value1', 'a_number' => '4'})
|
191
259
|
end
|
192
260
|
|
193
261
|
it "should properly intersect three sets" do
|
@@ -198,7 +266,7 @@ describe "redis" do
|
|
198
266
|
@namespaced.sadd('bar', 3)
|
199
267
|
@namespaced.sadd('bar', 4)
|
200
268
|
@namespaced.sadd('baz', 3)
|
201
|
-
@namespaced.sinter('foo', 'bar', 'baz').
|
269
|
+
expect(@namespaced.sinter('foo', 'bar', 'baz')).to eq(%w( 3 ))
|
202
270
|
end
|
203
271
|
|
204
272
|
it "should properly union two sets" do
|
@@ -207,7 +275,7 @@ describe "redis" do
|
|
207
275
|
@namespaced.sadd('bar', 2)
|
208
276
|
@namespaced.sadd('bar', 3)
|
209
277
|
@namespaced.sadd('bar', 4)
|
210
|
-
@namespaced.sunion('foo', 'bar').sort.
|
278
|
+
expect(@namespaced.sunion('foo', 'bar').sort).to eq(%w( 1 2 3 4 ))
|
211
279
|
end
|
212
280
|
|
213
281
|
it "should properly union two sorted sets with options" do
|
@@ -216,8 +284,8 @@ describe "redis" do
|
|
216
284
|
@namespaced.zadd('sort2', 2, 2)
|
217
285
|
@namespaced.zadd('sort2', 3, 3)
|
218
286
|
@namespaced.zadd('sort2', 4, 4)
|
219
|
-
@namespaced.zunionstore('union', ['sort1', 'sort2'], :
|
220
|
-
@namespaced.zrevrange('union', 0, -1).
|
287
|
+
@namespaced.zunionstore('union', ['sort1', 'sort2'], weights: [2, 1])
|
288
|
+
expect(@namespaced.zrevrange('union', 0, -1)).to eq(%w( 2 4 3 1 ))
|
221
289
|
end
|
222
290
|
|
223
291
|
it "should properly union two sorted sets without options" do
|
@@ -227,7 +295,7 @@ describe "redis" do
|
|
227
295
|
@namespaced.zadd('sort2', 3, 3)
|
228
296
|
@namespaced.zadd('sort2', 4, 4)
|
229
297
|
@namespaced.zunionstore('union', ['sort1', 'sort2'])
|
230
|
-
@namespaced.zrevrange('union', 0, -1).
|
298
|
+
expect(@namespaced.zrevrange('union', 0, -1)).to eq(%w( 4 2 3 1 ))
|
231
299
|
end
|
232
300
|
|
233
301
|
it "should properly intersect two sorted sets without options" do
|
@@ -242,7 +310,7 @@ describe "redis" do
|
|
242
310
|
@namespaced.zinterstore('inter', ['food', 'color'])
|
243
311
|
|
244
312
|
inter_values = @namespaced.zrevrange('inter', 0, -1, :with_scores => true)
|
245
|
-
inter_values.
|
313
|
+
expect(inter_values).to match_array([['orange', 3.0], ['eggplant', 7.0]])
|
246
314
|
end
|
247
315
|
|
248
316
|
it "should properly intersect two sorted sets with options" do
|
@@ -257,7 +325,48 @@ describe "redis" do
|
|
257
325
|
@namespaced.zinterstore('inter', ['food', 'color'], :aggregate => "min")
|
258
326
|
|
259
327
|
inter_values = @namespaced.zrevrange('inter', 0, -1, :with_scores => true)
|
260
|
-
inter_values.
|
328
|
+
expect(inter_values).to match_array([['orange', 1.0], ['eggplant', 3.0]])
|
329
|
+
end
|
330
|
+
|
331
|
+
it "should return lexicographical range for sorted set" do
|
332
|
+
@namespaced.zadd('food', 0, 'orange')
|
333
|
+
@namespaced.zadd('food', 0, 'banana')
|
334
|
+
@namespaced.zadd('food', 0, 'eggplant')
|
335
|
+
|
336
|
+
values = @namespaced.zrangebylex('food', '[b', '(o')
|
337
|
+
expect(values).to match_array(['banana', 'eggplant'])
|
338
|
+
end
|
339
|
+
|
340
|
+
it "should return the number of elements removed from the set" do
|
341
|
+
@namespaced.zadd('food', 0, 'orange')
|
342
|
+
@namespaced.zadd('food', 0, 'banana')
|
343
|
+
@namespaced.zadd('food', 0, 'eggplant')
|
344
|
+
|
345
|
+
removed = @namespaced.zremrangebylex('food', '[b', '(o')
|
346
|
+
expect(removed).to eq(2)
|
347
|
+
|
348
|
+
values = @namespaced.zrange('food', 0, -1)
|
349
|
+
expect(values).to eq(['orange'])
|
350
|
+
end
|
351
|
+
|
352
|
+
it "should return reverce lexicographical range for sorted set" do
|
353
|
+
@namespaced.zadd('food', 0, 'orange')
|
354
|
+
@namespaced.zadd('food', 0, 'banana')
|
355
|
+
@namespaced.zadd('food', 0, 'eggplant')
|
356
|
+
|
357
|
+
values = @namespaced.zrevrangebylex('food', '(o', '[b')
|
358
|
+
expect(values).to match_array(['banana', 'eggplant'])
|
359
|
+
end
|
360
|
+
|
361
|
+
it "should add a new member" do
|
362
|
+
expect(@namespaced.sadd?('foo', 1)).to eq(true)
|
363
|
+
expect(@namespaced.sadd?('foo', 1)).to eq(false)
|
364
|
+
end
|
365
|
+
|
366
|
+
it "should remove members" do
|
367
|
+
@namespaced.sadd('foo', 1)
|
368
|
+
expect(@namespaced.srem?('foo', 1)).to eq(true)
|
369
|
+
expect(@namespaced.srem?('foo', 1)).to eq(false)
|
261
370
|
end
|
262
371
|
|
263
372
|
it "should add namespace to sort" do
|
@@ -268,24 +377,24 @@ describe "redis" do
|
|
268
377
|
@namespaced.set('value_1', 'a')
|
269
378
|
@namespaced.set('value_2', 'b')
|
270
379
|
|
271
|
-
@namespaced.sort('foo').
|
272
|
-
@namespaced.sort('foo', :limit => [0, 1]).
|
273
|
-
@namespaced.sort('foo', :order => 'desc').
|
274
|
-
@namespaced.sort('foo', :by => 'weight_*').
|
275
|
-
@namespaced.sort('foo', :get => 'value_*').
|
276
|
-
@namespaced.sort('foo', :get => '#').
|
277
|
-
@namespaced.sort('foo', :get => ['#', 'value_*']).
|
380
|
+
expect(@namespaced.sort('foo')).to eq(%w( 1 2 ))
|
381
|
+
expect(@namespaced.sort('foo', :limit => [0, 1])).to eq(%w( 1 ))
|
382
|
+
expect(@namespaced.sort('foo', :order => 'desc')).to eq(%w( 2 1 ))
|
383
|
+
expect(@namespaced.sort('foo', :by => 'weight_*')).to eq(%w( 2 1 ))
|
384
|
+
expect(@namespaced.sort('foo', :get => 'value_*')).to eq(%w( a b ))
|
385
|
+
expect(@namespaced.sort('foo', :get => '#')).to eq(%w( 1 2 ))
|
386
|
+
expect(@namespaced.sort('foo', :get => ['#', 'value_*'])).to eq([["1", "a"], ["2", "b"]])
|
278
387
|
|
279
388
|
@namespaced.sort('foo', :store => 'result')
|
280
|
-
@namespaced.lrange('result', 0, -1).
|
389
|
+
expect(@namespaced.lrange('result', 0, -1)).to eq(%w( 1 2 ))
|
281
390
|
end
|
282
391
|
|
283
392
|
it "should yield the correct list of keys" do
|
284
393
|
@namespaced.set("foo", 1)
|
285
394
|
@namespaced.set("bar", 2)
|
286
395
|
@namespaced.set("baz", 3)
|
287
|
-
@namespaced.keys("*").sort.
|
288
|
-
@namespaced.keys.sort.
|
396
|
+
expect(@namespaced.keys("*").sort).to eq(%w( bar baz foo ))
|
397
|
+
expect(@namespaced.keys.sort).to eq(%w( bar baz foo ))
|
289
398
|
end
|
290
399
|
|
291
400
|
it "should add namepsace to multi blocks" do
|
@@ -294,7 +403,27 @@ describe "redis" do
|
|
294
403
|
r.del "foo"
|
295
404
|
r.mapped_hmset "foo", {"key1" => "value1"}
|
296
405
|
end
|
297
|
-
@namespaced.hgetall("foo").
|
406
|
+
expect(@namespaced.hgetall("foo")).to eq({"key1" => "value1"})
|
407
|
+
end
|
408
|
+
|
409
|
+
it "should utilize connection_pool while adding namepsace to multi blocks" do
|
410
|
+
memo = @namespaced
|
411
|
+
connection_pool = ConnectionPool.new(size: 2, timeout: 2) { Redis.new db: 15 }
|
412
|
+
@namespaced = Redis::Namespace.new(:ns, redis: connection_pool)
|
413
|
+
|
414
|
+
expect(connection_pool).to receive(:with).and_call_original do |arg|
|
415
|
+
expect(arg).to be(an_instance_of(Redis))
|
416
|
+
end.at_least(:once)
|
417
|
+
|
418
|
+
@namespaced.mapped_hmset "foo", {"key" => "value"}
|
419
|
+
@namespaced.multi do |r|
|
420
|
+
r.del "foo"
|
421
|
+
r.mapped_hmset "foo", {"key1" => "value1"}
|
422
|
+
end
|
423
|
+
expect(@redis.get("foo")).to eq("bar")
|
424
|
+
expect(@namespaced.hgetall("foo")).to eq({"key1" => "value1"})
|
425
|
+
|
426
|
+
@namespaced = memo
|
298
427
|
end
|
299
428
|
|
300
429
|
it "should pass through multi commands without block" do
|
@@ -305,14 +434,36 @@ describe "redis" do
|
|
305
434
|
@namespaced.mapped_hmset "foo", {"key1" => "value1"}
|
306
435
|
@namespaced.exec
|
307
436
|
|
308
|
-
@namespaced.hgetall("foo").
|
437
|
+
expect(@namespaced.hgetall("foo")).to eq({"key1" => "value1"})
|
438
|
+
end
|
439
|
+
|
440
|
+
it "should utilize connection_pool while passing through multi commands without block" do
|
441
|
+
memo = @namespaced
|
442
|
+
connection_pool = ConnectionPool.new(size: 2, timeout: 2) { Redis.new db: 15 }
|
443
|
+
@namespaced = Redis::Namespace.new(:ns, redis: connection_pool)
|
444
|
+
|
445
|
+
expect(connection_pool).to receive(:with).and_call_original do |arg|
|
446
|
+
expect(arg).to be(an_instance_of(Redis))
|
447
|
+
end.at_least(:once)
|
448
|
+
|
449
|
+
@namespaced.mapped_hmset "foo", {"key" => "value"}
|
450
|
+
|
451
|
+
@namespaced.multi
|
452
|
+
@namespaced.del "foo"
|
453
|
+
@namespaced.mapped_hmset "foo", {"key1" => "value1"}
|
454
|
+
@namespaced.exec
|
455
|
+
|
456
|
+
expect(@namespaced.hgetall("foo")).to eq({"key1" => "value1"})
|
457
|
+
expect(@redis.get("foo")).to eq("bar")
|
458
|
+
|
459
|
+
@namespaced = memo
|
309
460
|
end
|
310
461
|
|
311
462
|
it 'should return futures without attempting to remove namespaces' do
|
312
463
|
@namespaced.multi do
|
313
464
|
@future = @namespaced.keys('*')
|
314
465
|
end
|
315
|
-
@future.class.
|
466
|
+
expect(@future.class).to be(Redis::Future)
|
316
467
|
end
|
317
468
|
|
318
469
|
it "should add namespace to pipelined blocks" do
|
@@ -321,7 +472,27 @@ describe "redis" do
|
|
321
472
|
r.del "foo"
|
322
473
|
r.mapped_hmset "foo", {"key1" => "value1"}
|
323
474
|
end
|
324
|
-
@namespaced.hgetall("foo").
|
475
|
+
expect(@namespaced.hgetall("foo")).to eq({"key1" => "value1"})
|
476
|
+
end
|
477
|
+
|
478
|
+
it "should utilize connection_pool while adding namespace to pipelined blocks" do
|
479
|
+
memo = @namespaced
|
480
|
+
connection_pool = ConnectionPool.new(size: 2, timeout: 2) { Redis.new db: 15 }
|
481
|
+
@namespaced = Redis::Namespace.new(:ns, redis: connection_pool)
|
482
|
+
|
483
|
+
expect(connection_pool).to receive(:with).and_call_original do |arg|
|
484
|
+
expect(arg).to be(an_instance_of(Redis))
|
485
|
+
end.at_least(:once)
|
486
|
+
|
487
|
+
@namespaced.mapped_hmset "foo", {"key" => "value"}
|
488
|
+
@namespaced.pipelined do |r|
|
489
|
+
r.del "foo"
|
490
|
+
r.mapped_hmset "foo", {"key1" => "value1"}
|
491
|
+
end
|
492
|
+
expect(@namespaced.hgetall("foo")).to eq({"key1" => "value1"})
|
493
|
+
expect(@redis.get("foo")).to eq("bar")
|
494
|
+
|
495
|
+
@namespaced = memo
|
325
496
|
end
|
326
497
|
|
327
498
|
it "should returned response array from pipelined block" do
|
@@ -330,65 +501,125 @@ describe "redis" do
|
|
330
501
|
r.get("foo")
|
331
502
|
r.get("key")
|
332
503
|
end
|
333
|
-
result.
|
504
|
+
expect(result).to eq(["bar", "value"])
|
505
|
+
end
|
506
|
+
|
507
|
+
it "is thread safe for multi blocks" do
|
508
|
+
mon = Monitor.new
|
509
|
+
entered = false
|
510
|
+
entered_cond = mon.new_cond
|
511
|
+
|
512
|
+
thread = Thread.new do
|
513
|
+
mon.synchronize do
|
514
|
+
entered_cond.wait_until { entered }
|
515
|
+
@namespaced.multi
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
@namespaced.multi do |transaction|
|
520
|
+
entered = true
|
521
|
+
mon.synchronize { entered_cond.signal }
|
522
|
+
thread.join(0.1)
|
523
|
+
transaction.get("foo")
|
524
|
+
end
|
525
|
+
thread.join
|
334
526
|
end
|
335
527
|
|
336
528
|
it "should add namespace to strlen" do
|
337
529
|
@namespaced.set("mykey", "123456")
|
338
|
-
@namespaced.strlen("mykey").
|
530
|
+
expect(@namespaced.strlen("mykey")).to eq(6)
|
339
531
|
end
|
340
532
|
|
341
533
|
it "should not add namespace to echo" do
|
342
|
-
@namespaced.echo(123).
|
534
|
+
expect(@namespaced.echo(123)).to eq("123")
|
343
535
|
end
|
344
536
|
|
345
537
|
it 'should not add namespace to disconnect!' do
|
346
|
-
expect(@redis).to receive(:disconnect!).with().and_call_original
|
538
|
+
expect(@redis).to receive(:disconnect!).with(no_args).and_call_original
|
347
539
|
|
348
540
|
expect(@namespaced.disconnect!).to be nil
|
349
541
|
end
|
350
542
|
|
351
543
|
it "can change its namespace" do
|
352
|
-
@namespaced.get('foo').
|
544
|
+
expect(@namespaced.get('foo')).to eq(nil)
|
353
545
|
@namespaced.set('foo', 'chris')
|
354
|
-
@namespaced.get('foo').
|
546
|
+
expect(@namespaced.get('foo')).to eq('chris')
|
355
547
|
|
356
|
-
@namespaced.namespace.
|
548
|
+
expect(@namespaced.namespace).to eq(:ns)
|
357
549
|
@namespaced.namespace = :spec
|
358
|
-
@namespaced.namespace.
|
550
|
+
expect(@namespaced.namespace).to eq(:spec)
|
359
551
|
|
360
|
-
@namespaced.get('foo').
|
552
|
+
expect(@namespaced.get('foo')).to eq(nil)
|
361
553
|
@namespaced.set('foo', 'chris')
|
362
|
-
@namespaced.get('foo').
|
554
|
+
expect(@namespaced.get('foo')).to eq('chris')
|
363
555
|
end
|
364
556
|
|
365
557
|
it "can accept a temporary namespace" do
|
366
|
-
@namespaced.namespace.
|
367
|
-
@namespaced.get('foo').
|
558
|
+
expect(@namespaced.namespace).to eq(:ns)
|
559
|
+
expect(@namespaced.get('foo')).to eq(nil)
|
368
560
|
|
369
561
|
@namespaced.namespace(:spec) do |temp_ns|
|
370
|
-
temp_ns.namespace.
|
371
|
-
temp_ns.get('foo').
|
562
|
+
expect(temp_ns.namespace).to eq(:spec)
|
563
|
+
expect(temp_ns.get('foo')).to eq(nil)
|
372
564
|
temp_ns.set('foo', 'jake')
|
373
|
-
temp_ns.get('foo').
|
565
|
+
expect(temp_ns.get('foo')).to eq('jake')
|
374
566
|
end
|
375
567
|
|
376
|
-
@namespaced.namespace.
|
377
|
-
@namespaced.get('foo').
|
568
|
+
expect(@namespaced.namespace).to eq(:ns)
|
569
|
+
expect(@namespaced.get('foo')).to eq(nil)
|
378
570
|
end
|
379
571
|
|
380
572
|
it "should respond to :namespace=" do
|
381
|
-
@namespaced.respond_to?(:namespace=).
|
573
|
+
expect(@namespaced.respond_to?(:namespace=)).to eq(true)
|
382
574
|
end
|
383
575
|
|
384
576
|
it "should respond to :warning=" do
|
385
|
-
@namespaced.respond_to?(:warning=).
|
577
|
+
expect(@namespaced.respond_to?(:warning=)).to eq(true)
|
386
578
|
end
|
387
579
|
|
388
580
|
it "should raise an exception when an unknown command is passed" do
|
389
581
|
expect { @namespaced.unknown('foo') }.to raise_exception NoMethodError
|
390
582
|
end
|
391
583
|
|
584
|
+
describe '#inspect' do
|
585
|
+
let(:single_level_names) { %i[first] }
|
586
|
+
let(:double_level_names) { %i[first second] }
|
587
|
+
let(:triple_level_names) { %i[first second third] }
|
588
|
+
let(:namespace_builder) do
|
589
|
+
->(redis, *namespaces) { namespaces.reduce(redis) { |r, n| Redis::Namespace.new(n, redis: r) } }
|
590
|
+
end
|
591
|
+
let(:regexp_builder) do
|
592
|
+
->(*namespaces) { %r{/#{namespaces.join(':')}>\z} }
|
593
|
+
end
|
594
|
+
|
595
|
+
context 'when one namespace' do
|
596
|
+
let(:single_namespaced) { namespace_builder.call(@redis, *single_level_names) }
|
597
|
+
let(:regexp) { regexp_builder.call(*single_level_names) }
|
598
|
+
|
599
|
+
it 'should have correct ending of inspect string' do
|
600
|
+
expect(regexp =~ single_namespaced.inspect).not_to be(nil)
|
601
|
+
end
|
602
|
+
end
|
603
|
+
|
604
|
+
context 'when two namespaces' do
|
605
|
+
let(:double_namespaced) { namespace_builder.call(@redis, *double_level_names) }
|
606
|
+
let(:regexp) { regexp_builder.call(*double_level_names) }
|
607
|
+
|
608
|
+
it 'should have correct ending of inspect string' do
|
609
|
+
expect(regexp =~ double_namespaced.inspect).not_to be(nil)
|
610
|
+
end
|
611
|
+
end
|
612
|
+
|
613
|
+
context 'when three namespaces' do
|
614
|
+
let(:triple_namespaced) { namespace_builder.call(@redis, *triple_level_names) }
|
615
|
+
let(:regexp) { regexp_builder.call(*triple_level_names) }
|
616
|
+
|
617
|
+
it 'should have correct ending of inspect string' do
|
618
|
+
expect(regexp =~ triple_namespaced.inspect).not_to be(nil)
|
619
|
+
end
|
620
|
+
end
|
621
|
+
end
|
622
|
+
|
392
623
|
# Redis 2.6 RC reports its version as 2.5.
|
393
624
|
if @redis_version >= Gem::Version.new("2.5.0")
|
394
625
|
describe "redis 2.6 commands" do
|
@@ -422,7 +653,7 @@ describe "redis" do
|
|
422
653
|
v = @namespaced.dump("foo")
|
423
654
|
@redis.del("ns:foo")
|
424
655
|
|
425
|
-
expect(@namespaced.restore("foo", 1000, v)).to
|
656
|
+
expect(@namespaced.restore("foo", 1000, v)).to be_truthy
|
426
657
|
expect(@redis.get("ns:foo")).to eq 'a'
|
427
658
|
expect(@redis.ttl("ns:foo")).to satisfy {|v| (0..1).include?(v) }
|
428
659
|
|
@@ -430,72 +661,84 @@ describe "redis" do
|
|
430
661
|
w = @namespaced.dump("bar")
|
431
662
|
@redis.del("ns:bar")
|
432
663
|
|
433
|
-
expect(@namespaced.restore("bar", 1000, w)).to
|
664
|
+
expect(@namespaced.restore("bar", 1000, w)).to be_truthy
|
434
665
|
expect(@redis.lrange('ns:bar', 0, -1)).to eq %w(b c d)
|
435
666
|
expect(@redis.ttl("ns:foo")).to satisfy {|v| (0..1).include?(v) }
|
436
667
|
end
|
437
668
|
|
669
|
+
it "should namespace expiretime" do
|
670
|
+
@namespaced.set('mykey', 'Hello')
|
671
|
+
@namespaced.expireat('mykey', 2000000000)
|
672
|
+
expect(@namespaced.expiretime('mykey')).to eq(2000000000)
|
673
|
+
end
|
674
|
+
|
438
675
|
it "should namespace hincrbyfloat" do
|
439
676
|
@namespaced.hset('mykey', 'field', 10.50)
|
440
|
-
@namespaced.hincrbyfloat('mykey', 'field', 0.1).
|
677
|
+
expect(@namespaced.hincrbyfloat('mykey', 'field', 0.1)).to eq(10.6)
|
441
678
|
end
|
442
679
|
|
443
680
|
it "should namespace incrbyfloat" do
|
444
681
|
@namespaced.set('mykey', 10.50)
|
445
|
-
@namespaced.incrbyfloat('mykey', 0.1).
|
682
|
+
expect(@namespaced.incrbyfloat('mykey', 0.1)).to eq(10.6)
|
446
683
|
end
|
447
684
|
|
448
685
|
it "should namespace object" do
|
449
686
|
@namespaced.set('foo', 1000)
|
450
|
-
@namespaced.object('encoding', 'foo').
|
687
|
+
expect(@namespaced.object('encoding', 'foo')).to eq('int')
|
451
688
|
end
|
452
689
|
|
453
690
|
it "should namespace persist" do
|
454
691
|
@namespaced.set('mykey', 'Hello')
|
455
692
|
@namespaced.expire('mykey', 60)
|
456
|
-
@namespaced.persist('mykey').
|
457
|
-
@namespaced.ttl('mykey').
|
693
|
+
expect(@namespaced.persist('mykey')).to eq(true)
|
694
|
+
expect(@namespaced.ttl('mykey')).to eq(-1)
|
458
695
|
end
|
459
696
|
|
460
697
|
it "should namespace pexpire" do
|
461
698
|
@namespaced.set('mykey', 'Hello')
|
462
|
-
@namespaced.pexpire('mykey', 60000).
|
699
|
+
expect(@namespaced.pexpire('mykey', 60000)).to eq(true)
|
463
700
|
end
|
464
701
|
|
465
702
|
it "should namespace pexpireat" do
|
466
703
|
@namespaced.set('mykey', 'Hello')
|
467
|
-
@namespaced.pexpire('mykey', 1555555555005).
|
704
|
+
expect(@namespaced.pexpire('mykey', 1555555555005)).to eq(true)
|
705
|
+
end
|
706
|
+
|
707
|
+
it "should namespace pexpiretime" do
|
708
|
+
@namespaced.set('mykey', 'Hello')
|
709
|
+
@namespaced.pexpireat('mykey', 2000000000000)
|
710
|
+
expect(@namespaced.pexpiretime('mykey')).to eq(2000000000000)
|
468
711
|
end
|
469
712
|
|
470
713
|
it "should namespace psetex" do
|
471
|
-
@namespaced.psetex('mykey', 10000, 'Hello').
|
472
|
-
@namespaced.get('mykey').
|
714
|
+
expect(@namespaced.psetex('mykey', 10000, 'Hello')).to eq('OK')
|
715
|
+
expect(@namespaced.get('mykey')).to eq('Hello')
|
473
716
|
end
|
474
717
|
|
475
718
|
it "should namespace pttl" do
|
476
719
|
@namespaced.set('mykey', 'Hello')
|
477
720
|
@namespaced.expire('mykey', 1)
|
478
|
-
@namespaced.pttl('mykey').
|
721
|
+
expect(@namespaced.pttl('mykey')).to be >= 0
|
479
722
|
end
|
480
723
|
|
481
724
|
it "should namespace eval keys passed in as array args" do
|
482
|
-
@namespaced.
|
483
|
-
eval("return {KEYS[1], KEYS[2]}", %w[k1 k2], %w[arg1 arg2]).
|
484
|
-
|
725
|
+
expect(@namespaced.
|
726
|
+
eval("return {KEYS[1], KEYS[2]}", %w[k1 k2], %w[arg1 arg2])).
|
727
|
+
to eq(%w[ns:k1 ns:k2])
|
485
728
|
end
|
486
729
|
|
487
730
|
it "should namespace eval keys passed in as hash args" do
|
488
|
-
@namespaced.
|
489
|
-
eval("return {KEYS[1], KEYS[2]}", :keys => %w[k1 k2], :argv => %w[arg1 arg2]).
|
490
|
-
|
731
|
+
expect(@namespaced.
|
732
|
+
eval("return {KEYS[1], KEYS[2]}", :keys => %w[k1 k2], :argv => %w[arg1 arg2])).
|
733
|
+
to eq(%w[ns:k1 ns:k2])
|
491
734
|
end
|
492
735
|
|
493
736
|
it "should namespace eval keys passed in as hash args unmodified" do
|
494
737
|
args = { :keys => %w[k1 k2], :argv => %w[arg1 arg2] }
|
495
738
|
args.freeze
|
496
|
-
@namespaced.
|
497
|
-
eval("return {KEYS[1], KEYS[2]}", args).
|
498
|
-
|
739
|
+
expect(@namespaced.
|
740
|
+
eval("return {KEYS[1], KEYS[2]}", args)).
|
741
|
+
to eq(%w[ns:k1 ns:k2])
|
499
742
|
end
|
500
743
|
|
501
744
|
context '#evalsha' do
|
@@ -504,23 +747,23 @@ describe "redis" do
|
|
504
747
|
end
|
505
748
|
|
506
749
|
it "should namespace evalsha keys passed in as array args" do
|
507
|
-
@namespaced.
|
508
|
-
evalsha(sha, %w[k1 k2], %w[arg1 arg2]).
|
509
|
-
|
750
|
+
expect(@namespaced.
|
751
|
+
evalsha(sha, %w[k1 k2], %w[arg1 arg2])).
|
752
|
+
to eq(%w[ns:k1 ns:k2])
|
510
753
|
end
|
511
754
|
|
512
755
|
it "should namespace evalsha keys passed in as hash args" do
|
513
|
-
@namespaced.
|
514
|
-
evalsha(sha, :keys => %w[k1 k2], :argv => %w[arg1 arg2]).
|
515
|
-
|
756
|
+
expect(@namespaced.
|
757
|
+
evalsha(sha, :keys => %w[k1 k2], :argv => %w[arg1 arg2])).
|
758
|
+
to eq(%w[ns:k1 ns:k2])
|
516
759
|
end
|
517
760
|
|
518
761
|
it "should namespace evalsha keys passed in as hash args unmodified" do
|
519
762
|
args = { :keys => %w[k1 k2], :argv => %w[arg1 arg2] }
|
520
763
|
args.freeze
|
521
|
-
@namespaced.
|
522
|
-
evalsha(sha, args).
|
523
|
-
|
764
|
+
expect(@namespaced.
|
765
|
+
evalsha(sha, args)).
|
766
|
+
to eq(%w[ns:k1 ns:k2])
|
524
767
|
end
|
525
768
|
end
|
526
769
|
|
@@ -529,13 +772,13 @@ describe "redis" do
|
|
529
772
|
let(:sha) { @redis.script(:load, "return {KEYS[1], KEYS[2]}") }
|
530
773
|
|
531
774
|
it "should namespace eval keys passed in as hash args" do
|
532
|
-
nested_namespace.
|
533
|
-
eval("return {KEYS[1], KEYS[2]}", :keys => %w[k1 k2], :argv => %w[arg1 arg2]).
|
534
|
-
|
775
|
+
expect(nested_namespace.
|
776
|
+
eval("return {KEYS[1], KEYS[2]}", :keys => %w[k1 k2], :argv => %w[arg1 arg2])).
|
777
|
+
to eq(%w[ns:nest:k1 ns:nest:k2])
|
535
778
|
end
|
536
779
|
it "should namespace evalsha keys passed in as hash args" do
|
537
|
-
nested_namespace.evalsha(sha, :keys => %w[k1 k2], :argv => %w[arg1 arg2]).
|
538
|
-
|
780
|
+
expect(nested_namespace.evalsha(sha, :keys => %w[k1 k2], :argv => %w[arg1 arg2])).
|
781
|
+
to eq(%w[ns:nest:k1 ns:nest:k2])
|
539
782
|
end
|
540
783
|
end
|
541
784
|
end
|
@@ -564,16 +807,16 @@ describe "redis" do
|
|
564
807
|
context 'when :match supplied' do
|
565
808
|
it 'should retrieve the proper keys' do
|
566
809
|
_, result = @namespaced.scan(0, :match => 'zeta:*', :count => 1000)
|
567
|
-
result.
|
810
|
+
expect(result).to match_array(matching_namespaced_keys)
|
568
811
|
end
|
569
812
|
end
|
570
813
|
context 'without :match supplied' do
|
571
814
|
it 'should retrieve the proper keys' do
|
572
815
|
_, result = @namespaced.scan(0, :count => 1000)
|
573
|
-
result.
|
816
|
+
expect(result).to match_array(namespaced_keys)
|
574
817
|
end
|
575
818
|
end
|
576
|
-
end if Redis.
|
819
|
+
end if Redis.new.respond_to?(:scan)
|
577
820
|
|
578
821
|
context '#scan_each' do
|
579
822
|
context 'when :match supplied' do
|
@@ -581,13 +824,13 @@ describe "redis" do
|
|
581
824
|
it 'should yield unnamespaced' do
|
582
825
|
results = []
|
583
826
|
@namespaced.scan_each(:match => 'zeta:*', :count => 1000) {|k| results << k }
|
584
|
-
results.
|
827
|
+
expect(results).to match_array(matching_namespaced_keys)
|
585
828
|
end
|
586
829
|
end
|
587
830
|
context 'without a block' do
|
588
831
|
it 'should return an Enumerator that un-namespaces' do
|
589
832
|
enum = @namespaced.scan_each(:match => 'zeta:*', :count => 1000)
|
590
|
-
enum.to_a.
|
833
|
+
expect(enum.to_a).to match_array(matching_namespaced_keys)
|
591
834
|
end
|
592
835
|
end
|
593
836
|
end
|
@@ -596,17 +839,17 @@ describe "redis" do
|
|
596
839
|
it 'should yield unnamespaced' do
|
597
840
|
results = []
|
598
841
|
@namespaced.scan_each(:count => 1000){ |k| results << k }
|
599
|
-
results.
|
842
|
+
expect(results).to match_array(namespaced_keys)
|
600
843
|
end
|
601
844
|
end
|
602
845
|
context 'without a block' do
|
603
846
|
it 'should return an Enumerator that un-namespaces' do
|
604
847
|
enum = @namespaced.scan_each(:count => 1000)
|
605
|
-
enum.to_a.
|
848
|
+
expect(enum.to_a).to match_array(namespaced_keys)
|
606
849
|
end
|
607
850
|
end
|
608
851
|
end
|
609
|
-
end if Redis.
|
852
|
+
end if Redis.new.respond_to?(:scan_each)
|
610
853
|
end
|
611
854
|
|
612
855
|
context 'hash scan methods' do
|
@@ -625,16 +868,16 @@ describe "redis" do
|
|
625
868
|
context 'when supplied :match' do
|
626
869
|
it 'should retrieve the proper keys' do
|
627
870
|
_, results = @namespaced.hscan('hsh', 0, :match => 'zeta:*')
|
628
|
-
results.
|
871
|
+
expect(results).to match_array(hash_matching_subset.to_a)
|
629
872
|
end
|
630
873
|
end
|
631
874
|
context 'without :match supplied' do
|
632
875
|
it 'should retrieve all hash keys' do
|
633
876
|
_, results = @namespaced.hscan('hsh', 0)
|
634
|
-
results.
|
877
|
+
expect(results).to match_array(@redis.hgetall('ns:hsh').to_a)
|
635
878
|
end
|
636
879
|
end
|
637
|
-
end if Redis.
|
880
|
+
end if Redis.new.respond_to?(:hscan)
|
638
881
|
|
639
882
|
context '#hscan_each' do
|
640
883
|
context 'when :match supplied' do
|
@@ -642,13 +885,13 @@ describe "redis" do
|
|
642
885
|
it 'should yield the correct hash keys unchanged' do
|
643
886
|
results = []
|
644
887
|
@namespaced.hscan_each('hsh', :match => 'zeta:*', :count => 1000) { |kv| results << kv}
|
645
|
-
results.
|
888
|
+
expect(results).to match_array(hash_matching_subset.to_a)
|
646
889
|
end
|
647
890
|
end
|
648
891
|
context 'without a block' do
|
649
892
|
it 'should return an Enumerator that yields the correct hash keys unchanged' do
|
650
893
|
enum = @namespaced.hscan_each('hsh', :match => 'zeta:*', :count => 1000)
|
651
|
-
enum.to_a.
|
894
|
+
expect(enum.to_a).to match_array(hash_matching_subset.to_a)
|
652
895
|
end
|
653
896
|
end
|
654
897
|
end
|
@@ -657,17 +900,17 @@ describe "redis" do
|
|
657
900
|
it 'should yield all hash keys unchanged' do
|
658
901
|
results = []
|
659
902
|
@namespaced.hscan_each('hsh', :count => 1000){ |k| results << k }
|
660
|
-
results.
|
903
|
+
expect(results).to match_array(hash.to_a)
|
661
904
|
end
|
662
905
|
end
|
663
906
|
context 'without a block' do
|
664
907
|
it 'should return an Enumerator that yields all keys unchanged' do
|
665
908
|
enum = @namespaced.hscan_each('hsh', :count => 1000)
|
666
|
-
enum.to_a.
|
909
|
+
expect(enum.to_a).to match_array(hash.to_a)
|
667
910
|
end
|
668
911
|
end
|
669
912
|
end
|
670
|
-
end if Redis.
|
913
|
+
end if Redis.new.respond_to?(:hscan_each)
|
671
914
|
end
|
672
915
|
|
673
916
|
context 'set scan methods' do
|
@@ -686,16 +929,16 @@ describe "redis" do
|
|
686
929
|
context 'when supplied :match' do
|
687
930
|
it 'should retrieve the matching set members from the proper set' do
|
688
931
|
_, results = @namespaced.sscan('set', 0, :match => 'zeta:*', :count => 1000)
|
689
|
-
results.
|
932
|
+
expect(results).to match_array(matching_subset)
|
690
933
|
end
|
691
934
|
end
|
692
935
|
context 'without :match supplied' do
|
693
936
|
it 'should retrieve all set members from the proper set' do
|
694
937
|
_, results = @namespaced.sscan('set', 0, :count => 1000)
|
695
|
-
results.
|
938
|
+
expect(results).to match_array(set)
|
696
939
|
end
|
697
940
|
end
|
698
|
-
end if Redis.
|
941
|
+
end if Redis.new.respond_to?(:sscan)
|
699
942
|
|
700
943
|
context '#sscan_each' do
|
701
944
|
context 'when :match supplied' do
|
@@ -703,13 +946,13 @@ describe "redis" do
|
|
703
946
|
it 'should yield the correct hset elements unchanged' do
|
704
947
|
results = []
|
705
948
|
@namespaced.sscan_each('set', :match => 'zeta:*', :count => 1000) { |kv| results << kv}
|
706
|
-
results.
|
949
|
+
expect(results).to match_array(matching_subset)
|
707
950
|
end
|
708
951
|
end
|
709
952
|
context 'without a block' do
|
710
953
|
it 'should return an Enumerator that yields the correct set elements unchanged' do
|
711
954
|
enum = @namespaced.sscan_each('set', :match => 'zeta:*', :count => 1000)
|
712
|
-
enum.to_a.
|
955
|
+
expect(enum.to_a).to match_array(matching_subset)
|
713
956
|
end
|
714
957
|
end
|
715
958
|
end
|
@@ -718,17 +961,17 @@ describe "redis" do
|
|
718
961
|
it 'should yield all set elements unchanged' do
|
719
962
|
results = []
|
720
963
|
@namespaced.sscan_each('set', :count => 1000){ |k| results << k }
|
721
|
-
results.
|
964
|
+
expect(results).to match_array(set)
|
722
965
|
end
|
723
966
|
end
|
724
967
|
context 'without a block' do
|
725
968
|
it 'should return an Enumerator that yields all set elements unchanged' do
|
726
969
|
enum = @namespaced.sscan_each('set', :count => 1000)
|
727
|
-
enum.to_a.
|
970
|
+
expect(enum.to_a).to match_array(set)
|
728
971
|
end
|
729
972
|
end
|
730
973
|
end
|
731
|
-
end if Redis.
|
974
|
+
end if Redis.new.respond_to?(:sscan_each)
|
732
975
|
end
|
733
976
|
|
734
977
|
context 'zset scan methods' do
|
@@ -748,17 +991,17 @@ describe "redis" do
|
|
748
991
|
it 'should retrieve the matching set elements and their scores' do
|
749
992
|
results = []
|
750
993
|
@namespaced.zscan_each('zset', :match => 'zeta:*', :count => 1000) { |ms| results << ms }
|
751
|
-
results.
|
994
|
+
expect(results).to match_array(hash_matching_subset.to_a)
|
752
995
|
end
|
753
996
|
end
|
754
997
|
context 'without :match supplied' do
|
755
998
|
it 'should retrieve all set elements and their scores' do
|
756
999
|
results = []
|
757
1000
|
@namespaced.zscan_each('zset', :count => 1000) { |ms| results << ms }
|
758
|
-
results.
|
1001
|
+
expect(results).to match_array(hash.to_a)
|
759
1002
|
end
|
760
1003
|
end
|
761
|
-
end if Redis.
|
1004
|
+
end if Redis.new.respond_to?(:zscan)
|
762
1005
|
|
763
1006
|
context '#zscan_each' do
|
764
1007
|
context 'when :match supplied' do
|
@@ -766,13 +1009,13 @@ describe "redis" do
|
|
766
1009
|
it 'should yield the correct set elements and scores unchanged' do
|
767
1010
|
results = []
|
768
1011
|
@namespaced.zscan_each('zset', :match => 'zeta:*', :count => 1000) { |ms| results << ms}
|
769
|
-
results.
|
1012
|
+
expect(results).to match_array(hash_matching_subset.to_a)
|
770
1013
|
end
|
771
1014
|
end
|
772
1015
|
context 'without a block' do
|
773
1016
|
it 'should return an Enumerator that yields the correct set elements and scoresunchanged' do
|
774
1017
|
enum = @namespaced.zscan_each('zset', :match => 'zeta:*', :count => 1000)
|
775
|
-
enum.to_a.
|
1018
|
+
expect(enum.to_a).to match_array(hash_matching_subset.to_a)
|
776
1019
|
end
|
777
1020
|
end
|
778
1021
|
end
|
@@ -781,17 +1024,17 @@ describe "redis" do
|
|
781
1024
|
it 'should yield all set elements and scores unchanged' do
|
782
1025
|
results = []
|
783
1026
|
@namespaced.zscan_each('zset', :count => 1000){ |ms| results << ms }
|
784
|
-
results.
|
1027
|
+
expect(results).to match_array(hash.to_a)
|
785
1028
|
end
|
786
1029
|
end
|
787
1030
|
context 'without a block' do
|
788
1031
|
it 'should return an Enumerator that yields all set elements and scores unchanged' do
|
789
1032
|
enum = @namespaced.zscan_each('zset', :count => 1000)
|
790
|
-
enum.to_a.
|
1033
|
+
expect(enum.to_a).to match_array(hash.to_a)
|
791
1034
|
end
|
792
1035
|
end
|
793
1036
|
end
|
794
|
-
end if Redis.
|
1037
|
+
end if Redis.new.respond_to?(:zscan_each)
|
795
1038
|
end
|
796
1039
|
end
|
797
1040
|
end
|
@@ -799,12 +1042,12 @@ describe "redis" do
|
|
799
1042
|
if @redis_version >= Gem::Version.new("2.8.9")
|
800
1043
|
it 'should namespace pfadd' do
|
801
1044
|
5.times { |n| @namespaced.pfadd("pf", n) }
|
802
|
-
@redis.pfcount("ns:pf").
|
1045
|
+
expect(@redis.pfcount("ns:pf")).to eq(5)
|
803
1046
|
end
|
804
1047
|
|
805
1048
|
it 'should namespace pfcount' do
|
806
1049
|
5.times { |n| @redis.pfadd("ns:pf", n) }
|
807
|
-
@namespaced.pfcount("pf").
|
1050
|
+
expect(@namespaced.pfcount("pf")).to eq(5)
|
808
1051
|
end
|
809
1052
|
|
810
1053
|
it 'should namespace pfmerge' do
|
@@ -814,7 +1057,44 @@ describe "redis" do
|
|
814
1057
|
end
|
815
1058
|
|
816
1059
|
@namespaced.pfmerge("pfc", "pfa", "pfb")
|
817
|
-
@redis.pfcount("ns:pfc").
|
1060
|
+
expect(@redis.pfcount("ns:pfc")).to eq(10)
|
1061
|
+
end
|
1062
|
+
end
|
1063
|
+
|
1064
|
+
describe :full_namespace do
|
1065
|
+
it "should return the full namespace including sub namespaces" do
|
1066
|
+
sub_namespaced = Redis::Namespace.new(:sub1, :redis => @namespaced)
|
1067
|
+
sub_sub_namespaced = Redis::Namespace.new(:sub2, :redis => sub_namespaced)
|
1068
|
+
|
1069
|
+
expect(@namespaced.full_namespace).to eql("ns")
|
1070
|
+
expect(sub_namespaced.full_namespace).to eql("ns:sub1")
|
1071
|
+
expect(sub_sub_namespaced.full_namespace).to eql("ns:sub1:sub2")
|
1072
|
+
end
|
1073
|
+
end
|
1074
|
+
|
1075
|
+
describe :clear do
|
1076
|
+
it "warns with helpful output" do
|
1077
|
+
expect { @namespaced.clear }.to output(/can run for a very long time/).to_stderr
|
1078
|
+
end
|
1079
|
+
|
1080
|
+
it "should delete all the keys" do
|
1081
|
+
@redis.set("foo", "bar")
|
1082
|
+
@namespaced.mset("foo1", "bar", "foo2", "bar")
|
1083
|
+
capture_stderr { @namespaced.clear }
|
1084
|
+
|
1085
|
+
expect(@redis.keys).to eq ["foo"]
|
1086
|
+
expect(@namespaced.keys).to be_empty
|
1087
|
+
end
|
1088
|
+
|
1089
|
+
it "should delete all the keys in older redis" do
|
1090
|
+
allow(@redis).to receive(:info).and_return({ "redis_version" => "2.7.0" })
|
1091
|
+
|
1092
|
+
@redis.set("foo", "bar")
|
1093
|
+
@namespaced.mset("foo1", "bar", "foo2", "bar")
|
1094
|
+
capture_stderr { @namespaced.clear }
|
1095
|
+
|
1096
|
+
expect(@redis.keys).to eq ["foo"]
|
1097
|
+
expect(@namespaced.keys).to be_empty
|
818
1098
|
end
|
819
1099
|
end
|
820
1100
|
end
|