mock_redis 0.5.4 → 0.31.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/lint.yml +31 -0
- data/.github/workflows/tests.yml +63 -0
- data/.gitignore +1 -1
- data/.overcommit.yml +21 -0
- data/.rspec +1 -1
- data/.rubocop.yml +148 -0
- data/.rubocop_todo.yml +35 -0
- data/.simplecov +4 -0
- data/CHANGELOG.md +278 -0
- data/Gemfile +9 -5
- data/LICENSE.md +21 -0
- data/README.md +52 -16
- data/Rakefile +0 -8
- data/lib/mock_redis/assertions.rb +0 -1
- data/lib/mock_redis/connection_method.rb +13 -0
- data/lib/mock_redis/database.rb +193 -257
- data/lib/mock_redis/expire_wrapper.rb +2 -2
- data/lib/mock_redis/future.rb +23 -0
- data/lib/mock_redis/geospatial_methods.rb +240 -0
- data/lib/mock_redis/hash_methods.rb +83 -24
- data/lib/mock_redis/indifferent_hash.rb +11 -0
- data/lib/mock_redis/info_method.rb +160 -0
- data/lib/mock_redis/list_methods.rb +34 -19
- data/lib/mock_redis/multi_db_wrapper.rb +8 -7
- data/lib/mock_redis/pipelined_wrapper.rb +42 -16
- data/lib/mock_redis/set_methods.rb +62 -19
- data/lib/mock_redis/sort_method.rb +81 -0
- data/lib/mock_redis/stream/id.rb +58 -0
- data/lib/mock_redis/stream.rb +88 -0
- data/lib/mock_redis/stream_methods.rb +102 -0
- data/lib/mock_redis/string_methods.rb +235 -42
- data/lib/mock_redis/transaction_wrapper.rb +62 -28
- data/lib/mock_redis/utility_methods.rb +62 -11
- data/lib/mock_redis/version.rb +4 -1
- data/lib/mock_redis/zset.rb +24 -29
- data/lib/mock_redis/zset_methods.rb +187 -59
- data/lib/mock_redis.rb +77 -27
- data/mock_redis.gemspec +23 -15
- data/spec/client_spec.rb +29 -0
- data/spec/cloning_spec.rb +17 -18
- data/spec/commands/append_spec.rb +4 -4
- data/spec/commands/auth_spec.rb +1 -1
- data/spec/commands/bgrewriteaof_spec.rb +2 -2
- data/spec/commands/bgsave_spec.rb +2 -2
- data/spec/commands/bitcount_spec.rb +25 -0
- data/spec/commands/bitfield_spec.rb +169 -0
- data/spec/commands/blpop_spec.rb +19 -21
- data/spec/commands/brpop_spec.rb +25 -20
- data/spec/commands/brpoplpush_spec.rb +16 -17
- data/spec/commands/connected_spec.rb +7 -0
- data/spec/commands/connection_spec.rb +15 -0
- data/spec/commands/dbsize_spec.rb +3 -3
- data/spec/commands/decr_spec.rb +8 -8
- data/spec/commands/decrby_spec.rb +8 -8
- data/spec/commands/del_spec.rb +35 -3
- data/spec/commands/disconnect_spec.rb +7 -0
- data/spec/commands/dump_spec.rb +19 -0
- data/spec/commands/echo_spec.rb +4 -4
- data/spec/commands/eval_spec.rb +7 -0
- data/spec/commands/evalsha_spec.rb +10 -0
- data/spec/commands/exists_spec.rb +36 -7
- data/spec/commands/expire_spec.rb +48 -20
- data/spec/commands/expireat_spec.rb +12 -13
- data/spec/commands/flushall_spec.rb +5 -5
- data/spec/commands/flushdb_spec.rb +5 -5
- data/spec/commands/future_spec.rb +30 -0
- data/spec/commands/geoadd_spec.rb +58 -0
- data/spec/commands/geodist_spec.rb +118 -0
- data/spec/commands/geohash_spec.rb +52 -0
- data/spec/commands/geopos_spec.rb +55 -0
- data/spec/commands/get_spec.rb +14 -6
- data/spec/commands/getbit_spec.rb +7 -7
- data/spec/commands/getrange_spec.rb +9 -9
- data/spec/commands/getset_spec.rb +7 -7
- data/spec/commands/hdel_spec.rb +41 -11
- data/spec/commands/hexists_spec.rb +11 -11
- data/spec/commands/hget_spec.rb +7 -7
- data/spec/commands/hgetall_spec.rb +15 -5
- data/spec/commands/hincrby_spec.rb +16 -16
- data/spec/commands/hincrbyfloat_spec.rb +58 -0
- data/spec/commands/hkeys_spec.rb +5 -5
- data/spec/commands/hlen_spec.rb +5 -5
- data/spec/commands/hmget_spec.rb +19 -9
- data/spec/commands/hmset_spec.rb +38 -12
- data/spec/commands/hscan_each_spec.rb +48 -0
- data/spec/commands/hscan_spec.rb +27 -0
- data/spec/commands/hset_spec.rb +26 -12
- data/spec/commands/hsetnx_spec.rb +16 -16
- data/spec/commands/hvals_spec.rb +5 -5
- data/spec/commands/incr_spec.rb +8 -8
- data/spec/commands/incrby_spec.rb +13 -13
- data/spec/commands/incrbyfloat_spec.rb +13 -13
- data/spec/commands/info_spec.rb +54 -5
- data/spec/commands/keys_spec.rb +83 -31
- data/spec/commands/lastsave_spec.rb +2 -2
- data/spec/commands/lindex_spec.rb +20 -10
- data/spec/commands/linsert_spec.rb +14 -14
- data/spec/commands/llen_spec.rb +4 -4
- data/spec/commands/lpop_spec.rb +6 -6
- data/spec/commands/lpush_spec.rb +21 -15
- data/spec/commands/lpushx_spec.rb +24 -11
- data/spec/commands/lrange_spec.rb +24 -8
- data/spec/commands/lrem_spec.rb +16 -16
- data/spec/commands/lset_spec.rb +17 -12
- data/spec/commands/ltrim_spec.rb +17 -7
- data/spec/commands/mapped_hmget_spec.rb +13 -9
- data/spec/commands/mapped_hmset_spec.rb +12 -12
- data/spec/commands/mapped_mget_spec.rb +22 -0
- data/spec/commands/mapped_mset_spec.rb +19 -0
- data/spec/commands/mapped_msetnx_spec.rb +26 -0
- data/spec/commands/mget_spec.rb +48 -17
- data/spec/commands/move_spec.rb +37 -37
- data/spec/commands/mset_spec.rb +20 -6
- data/spec/commands/msetnx_spec.rb +14 -14
- data/spec/commands/persist_spec.rb +15 -16
- data/spec/commands/pexpire_spec.rb +86 -0
- data/spec/commands/pexpireat_spec.rb +48 -0
- data/spec/commands/ping_spec.rb +6 -2
- data/spec/commands/pipelined_spec.rb +98 -7
- data/spec/commands/pttl_spec.rb +41 -0
- data/spec/commands/randomkey_spec.rb +3 -3
- data/spec/commands/rename_spec.rb +16 -12
- data/spec/commands/renamenx_spec.rb +13 -15
- data/spec/commands/restore_spec.rb +47 -0
- data/spec/commands/rpop_spec.rb +6 -6
- data/spec/commands/rpoplpush_spec.rb +13 -8
- data/spec/commands/rpush_spec.rb +21 -15
- data/spec/commands/rpushx_spec.rb +24 -11
- data/spec/commands/sadd_spec.rb +14 -10
- data/spec/commands/scan_each_spec.rb +39 -0
- data/spec/commands/scan_spec.rb +64 -0
- data/spec/commands/scard_spec.rb +3 -3
- data/spec/commands/script_spec.rb +9 -0
- data/spec/commands/sdiff_spec.rb +13 -13
- data/spec/commands/sdiffstore_spec.rb +13 -13
- data/spec/commands/select_spec.rb +13 -5
- data/spec/commands/set_spec.rb +112 -0
- data/spec/commands/setbit_spec.rb +25 -16
- data/spec/commands/setex_spec.rb +20 -4
- data/spec/commands/setnx_spec.rb +6 -6
- data/spec/commands/setrange_spec.rb +12 -12
- data/spec/commands/sinter_spec.rb +11 -13
- data/spec/commands/sinterstore_spec.rb +12 -12
- data/spec/commands/sismember_spec.rb +10 -10
- data/spec/commands/smembers_spec.rb +15 -5
- data/spec/commands/smove_spec.rb +13 -13
- data/spec/commands/sort_list_spec.rb +21 -0
- data/spec/commands/sort_set_spec.rb +21 -0
- data/spec/commands/sort_zset_spec.rb +21 -0
- data/spec/commands/spop_spec.rb +19 -4
- data/spec/commands/srandmember_spec.rb +28 -4
- data/spec/commands/srem_spec.rb +17 -12
- data/spec/commands/sscan_each_spec.rb +48 -0
- data/spec/commands/sscan_spec.rb +39 -0
- data/spec/commands/strlen_spec.rb +4 -5
- data/spec/commands/sunion_spec.rb +13 -11
- data/spec/commands/sunionstore_spec.rb +12 -12
- data/spec/commands/ttl_spec.rb +11 -6
- data/spec/commands/type_spec.rb +1 -1
- data/spec/commands/watch_spec.rb +9 -4
- data/spec/commands/xadd_spec.rb +122 -0
- data/spec/commands/xlen_spec.rb +22 -0
- data/spec/commands/xrange_spec.rb +164 -0
- data/spec/commands/xread_spec.rb +66 -0
- data/spec/commands/xrevrange_spec.rb +130 -0
- data/spec/commands/xtrim_spec.rb +36 -0
- data/spec/commands/zadd_spec.rb +100 -11
- data/spec/commands/zcard_spec.rb +4 -4
- data/spec/commands/zcount_spec.rb +18 -10
- data/spec/commands/zincrby_spec.rb +6 -6
- data/spec/commands/zinterstore_spec.rb +54 -20
- data/spec/commands/zpopmax_spec.rb +60 -0
- data/spec/commands/zpopmin_spec.rb +60 -0
- data/spec/commands/zrange_spec.rb +54 -13
- data/spec/commands/zrangebyscore_spec.rb +42 -27
- data/spec/commands/zrank_spec.rb +4 -4
- data/spec/commands/zrem_spec.rb +18 -12
- data/spec/commands/zremrangebyrank_spec.rb +5 -5
- data/spec/commands/zremrangebyscore_spec.rb +12 -5
- data/spec/commands/zrevrange_spec.rb +35 -10
- data/spec/commands/zrevrangebyscore_spec.rb +26 -15
- data/spec/commands/zrevrank_spec.rb +4 -4
- data/spec/commands/zscan_each_spec.rb +48 -0
- data/spec/commands/zscan_spec.rb +26 -0
- data/spec/commands/zscore_spec.rb +7 -7
- data/spec/commands/zunionstore_spec.rb +54 -21
- data/spec/mock_redis_spec.rb +61 -0
- data/spec/spec_helper.rb +35 -8
- data/spec/support/redis_multiplexer.rb +62 -37
- data/spec/support/shared_examples/does_not_cleanup_empty_strings.rb +14 -0
- data/spec/support/shared_examples/only_operates_on_hashes.rb +5 -3
- data/spec/support/shared_examples/only_operates_on_lists.rb +5 -3
- data/spec/support/shared_examples/only_operates_on_sets.rb +5 -3
- data/spec/support/shared_examples/only_operates_on_strings.rb +4 -4
- data/spec/support/shared_examples/only_operates_on_zsets.rb +18 -16
- data/spec/support/shared_examples/sorts_enumerables.rb +56 -0
- data/spec/transactions_spec.rb +79 -29
- metadata +162 -42
- data/LICENSE +0 -19
- data/spec/commands/hash_operator_spec.rb +0 -21
data/spec/commands/move_spec.rb
CHANGED
@@ -8,7 +8,7 @@ describe '#move(key, db)' do
|
|
8
8
|
@key = 'mock-redis-test:move'
|
9
9
|
end
|
10
10
|
|
11
|
-
context
|
11
|
+
context 'when key exists in destdb' do
|
12
12
|
before do
|
13
13
|
@redises.set(@key, 'srcvalue')
|
14
14
|
@redises.select(@destdb)
|
@@ -16,81 +16,81 @@ describe '#move(key, db)' do
|
|
16
16
|
@redises.select(@srcdb)
|
17
17
|
end
|
18
18
|
|
19
|
-
it
|
20
|
-
@redises.move(@key, @destdb).should
|
19
|
+
it 'returns false' do
|
20
|
+
@redises.move(@key, @destdb).should == false
|
21
21
|
end
|
22
22
|
|
23
|
-
it
|
23
|
+
it 'leaves destdb/key alone' do
|
24
24
|
@redises.select(@destdb)
|
25
|
-
@redises.get(@key).should ==
|
25
|
+
@redises.get(@key).should == 'destvalue'
|
26
26
|
end
|
27
27
|
|
28
|
-
it
|
29
|
-
@redises.get(@key).should ==
|
28
|
+
it 'leaves srcdb/key alone' do
|
29
|
+
@redises.get(@key).should == 'srcvalue'
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
context
|
33
|
+
context 'when key does not exist in srcdb' do
|
34
34
|
before do
|
35
35
|
@redises.select(@destdb)
|
36
36
|
@redises.set(@key, 'destvalue')
|
37
37
|
@redises.select(@srcdb)
|
38
38
|
end
|
39
39
|
|
40
|
-
it
|
41
|
-
@redises.move(@key, @destdb).should
|
40
|
+
it 'returns false' do
|
41
|
+
@redises.move(@key, @destdb).should == false
|
42
42
|
end
|
43
43
|
|
44
|
-
it
|
44
|
+
it 'leaves destdb/key alone' do
|
45
45
|
@redises.select(@destdb)
|
46
|
-
@redises.get(@key).should ==
|
46
|
+
@redises.get(@key).should == 'destvalue'
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
context
|
50
|
+
context 'when key exists in the currently-selected DB and not in db' do
|
51
51
|
before do
|
52
52
|
@redises.set(@key, 'value')
|
53
53
|
end
|
54
54
|
|
55
|
-
it
|
56
|
-
@redises.move(@key, @destdb).should
|
55
|
+
it 'returns true' do
|
56
|
+
@redises.move(@key, @destdb).should == true
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
context
|
60
|
+
context 'on a string' do
|
61
61
|
before do
|
62
62
|
@redises.set(@key, 'value')
|
63
63
|
@redises.move(@key, @destdb)
|
64
64
|
end
|
65
65
|
|
66
|
-
it
|
67
|
-
@redises.exists(@key).should
|
66
|
+
it 'removes key from srcdb' do
|
67
|
+
@redises.exists?(@key).should == false
|
68
68
|
end
|
69
69
|
|
70
|
-
it
|
70
|
+
it 'copies key to destdb' do
|
71
71
|
@redises.select(@destdb)
|
72
72
|
@redises.get(@key).should == 'value'
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
context
|
76
|
+
context 'on a list' do
|
77
77
|
before do
|
78
78
|
@redises.rpush(@key, 'bert')
|
79
79
|
@redises.rpush(@key, 'ernie')
|
80
80
|
@redises.move(@key, @destdb)
|
81
81
|
end
|
82
82
|
|
83
|
-
it
|
84
|
-
@redises.exists(@key).should
|
83
|
+
it 'removes key from srcdb' do
|
84
|
+
@redises.exists?(@key).should == false
|
85
85
|
end
|
86
86
|
|
87
|
-
it
|
87
|
+
it 'copies key to destdb' do
|
88
88
|
@redises.select(@destdb)
|
89
89
|
@redises.lrange(@key, 0, -1).should == %w[bert ernie]
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
-
context
|
93
|
+
context 'on a hash' do
|
94
94
|
before do
|
95
95
|
@redises.hset(@key, 'a', 1)
|
96
96
|
@redises.hset(@key, 'b', 2)
|
@@ -98,17 +98,17 @@ describe '#move(key, db)' do
|
|
98
98
|
@redises.move(@key, @destdb)
|
99
99
|
end
|
100
100
|
|
101
|
-
it
|
102
|
-
@redises.exists(@key).should
|
101
|
+
it 'removes key from srcdb' do
|
102
|
+
@redises.exists?(@key).should == false
|
103
103
|
end
|
104
104
|
|
105
|
-
it
|
105
|
+
it 'copies key to destdb' do
|
106
106
|
@redises.select(@destdb)
|
107
|
-
@redises.hgetall(@key).should == {'a' =>
|
107
|
+
@redises.hgetall(@key).should == { 'a' => '1', 'b' => '2' }
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
-
context
|
111
|
+
context 'on a set' do
|
112
112
|
before do
|
113
113
|
@redises.sadd(@key, 'beer')
|
114
114
|
@redises.sadd(@key, 'wine')
|
@@ -116,17 +116,17 @@ describe '#move(key, db)' do
|
|
116
116
|
@redises.move(@key, @destdb)
|
117
117
|
end
|
118
118
|
|
119
|
-
it
|
120
|
-
@redises.exists(@key).should
|
119
|
+
it 'removes key from srcdb' do
|
120
|
+
@redises.exists?(@key).should == false
|
121
121
|
end
|
122
122
|
|
123
|
-
it
|
123
|
+
it 'copies key to destdb' do
|
124
124
|
@redises.select(@destdb)
|
125
125
|
@redises.smembers(@key).should == %w[wine beer]
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
129
|
-
context
|
129
|
+
context 'on a zset' do
|
130
130
|
before do
|
131
131
|
@redises.zadd(@key, 1, 'beer')
|
132
132
|
@redises.zadd(@key, 2, 'wine')
|
@@ -134,14 +134,14 @@ describe '#move(key, db)' do
|
|
134
134
|
@redises.move(@key, @destdb)
|
135
135
|
end
|
136
136
|
|
137
|
-
it
|
138
|
-
@redises.exists(@key).should
|
137
|
+
it 'removes key from srcdb' do
|
138
|
+
@redises.exists?(@key).should == false
|
139
139
|
end
|
140
140
|
|
141
|
-
it
|
141
|
+
it 'copies key to destdb' do
|
142
142
|
@redises.select(@destdb)
|
143
143
|
@redises.zrange(@key, 0, -1, :with_scores => true).should ==
|
144
|
-
[[
|
144
|
+
[['beer', 1.0], ['wine', 2.0]]
|
145
145
|
end
|
146
146
|
end
|
147
147
|
end
|
data/spec/commands/mset_spec.rb
CHANGED
@@ -10,20 +10,34 @@ describe '#mset(key, value [, key, value, ...])' do
|
|
10
10
|
@redises.mset(@key1, 1).should == 'OK'
|
11
11
|
end
|
12
12
|
|
13
|
-
it "
|
13
|
+
it "responds with 'OK' for passed Array with 1 item" do
|
14
|
+
@redises.mset([@key1, 1]).should == 'OK'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "responds with 'OK' for passed Array with 2 items" do
|
18
|
+
@redises.mset([@key1, 1, @key2, 2]).should == 'OK'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'sets the values' do
|
14
22
|
@redises.mset(@key1, 'value1', @key2, 'value2')
|
15
23
|
@redises.mget(@key1, @key2).should == %w[value1 value2]
|
16
24
|
end
|
17
25
|
|
18
|
-
it
|
26
|
+
it 'raises an error if given an odd number of arguments' do
|
19
27
|
lambda do
|
20
28
|
@redises.mset(@key1, 'value1', @key2)
|
21
|
-
end.should raise_error(
|
29
|
+
end.should raise_error(Redis::CommandError)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'raises an error if given 0 arguments' do
|
33
|
+
lambda do
|
34
|
+
@redises.mset
|
35
|
+
end.should raise_error(Redis::CommandError)
|
22
36
|
end
|
23
37
|
|
24
|
-
it
|
38
|
+
it 'raises an error if given odd-sized array' do
|
25
39
|
lambda do
|
26
|
-
@redises.mset()
|
27
|
-
end.should raise_error(
|
40
|
+
@redises.mset([@key1, 1, @key2])
|
41
|
+
end.should raise_error(Redis::CommandError)
|
28
42
|
end
|
29
43
|
end
|
@@ -6,35 +6,35 @@ describe '#msetnx(key, value [, key, value, ...])' do
|
|
6
6
|
@key2 = 'mock-redis-test:msetnx2'
|
7
7
|
end
|
8
8
|
|
9
|
-
it
|
10
|
-
@redises.msetnx(@key1, 1).should
|
9
|
+
it 'responds with 1 if any keys were set' do
|
10
|
+
@redises.msetnx(@key1, 1).should == true
|
11
11
|
end
|
12
12
|
|
13
|
-
it
|
13
|
+
it 'sets the values' do
|
14
14
|
@redises.msetnx(@key1, 'value1', @key2, 'value2')
|
15
15
|
@redises.mget(@key1, @key2).should == %w[value1 value2]
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
19
|
-
@redises.set(@key1,
|
18
|
+
it 'does nothing if any value is already set' do
|
19
|
+
@redises.set(@key1, 'old1')
|
20
20
|
@redises.msetnx(@key1, 'value1', @key2, 'value2')
|
21
|
-
@redises.mget(@key1, @key2).should == [
|
21
|
+
@redises.mget(@key1, @key2).should == ['old1', nil]
|
22
22
|
end
|
23
23
|
|
24
|
-
it
|
25
|
-
@redises.set(@key1,
|
26
|
-
@redises.msetnx(@key1, 'value1', @key2, 'value2').should
|
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 == false
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
29
|
+
it 'raises an error if given an odd number of arguments' do
|
30
30
|
lambda do
|
31
31
|
@redises.msetnx(@key1, 'value1', @key2)
|
32
|
-
end.should raise_error(
|
32
|
+
end.should raise_error(Redis::CommandError)
|
33
33
|
end
|
34
34
|
|
35
|
-
it
|
35
|
+
it 'raises an error if given 0 arguments' do
|
36
36
|
lambda do
|
37
|
-
@redises.msetnx
|
38
|
-
end.should raise_error(
|
37
|
+
@redises.msetnx
|
38
|
+
end.should raise_error(Redis::CommandError)
|
39
39
|
end
|
40
40
|
end
|
@@ -1,31 +1,31 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#persist(key)' do
|
4
4
|
before do
|
5
5
|
@key = 'mock-redis-test:persist'
|
6
6
|
@redises.set(@key, 'spork')
|
7
7
|
end
|
8
8
|
|
9
|
-
it
|
10
|
-
@redises.expire(@key,
|
11
|
-
@redises.persist(@key).should
|
9
|
+
it 'returns true for a key with a timeout' do
|
10
|
+
@redises.expire(@key, 10_000)
|
11
|
+
@redises.persist(@key).should == true
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
15
|
-
@redises.persist(@key).should
|
14
|
+
it 'returns false for a key with no timeout' do
|
15
|
+
@redises.persist(@key).should == false
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
19
|
-
@redises.persist('mock-redis-test:nonesuch').should
|
18
|
+
it 'returns false for a key that does not exist' do
|
19
|
+
@redises.persist('mock-redis-test:nonesuch').should == false
|
20
20
|
end
|
21
21
|
|
22
|
-
it
|
23
|
-
@redises.expire(@key,
|
22
|
+
it 'removes the timeout' do
|
23
|
+
@redises.expire(@key, 10_000)
|
24
24
|
@redises.persist(@key)
|
25
|
-
@redises.persist(@key).should
|
25
|
+
@redises.persist(@key).should == false
|
26
26
|
end
|
27
27
|
|
28
|
-
context
|
28
|
+
context '[mock only]' do
|
29
29
|
# These are mock-only since we can't actually manipulate time in
|
30
30
|
# the real Redis.
|
31
31
|
|
@@ -35,15 +35,14 @@ describe "#persist(key)" do
|
|
35
35
|
|
36
36
|
before do
|
37
37
|
@now = Time.now
|
38
|
-
Time.stub
|
38
|
+
Time.stub(:now).and_return(@now)
|
39
39
|
end
|
40
40
|
|
41
|
-
it
|
41
|
+
it 'makes keys stay around' do
|
42
42
|
@mock.expire(@key, 5)
|
43
43
|
@mock.persist(@key)
|
44
|
-
Time.stub
|
44
|
+
Time.stub(:now).and_return(@now + 5)
|
45
45
|
@mock.get(@key).should_not be_nil
|
46
46
|
end
|
47
|
-
|
48
47
|
end
|
49
48
|
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#pexpire(key, ms)' do
|
4
|
+
before do
|
5
|
+
@key = 'mock-redis-test:pexpire'
|
6
|
+
@redises.set(@key, 'spork')
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'returns true for a key that exists' do
|
10
|
+
@redises.pexpire(@key, 1).should == true
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns false for a key that does not exist' do
|
14
|
+
@redises.pexpire('mock-redis-test:nonesuch', 1).should == false
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'removes a key immediately when ms==0' do
|
18
|
+
@redises.pexpire(@key, 0)
|
19
|
+
@redises.get(@key).should be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'raises an error if ms is bogus' do
|
23
|
+
lambda do
|
24
|
+
@redises.pexpire(@key, 'a couple minutes or so')
|
25
|
+
end.should raise_error(Redis::CommandError)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'stringifies key' do
|
29
|
+
@redises.pexpire(@key.to_sym, 9).should == true
|
30
|
+
end
|
31
|
+
|
32
|
+
context '[mock only]' do
|
33
|
+
# These are mock-only since we can't actually manipulate time in
|
34
|
+
# the real Redis.
|
35
|
+
|
36
|
+
before(:all) do
|
37
|
+
@mock = @redises.mock
|
38
|
+
end
|
39
|
+
|
40
|
+
before do
|
41
|
+
@now = Time.now.round
|
42
|
+
Time.stub(:now).and_return(@now)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'removes keys after enough time has passed' do
|
46
|
+
@mock.pexpire(@key, 5)
|
47
|
+
Time.stub(:now).and_return(@now + Rational(6, 1000))
|
48
|
+
@mock.get(@key).should be_nil
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'updates an existing pexpire time' do
|
52
|
+
@mock.pexpire(@key, 5)
|
53
|
+
@mock.pexpire(@key, 6)
|
54
|
+
|
55
|
+
Time.stub(:now).and_return(@now + Rational(5, 1000))
|
56
|
+
@mock.get(@key).should_not be_nil
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'expirations on a deleted key' do
|
60
|
+
before { @mock.del(@key) }
|
61
|
+
|
62
|
+
it 'cleans up the expiration once the key is gone (string)' do
|
63
|
+
@mock.set(@key, 'string')
|
64
|
+
@mock.pexpire(@key, 2)
|
65
|
+
@mock.del(@key)
|
66
|
+
@mock.set(@key, 'string')
|
67
|
+
|
68
|
+
Time.stub(:now).and_return(@now + 0.003)
|
69
|
+
|
70
|
+
@mock.get(@key).should_not be_nil
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'cleans up the expiration once the key is gone (list)' do
|
74
|
+
@mock.rpush(@key, 'coconuts')
|
75
|
+
@mock.pexpire(@key, 2)
|
76
|
+
@mock.rpop(@key)
|
77
|
+
|
78
|
+
@mock.rpush(@key, 'coconuts')
|
79
|
+
|
80
|
+
Time.stub(:now).and_return(@now + 0.003)
|
81
|
+
|
82
|
+
@mock.lindex(@key, 0).should_not be_nil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#pexpireat(key, timestamp_ms)' do
|
4
|
+
before do
|
5
|
+
@key = 'mock-redis-test:pexpireat'
|
6
|
+
@redises.set(@key, 'spork')
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'returns true for a key that exists' do
|
10
|
+
@redises.pexpireat(@key, (Time.now.to_f * 1000).to_i + 1).should == true
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns false for a key that does not exist' do
|
14
|
+
@redises.pexpireat('mock-redis-test:nonesuch',
|
15
|
+
(Time.now.to_f * 1000).to_i + 1).should == false
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'removes a key immediately when timestamp is now' do
|
19
|
+
@redises.pexpireat(@key, (Time.now.to_f * 1000).to_i)
|
20
|
+
@redises.get(@key).should be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it "raises an error if you don't give it a Unix timestamp" do
|
24
|
+
lambda do
|
25
|
+
@redises.pexpireat(@key, Time.now) # oops, forgot .to_i
|
26
|
+
end.should raise_error(Redis::CommandError)
|
27
|
+
end
|
28
|
+
|
29
|
+
context '[mock only]' do
|
30
|
+
# These are mock-only since we can't actually manipulate time in
|
31
|
+
# the real Redis.
|
32
|
+
|
33
|
+
before(:all) do
|
34
|
+
@mock = @redises.mock
|
35
|
+
end
|
36
|
+
|
37
|
+
before do
|
38
|
+
@now = Time.now
|
39
|
+
Time.stub(:now).and_return(@now)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'removes keys after enough time has passed' do
|
43
|
+
@mock.pexpireat(@key, (@now.to_f * 1000).to_i + 5)
|
44
|
+
Time.stub(:now).and_return(@now + 0.006)
|
45
|
+
@mock.get(@key).should be_nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/commands/ping_spec.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
4
|
-
it
|
3
|
+
describe '#ping' do
|
4
|
+
it 'returns "PONG" with no arguments' do
|
5
5
|
@redises.ping.should == 'PONG'
|
6
6
|
end
|
7
|
+
|
8
|
+
it 'returns the argument' do
|
9
|
+
@redises.ping('HELLO').should == 'HELLO'
|
10
|
+
end
|
7
11
|
end
|
@@ -9,15 +9,106 @@ describe '#pipelined' do
|
|
9
9
|
res.should == true
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
context 'with a few added data' do
|
13
|
+
let(:key1) { 'hello' }
|
14
|
+
let(:key2) { 'world' }
|
15
|
+
let(:value1) { 'foo' }
|
16
|
+
let(:value2) { 'bar' }
|
15
17
|
|
16
|
-
|
17
|
-
@redises.
|
18
|
-
@redises.
|
18
|
+
before do
|
19
|
+
@redises.set key1, value1
|
20
|
+
@redises.set key2, value2
|
19
21
|
end
|
20
22
|
|
21
|
-
results
|
23
|
+
it 'returns results of pipelined operations' do
|
24
|
+
results = @redises.pipelined do |redis|
|
25
|
+
redis.get key1
|
26
|
+
redis.get key2
|
27
|
+
end
|
28
|
+
|
29
|
+
results.should == [value1, value2]
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'returns futures' do
|
33
|
+
future = nil
|
34
|
+
|
35
|
+
@redises.mock.pipelined do |redis|
|
36
|
+
future = redis.get key1
|
37
|
+
end
|
38
|
+
|
39
|
+
future.class.should be MockRedis::Future
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'with pipelined operations returning array replies' do
|
44
|
+
let(:key1) { 'colors' }
|
45
|
+
let(:key2) { 'seasons' }
|
46
|
+
let(:value1) { %w[blue yellow] }
|
47
|
+
let(:value2) { %w[summer winter] }
|
48
|
+
|
49
|
+
before do
|
50
|
+
@redises.rpush(key1, value1)
|
51
|
+
@redises.rpush(key2, value2)
|
52
|
+
end
|
53
|
+
|
54
|
+
after do
|
55
|
+
@redises.del key1
|
56
|
+
@redises.del key2
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'returns an array of the array replies' do
|
60
|
+
results = @redises.pipelined do |_redis|
|
61
|
+
@redises.lrange(key1, 0, -1)
|
62
|
+
@redises.lrange(key2, 0, -1)
|
63
|
+
end
|
64
|
+
|
65
|
+
results.should == [value1, value2]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'with redis time return value' do
|
70
|
+
let(:time_stub) { double 'Time', :now => Time.new(2019, 1, 2, 3, 4, 6, '+00:00') }
|
71
|
+
let(:options) { { :time_class => time_stub } }
|
72
|
+
|
73
|
+
subject { MockRedis.new(options) }
|
74
|
+
|
75
|
+
it 'returns the time value' do
|
76
|
+
subject.set('foo', 'bar')
|
77
|
+
|
78
|
+
results = subject.pipelined do
|
79
|
+
subject.get('foo')
|
80
|
+
subject.host # defined on MockRedis, so not captured
|
81
|
+
subject.time
|
82
|
+
subject.echo('baz')
|
83
|
+
end
|
84
|
+
|
85
|
+
expect(results).to eq(['bar', [1_546_398_246, 0], 'baz'])
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'with nested pipelines' do
|
90
|
+
let(:key1) { 'hello' }
|
91
|
+
let(:key2) { 'world' }
|
92
|
+
let(:value1) { 'foo' }
|
93
|
+
let(:value2) { 'bar' }
|
94
|
+
|
95
|
+
before do
|
96
|
+
@redises.set key1, value1
|
97
|
+
@redises.set key2, value2
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'returns results of the nested pipelines' do
|
101
|
+
results = @redises.pipelined do |pr1|
|
102
|
+
pr1.pipelined do |pr2|
|
103
|
+
pr2.get key1
|
104
|
+
end
|
105
|
+
|
106
|
+
pr1.pipelined do |pr2|
|
107
|
+
pr2.get key2
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
results.should == [value1, value2]
|
112
|
+
end
|
22
113
|
end
|
23
114
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#pttl(key)' do
|
4
|
+
before do
|
5
|
+
@key = 'mock-redis-test:persist'
|
6
|
+
@redises.set(@key, 'spork')
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'returns -1 for a key with no expiration' do
|
10
|
+
@redises.pttl(@key).should == -1
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns -2 for a key that does not exist' do
|
14
|
+
@redises.pttl('mock-redis-test:nonesuch').should == -2
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'stringifies key' do
|
18
|
+
@redises.expire(@key, 8)
|
19
|
+
# Don't check against Redis since millisecond timing differences are likely
|
20
|
+
@redises.send_without_checking(:pttl, @key.to_sym).should > 0
|
21
|
+
end
|
22
|
+
|
23
|
+
context '[mock only]' do
|
24
|
+
# These are mock-only since we can't actually manipulate time in
|
25
|
+
# the real Redis.
|
26
|
+
|
27
|
+
before(:all) do
|
28
|
+
@mock = @redises.mock
|
29
|
+
end
|
30
|
+
|
31
|
+
before do
|
32
|
+
@now = Time.now.round
|
33
|
+
Time.stub(:now).and_return(@now)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "gives you the key's remaining lifespan in milliseconds" do
|
37
|
+
@mock.expire(@key, 5)
|
38
|
+
@mock.pttl(@key).should == 5000
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe '#randomkey [mock only]' do
|
4
4
|
before { @mock = @redises.mock }
|
5
5
|
|
6
|
-
it
|
6
|
+
it 'finds a random key' do
|
7
7
|
@keys = ['mock-redis-test:1', 'mock-redis-test:2', 'mock-redis-test:3']
|
8
8
|
|
9
9
|
@keys.each do |k|
|
@@ -13,8 +13,8 @@ describe '#randomkey [mock only]' do
|
|
13
13
|
@keys.should include(@mock.randomkey)
|
14
14
|
end
|
15
15
|
|
16
|
-
it
|
17
|
-
@mock.keys('*').each {|k| @mock.del(k)}
|
16
|
+
it 'returns nil when there are no keys' do
|
17
|
+
@mock.keys('*').each { |k| @mock.del(k) }
|
18
18
|
@mock.randomkey.should be_nil
|
19
19
|
end
|
20
20
|
end
|