mock_redis 0.0.1

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.
Files changed (136) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +9 -0
  4. data/LICENSE +19 -0
  5. data/README.md +88 -0
  6. data/Rakefile +10 -0
  7. data/lib/mock_redis/assertions.rb +13 -0
  8. data/lib/mock_redis/database.rb +432 -0
  9. data/lib/mock_redis/exceptions.rb +3 -0
  10. data/lib/mock_redis/expire_wrapper.rb +25 -0
  11. data/lib/mock_redis/hash_methods.rb +102 -0
  12. data/lib/mock_redis/list_methods.rb +187 -0
  13. data/lib/mock_redis/multi_db_wrapper.rb +48 -0
  14. data/lib/mock_redis/set_methods.rb +125 -0
  15. data/lib/mock_redis/string_methods.rb +195 -0
  16. data/lib/mock_redis/transaction_wrapper.rb +75 -0
  17. data/lib/mock_redis/undef_redis_methods.rb +11 -0
  18. data/lib/mock_redis/utility_methods.rb +22 -0
  19. data/lib/mock_redis/version.rb +3 -0
  20. data/lib/mock_redis/zset.rb +80 -0
  21. data/lib/mock_redis/zset_methods.rb +210 -0
  22. data/lib/mock_redis.rb +32 -0
  23. data/mock_redis.gemspec +24 -0
  24. data/spec/cloning_spec.rb +96 -0
  25. data/spec/commands/append_spec.rb +24 -0
  26. data/spec/commands/auth_spec.rb +7 -0
  27. data/spec/commands/bgrewriteaof_spec.rb +7 -0
  28. data/spec/commands/bgsave_spec.rb +7 -0
  29. data/spec/commands/blpop_spec.rb +55 -0
  30. data/spec/commands/brpop_spec.rb +54 -0
  31. data/spec/commands/brpoplpush_spec.rb +53 -0
  32. data/spec/commands/dbsize_spec.rb +18 -0
  33. data/spec/commands/decr_spec.rb +34 -0
  34. data/spec/commands/decrby_spec.rb +34 -0
  35. data/spec/commands/del_spec.rb +20 -0
  36. data/spec/commands/echo_spec.rb +11 -0
  37. data/spec/commands/exists_spec.rb +14 -0
  38. data/spec/commands/expire_spec.rb +83 -0
  39. data/spec/commands/expireat_spec.rb +48 -0
  40. data/spec/commands/flushall_spec.rb +38 -0
  41. data/spec/commands/flushdb_spec.rb +38 -0
  42. data/spec/commands/get_spec.rb +23 -0
  43. data/spec/commands/getbit_spec.rb +34 -0
  44. data/spec/commands/getrange_spec.rb +22 -0
  45. data/spec/commands/getset_spec.rb +23 -0
  46. data/spec/commands/hdel_spec.rb +35 -0
  47. data/spec/commands/hexists_spec.rb +22 -0
  48. data/spec/commands/hget_spec.rb +23 -0
  49. data/spec/commands/hgetall_spec.rb +22 -0
  50. data/spec/commands/hincrby_spec.rb +52 -0
  51. data/spec/commands/hkeys_spec.rb +19 -0
  52. data/spec/commands/hlen_spec.rb +19 -0
  53. data/spec/commands/hmget_spec.rb +30 -0
  54. data/spec/commands/hmset_spec.rb +43 -0
  55. data/spec/commands/hset_spec.rb +23 -0
  56. data/spec/commands/hsetnx_spec.rb +39 -0
  57. data/spec/commands/hvals_spec.rb +19 -0
  58. data/spec/commands/incr_spec.rb +34 -0
  59. data/spec/commands/incrby_spec.rb +44 -0
  60. data/spec/commands/info_spec.rb +13 -0
  61. data/spec/commands/keys_spec.rb +87 -0
  62. data/spec/commands/lastsave_spec.rb +8 -0
  63. data/spec/commands/lindex_spec.rb +39 -0
  64. data/spec/commands/linsert_spec.rb +68 -0
  65. data/spec/commands/llen_spec.rb +16 -0
  66. data/spec/commands/lpop_spec.rb +34 -0
  67. data/spec/commands/lpush_spec.rb +30 -0
  68. data/spec/commands/lpushx_spec.rb +33 -0
  69. data/spec/commands/lrange_spec.rb +35 -0
  70. data/spec/commands/lrem_spec.rb +79 -0
  71. data/spec/commands/lset_spec.rb +38 -0
  72. data/spec/commands/ltrim_spec.rb +35 -0
  73. data/spec/commands/mget_spec.rb +34 -0
  74. data/spec/commands/mset_spec.rb +29 -0
  75. data/spec/commands/msetnx_spec.rb +40 -0
  76. data/spec/commands/persist_spec.rb +49 -0
  77. data/spec/commands/ping_spec.rb +7 -0
  78. data/spec/commands/quit_spec.rb +7 -0
  79. data/spec/commands/randomkey_spec.rb +20 -0
  80. data/spec/commands/rename_spec.rb +31 -0
  81. data/spec/commands/renamenx_spec.rb +36 -0
  82. data/spec/commands/rpop_spec.rb +34 -0
  83. data/spec/commands/rpoplpush_spec.rb +45 -0
  84. data/spec/commands/rpush_spec.rb +30 -0
  85. data/spec/commands/rpushx_spec.rb +33 -0
  86. data/spec/commands/sadd_spec.rb +22 -0
  87. data/spec/commands/save_spec.rb +7 -0
  88. data/spec/commands/scard_spec.rb +18 -0
  89. data/spec/commands/sdiff_spec.rb +47 -0
  90. data/spec/commands/sdiffstore_spec.rb +51 -0
  91. data/spec/commands/select_spec.rb +53 -0
  92. data/spec/commands/set_spec.rb +7 -0
  93. data/spec/commands/setbit_spec.rb +46 -0
  94. data/spec/commands/setex_spec.rb +22 -0
  95. data/spec/commands/setnx_spec.rb +25 -0
  96. data/spec/commands/setrange_spec.rb +30 -0
  97. data/spec/commands/sinter_spec.rb +41 -0
  98. data/spec/commands/sinterstore_spec.rb +46 -0
  99. data/spec/commands/sismember_spec.rb +29 -0
  100. data/spec/commands/smembers_spec.rb +18 -0
  101. data/spec/commands/smove_spec.rb +41 -0
  102. data/spec/commands/spop_spec.rb +25 -0
  103. data/spec/commands/srandmember_spec.rb +25 -0
  104. data/spec/commands/srem_spec.rb +35 -0
  105. data/spec/commands/strlen_spec.rb +19 -0
  106. data/spec/commands/sunion_spec.rb +40 -0
  107. data/spec/commands/sunionstore_spec.rb +46 -0
  108. data/spec/commands/ttl_spec.rb +36 -0
  109. data/spec/commands/type_spec.rb +36 -0
  110. data/spec/commands/unwatch_spec.rb +7 -0
  111. data/spec/commands/watch_spec.rb +7 -0
  112. data/spec/commands/zadd_spec.rb +29 -0
  113. data/spec/commands/zcard_spec.rb +19 -0
  114. data/spec/commands/zcount_spec.rb +23 -0
  115. data/spec/commands/zincrby_spec.rb +24 -0
  116. data/spec/commands/zinterstore_spec.rb +88 -0
  117. data/spec/commands/zrange_spec.rb +31 -0
  118. data/spec/commands/zrangebyscore_spec.rb +42 -0
  119. data/spec/commands/zrank_spec.rb +23 -0
  120. data/spec/commands/zrem_spec.rb +25 -0
  121. data/spec/commands/zremrangebyrank_spec.rb +22 -0
  122. data/spec/commands/zremrangebyscore_spec.rb +21 -0
  123. data/spec/commands/zrevrange_spec.rb +31 -0
  124. data/spec/commands/zrevrangebyscore_spec.rb +42 -0
  125. data/spec/commands/zrevrank_spec.rb +23 -0
  126. data/spec/commands/zscore_spec.rb +16 -0
  127. data/spec/commands/zunionstore_spec.rb +96 -0
  128. data/spec/spec_helper.rb +44 -0
  129. data/spec/support/redis_multiplexer.rb +91 -0
  130. data/spec/support/shared_examples/only_operates_on_hashes.rb +13 -0
  131. data/spec/support/shared_examples/only_operates_on_lists.rb +13 -0
  132. data/spec/support/shared_examples/only_operates_on_sets.rb +13 -0
  133. data/spec/support/shared_examples/only_operates_on_strings.rb +13 -0
  134. data/spec/support/shared_examples/only_operates_on_zsets.rb +57 -0
  135. data/spec/transactions_spec.rb +73 -0
  136. metadata +358 -0
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#lrange(key, start, stop)" do
4
+ before do
5
+ @key = 'mock-redis-test:68036'
6
+
7
+ @redises.lpush(@key, 'v4')
8
+ @redises.lpush(@key, 'v3')
9
+ @redises.lpush(@key, 'v2')
10
+ @redises.lpush(@key, 'v1')
11
+ @redises.lpush(@key, 'v0')
12
+ end
13
+
14
+ it "returns a subset of the list inclusive of the right end" do
15
+ @redises.lrange(@key, 0, 2).should == %w[v0 v1 v2]
16
+ end
17
+
18
+ it "returns an empty list when start > end" do
19
+ @redises.lrange(@key, 3, 2).should == []
20
+ end
21
+
22
+ it "works with negative indices" do
23
+ @redises.lrange(@key, 2, -1).should == %w[v2 v3 v4]
24
+ end
25
+
26
+ it "returns [] when run against a nonexistent value" do
27
+ @redises.lrange("mock-redis-test:bogus-key", 0, 1).should == []
28
+ end
29
+
30
+ it "finds the end of the list correctly when end is too large" do
31
+ @redises.lrange(@key, 4, 10).should == %w[v4]
32
+ end
33
+
34
+ it_should_behave_like "a list-only command"
35
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#lrem(key, count, value)" do
4
+ before do
5
+ @key = 'mock-redis-test:66767'
6
+
7
+ %w[99 bottles of beer on the wall
8
+ 99 bottles of beer
9
+ take one down
10
+ pass it around
11
+ 98 bottles of beer on the wall].reverse.each do |x|
12
+ @redises.lpush(@key, x)
13
+ end
14
+ end
15
+
16
+ it "deletes the first count instances of key when count > 0" do
17
+ @redises.lrem(@key, 2, 'bottles')
18
+
19
+ @redises.lrange(@key, 0, 8).should == %w[
20
+ 99 of beer on the wall
21
+ 99 of beer
22
+ ]
23
+
24
+ @redises.lrange(@key, -7, -1).should == %w[98 bottles of beer on the wall]
25
+ end
26
+
27
+ it "deletes the last count instances of key when count < 0" do
28
+ @redises.lrem(@key, -2, 'bottles')
29
+
30
+ @redises.lrange(@key, 0, 9).should == %w[
31
+ 99 bottles of beer on the wall
32
+ 99 of beer
33
+ ]
34
+
35
+ @redises.lrange(@key, -6, -1).should == %w[98 of beer on the wall]
36
+ end
37
+
38
+ it "deletes all instances of key when count == 0" do
39
+ @redises.lrem(@key, 0, 'bottles')
40
+ @redises.lrange(@key, 0, -1).grep(/bottles/).should be_empty
41
+ end
42
+
43
+ it "returns the number of elements deleted" do
44
+ @redises.lrem(@key, 2, 'bottles').should == 2
45
+ end
46
+
47
+ it "returns the number of elements deleted even if you ask for more" do
48
+ @redises.lrem(@key, 10, 'bottles').should == 3
49
+ end
50
+
51
+ it "stringifies value" do
52
+ @redises.lrem(@key, 0, 99).should == 2
53
+ end
54
+
55
+ it "returns 0 when run against a nonexistent value" do
56
+ @redises.lrem("mock-redis-test:bogus-key", 0, 1).should == 0
57
+ end
58
+
59
+ it "returns 0 when run against an empty list" do
60
+ @redises.llen(@key).times { @redises.lpop(@key) } # empty the list
61
+ @redises.lrem(@key, 0, "beer").should == 0
62
+ end
63
+
64
+ it "treats non-integer count as 0" do
65
+ @redises.lrem(@key, 'foo', 'bottles').should == 3
66
+ @redises.lrange(@key, 0, -1).grep(/bottles/).should be_empty
67
+ end
68
+
69
+ it "removes empty lists" do
70
+ other_key = "mock-redis-test:lrem-#{__LINE__}"
71
+
72
+ @redises.lpush(other_key, 'foo')
73
+ @redises.lrem(other_key, 0, 'foo')
74
+
75
+ @redises.get(other_key).should be_nil
76
+ end
77
+
78
+ it_should_behave_like "a list-only command"
79
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#lset(key, index, value)" do
4
+ before do
5
+ @key = 'mock-redis-test:21522'
6
+
7
+ @redises.lpush(@key, 'v1')
8
+ @redises.lpush(@key, 'v0')
9
+ end
10
+
11
+ it "returns 'OK'" do
12
+ @redises.lset(@key, 0, "newthing").should == 'OK'
13
+ end
14
+
15
+ it "sets the list's value at index to value" do
16
+ @redises.lset(@key, 0, "newthing")
17
+ @redises.lindex(@key, 0).should == "newthing"
18
+ end
19
+
20
+ it "stringifies value" do
21
+ @redises.lset(@key, 0, 12345)
22
+ @redises.lindex(@key, 0).should == "12345"
23
+ end
24
+
25
+ it "raises an exception for nonexistent keys" do
26
+ lambda do
27
+ @redises.lset('mock-redis-test:bogus-key', 100, 'value')
28
+ end.should raise_error(RuntimeError)
29
+ end
30
+
31
+ it "raises an exception for out-of-range indices" do
32
+ lambda do
33
+ @redises.lset(@key, 100, 'value')
34
+ end.should raise_error(RuntimeError)
35
+ end
36
+
37
+ it_should_behave_like "a list-only command"
38
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#ltrim(key, start, stop)" do
4
+ before do
5
+ @key = 'mock-redis-test:22310'
6
+
7
+ %w[v0 v1 v2 v3 v4].reverse.each{|v| @redises.lpush(@key, v)}
8
+ end
9
+
10
+ it "returns 'OK'" do
11
+ @redises.ltrim(@key, 1, 3).should == 'OK'
12
+ end
13
+
14
+ it "trims the list to include only the specified elements" do
15
+ @redises.ltrim(@key, 1, 3)
16
+ @redises.lrange(@key, 0, -1).should == %w[v1 v2 v3]
17
+ end
18
+
19
+ it "trims the list to include only the specified elements (negative indices)" do
20
+ @redises.ltrim(@key, -2, -1)
21
+ @redises.lrange(@key, 0, -1).should == %w[v3 v4]
22
+ end
23
+
24
+ it "does not crash on overly-large indices" do
25
+ @redises.ltrim(@key, 100, 200)
26
+ @redises.lrange(@key, 0, -1).should == %w[]
27
+ end
28
+
29
+ it "removes empty lists" do
30
+ @redises.ltrim(@key, 1, 0)
31
+ @redises.get(@key).should be_nil
32
+ end
33
+
34
+ it_should_behave_like "a list-only command"
35
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#mget(key [, key, ...])" do
4
+ before do
5
+ @key1 = "mock-redis-test:mget1"
6
+ @key2 = "mock-redis-test:mget2"
7
+
8
+ @redises.set(@key1, 1)
9
+ @redises.set(@key2, 2)
10
+ end
11
+
12
+ it "returns an array of values" do
13
+ @redises.mget(@key1, @key2).should == %w[1 2]
14
+ end
15
+
16
+ it "returns nil for missing keys" do
17
+ @redises.mget(@key1, 'mock-redis-test:not-found', @key2).
18
+ should == ["1", nil, "2"]
19
+ end
20
+
21
+ it "returns nil for non-string keys" do
22
+ list = "mock-redis-test:mget-list"
23
+
24
+ @redises.lpush(list, 'bork bork bork')
25
+
26
+ @redises.mget(@key1, @key2, list).should == ["1", "2", nil]
27
+ end
28
+
29
+ it "raises an error if you pass it 0 arguments" do
30
+ lambda do
31
+ @redises.mget()
32
+ end.should raise_error(RuntimeError)
33
+ end
34
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#mset(key, value [, key, value, ...])' do
4
+ before do
5
+ @key1 = 'mock-redis-test:mset1'
6
+ @key2 = 'mock-redis-test:mset2'
7
+ end
8
+
9
+ it "responds with 'OK'" do
10
+ @redises.mset(@key1, 1).should == 'OK'
11
+ end
12
+
13
+ it "sets the values" do
14
+ @redises.mset(@key1, 'value1', @key2, 'value2')
15
+ @redises.mget(@key1, @key2).should == %w[value1 value2]
16
+ end
17
+
18
+ it "raises an error if given an odd number of arguments" do
19
+ lambda do
20
+ @redises.mset(@key1, 'value1', @key2)
21
+ end.should raise_error(RuntimeError)
22
+ end
23
+
24
+ it "raises an error if given 0 arguments" do
25
+ lambda do
26
+ @redises.mset()
27
+ end.should raise_error(RuntimeError)
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#msetnx(key, value [, key, value, ...])' do
4
+ before do
5
+ @key1 = 'mock-redis-test:msetnx1'
6
+ @key2 = 'mock-redis-test:msetnx2'
7
+ end
8
+
9
+ it "responds with 1 if any keys were set" do
10
+ @redises.msetnx(@key1, 1).should == 1
11
+ end
12
+
13
+ it "sets the values" do
14
+ @redises.msetnx(@key1, 'value1', @key2, 'value2')
15
+ @redises.mget(@key1, @key2).should == %w[value1 value2]
16
+ end
17
+
18
+ it "does nothing if any value is already set" do
19
+ @redises.set(@key1, "old1")
20
+ @redises.msetnx(@key1, 'value1', @key2, 'value2')
21
+ @redises.mget(@key1, @key2).should == ["old1", nil]
22
+ end
23
+
24
+ it "responds with 0 if any value is already set" do
25
+ @redises.set(@key1, "old1")
26
+ @redises.msetnx(@key1, 'value1', @key2, 'value2').should == 0
27
+ end
28
+
29
+ it "raises an error if given an odd number of arguments" do
30
+ lambda do
31
+ @redises.msetnx(@key1, 'value1', @key2)
32
+ end.should raise_error(RuntimeError)
33
+ end
34
+
35
+ it "raises an error if given 0 arguments" do
36
+ lambda do
37
+ @redises.msetnx()
38
+ end.should raise_error(RuntimeError)
39
+ end
40
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#persist(key)" do
4
+ before do
5
+ @key = 'mock-redis-test:persist'
6
+ @redises.set(@key, 'spork')
7
+ end
8
+
9
+ it "returns true for a key with a timeout" do
10
+ @redises.expire(@key, 10000)
11
+ @redises.persist(@key).should be_true
12
+ end
13
+
14
+ it "returns false for a key with no timeout" do
15
+ @redises.persist(@key).should be_false
16
+ end
17
+
18
+ it "returns false for a key that does not exist" do
19
+ @redises.persist('mock-redis-test:nonesuch').should be_false
20
+ end
21
+
22
+ it "removes the timeout" do
23
+ @redises.expire(@key, 10000)
24
+ @redises.persist(@key)
25
+ @redises.persist(@key).should be_false
26
+ end
27
+
28
+ context "[mock only]" do
29
+ # These are mock-only since we can't actually manipulate time in
30
+ # the real Redis.
31
+
32
+ before(:all) do
33
+ @mock = @redises.mock
34
+ end
35
+
36
+ before do
37
+ @now = Time.now
38
+ Time.stub!(:now).and_return(@now)
39
+ end
40
+
41
+ it "makes keys stay around" do
42
+ @mock.expire(@key, 5)
43
+ @mock.persist(@key)
44
+ Time.stub!(:now).and_return(@now + 5)
45
+ @mock.get(@key).should_not be_nil
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#ping" do
4
+ it "returns 'PONG'" do
5
+ @redises.ping.should == 'PONG'
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#quit' do
4
+ it "responds with 'OK'" do
5
+ @redises.quit.should == 'OK'
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#randomkey [mock only]' do
4
+ before { @mock = @redises.mock }
5
+
6
+ it "finds a random key" do
7
+ @keys = ['mock-redis-test:1', 'mock-redis-test:2', 'mock-redis-test:3']
8
+
9
+ @keys.each do |k|
10
+ @mock.set(k, 1)
11
+ end
12
+
13
+ @keys.should include(@mock.randomkey)
14
+ end
15
+
16
+ it "returns nil when there are no keys" do
17
+ @mock.keys('*').each {|k| @mock.del(k)}
18
+ @mock.randomkey.should be_nil
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#rename(key, newkey)' do
4
+ before do
5
+ @key = 'mock-redis-test:rename:key'
6
+ @newkey = 'mock-redis-test:rename:newkey'
7
+
8
+ @redises.set(@key, "oof")
9
+ end
10
+
11
+ it "responds with 'OK'" do
12
+ @redises.rename(@key, @newkey).should == 'OK'
13
+ end
14
+
15
+ it "moves the data" do
16
+ @redises.rename(@key, @newkey)
17
+ @redises.get(@newkey).should == "oof"
18
+ end
19
+
20
+ it "raises an error when key == newkey" do
21
+ lambda do
22
+ @redises.rename(@key, @key)
23
+ end.should raise_error(RuntimeError)
24
+ end
25
+
26
+ it "overwrites any existing value at newkey" do
27
+ @redises.set(@newkey, "rab")
28
+ @redises.rename(@key, @newkey)
29
+ @redises.get(@newkey).should == "oof"
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#renamenx(key, newkey)' do
4
+ before do
5
+ @key = 'mock-redis-test:renamenx:key'
6
+ @newkey = 'mock-redis-test:renamenx:newkey'
7
+
8
+ @redises.set(@key, "oof")
9
+ end
10
+
11
+ it "responds with true when newkey does not exist" do
12
+ @redises.renamenx(@key, @newkey).should be_true
13
+ end
14
+
15
+ it "responds with false when newkey exists" do
16
+ @redises.set(@newkey, 'monkey')
17
+ @redises.renamenx(@key, @newkey).should be_false
18
+ end
19
+
20
+ it "moves the data" do
21
+ @redises.renamenx(@key, @newkey)
22
+ @redises.get(@newkey).should == "oof"
23
+ end
24
+
25
+ it "raises an error when key == newkey" do
26
+ lambda do
27
+ @redises.renamenx(@key, @key)
28
+ end.should raise_error(RuntimeError)
29
+ end
30
+
31
+ it "leaves any existing value at newkey alone" do
32
+ @redises.set(@newkey, "rab")
33
+ @redises.renamenx(@key, @newkey)
34
+ @redises.get(@newkey).should == "rab"
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#rpop(key)' do
4
+ before { @key = 'mock-redis-test:43093' }
5
+
6
+ it "returns and removes the first element of a list" do
7
+ @redises.lpush(@key, 1)
8
+ @redises.lpush(@key, 2)
9
+
10
+ @redises.rpop(@key).should == "1"
11
+
12
+ @redises.llen(@key).should == 1
13
+ end
14
+
15
+ it "returns nil if the list is empty" do
16
+ @redises.lpush(@key, 'foo')
17
+ @redises.rpop(@key)
18
+
19
+ @redises.rpop(@key).should be_nil
20
+ end
21
+
22
+ it "returns nil for nonexistent values" do
23
+ @redises.rpop(@key).should be_nil
24
+ end
25
+
26
+ it "removes empty lists" do
27
+ @redises.lpush(@key, 'foo')
28
+ @redises.rpop(@key)
29
+
30
+ @redises.get(@key).should be_nil
31
+ end
32
+
33
+ it_should_behave_like "a list-only command"
34
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#rpoplpush(source, destination)' do
4
+ before do
5
+ @list1 = 'mock-redis-test:rpoplpush-list1'
6
+ @list2 = 'mock-redis-test:rpoplpush-list2'
7
+
8
+ @redises.lpush(@list1, 'b')
9
+ @redises.lpush(@list1, 'a')
10
+
11
+ @redises.lpush(@list2, 'y')
12
+ @redises.lpush(@list2, 'x')
13
+ end
14
+
15
+ it "returns the value moved" do
16
+ @redises.rpoplpush(@list1, @list2).should == "b"
17
+ end
18
+
19
+ it "takes the last element of destination and prepends it to source" do
20
+ @redises.rpoplpush(@list1, @list2)
21
+
22
+ @redises.lrange(@list1, 0, -1).should == %w[a]
23
+ @redises.lrange(@list2, 0, -1).should == %w[b x y]
24
+ end
25
+
26
+ it "rotates a list when source and destination are the same" do
27
+ @redises.rpoplpush(@list1, @list1)
28
+ @redises.lrange(@list1, 0, -1).should == %w[b a]
29
+ end
30
+
31
+ it "removes empty lists" do
32
+ @redises.llen(@list1).times { @redises.rpoplpush(@list1, @list2) }
33
+ @redises.get(@list1).should be_nil
34
+ end
35
+
36
+ it "raises an error for non-list source value" do
37
+ @redises.set(@list1, 'string value')
38
+
39
+ lambda do
40
+ @redises.rpoplpush(@list1, @list2)
41
+ end.should raise_error(RuntimeError)
42
+ end
43
+
44
+ it_should_behave_like "a list-only command"
45
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#rpush(key)" do
4
+ before { @key = "mock-redis-test:#{__FILE__}" }
5
+
6
+ it "returns the new size of the list" do
7
+ @redises.rpush(@key, 'X').should == 1
8
+ @redises.rpush(@key, 'X').should == 2
9
+ end
10
+
11
+ it "creates a new list when run against a nonexistent key" do
12
+ @redises.rpush(@key, 'value')
13
+ @redises.llen(@key).should == 1
14
+ end
15
+
16
+ it "appends items to the list" do
17
+ @redises.rpush(@key, "bert")
18
+ @redises.rpush(@key, "ernie")
19
+
20
+ @redises.lindex(@key, 0).should == "bert"
21
+ @redises.lindex(@key, 1).should == "ernie"
22
+ end
23
+
24
+ it "stores values as strings" do
25
+ @redises.rpush(@key, 1)
26
+ @redises.lindex(@key, 0).should == "1"
27
+ end
28
+
29
+ it_should_behave_like "a list-only command"
30
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#rpushx(key, value)" do
4
+ before { @key = 'mock-redis-test:92925' }
5
+
6
+ it "returns the new size of the list" do
7
+ @redises.lpush(@key, 'X')
8
+
9
+ @redises.rpushx(@key, 'X').should == 2
10
+ @redises.rpushx(@key, 'X').should == 3
11
+ end
12
+
13
+ it "does nothing when run against a nonexistent key" do
14
+ @redises.rpushx(@key, 'value')
15
+ @redises.get(@key).should be_nil
16
+ end
17
+
18
+ it "appends items to the list" do
19
+ @redises.lpush(@key, "bert")
20
+ @redises.rpushx(@key, "ernie")
21
+
22
+ @redises.lindex(@key, 0).should == "bert"
23
+ @redises.lindex(@key, 1).should == "ernie"
24
+ end
25
+
26
+ it "stores values as strings" do
27
+ @redises.rpush(@key, 1)
28
+ @redises.rpushx(@key, 2)
29
+ @redises.lindex(@key, 1).should == "2"
30
+ end
31
+
32
+ it_should_behave_like "a list-only command"
33
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#sadd(key, member)' do
4
+ before { @key = 'mock-redis-test:sadd' }
5
+
6
+ it "returns true if the set did not already contain member" do
7
+ @redises.sadd(@key, 1).should be_true
8
+ end
9
+
10
+ it "returns false if the set did already contain member" do
11
+ @redises.sadd(@key, 1)
12
+ @redises.sadd(@key, 1).should be_false
13
+ end
14
+
15
+ it "adds member to the set" do
16
+ @redises.sadd(@key, 1)
17
+ @redises.sadd(@key, 2)
18
+ @redises.smembers(@key).should == %w[1 2]
19
+ end
20
+
21
+ it_should_behave_like "a set-only command"
22
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#save' do
4
+ it "responds with 'OK'" do
5
+ @redises.save.should == 'OK'
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#scard(key)' do
4
+ before { @key = 'mock-redis-test:scard' }
5
+
6
+ it "returns 0 for an empty set" do
7
+ @redises.scard(@key).should == 0
8
+ end
9
+
10
+ it "returns the number of elements in the set" do
11
+ @redises.sadd(@key, 'one')
12
+ @redises.sadd(@key, 'two')
13
+ @redises.sadd(@key, 'three')
14
+ @redises.scard(@key).should == 3
15
+ end
16
+
17
+ it_should_behave_like "a set-only command"
18
+ end