ryansch-mock_redis 0.2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +1 -0
  3. data/CHANGELOG.md +20 -0
  4. data/Gemfile +9 -0
  5. data/LICENSE +19 -0
  6. data/README.md +94 -0
  7. data/Rakefile +10 -0
  8. data/lib/mock_redis.rb +32 -0
  9. data/lib/mock_redis/assertions.rb +13 -0
  10. data/lib/mock_redis/database.rb +432 -0
  11. data/lib/mock_redis/exceptions.rb +3 -0
  12. data/lib/mock_redis/expire_wrapper.rb +25 -0
  13. data/lib/mock_redis/hash_methods.rb +102 -0
  14. data/lib/mock_redis/list_methods.rb +187 -0
  15. data/lib/mock_redis/multi_db_wrapper.rb +86 -0
  16. data/lib/mock_redis/set_methods.rb +125 -0
  17. data/lib/mock_redis/string_methods.rb +195 -0
  18. data/lib/mock_redis/transaction_wrapper.rb +80 -0
  19. data/lib/mock_redis/undef_redis_methods.rb +11 -0
  20. data/lib/mock_redis/utility_methods.rb +22 -0
  21. data/lib/mock_redis/version.rb +3 -0
  22. data/lib/mock_redis/zset.rb +110 -0
  23. data/lib/mock_redis/zset_methods.rb +209 -0
  24. data/mock_redis.gemspec +24 -0
  25. data/spec/cloning_spec.rb +96 -0
  26. data/spec/commands/append_spec.rb +24 -0
  27. data/spec/commands/auth_spec.rb +7 -0
  28. data/spec/commands/bgrewriteaof_spec.rb +7 -0
  29. data/spec/commands/bgsave_spec.rb +7 -0
  30. data/spec/commands/blpop_spec.rb +55 -0
  31. data/spec/commands/brpop_spec.rb +54 -0
  32. data/spec/commands/brpoplpush_spec.rb +53 -0
  33. data/spec/commands/dbsize_spec.rb +18 -0
  34. data/spec/commands/decr_spec.rb +34 -0
  35. data/spec/commands/decrby_spec.rb +34 -0
  36. data/spec/commands/del_spec.rb +20 -0
  37. data/spec/commands/echo_spec.rb +11 -0
  38. data/spec/commands/exists_spec.rb +14 -0
  39. data/spec/commands/expire_spec.rb +83 -0
  40. data/spec/commands/expireat_spec.rb +48 -0
  41. data/spec/commands/flushall_spec.rb +38 -0
  42. data/spec/commands/flushdb_spec.rb +38 -0
  43. data/spec/commands/get_spec.rb +23 -0
  44. data/spec/commands/getbit_spec.rb +34 -0
  45. data/spec/commands/getrange_spec.rb +22 -0
  46. data/spec/commands/getset_spec.rb +23 -0
  47. data/spec/commands/hdel_spec.rb +35 -0
  48. data/spec/commands/hexists_spec.rb +22 -0
  49. data/spec/commands/hget_spec.rb +23 -0
  50. data/spec/commands/hgetall_spec.rb +22 -0
  51. data/spec/commands/hincrby_spec.rb +52 -0
  52. data/spec/commands/hkeys_spec.rb +19 -0
  53. data/spec/commands/hlen_spec.rb +19 -0
  54. data/spec/commands/hmget_spec.rb +30 -0
  55. data/spec/commands/hmset_spec.rb +43 -0
  56. data/spec/commands/hset_spec.rb +23 -0
  57. data/spec/commands/hsetnx_spec.rb +39 -0
  58. data/spec/commands/hvals_spec.rb +19 -0
  59. data/spec/commands/incr_spec.rb +34 -0
  60. data/spec/commands/incrby_spec.rb +44 -0
  61. data/spec/commands/info_spec.rb +13 -0
  62. data/spec/commands/keys_spec.rb +87 -0
  63. data/spec/commands/lastsave_spec.rb +8 -0
  64. data/spec/commands/lindex_spec.rb +39 -0
  65. data/spec/commands/linsert_spec.rb +68 -0
  66. data/spec/commands/llen_spec.rb +16 -0
  67. data/spec/commands/lpop_spec.rb +34 -0
  68. data/spec/commands/lpush_spec.rb +30 -0
  69. data/spec/commands/lpushx_spec.rb +33 -0
  70. data/spec/commands/lrange_spec.rb +35 -0
  71. data/spec/commands/lrem_spec.rb +79 -0
  72. data/spec/commands/lset_spec.rb +38 -0
  73. data/spec/commands/ltrim_spec.rb +35 -0
  74. data/spec/commands/mget_spec.rb +34 -0
  75. data/spec/commands/move_spec.rb +147 -0
  76. data/spec/commands/mset_spec.rb +29 -0
  77. data/spec/commands/msetnx_spec.rb +40 -0
  78. data/spec/commands/persist_spec.rb +49 -0
  79. data/spec/commands/ping_spec.rb +7 -0
  80. data/spec/commands/quit_spec.rb +7 -0
  81. data/spec/commands/randomkey_spec.rb +20 -0
  82. data/spec/commands/rename_spec.rb +31 -0
  83. data/spec/commands/renamenx_spec.rb +36 -0
  84. data/spec/commands/rpop_spec.rb +34 -0
  85. data/spec/commands/rpoplpush_spec.rb +45 -0
  86. data/spec/commands/rpush_spec.rb +30 -0
  87. data/spec/commands/rpushx_spec.rb +33 -0
  88. data/spec/commands/sadd_spec.rb +22 -0
  89. data/spec/commands/save_spec.rb +7 -0
  90. data/spec/commands/scard_spec.rb +18 -0
  91. data/spec/commands/sdiff_spec.rb +47 -0
  92. data/spec/commands/sdiffstore_spec.rb +58 -0
  93. data/spec/commands/select_spec.rb +53 -0
  94. data/spec/commands/set_spec.rb +7 -0
  95. data/spec/commands/setbit_spec.rb +46 -0
  96. data/spec/commands/setex_spec.rb +22 -0
  97. data/spec/commands/setnx_spec.rb +25 -0
  98. data/spec/commands/setrange_spec.rb +30 -0
  99. data/spec/commands/sinter_spec.rb +41 -0
  100. data/spec/commands/sinterstore_spec.rb +53 -0
  101. data/spec/commands/sismember_spec.rb +29 -0
  102. data/spec/commands/smembers_spec.rb +18 -0
  103. data/spec/commands/smove_spec.rb +41 -0
  104. data/spec/commands/spop_spec.rb +25 -0
  105. data/spec/commands/srandmember_spec.rb +25 -0
  106. data/spec/commands/srem_spec.rb +35 -0
  107. data/spec/commands/strlen_spec.rb +19 -0
  108. data/spec/commands/sunion_spec.rb +40 -0
  109. data/spec/commands/sunionstore_spec.rb +53 -0
  110. data/spec/commands/ttl_spec.rb +36 -0
  111. data/spec/commands/type_spec.rb +36 -0
  112. data/spec/commands/unwatch_spec.rb +7 -0
  113. data/spec/commands/watch_spec.rb +7 -0
  114. data/spec/commands/zadd_spec.rb +29 -0
  115. data/spec/commands/zcard_spec.rb +19 -0
  116. data/spec/commands/zcount_spec.rb +23 -0
  117. data/spec/commands/zincrby_spec.rb +24 -0
  118. data/spec/commands/zinterstore_spec.rb +96 -0
  119. data/spec/commands/zrange_spec.rb +31 -0
  120. data/spec/commands/zrangebyscore_spec.rb +68 -0
  121. data/spec/commands/zrank_spec.rb +23 -0
  122. data/spec/commands/zrem_spec.rb +25 -0
  123. data/spec/commands/zremrangebyrank_spec.rb +22 -0
  124. data/spec/commands/zremrangebyscore_spec.rb +28 -0
  125. data/spec/commands/zrevrange_spec.rb +31 -0
  126. data/spec/commands/zrevrangebyscore_spec.rb +47 -0
  127. data/spec/commands/zrevrank_spec.rb +23 -0
  128. data/spec/commands/zscore_spec.rb +16 -0
  129. data/spec/commands/zunionstore_spec.rb +104 -0
  130. data/spec/spec_helper.rb +44 -0
  131. data/spec/support/redis_multiplexer.rb +91 -0
  132. data/spec/support/shared_examples/only_operates_on_hashes.rb +13 -0
  133. data/spec/support/shared_examples/only_operates_on_lists.rb +13 -0
  134. data/spec/support/shared_examples/only_operates_on_sets.rb +13 -0
  135. data/spec/support/shared_examples/only_operates_on_strings.rb +13 -0
  136. data/spec/support/shared_examples/only_operates_on_zsets.rb +57 -0
  137. data/spec/transactions_spec.rb +96 -0
  138. metadata +361 -0
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#hincrby(key, field, increment)' do
4
+ before do
5
+ @key = 'mock-redis-test:hincrby'
6
+ @field = 'count'
7
+ end
8
+
9
+ it "returns the value after the increment" do
10
+ @redises.hset(@key, @field, 2)
11
+ @redises.hincrby(@key, @field, 2).should == 4
12
+ end
13
+
14
+ it "treats a missing key like 0" do
15
+ @redises.hincrby(@key, @field, 1).should == 1
16
+ end
17
+
18
+ it "creates a hash if nothing is present" do
19
+ @redises.hincrby(@key, @field, 1)
20
+ @redises.hget(@key, @field).should == "1"
21
+ end
22
+
23
+ it "increments negative numbers" do
24
+ @redises.hset(@key, @field, -10)
25
+ @redises.hincrby(@key, @field, 2).should == -8
26
+ end
27
+
28
+ it "works multiple times" do
29
+ @redises.hincrby(@key, @field, 2).should == 2
30
+ @redises.hincrby(@key, @field, 2).should == 4
31
+ @redises.hincrby(@key, @field, 2).should == 6
32
+ end
33
+
34
+ it "accepts an integer-ish string" do
35
+ @redises.hincrby(@key, @field, "2").should == 2
36
+ end
37
+
38
+ it "raises an error if the value does not look like an integer" do
39
+ @redises.hset(@key, @field, "one")
40
+ lambda do
41
+ @redises.hincrby(@key, @field, 1)
42
+ end.should raise_error(RuntimeError)
43
+ end
44
+
45
+ it "raises an error if the delta does not look like an integer" do
46
+ lambda do
47
+ @redises.hincrby(@key, @field, "foo")
48
+ end.should raise_error(RuntimeError)
49
+ end
50
+
51
+ it_should_behave_like "a hash-only command"
52
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hkeys(key)" do
4
+ before do
5
+ @key = "mock-redis-test:hkeys"
6
+ @redises.hset(@key, 'k1', 'v1')
7
+ @redises.hset(@key, 'k2', 'v2')
8
+ end
9
+
10
+ it "returns the keys stored in the hash" do
11
+ @redises.hkeys(@key).sort.should == %w[k1 k2]
12
+ end
13
+
14
+ it "returns [] when there is no such key" do
15
+ @redises.hkeys('mock-redis-test:nonesuch').should == []
16
+ end
17
+
18
+ it_should_behave_like "a hash-only command"
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hlen(key)" do
4
+ before do
5
+ @key = "mock-redis-test:hlen"
6
+ @redises.hset(@key, 'k1', 'v1')
7
+ @redises.hset(@key, 'k2', 'v2')
8
+ end
9
+
10
+ it "returns the number of keys stored in the hash" do
11
+ @redises.hlen(@key).should == 2
12
+ end
13
+
14
+ it "returns [] when there is no such key" do
15
+ @redises.hlen('mock-redis-test:nonesuch').should == 0
16
+ end
17
+
18
+ it_should_behave_like "a hash-only command"
19
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hmget(key, field [, field, ...])" do
4
+ before do
5
+ @key = "mock-redis-test:hmget"
6
+ @redises.hset(@key, 'k1', 'v1')
7
+ @redises.hset(@key, 'k2', 'v2')
8
+ end
9
+
10
+ it "returns the values for those keys" do
11
+ @redises.hmget(@key, 'k1', 'k2').sort.should == %w[v1 v2]
12
+ end
13
+
14
+ it "returns nils when there are no such fields" do
15
+ @redises.hmget(@key, 'k1', 'mock-redis-test:nonesuch').
16
+ should == ['v1', nil]
17
+ end
18
+
19
+ it "returns nils when there is no such key" do
20
+ @redises.hmget(@key, 'mock-redis-test:nonesuch').should == [nil]
21
+ end
22
+
23
+ it "raises an error if given no fields" do
24
+ lambda do
25
+ @redises.hmget(@key)
26
+ end.should raise_error(RuntimeError)
27
+ end
28
+
29
+ it_should_behave_like "a hash-only command"
30
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hmset(key, field, value [, field, value ...])" do
4
+ before do
5
+ @key = "mock-redis-test:hmset"
6
+ end
7
+
8
+ it "returns 'OK'" do
9
+ @redises.hmset(@key, 'k1', 'v1', 'k2', 'v2').should == "OK"
10
+ end
11
+
12
+ it "sets the values" do
13
+ @redises.hmset(@key, 'k1', 'v1', 'k2', 'v2')
14
+ @redises.hmget(@key, 'k1', 'k2').should == %w[v1 v2]
15
+ end
16
+
17
+ it "updates an existing hash" do
18
+ @redises.hset(@key, 'foo', 'bar')
19
+ @redises.hmset(@key, 'bert', 'ernie', 'diet', 'coke')
20
+
21
+ @redises.hmget(@key, 'foo', 'bert', 'diet').
22
+ should == %w[bar ernie coke]
23
+ end
24
+
25
+ it "stores the values as strings" do
26
+ @redises.hmset(@key, 'one', 1)
27
+ @redises.hget(@key, 'one').should == "1"
28
+ end
29
+
30
+ it "raises an error if given no fields or values" do
31
+ lambda do
32
+ @redises.hmset(@key)
33
+ end.should raise_error(RuntimeError)
34
+ end
35
+
36
+ it "raises an error if given an odd number of fields+values" do
37
+ lambda do
38
+ @redises.hmset(@key, 'k1', 1, 'k2')
39
+ end.should raise_error(RuntimeError)
40
+ end
41
+
42
+ it_should_behave_like "a hash-only command"
43
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hset(key, field)" do
4
+ before do
5
+ @key = "mock-redis-test:hset"
6
+ end
7
+
8
+ it "returns true" do
9
+ @redises.hset(@key, 'k1', 'v1').should == true
10
+ end
11
+
12
+ it "creates a hash there is no such field" do
13
+ @redises.hset(@key, 'k1', 'v1')
14
+ @redises.hget(@key, 'k1').should == 'v1'
15
+ end
16
+
17
+ it "stores values as strings" do
18
+ @redises.hset(@key, "num", 1)
19
+ @redises.hget(@key, 'num').should == "1"
20
+ end
21
+
22
+ it_should_behave_like "a hash-only command"
23
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hsetnx(key, field)" do
4
+ before do
5
+ @key = "mock-redis-test:hsetnx"
6
+ end
7
+
8
+ it "returns true if the field is absent" do
9
+ @redises.hsetnx(@key, 'field', 'val').should be_true
10
+ end
11
+
12
+ it "returns 0 if the field is present" do
13
+ @redises.hset(@key, 'field', 'val')
14
+ @redises.hsetnx(@key, 'field', 'val').should be_false
15
+ end
16
+
17
+ it "leaves the field unchanged if the field is present" do
18
+ @redises.hset(@key, 'field', 'old')
19
+ @redises.hsetnx(@key, 'field', 'new')
20
+ @redises.hget(@key, 'field').should == 'old'
21
+ end
22
+
23
+ it "sets the field if the field is absent" do
24
+ @redises.hsetnx(@key, 'field', 'new')
25
+ @redises.hget(@key, 'field').should == 'new'
26
+ end
27
+
28
+ it "creates a hash if there is no such field" do
29
+ @redises.hsetnx(@key, 'field', 'val')
30
+ @redises.hget(@key, 'field').should == 'val'
31
+ end
32
+
33
+ it "stores values as strings" do
34
+ @redises.hsetnx(@key, "num", 1)
35
+ @redises.hget(@key, 'num').should == "1"
36
+ end
37
+
38
+ it_should_behave_like "a hash-only command"
39
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hvals(key)" do
4
+ before do
5
+ @key = "mock-redis-test:hvals"
6
+ @redises.hset(@key, 'k1', 'v1')
7
+ @redises.hset(@key, 'k2', 'v2')
8
+ end
9
+
10
+ it "returns the values stored in the hash" do
11
+ @redises.hvals(@key).sort.should == %w[v1 v2]
12
+ end
13
+
14
+ it "returns [] when there is no such key" do
15
+ @redises.hvals('mock-redis-test:nonesuch').should == []
16
+ end
17
+
18
+ it_should_behave_like "a hash-only command"
19
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#incr(key)' do
4
+ before { @key = 'mock-redis-test:33888' }
5
+
6
+ it "returns the value after the increment" do
7
+ @redises.set(@key, 1)
8
+ @redises.incr(@key).should == 2
9
+ end
10
+
11
+ it "treats a missing key like 0" do
12
+ @redises.incr(@key).should == 1
13
+ end
14
+
15
+ it "increments negative numbers" do
16
+ @redises.set(@key, -10)
17
+ @redises.incr(@key).should == -9
18
+ end
19
+
20
+ it "works multiple times" do
21
+ @redises.incr(@key).should == 1
22
+ @redises.incr(@key).should == 2
23
+ @redises.incr(@key).should == 3
24
+ end
25
+
26
+ it "raises an error if the value does not look like an integer" do
27
+ @redises.set(@key, "one")
28
+ lambda do
29
+ @redises.incr(@key)
30
+ end.should raise_error(RuntimeError)
31
+ end
32
+
33
+ it_should_behave_like "a string-only command"
34
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#incrby(key, increment)' do
4
+ before { @key = 'mock-redis-test:65374' }
5
+
6
+ it "returns the value after the increment" do
7
+ @redises.set(@key, 2)
8
+ @redises.incrby(@key, 2).should == 4
9
+ end
10
+
11
+ it "treats a missing key like 0" do
12
+ @redises.incrby(@key, 1).should == 1
13
+ end
14
+
15
+ it "increments negative numbers" do
16
+ @redises.set(@key, -10)
17
+ @redises.incrby(@key, 2).should == -8
18
+ end
19
+
20
+ it "works multiple times" do
21
+ @redises.incrby(@key, 2).should == 2
22
+ @redises.incrby(@key, 2).should == 4
23
+ @redises.incrby(@key, 2).should == 6
24
+ end
25
+
26
+ it "accepts an integer-ish string" do
27
+ @redises.incrby(@key, "2").should == 2
28
+ end
29
+
30
+ it "raises an error if the value does not look like an integer" do
31
+ @redises.set(@key, "one")
32
+ lambda do
33
+ @redises.incrby(@key, 1)
34
+ end.should raise_error(RuntimeError)
35
+ end
36
+
37
+ it "raises an error if the delta does not look like an integer" do
38
+ lambda do
39
+ @redises.incrby(@key, "foo")
40
+ end.should raise_error(RuntimeError)
41
+ end
42
+
43
+ it_should_behave_like "a string-only command"
44
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#info [mock only]' do
4
+ before { @info = @redises.mock.info }
5
+
6
+ it "responds with a config hash" do
7
+ @info.should be_a(Hash)
8
+ end
9
+
10
+ it "has some data in it" do
11
+ @info.keys.length.should > 0
12
+ end
13
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#keys()' do
4
+
5
+ it "returns [] when no keys are found (no regex characters)" do
6
+ @redises.keys("mock-redis-test:29016").should == []
7
+ end
8
+
9
+ it "returns [] when no keys are found (some regex characters)" do
10
+ @redises.keys("mock-redis-test:29016*").should == []
11
+ end
12
+
13
+ describe "with pattern matching" do
14
+ before do
15
+ @redises.set("mock-redis-test:key1", 1)
16
+ @redises.set("mock-redis-test:key2", 2)
17
+ @redises.set("mock-redis-test:key3", 3)
18
+ @redises.set("mock-redis-test:key10", 10)
19
+ @redises.set("mock-redis-test:key20", 20)
20
+ @redises.set("mock-redis-test:key30", 30)
21
+
22
+ @redises.set("mock-redis-test:special-key?", 'true')
23
+ @redises.set("mock-redis-test:special-key*", 'true')
24
+ end
25
+
26
+ describe "the ? character" do
27
+ it "is treated as a single character (at the end of the pattern)" do
28
+ @redises.keys("mock-redis-test:key?").sort.should == [
29
+ 'mock-redis-test:key1',
30
+ 'mock-redis-test:key2',
31
+ 'mock-redis-test:key3',
32
+ ]
33
+ end
34
+
35
+ it "is treated as a single character (in the middle of the pattern)" do
36
+ @redises.keys("mock-redis-test:key?0").sort.should == [
37
+ 'mock-redis-test:key10',
38
+ 'mock-redis-test:key20',
39
+ 'mock-redis-test:key30',
40
+ ]
41
+ end
42
+
43
+ it "treats \? as a literal ?" do
44
+ @redises.keys('mock-redis-test:special-key\?').sort.should == [
45
+ 'mock-redis-test:special-key?'
46
+ ]
47
+ end
48
+ end
49
+
50
+ describe "the * character" do
51
+ it "is treated as 1 or more characters" do
52
+ @redises.keys("mock-redis-test:key*").sort.should == [
53
+ 'mock-redis-test:key1',
54
+ 'mock-redis-test:key10',
55
+ 'mock-redis-test:key2',
56
+ 'mock-redis-test:key20',
57
+ 'mock-redis-test:key3',
58
+ 'mock-redis-test:key30',
59
+ ]
60
+ end
61
+
62
+ it "treats \* as a literal *" do
63
+ @redises.keys('mock-redis-test:special-key\*').sort.should == [
64
+ 'mock-redis-test:special-key*'
65
+ ]
66
+ end
67
+ end
68
+
69
+ describe "character classes ([abcde])" do
70
+ it "matches any one of those characters" do
71
+ @redises.keys('mock-redis-test:key[12]').sort.should == [
72
+ 'mock-redis-test:key1',
73
+ 'mock-redis-test:key2',
74
+ ]
75
+ end
76
+ end
77
+
78
+ describe "combining metacharacters" do
79
+ it "works with different metacharacters simultaneously" do
80
+ @redises.keys('mock-redis-test:k*[12]?').sort.should == [
81
+ 'mock-redis-test:key10',
82
+ 'mock-redis-test:key20',
83
+ ]
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#lastsave [mock only]" do
4
+ # can't test against both since it's timing-dependent
5
+ it "returns a Unix time" do
6
+ @redises.mock.lastsave.to_s.should =~ /^\d+$/
7
+ end
8
+ end