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.
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/Gemfile +9 -0
- data/LICENSE +19 -0
- data/README.md +88 -0
- data/Rakefile +10 -0
- data/lib/mock_redis/assertions.rb +13 -0
- data/lib/mock_redis/database.rb +432 -0
- data/lib/mock_redis/exceptions.rb +3 -0
- data/lib/mock_redis/expire_wrapper.rb +25 -0
- data/lib/mock_redis/hash_methods.rb +102 -0
- data/lib/mock_redis/list_methods.rb +187 -0
- data/lib/mock_redis/multi_db_wrapper.rb +48 -0
- data/lib/mock_redis/set_methods.rb +125 -0
- data/lib/mock_redis/string_methods.rb +195 -0
- data/lib/mock_redis/transaction_wrapper.rb +75 -0
- data/lib/mock_redis/undef_redis_methods.rb +11 -0
- data/lib/mock_redis/utility_methods.rb +22 -0
- data/lib/mock_redis/version.rb +3 -0
- data/lib/mock_redis/zset.rb +80 -0
- data/lib/mock_redis/zset_methods.rb +210 -0
- data/lib/mock_redis.rb +32 -0
- data/mock_redis.gemspec +24 -0
- data/spec/cloning_spec.rb +96 -0
- data/spec/commands/append_spec.rb +24 -0
- data/spec/commands/auth_spec.rb +7 -0
- data/spec/commands/bgrewriteaof_spec.rb +7 -0
- data/spec/commands/bgsave_spec.rb +7 -0
- data/spec/commands/blpop_spec.rb +55 -0
- data/spec/commands/brpop_spec.rb +54 -0
- data/spec/commands/brpoplpush_spec.rb +53 -0
- data/spec/commands/dbsize_spec.rb +18 -0
- data/spec/commands/decr_spec.rb +34 -0
- data/spec/commands/decrby_spec.rb +34 -0
- data/spec/commands/del_spec.rb +20 -0
- data/spec/commands/echo_spec.rb +11 -0
- data/spec/commands/exists_spec.rb +14 -0
- data/spec/commands/expire_spec.rb +83 -0
- data/spec/commands/expireat_spec.rb +48 -0
- data/spec/commands/flushall_spec.rb +38 -0
- data/spec/commands/flushdb_spec.rb +38 -0
- data/spec/commands/get_spec.rb +23 -0
- data/spec/commands/getbit_spec.rb +34 -0
- data/spec/commands/getrange_spec.rb +22 -0
- data/spec/commands/getset_spec.rb +23 -0
- data/spec/commands/hdel_spec.rb +35 -0
- data/spec/commands/hexists_spec.rb +22 -0
- data/spec/commands/hget_spec.rb +23 -0
- data/spec/commands/hgetall_spec.rb +22 -0
- data/spec/commands/hincrby_spec.rb +52 -0
- data/spec/commands/hkeys_spec.rb +19 -0
- data/spec/commands/hlen_spec.rb +19 -0
- data/spec/commands/hmget_spec.rb +30 -0
- data/spec/commands/hmset_spec.rb +43 -0
- data/spec/commands/hset_spec.rb +23 -0
- data/spec/commands/hsetnx_spec.rb +39 -0
- data/spec/commands/hvals_spec.rb +19 -0
- data/spec/commands/incr_spec.rb +34 -0
- data/spec/commands/incrby_spec.rb +44 -0
- data/spec/commands/info_spec.rb +13 -0
- data/spec/commands/keys_spec.rb +87 -0
- data/spec/commands/lastsave_spec.rb +8 -0
- data/spec/commands/lindex_spec.rb +39 -0
- data/spec/commands/linsert_spec.rb +68 -0
- data/spec/commands/llen_spec.rb +16 -0
- data/spec/commands/lpop_spec.rb +34 -0
- data/spec/commands/lpush_spec.rb +30 -0
- data/spec/commands/lpushx_spec.rb +33 -0
- data/spec/commands/lrange_spec.rb +35 -0
- data/spec/commands/lrem_spec.rb +79 -0
- data/spec/commands/lset_spec.rb +38 -0
- data/spec/commands/ltrim_spec.rb +35 -0
- data/spec/commands/mget_spec.rb +34 -0
- data/spec/commands/mset_spec.rb +29 -0
- data/spec/commands/msetnx_spec.rb +40 -0
- data/spec/commands/persist_spec.rb +49 -0
- data/spec/commands/ping_spec.rb +7 -0
- data/spec/commands/quit_spec.rb +7 -0
- data/spec/commands/randomkey_spec.rb +20 -0
- data/spec/commands/rename_spec.rb +31 -0
- data/spec/commands/renamenx_spec.rb +36 -0
- data/spec/commands/rpop_spec.rb +34 -0
- data/spec/commands/rpoplpush_spec.rb +45 -0
- data/spec/commands/rpush_spec.rb +30 -0
- data/spec/commands/rpushx_spec.rb +33 -0
- data/spec/commands/sadd_spec.rb +22 -0
- data/spec/commands/save_spec.rb +7 -0
- data/spec/commands/scard_spec.rb +18 -0
- data/spec/commands/sdiff_spec.rb +47 -0
- data/spec/commands/sdiffstore_spec.rb +51 -0
- data/spec/commands/select_spec.rb +53 -0
- data/spec/commands/set_spec.rb +7 -0
- data/spec/commands/setbit_spec.rb +46 -0
- data/spec/commands/setex_spec.rb +22 -0
- data/spec/commands/setnx_spec.rb +25 -0
- data/spec/commands/setrange_spec.rb +30 -0
- data/spec/commands/sinter_spec.rb +41 -0
- data/spec/commands/sinterstore_spec.rb +46 -0
- data/spec/commands/sismember_spec.rb +29 -0
- data/spec/commands/smembers_spec.rb +18 -0
- data/spec/commands/smove_spec.rb +41 -0
- data/spec/commands/spop_spec.rb +25 -0
- data/spec/commands/srandmember_spec.rb +25 -0
- data/spec/commands/srem_spec.rb +35 -0
- data/spec/commands/strlen_spec.rb +19 -0
- data/spec/commands/sunion_spec.rb +40 -0
- data/spec/commands/sunionstore_spec.rb +46 -0
- data/spec/commands/ttl_spec.rb +36 -0
- data/spec/commands/type_spec.rb +36 -0
- data/spec/commands/unwatch_spec.rb +7 -0
- data/spec/commands/watch_spec.rb +7 -0
- data/spec/commands/zadd_spec.rb +29 -0
- data/spec/commands/zcard_spec.rb +19 -0
- data/spec/commands/zcount_spec.rb +23 -0
- data/spec/commands/zincrby_spec.rb +24 -0
- data/spec/commands/zinterstore_spec.rb +88 -0
- data/spec/commands/zrange_spec.rb +31 -0
- data/spec/commands/zrangebyscore_spec.rb +42 -0
- data/spec/commands/zrank_spec.rb +23 -0
- data/spec/commands/zrem_spec.rb +25 -0
- data/spec/commands/zremrangebyrank_spec.rb +22 -0
- data/spec/commands/zremrangebyscore_spec.rb +21 -0
- data/spec/commands/zrevrange_spec.rb +31 -0
- data/spec/commands/zrevrangebyscore_spec.rb +42 -0
- data/spec/commands/zrevrank_spec.rb +23 -0
- data/spec/commands/zscore_spec.rb +16 -0
- data/spec/commands/zunionstore_spec.rb +96 -0
- data/spec/spec_helper.rb +44 -0
- data/spec/support/redis_multiplexer.rb +91 -0
- data/spec/support/shared_examples/only_operates_on_hashes.rb +13 -0
- data/spec/support/shared_examples/only_operates_on_lists.rb +13 -0
- data/spec/support/shared_examples/only_operates_on_sets.rb +13 -0
- data/spec/support/shared_examples/only_operates_on_strings.rb +13 -0
- data/spec/support/shared_examples/only_operates_on_zsets.rb +57 -0
- data/spec/transactions_spec.rb +73 -0
- metadata +358 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe '#brpop(key [, key, ...,], timeout)' do
|
|
4
|
+
before do
|
|
5
|
+
@list1 = 'mock-redis-test:brpop1'
|
|
6
|
+
@list2 = 'mock-redis-test:brpop2'
|
|
7
|
+
|
|
8
|
+
@redises.rpush(@list1, 'one')
|
|
9
|
+
@redises.rpush(@list1, 'two')
|
|
10
|
+
|
|
11
|
+
@redises.rpush(@list2, 'ten')
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "returns [first-nonempty-list, popped-value]" do
|
|
15
|
+
@redises.brpop(@list1, @list2, 1).should == [@list1, 'two']
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "pops that value off the list" do
|
|
19
|
+
@redises.brpop(@list1, @list2, 1)
|
|
20
|
+
@redises.brpop(@list1, @list2, 1)
|
|
21
|
+
@redises.brpop(@list1, @list2, 1).should == [@list2, 'ten']
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "ignores empty keys" do
|
|
25
|
+
@redises.brpop('mock-redis-test:not-here', @list1, 1).should ==
|
|
26
|
+
[@list1, 'two']
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "raises an error on non-integer timeout" do
|
|
30
|
+
lambda do
|
|
31
|
+
@redises.brpop(@list1, @list2, 0.5)
|
|
32
|
+
end.should raise_error(RuntimeError)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "raises an error on negative timeout" do
|
|
36
|
+
lambda do
|
|
37
|
+
@redises.brpop(@list1, @list2, -1)
|
|
38
|
+
end.should raise_error(RuntimeError)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it_should_behave_like "a list-only command"
|
|
42
|
+
|
|
43
|
+
context "[mock only]" do
|
|
44
|
+
it "ignores positive timeouts and returns nil" do
|
|
45
|
+
@redises.mock.brpop('mock-redis-test:not-here', 1).should be_nil
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it "raises WouldBlock on zero timeout (no blocking in the mock)" do
|
|
49
|
+
lambda do
|
|
50
|
+
@redises.mock.brpop('mock-redis-test:not-here', 0)
|
|
51
|
+
end.should raise_error(MockRedis::WouldBlock)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe '#brpoplpush(source, destination, timeout)' do
|
|
6
|
+
before do
|
|
7
|
+
@list1 = 'mock-redis-test:brpoplpush1'
|
|
8
|
+
@list2 = 'mock-redis-test:brpoplpush2'
|
|
9
|
+
|
|
10
|
+
@redises.rpush(@list1, 'A')
|
|
11
|
+
@redises.rpush(@list1, 'B')
|
|
12
|
+
|
|
13
|
+
@redises.rpush(@list2, 'alpha')
|
|
14
|
+
@redises.rpush(@list2, 'beta')
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "takes the last element of source and prepends it to destination" do
|
|
18
|
+
@redises.brpoplpush(@list1, @list2, 0)
|
|
19
|
+
@redises.lrange(@list1, 0, -1).should == %w[A]
|
|
20
|
+
@redises.lrange(@list2, 0, -1).should == %w[B alpha beta]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "returns the moved element" do
|
|
24
|
+
@redises.brpoplpush(@list1, @list2, 0).should == "B"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "raises an error on non-integer timeout" do
|
|
28
|
+
lambda do
|
|
29
|
+
@redises.brpoplpush(@list1, @list2, 0.5)
|
|
30
|
+
end.should raise_error(RuntimeError)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "raises an error on negative timeout" do
|
|
34
|
+
lambda do
|
|
35
|
+
@redises.brpoplpush(@list1, @list2, -1)
|
|
36
|
+
end.should raise_error(RuntimeError)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it_should_behave_like "a list-only command"
|
|
40
|
+
|
|
41
|
+
context "[mock only]" do
|
|
42
|
+
it "ignores positive timeouts and returns nil" do
|
|
43
|
+
@redises.mock.brpoplpush('mock-redis-test:not-here', @list1, 1).
|
|
44
|
+
should be_nil
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "raises WouldBlock on zero timeout (no blocking in the mock)" do
|
|
48
|
+
lambda do
|
|
49
|
+
@redises.mock.brpoplpush('mock-redis-test:not-here', @list1, 0)
|
|
50
|
+
end.should raise_error(MockRedis::WouldBlock)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "#dbsize [mock only]" do
|
|
4
|
+
# mock only since we can't guarantee that the real Redis is empty
|
|
5
|
+
before { @mock = @redises.mock }
|
|
6
|
+
|
|
7
|
+
it "returns 0 for an empty DB" do
|
|
8
|
+
@mock.dbsize.should == 0
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "returns the number of keys in the DB" do
|
|
12
|
+
@mock.set('foo', 1)
|
|
13
|
+
@mock.lpush('bar', 2)
|
|
14
|
+
@mock.hset('baz', 3, 4)
|
|
15
|
+
|
|
16
|
+
@mock.dbsize.should == 3
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe '#decr(key)' do
|
|
4
|
+
before { @key = 'mock-redis-test:46895' }
|
|
5
|
+
|
|
6
|
+
it "returns the value after the decrement" do
|
|
7
|
+
@redises.set(@key, 2)
|
|
8
|
+
@redises.decr(@key).should == 1
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "treats a missing key like 0" do
|
|
12
|
+
@redises.decr(@key).should == -1
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "decrements negative numbers" do
|
|
16
|
+
@redises.set(@key, -10)
|
|
17
|
+
@redises.decr(@key).should == -11
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "works multiple times" do
|
|
21
|
+
@redises.decr(@key).should == -1
|
|
22
|
+
@redises.decr(@key).should == -2
|
|
23
|
+
@redises.decr(@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, "minus one")
|
|
28
|
+
lambda do
|
|
29
|
+
@redises.decr(@key)
|
|
30
|
+
end.should raise_error(RuntimeError)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it_should_behave_like "a string-only command"
|
|
34
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe '#decrby(key, decrement)' do
|
|
4
|
+
before { @key = 'mock-redis-test:43650' }
|
|
5
|
+
|
|
6
|
+
it "returns the value after the decrement" do
|
|
7
|
+
@redises.set(@key, 4)
|
|
8
|
+
@redises.decrby(@key, 2).should == 2
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "treats a missing key like 0" do
|
|
12
|
+
@redises.decrby(@key, 2).should == -2
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "decrements negative numbers" do
|
|
16
|
+
@redises.set(@key, -10)
|
|
17
|
+
@redises.decrby(@key, 2).should == -12
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "works multiple times" do
|
|
21
|
+
@redises.decrby(@key, 2).should == -2
|
|
22
|
+
@redises.decrby(@key, 2).should == -4
|
|
23
|
+
@redises.decrby(@key, 2).should == -6
|
|
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.decrby(@key, 1)
|
|
30
|
+
end.should raise_error(RuntimeError)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it_should_behave_like "a string-only command"
|
|
34
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe '#del(key [, key, ...])' do
|
|
4
|
+
it "returns the number of keys deleted" do
|
|
5
|
+
@redises.set('mock-redis-test:1', 1)
|
|
6
|
+
@redises.set('mock-redis-test:2', 1)
|
|
7
|
+
|
|
8
|
+
@redises.del(
|
|
9
|
+
'mock-redis-test:1',
|
|
10
|
+
'mock-redis-test:2',
|
|
11
|
+
'mock-redis-test:other').should == 2
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "actually removes the key" do
|
|
15
|
+
@redises.set('mock-redis-test:1', 1)
|
|
16
|
+
@redises.del('mock-redis-test:1')
|
|
17
|
+
|
|
18
|
+
@redises.get('mock-redis-test:1').should be_nil
|
|
19
|
+
end
|
|
20
|
+
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
|