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,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
@@ -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