mock_redis 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -4
- data/lib/mock_redis/multi_db_wrapper.rb +38 -0
- data/lib/mock_redis/version.rb +1 -1
- data/spec/commands/move_spec.rb +147 -0
- metadata +143 -6
data/README.md
CHANGED
@@ -83,7 +83,12 @@ please submit a pull request with your (tested!) implementation.
|
|
83
83
|
validation will be a pain in the butt, but other than that, it
|
84
84
|
shouldn't be too bad.
|
85
85
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
86
|
+
## Running the Tests
|
87
|
+
|
88
|
+
If you want to work on this, you'll probably want to run the
|
89
|
+
tests. (Just kidding! There's no probably about it.) These tests were
|
90
|
+
written with Redis 2.2.11 running on localhost without any passwords
|
91
|
+
required. If you're using a different version of Redis, you may see
|
92
|
+
failures due to error message text being different. If you're running
|
93
|
+
a really old version of Redis, you'll definitely see failures due to
|
94
|
+
stuff that doesn't work!
|
@@ -35,6 +35,40 @@ class MockRedis
|
|
35
35
|
'OK'
|
36
36
|
end
|
37
37
|
|
38
|
+
def move(key, db_index)
|
39
|
+
src = current_db
|
40
|
+
dest = db(db_index)
|
41
|
+
|
42
|
+
if !src.exists(key) || dest.exists(key)
|
43
|
+
false
|
44
|
+
else
|
45
|
+
case current_db.type(key)
|
46
|
+
when 'hash'
|
47
|
+
dest.hmset(key, *(src.hgetall(key).map{|k,v| [k,v]}.flatten))
|
48
|
+
when 'list'
|
49
|
+
while value = src.rpop(key)
|
50
|
+
dest.lpush(key, value)
|
51
|
+
end
|
52
|
+
when 'set'
|
53
|
+
while value = src.spop(key)
|
54
|
+
dest.sadd(key, value)
|
55
|
+
end
|
56
|
+
when 'string'
|
57
|
+
dest.set(key, src.get(key))
|
58
|
+
when 'zset'
|
59
|
+
src.zrange(key, 0, -1, :with_scores => true).each_slice(2) do |(m,s)|
|
60
|
+
dest.zadd(key, s, m)
|
61
|
+
end
|
62
|
+
else
|
63
|
+
raise ArgumentError,
|
64
|
+
"Can't move a key of type #{current_db.type(key).inspect}"
|
65
|
+
end
|
66
|
+
|
67
|
+
src.del(key)
|
68
|
+
true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
38
72
|
def select(db_index)
|
39
73
|
@db_index = db_index.to_i
|
40
74
|
'OK'
|
@@ -44,5 +78,9 @@ class MockRedis
|
|
44
78
|
def current_db
|
45
79
|
@databases[@db_index]
|
46
80
|
end
|
81
|
+
|
82
|
+
def db(index)
|
83
|
+
@databases[index]
|
84
|
+
end
|
47
85
|
end
|
48
86
|
end
|
data/lib/mock_redis/version.rb
CHANGED
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#move(key, db)' do
|
4
|
+
before do
|
5
|
+
@srcdb = 0
|
6
|
+
@destdb = 1
|
7
|
+
|
8
|
+
@key = 'mock-redis-test:move'
|
9
|
+
end
|
10
|
+
|
11
|
+
context "when key exists in destdb" do
|
12
|
+
before do
|
13
|
+
@redises.set(@key, 'srcvalue')
|
14
|
+
@redises.select(@destdb)
|
15
|
+
@redises.set(@key, 'destvalue')
|
16
|
+
@redises.select(@srcdb)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns false" do
|
20
|
+
@redises.move(@key, @destdb).should be_false
|
21
|
+
end
|
22
|
+
|
23
|
+
it "leaves destdb/key alone" do
|
24
|
+
@redises.select(@destdb)
|
25
|
+
@redises.get(@key).should == "destvalue"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "leaves srcdb/key alone" do
|
29
|
+
@redises.get(@key).should == "srcvalue"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when key does not exist in srcdb" do
|
34
|
+
before do
|
35
|
+
@redises.select(@destdb)
|
36
|
+
@redises.set(@key, 'destvalue')
|
37
|
+
@redises.select(@srcdb)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "returns false" do
|
41
|
+
@redises.move(@key, @destdb).should be_false
|
42
|
+
end
|
43
|
+
|
44
|
+
it "leaves destdb/key alone" do
|
45
|
+
@redises.select(@destdb)
|
46
|
+
@redises.get(@key).should == "destvalue"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when key exists in the currently-selected DB and not in db" do
|
51
|
+
before do
|
52
|
+
@redises.set(@key, 'value')
|
53
|
+
end
|
54
|
+
|
55
|
+
it "returns true" do
|
56
|
+
@redises.move(@key, @destdb).should be_true
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "on a string" do
|
61
|
+
before do
|
62
|
+
@redises.set(@key, 'value')
|
63
|
+
@redises.move(@key, @destdb)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "removes key from srcdb" do
|
67
|
+
@redises.exists(@key).should be_false
|
68
|
+
end
|
69
|
+
|
70
|
+
it "copies key to destdb" do
|
71
|
+
@redises.select(@destdb)
|
72
|
+
@redises.get(@key).should == 'value'
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "on a list" do
|
77
|
+
before do
|
78
|
+
@redises.rpush(@key, 'bert')
|
79
|
+
@redises.rpush(@key, 'ernie')
|
80
|
+
@redises.move(@key, @destdb)
|
81
|
+
end
|
82
|
+
|
83
|
+
it "removes key from srcdb" do
|
84
|
+
@redises.exists(@key).should be_false
|
85
|
+
end
|
86
|
+
|
87
|
+
it "copies key to destdb" do
|
88
|
+
@redises.select(@destdb)
|
89
|
+
@redises.lrange(@key, 0, -1).should == %w[bert ernie]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "on a hash" do
|
94
|
+
before do
|
95
|
+
@redises.hset(@key, 'a', 1)
|
96
|
+
@redises.hset(@key, 'b', 2)
|
97
|
+
|
98
|
+
@redises.move(@key, @destdb)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "removes key from srcdb" do
|
102
|
+
@redises.exists(@key).should be_false
|
103
|
+
end
|
104
|
+
|
105
|
+
it "copies key to destdb" do
|
106
|
+
@redises.select(@destdb)
|
107
|
+
@redises.hgetall(@key).should == {'a' => "1", 'b' => "2"}
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "on a set" do
|
112
|
+
before do
|
113
|
+
@redises.sadd(@key, 'beer')
|
114
|
+
@redises.sadd(@key, 'wine')
|
115
|
+
|
116
|
+
@redises.move(@key, @destdb)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "removes key from srcdb" do
|
120
|
+
@redises.exists(@key).should be_false
|
121
|
+
end
|
122
|
+
|
123
|
+
it "copies key to destdb" do
|
124
|
+
@redises.select(@destdb)
|
125
|
+
@redises.smembers(@key).should == %w[beer wine]
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context "on a zset" do
|
130
|
+
before do
|
131
|
+
@redises.zadd(@key, 1, 'beer')
|
132
|
+
@redises.zadd(@key, 2, 'wine')
|
133
|
+
|
134
|
+
@redises.move(@key, @destdb)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "removes key from srcdb" do
|
138
|
+
@redises.exists(@key).should be_false
|
139
|
+
end
|
140
|
+
|
141
|
+
it "copies key to destdb" do
|
142
|
+
@redises.select(@destdb)
|
143
|
+
@redises.zrange(@key, 0, -1, :with_scores => true).should ==
|
144
|
+
%w[beer 1 wine 2]
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mock_redis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- Samuel Merritt
|
@@ -10,7 +15,7 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date: 2011-07-
|
18
|
+
date: 2011-07-18 00:00:00 -07:00
|
14
19
|
default_executable:
|
15
20
|
dependencies:
|
16
21
|
- !ruby/object:Gem::Dependency
|
@@ -21,6 +26,11 @@ dependencies:
|
|
21
26
|
requirements:
|
22
27
|
- - ~>
|
23
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 5
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 2
|
33
|
+
- 1
|
24
34
|
version: 2.2.1
|
25
35
|
type: :development
|
26
36
|
version_requirements: *id001
|
@@ -32,6 +42,11 @@ dependencies:
|
|
32
42
|
requirements:
|
33
43
|
- - ~>
|
34
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 23
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 6
|
49
|
+
- 0
|
35
50
|
version: 2.6.0
|
36
51
|
type: :development
|
37
52
|
version_requirements: *id002
|
@@ -43,6 +58,9 @@ dependencies:
|
|
43
58
|
requirements:
|
44
59
|
- - ">="
|
45
60
|
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
46
64
|
version: "0"
|
47
65
|
type: :development
|
48
66
|
version_requirements: *id003
|
@@ -129,6 +147,7 @@ files:
|
|
129
147
|
- spec/commands/lset_spec.rb
|
130
148
|
- spec/commands/ltrim_spec.rb
|
131
149
|
- spec/commands/mget_spec.rb
|
150
|
+
- spec/commands/move_spec.rb
|
132
151
|
- spec/commands/mset_spec.rb
|
133
152
|
- spec/commands/msetnx_spec.rb
|
134
153
|
- spec/commands/persist_spec.rb
|
@@ -205,19 +224,137 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
205
224
|
requirements:
|
206
225
|
- - ">="
|
207
226
|
- !ruby/object:Gem::Version
|
227
|
+
hash: 3
|
228
|
+
segments:
|
229
|
+
- 0
|
208
230
|
version: "0"
|
209
231
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
210
232
|
none: false
|
211
233
|
requirements:
|
212
234
|
- - ">="
|
213
235
|
- !ruby/object:Gem::Version
|
236
|
+
hash: 3
|
237
|
+
segments:
|
238
|
+
- 0
|
214
239
|
version: "0"
|
215
240
|
requirements: []
|
216
241
|
|
217
242
|
rubyforge_project:
|
218
|
-
rubygems_version: 1.
|
243
|
+
rubygems_version: 1.3.7
|
219
244
|
signing_key:
|
220
245
|
specification_version: 3
|
221
246
|
summary: Redis mock that just lives in memory; useful for testing.
|
222
|
-
test_files:
|
223
|
-
|
247
|
+
test_files:
|
248
|
+
- spec/cloning_spec.rb
|
249
|
+
- spec/commands/append_spec.rb
|
250
|
+
- spec/commands/auth_spec.rb
|
251
|
+
- spec/commands/bgrewriteaof_spec.rb
|
252
|
+
- spec/commands/bgsave_spec.rb
|
253
|
+
- spec/commands/blpop_spec.rb
|
254
|
+
- spec/commands/brpop_spec.rb
|
255
|
+
- spec/commands/brpoplpush_spec.rb
|
256
|
+
- spec/commands/dbsize_spec.rb
|
257
|
+
- spec/commands/decr_spec.rb
|
258
|
+
- spec/commands/decrby_spec.rb
|
259
|
+
- spec/commands/del_spec.rb
|
260
|
+
- spec/commands/echo_spec.rb
|
261
|
+
- spec/commands/exists_spec.rb
|
262
|
+
- spec/commands/expire_spec.rb
|
263
|
+
- spec/commands/expireat_spec.rb
|
264
|
+
- spec/commands/flushall_spec.rb
|
265
|
+
- spec/commands/flushdb_spec.rb
|
266
|
+
- spec/commands/get_spec.rb
|
267
|
+
- spec/commands/getbit_spec.rb
|
268
|
+
- spec/commands/getrange_spec.rb
|
269
|
+
- spec/commands/getset_spec.rb
|
270
|
+
- spec/commands/hdel_spec.rb
|
271
|
+
- spec/commands/hexists_spec.rb
|
272
|
+
- spec/commands/hget_spec.rb
|
273
|
+
- spec/commands/hgetall_spec.rb
|
274
|
+
- spec/commands/hincrby_spec.rb
|
275
|
+
- spec/commands/hkeys_spec.rb
|
276
|
+
- spec/commands/hlen_spec.rb
|
277
|
+
- spec/commands/hmget_spec.rb
|
278
|
+
- spec/commands/hmset_spec.rb
|
279
|
+
- spec/commands/hset_spec.rb
|
280
|
+
- spec/commands/hsetnx_spec.rb
|
281
|
+
- spec/commands/hvals_spec.rb
|
282
|
+
- spec/commands/incr_spec.rb
|
283
|
+
- spec/commands/incrby_spec.rb
|
284
|
+
- spec/commands/info_spec.rb
|
285
|
+
- spec/commands/keys_spec.rb
|
286
|
+
- spec/commands/lastsave_spec.rb
|
287
|
+
- spec/commands/lindex_spec.rb
|
288
|
+
- spec/commands/linsert_spec.rb
|
289
|
+
- spec/commands/llen_spec.rb
|
290
|
+
- spec/commands/lpop_spec.rb
|
291
|
+
- spec/commands/lpush_spec.rb
|
292
|
+
- spec/commands/lpushx_spec.rb
|
293
|
+
- spec/commands/lrange_spec.rb
|
294
|
+
- spec/commands/lrem_spec.rb
|
295
|
+
- spec/commands/lset_spec.rb
|
296
|
+
- spec/commands/ltrim_spec.rb
|
297
|
+
- spec/commands/mget_spec.rb
|
298
|
+
- spec/commands/move_spec.rb
|
299
|
+
- spec/commands/mset_spec.rb
|
300
|
+
- spec/commands/msetnx_spec.rb
|
301
|
+
- spec/commands/persist_spec.rb
|
302
|
+
- spec/commands/ping_spec.rb
|
303
|
+
- spec/commands/quit_spec.rb
|
304
|
+
- spec/commands/randomkey_spec.rb
|
305
|
+
- spec/commands/rename_spec.rb
|
306
|
+
- spec/commands/renamenx_spec.rb
|
307
|
+
- spec/commands/rpop_spec.rb
|
308
|
+
- spec/commands/rpoplpush_spec.rb
|
309
|
+
- spec/commands/rpush_spec.rb
|
310
|
+
- spec/commands/rpushx_spec.rb
|
311
|
+
- spec/commands/sadd_spec.rb
|
312
|
+
- spec/commands/save_spec.rb
|
313
|
+
- spec/commands/scard_spec.rb
|
314
|
+
- spec/commands/sdiff_spec.rb
|
315
|
+
- spec/commands/sdiffstore_spec.rb
|
316
|
+
- spec/commands/select_spec.rb
|
317
|
+
- spec/commands/set_spec.rb
|
318
|
+
- spec/commands/setbit_spec.rb
|
319
|
+
- spec/commands/setex_spec.rb
|
320
|
+
- spec/commands/setnx_spec.rb
|
321
|
+
- spec/commands/setrange_spec.rb
|
322
|
+
- spec/commands/sinter_spec.rb
|
323
|
+
- spec/commands/sinterstore_spec.rb
|
324
|
+
- spec/commands/sismember_spec.rb
|
325
|
+
- spec/commands/smembers_spec.rb
|
326
|
+
- spec/commands/smove_spec.rb
|
327
|
+
- spec/commands/spop_spec.rb
|
328
|
+
- spec/commands/srandmember_spec.rb
|
329
|
+
- spec/commands/srem_spec.rb
|
330
|
+
- spec/commands/strlen_spec.rb
|
331
|
+
- spec/commands/sunion_spec.rb
|
332
|
+
- spec/commands/sunionstore_spec.rb
|
333
|
+
- spec/commands/ttl_spec.rb
|
334
|
+
- spec/commands/type_spec.rb
|
335
|
+
- spec/commands/unwatch_spec.rb
|
336
|
+
- spec/commands/watch_spec.rb
|
337
|
+
- spec/commands/zadd_spec.rb
|
338
|
+
- spec/commands/zcard_spec.rb
|
339
|
+
- spec/commands/zcount_spec.rb
|
340
|
+
- spec/commands/zincrby_spec.rb
|
341
|
+
- spec/commands/zinterstore_spec.rb
|
342
|
+
- spec/commands/zrange_spec.rb
|
343
|
+
- spec/commands/zrangebyscore_spec.rb
|
344
|
+
- spec/commands/zrank_spec.rb
|
345
|
+
- spec/commands/zrem_spec.rb
|
346
|
+
- spec/commands/zremrangebyrank_spec.rb
|
347
|
+
- spec/commands/zremrangebyscore_spec.rb
|
348
|
+
- spec/commands/zrevrange_spec.rb
|
349
|
+
- spec/commands/zrevrangebyscore_spec.rb
|
350
|
+
- spec/commands/zrevrank_spec.rb
|
351
|
+
- spec/commands/zscore_spec.rb
|
352
|
+
- spec/commands/zunionstore_spec.rb
|
353
|
+
- spec/spec_helper.rb
|
354
|
+
- spec/support/redis_multiplexer.rb
|
355
|
+
- spec/support/shared_examples/only_operates_on_hashes.rb
|
356
|
+
- spec/support/shared_examples/only_operates_on_lists.rb
|
357
|
+
- spec/support/shared_examples/only_operates_on_sets.rb
|
358
|
+
- spec/support/shared_examples/only_operates_on_strings.rb
|
359
|
+
- spec/support/shared_examples/only_operates_on_zsets.rb
|
360
|
+
- spec/transactions_spec.rb
|