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,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#echo(str)" do
4
+ it "returns its argument" do
5
+ @redises.echo('foo').should == 'foo'
6
+ end
7
+
8
+ it "stringifies its argument" do
9
+ @redises.echo(1).should == "1"
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#exists(key)" do
4
+ before { @key = 'mock-redis-test:45794' }
5
+
6
+ it "returns false for keys that do not exist" do
7
+ @redises.exists(@key).should be_false
8
+ end
9
+
10
+ it "returns true for keys that do exist" do
11
+ @redises.set(@key, 1)
12
+ @redises.exists(@key).should be_true
13
+ end
14
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#expire(key, seconds)" do
4
+ before do
5
+ @key = 'mock-redis-test:expire'
6
+ @redises.set(@key, 'spork')
7
+ end
8
+
9
+ it "returns true for a key that exists" do
10
+ @redises.expire(@key, 1).should be_true
11
+ end
12
+
13
+ it "returns false for a key that does not exist" do
14
+ @redises.expire('mock-redis-test:nonesuch', 1).should be_false
15
+ end
16
+
17
+ it "removes a key immediately when seconds==0" do
18
+ @redises.expire(@key, 0)
19
+ @redises.get(@key).should be_nil
20
+ end
21
+
22
+ it "raises an error if seconds is bogus" do
23
+ lambda do
24
+ @redises.expireat(@key, 'a couple minutes or so')
25
+ end.should raise_error(RuntimeError)
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 "removes keys after enough time has passed" do
42
+ @mock.expire(@key, 5)
43
+ Time.stub!(:now).and_return(@now + 5)
44
+ @mock.get(@key).should be_nil
45
+ end
46
+
47
+ it "updates an existing expire time" do
48
+ @mock.expire(@key, 5)
49
+ @mock.expire(@key, 6)
50
+
51
+ Time.stub!(:now).and_return(@now + 5)
52
+ @mock.get(@key).should_not be_nil
53
+ end
54
+
55
+ context "expirations on a deleted key" do
56
+ before { @mock.del(@key) }
57
+
58
+ it "cleans up the expiration once the key is gone (string)" do
59
+ @mock.set(@key, 'string')
60
+ @mock.expire(@key, 2)
61
+ @mock.del(@key)
62
+ @mock.set(@key, 'string')
63
+
64
+ Time.stub!(:now).and_return(@now + 2)
65
+
66
+ @mock.get(@key).should_not be_nil
67
+ end
68
+
69
+ it "cleans up the expiration once the key is gone (list)" do
70
+ @mock.rpush(@key, 'coconuts')
71
+ @mock.expire(@key, 2)
72
+ @mock.rpop(@key)
73
+
74
+ @mock.rpush(@key, 'coconuts')
75
+
76
+ Time.stub!(:now).and_return(@now + 2)
77
+
78
+ @mock.lindex(@key, 0).should_not be_nil
79
+ end
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#expireat(key, timestamp)" do
4
+ before do
5
+ @key = 'mock-redis-test:expireat'
6
+ @redises.set(@key, 'spork')
7
+ end
8
+
9
+ it "returns true for a key that exists" do
10
+ @redises.expireat(@key, Time.now.to_i + 1).should be_true
11
+ end
12
+
13
+ it "returns false for a key that does not exist" do
14
+ @redises.expireat('mock-redis-test:nonesuch', Time.now.to_i + 1).should be_false
15
+ end
16
+
17
+ it "removes a key immediately when timestamp is now" do
18
+ @redises.expireat(@key, Time.now.to_i)
19
+ @redises.get(@key).should be_nil
20
+ end
21
+
22
+ it "raises an error if you don't give it a Unix timestamp" do
23
+ lambda do
24
+ @redises.expireat(@key, Time.now) # oops, forgot .to_i
25
+ end.should raise_error(RuntimeError)
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 "removes keys after enough time has passed" do
42
+ @mock.expireat(@key, @now.to_i + 5)
43
+ Time.stub!(:now).and_return(@now + 5)
44
+ @mock.get(@key).should be_nil
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#flushall [mock only]" do
4
+ # don't want to hurt things in the real redis that are outside our
5
+ # namespace.
6
+ before { @mock = @redises.mock }
7
+ before { @key = 'mock-redis-test:select' }
8
+
9
+ it "returns 'OK'" do
10
+ @mock.flushall.should == 'OK'
11
+ end
12
+
13
+ it "removes all keys in the current DB" do
14
+ @mock.set('k1', 'v1')
15
+ @mock.lpush('k2', 'v2')
16
+
17
+ @mock.flushall
18
+ @mock.keys('*').should == []
19
+ end
20
+
21
+ it "removes all keys in other DBs, too" do
22
+ @mock.set('k1', 'v1')
23
+
24
+ @mock.select(1)
25
+ @mock.flushall
26
+ @mock.select(0)
27
+
28
+ @mock.get('k1').should be_nil
29
+ end
30
+
31
+ it "removes expiration times" do
32
+ @mock.set('k1', 'v1')
33
+ @mock.expire('k1', 360000)
34
+ @mock.flushall
35
+ @mock.set('k1', 'v1')
36
+ @mock.ttl('k1').should == -1
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#flushdb [mock only]" do
4
+ # don't want to hurt things in the real redis that are outside our
5
+ # namespace.
6
+ before { @mock = @redises.mock }
7
+ before { @key = 'mock-redis-test:select' }
8
+
9
+ it "returns 'OK'" do
10
+ @mock.flushdb.should == 'OK'
11
+ end
12
+
13
+ it "removes all keys in the current DB" do
14
+ @mock.set('k1', 'v1')
15
+ @mock.lpush('k2', 'v2')
16
+
17
+ @mock.flushdb
18
+ @mock.keys('*').should == []
19
+ end
20
+
21
+ it "leaves other databases alone" do
22
+ @mock.set('k1', 'v1')
23
+
24
+ @mock.select(1)
25
+ @mock.flushdb
26
+ @mock.select(0)
27
+
28
+ @mock.get('k1').should == 'v1'
29
+ end
30
+
31
+ it "removes expiration times" do
32
+ @mock.set('k1', 'v1')
33
+ @mock.expire('k1', 360000)
34
+ @mock.flushdb
35
+ @mock.set('k1', 'v1')
36
+ @mock.ttl('k1').should == -1
37
+ end
38
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#get(key)" do
4
+ before do
5
+ @key = 'mock-redis-test:73288'
6
+ end
7
+
8
+ it "returns nil for a nonexistent value" do
9
+ @redises.get('mock-redis-test:does-not-exist').should be_nil
10
+ end
11
+
12
+ it "returns a stored string value" do
13
+ @redises.set(@key, 'forsooth')
14
+ @redises.get(@key).should == 'forsooth'
15
+ end
16
+
17
+ it "treats integers as strings" do
18
+ @redises.set(@key, 100)
19
+ @redises.get(@key).should == "100"
20
+ end
21
+
22
+ it_should_behave_like "a string-only command"
23
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#getbit(key, offset)" do
4
+ before do
5
+ @key = 'mock-redis-test:getbit'
6
+ @redises.set(@key, 'h') # ASCII 0x68
7
+ end
8
+
9
+ it "gets the bits from the key" do
10
+ @redises.getbit(@key, 0).should == 0
11
+ @redises.getbit(@key, 1).should == 1
12
+ @redises.getbit(@key, 2).should == 1
13
+ @redises.getbit(@key, 3).should == 0
14
+ @redises.getbit(@key, 4).should == 1
15
+ @redises.getbit(@key, 5).should == 0
16
+ @redises.getbit(@key, 6).should == 0
17
+ @redises.getbit(@key, 7).should == 0
18
+ end
19
+
20
+ it "returns 0 for out-of-range bits" do
21
+ @redises.getbit(@key, 100).should == 0
22
+ end
23
+
24
+ it "does not modify the stored value for out-of-range bits" do
25
+ @redises.getbit(@key, 100)
26
+ @redises.get(@key).should == 'h'
27
+ end
28
+
29
+ it "treats nonexistent keys as empty strings" do
30
+ @redises.getbit('mock-redis-test:not-found', 0).should == 0
31
+ end
32
+
33
+ it_should_behave_like "a string-only command"
34
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#getrange(key, start, stop)" do
4
+ before do
5
+ @key = 'mock-redis-test:getrange'
6
+ @redises.set(@key, "This is a string")
7
+ end
8
+
9
+ it "returns a substring" do
10
+ @redises.getrange(@key, 0, 3).should == "This"
11
+ end
12
+
13
+ it "works with negative indices" do
14
+ @redises.getrange(@key, -3, -1).should == "ing"
15
+ end
16
+
17
+ it "limits the result to the actual length of the string" do
18
+ @redises.getrange(@key, 10, 100).should == "string"
19
+ end
20
+
21
+ it_should_behave_like "a string-only command"
22
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#getrange(key, value)" do
4
+ before do
5
+ @key = 'mock-redis-test:getset'
6
+ @redises.set(@key, "oldvalue")
7
+ end
8
+
9
+ it "returns the old value" do
10
+ @redises.getset(@key, 'newvalue').should == "oldvalue"
11
+ end
12
+
13
+ it "sets the value to the new value" do
14
+ @redises.getset(@key, 'newvalue')
15
+ @redises.get(@key).should == 'newvalue'
16
+ end
17
+
18
+ it "returns nil for nonexistent keys" do
19
+ @redises.getset('mock-redis-test:not-found', 1).should be_nil
20
+ end
21
+
22
+ it_should_behave_like "a string-only command"
23
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hdel(key, field)" do
4
+ before do
5
+ @key = "mock-redis-test:hdel"
6
+ @redises.hset(@key, 'k1', 'v1')
7
+ @redises.hset(@key, 'k2', 'v2')
8
+ end
9
+
10
+ it "returns 1 when it removes a field" do
11
+ @redises.hdel(@key, 'k1').should == 1
12
+ end
13
+
14
+ it "returns 0 when it does not remove a field" do
15
+ @redises.hdel(@key, 'nonesuch').should == 0
16
+ end
17
+
18
+ it "actually removes the field" do
19
+ @redises.hdel(@key, 'k1')
20
+ @redises.hget(@key, 'k1').should be_nil
21
+ end
22
+
23
+ it "removes only the field specified" do
24
+ @redises.hdel(@key, 'k1')
25
+ @redises.hget(@key, 'k2').should == 'v2'
26
+ end
27
+
28
+ it "cleans up empty hashes" do
29
+ @redises.hdel(@key, 'k1')
30
+ @redises.hdel(@key, 'k2')
31
+ @redises.get(@key).should be_nil
32
+ end
33
+
34
+ it_should_behave_like "a hash-only command"
35
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hexists(key, field)" do
4
+ before do
5
+ @key = "mock-redis-test:hexists"
6
+ @redises.hset(@key, 'field', 'value')
7
+ end
8
+
9
+ it "returns true if the hash has that field" do
10
+ @redises.hexists(@key, 'field').should be_true
11
+ end
12
+
13
+ it "returns false if the hash lacks that field" do
14
+ @redises.hexists(@key, 'nonesuch').should be_false
15
+ end
16
+
17
+ it "returns nil when there is no such key" do
18
+ @redises.hexists('mock-redis-test:nonesuch', 'key').should be_false
19
+ end
20
+
21
+ it_should_behave_like "a hash-only command"
22
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hget(key, field)" do
4
+ before do
5
+ @key = "mock-redis-test:hget"
6
+ @redises.hset(@key, 'k1', 'v1')
7
+ @redises.hset(@key, 'k2', 'v2')
8
+ end
9
+
10
+ it "returns the value stored at field" do
11
+ @redises.hget(@key, 'k1').should == 'v1'
12
+ end
13
+
14
+ it "returns nil when there is no such field" do
15
+ @redises.hget(@key, 'nonesuch').should be_nil
16
+ end
17
+
18
+ it "returns nil when there is no such key" do
19
+ @redises.hget('mock-redis-test:nonesuch', 'k1').should be_nil
20
+ end
21
+
22
+ it_should_behave_like "a hash-only command"
23
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#hgetall(key)" do
4
+ before do
5
+ @key = "mock-redis-test:hgetall"
6
+ @redises.hset(@key, 'k1', 'v1')
7
+ @redises.hset(@key, 'k2', 'v2')
8
+ end
9
+
10
+ it "returns the (key, value) pairs stored in the hash" do
11
+ @redises.hgetall(@key).should == {
12
+ 'k1' => 'v1',
13
+ 'k2' => 'v2',
14
+ }
15
+ end
16
+
17
+ it "returns [] when there is no such key" do
18
+ @redises.hgetall('mock-redis-test:nonesuch').should == {}
19
+ end
20
+
21
+ it_should_behave_like "a hash-only command"
22
+ end