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
@@ -1,27 +1,27 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#hexists(key, field)' do
|
4
4
|
before do
|
5
|
-
@key =
|
5
|
+
@key = 'mock-redis-test:hexists'
|
6
6
|
@redises.hset(@key, 'field', 'value')
|
7
7
|
end
|
8
8
|
|
9
|
-
it
|
10
|
-
@redises.hexists(@key, 'field').should
|
9
|
+
it 'returns true if the hash has that field' do
|
10
|
+
@redises.hexists(@key, 'field').should == true
|
11
11
|
end
|
12
12
|
|
13
|
-
it
|
14
|
-
@redises.hexists(@key, 'nonesuch').should
|
13
|
+
it 'returns false if the hash lacks that field' do
|
14
|
+
@redises.hexists(@key, 'nonesuch').should == false
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'treats the field as a string' do
|
18
18
|
@redises.hset(@key, 1, 'one')
|
19
|
-
@redises.hexists(@key, 1).should
|
19
|
+
@redises.hexists(@key, 1).should == true
|
20
20
|
end
|
21
21
|
|
22
|
-
it
|
23
|
-
@redises.hexists('mock-redis-test:nonesuch', 'key').should
|
22
|
+
it 'returns nil when there is no such key' do
|
23
|
+
@redises.hexists('mock-redis-test:nonesuch', 'key').should == false
|
24
24
|
end
|
25
25
|
|
26
|
-
it_should_behave_like
|
26
|
+
it_should_behave_like 'a hash-only command'
|
27
27
|
end
|
data/spec/commands/hget_spec.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#hget(key, field)' do
|
4
4
|
before do
|
5
|
-
@key =
|
5
|
+
@key = 'mock-redis-test:hget'
|
6
6
|
@redises.hset(@key, 'k1', 'v1')
|
7
7
|
@redises.hset(@key, 'k2', 'v2')
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
10
|
+
it 'returns the value stored at field' do
|
11
11
|
@redises.hget(@key, 'k1').should == 'v1'
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
14
|
+
it 'treats the field as a string' do
|
15
15
|
@redises.hset(@key, '3', 'v3')
|
16
16
|
@redises.hget(@key, 3).should == 'v3'
|
17
17
|
end
|
18
18
|
|
19
|
-
it
|
19
|
+
it 'returns nil when there is no such field' do
|
20
20
|
@redises.hget(@key, 'nonesuch').should be_nil
|
21
21
|
end
|
22
22
|
|
23
|
-
it
|
23
|
+
it 'returns nil when there is no such key' do
|
24
24
|
@redises.hget('mock-redis-test:nonesuch', 'k1').should be_nil
|
25
25
|
end
|
26
26
|
|
27
|
-
it_should_behave_like
|
27
|
+
it_should_behave_like 'a hash-only command'
|
28
28
|
end
|
@@ -1,22 +1,32 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#hgetall(key)' do
|
4
4
|
before do
|
5
|
-
@key =
|
5
|
+
@key = 'mock-redis-test:hgetall'
|
6
6
|
@redises.hset(@key, 'k1', 'v1')
|
7
7
|
@redises.hset(@key, 'k2', 'v2')
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
10
|
+
it 'returns the (key, value) pairs stored in the hash' do
|
11
11
|
@redises.hgetall(@key).should == {
|
12
12
|
'k1' => 'v1',
|
13
13
|
'k2' => 'v2',
|
14
14
|
}
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'returns [] when there is no such key' do
|
18
18
|
@redises.hgetall('mock-redis-test:nonesuch').should == {}
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
it "doesn't return a mutable reference to the returned data" do
|
22
|
+
mr = MockRedis.new
|
23
|
+
mr.hset(@key, 'k1', 'v1')
|
24
|
+
mr.hset(@key, 'k2', 'v2')
|
25
|
+
hash = mr.hgetall(@key)
|
26
|
+
hash['dont'] = 'mutate'
|
27
|
+
new_hash = mr.hgetall(@key)
|
28
|
+
new_hash.keys.sort.should == %w[k1 k2]
|
29
|
+
end
|
30
|
+
|
31
|
+
it_should_behave_like 'a hash-only command'
|
22
32
|
end
|
@@ -6,53 +6,53 @@ describe '#hincrby(key, field, increment)' do
|
|
6
6
|
@field = 'count'
|
7
7
|
end
|
8
8
|
|
9
|
-
it
|
9
|
+
it 'returns the value after the increment' do
|
10
10
|
@redises.hset(@key, @field, 2)
|
11
11
|
@redises.hincrby(@key, @field, 2).should == 4
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
14
|
+
it 'treats a missing key like 0' do
|
15
15
|
@redises.hincrby(@key, @field, 1).should == 1
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
18
|
+
it 'creates a hash if nothing is present' do
|
19
19
|
@redises.hincrby(@key, @field, 1)
|
20
|
-
@redises.hget(@key, @field).should ==
|
20
|
+
@redises.hget(@key, @field).should == '1'
|
21
21
|
end
|
22
22
|
|
23
|
-
it
|
23
|
+
it 'increments negative numbers' do
|
24
24
|
@redises.hset(@key, @field, -10)
|
25
25
|
@redises.hincrby(@key, @field, 2).should == -8
|
26
26
|
end
|
27
27
|
|
28
|
-
it
|
28
|
+
it 'works multiple times' do
|
29
29
|
@redises.hincrby(@key, @field, 2).should == 2
|
30
30
|
@redises.hincrby(@key, @field, 2).should == 4
|
31
31
|
@redises.hincrby(@key, @field, 2).should == 6
|
32
32
|
end
|
33
33
|
|
34
|
-
it
|
35
|
-
@redises.hincrby(@key, @field,
|
34
|
+
it 'accepts an integer-ish string' do
|
35
|
+
@redises.hincrby(@key, @field, '2').should == 2
|
36
36
|
end
|
37
37
|
|
38
|
-
it
|
38
|
+
it 'treats the field as a string' do
|
39
39
|
field = 11
|
40
40
|
@redises.hset(@key, field, 2)
|
41
41
|
@redises.hincrby(@key, field, 2).should == 4
|
42
42
|
end
|
43
43
|
|
44
|
-
it
|
45
|
-
@redises.hset(@key, @field,
|
44
|
+
it 'raises an error if the value does not look like an integer' do
|
45
|
+
@redises.hset(@key, @field, 'one')
|
46
46
|
lambda do
|
47
47
|
@redises.hincrby(@key, @field, 1)
|
48
|
-
end.should raise_error(
|
48
|
+
end.should raise_error(Redis::CommandError)
|
49
49
|
end
|
50
50
|
|
51
|
-
it
|
51
|
+
it 'raises an error if the delta does not look like an integer' do
|
52
52
|
lambda do
|
53
|
-
@redises.hincrby(@key, @field,
|
54
|
-
end.should raise_error(
|
53
|
+
@redises.hincrby(@key, @field, 'foo')
|
54
|
+
end.should raise_error(Redis::CommandError)
|
55
55
|
end
|
56
56
|
|
57
|
-
it_should_behave_like
|
57
|
+
it_should_behave_like 'a hash-only command'
|
58
58
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#hincrbyfloat(key, field, increment)' do
|
4
|
+
before do
|
5
|
+
@key = 'mock-redis-test:hincrbyfloat'
|
6
|
+
@field = 'count'
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'returns the value after the increment' do
|
10
|
+
@redises.hset(@key, @field, 2.0)
|
11
|
+
@redises.hincrbyfloat(@key, @field, 2.1).should be_within(0.0001).of(4.1)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'treats a missing key like 0' do
|
15
|
+
@redises.hincrbyfloat(@key, @field, 1.2).should be_within(0.0001).of(1.2)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'creates a hash if nothing is present' do
|
19
|
+
@redises.hincrbyfloat(@key, @field, 1.0)
|
20
|
+
@redises.hget(@key, @field).should == '1'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'increments negative numbers' do
|
24
|
+
@redises.hset(@key, @field, -10.4)
|
25
|
+
@redises.hincrbyfloat(@key, @field, 2.3).should be_within(0.0001).of(-8.1)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'works multiple times' do
|
29
|
+
@redises.hincrbyfloat(@key, @field, 2.1).should be_within(0.0001).of(2.1)
|
30
|
+
@redises.hincrbyfloat(@key, @field, 2.2).should be_within(0.0001).of(4.3)
|
31
|
+
@redises.hincrbyfloat(@key, @field, 2.3).should be_within(0.0001).of(6.6)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'accepts a float-ish string' do
|
35
|
+
@redises.hincrbyfloat(@key, @field, '2.2').should be_within(0.0001).of(2.2)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'treats the field as a string' do
|
39
|
+
field = 11
|
40
|
+
@redises.hset(@key, field, 2)
|
41
|
+
@redises.hincrbyfloat(@key, field, 2).should == 4.0
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'raises an error if the value does not look like a float' do
|
45
|
+
@redises.hset(@key, @field, 'one.two')
|
46
|
+
lambda do
|
47
|
+
@redises.hincrbyfloat(@key, @field, 1)
|
48
|
+
end.should raise_error(Redis::CommandError)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'raises an error if the delta does not look like a float' do
|
52
|
+
lambda do
|
53
|
+
@redises.hincrbyfloat(@key, @field, 'foobar.baz')
|
54
|
+
end.should raise_error(Redis::CommandError)
|
55
|
+
end
|
56
|
+
|
57
|
+
it_should_behave_like 'a hash-only command'
|
58
|
+
end
|
data/spec/commands/hkeys_spec.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#hkeys(key)' do
|
4
4
|
before do
|
5
|
-
@key =
|
5
|
+
@key = 'mock-redis-test:hkeys'
|
6
6
|
@redises.hset(@key, 'k1', 'v1')
|
7
7
|
@redises.hset(@key, 'k2', 'v2')
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
10
|
+
it 'returns the keys stored in the hash' do
|
11
11
|
@redises.hkeys(@key).sort.should == %w[k1 k2]
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
14
|
+
it 'returns [] when there is no such key' do
|
15
15
|
@redises.hkeys('mock-redis-test:nonesuch').should == []
|
16
16
|
end
|
17
17
|
|
18
|
-
it_should_behave_like
|
18
|
+
it_should_behave_like 'a hash-only command'
|
19
19
|
end
|
data/spec/commands/hlen_spec.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#hlen(key)' do
|
4
4
|
before do
|
5
|
-
@key =
|
5
|
+
@key = 'mock-redis-test:hlen'
|
6
6
|
@redises.hset(@key, 'k1', 'v1')
|
7
7
|
@redises.hset(@key, 'k2', 'v2')
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
10
|
+
it 'returns the number of keys stored in the hash' do
|
11
11
|
@redises.hlen(@key).should == 2
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
14
|
+
it 'returns [] when there is no such key' do
|
15
15
|
@redises.hlen('mock-redis-test:nonesuch').should == 0
|
16
16
|
end
|
17
17
|
|
18
|
-
it_should_behave_like
|
18
|
+
it_should_behave_like 'a hash-only command'
|
19
19
|
end
|
data/spec/commands/hmget_spec.rb
CHANGED
@@ -1,36 +1,46 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#hmget(key, field [, field, ...])' do
|
4
4
|
before do
|
5
|
-
@key =
|
5
|
+
@key = 'mock-redis-test:hmget'
|
6
6
|
@redises.hset(@key, 'k1', 'v1')
|
7
7
|
@redises.hset(@key, 'k2', 'v2')
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
10
|
+
it 'returns the values for those keys' do
|
11
11
|
@redises.hmget(@key, 'k1', 'k2').sort.should == %w[v1 v2]
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
14
|
+
it 'treats an array as multiple keys' do
|
15
|
+
@redises.hmget(@key, %w[k1 k2]).sort.should == %w[v1 v2]
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'treats the fielsd as strings' do
|
15
19
|
@redises.hset(@key, 1, 'one')
|
16
20
|
@redises.hset(@key, 2, 'two')
|
17
21
|
@redises.hmget(@key, 1, 2).sort.should == %w[one two]
|
18
22
|
end
|
19
23
|
|
20
|
-
it
|
24
|
+
it 'returns nils when there are no such fields' do
|
21
25
|
@redises.hmget(@key, 'k1', 'mock-redis-test:nonesuch').
|
22
26
|
should == ['v1', nil]
|
23
27
|
end
|
24
28
|
|
25
|
-
it
|
29
|
+
it 'returns nils when there is no such key' do
|
26
30
|
@redises.hmget(@key, 'mock-redis-test:nonesuch').should == [nil]
|
27
31
|
end
|
28
32
|
|
29
|
-
it
|
33
|
+
it 'raises an error if given no fields' do
|
30
34
|
lambda do
|
31
35
|
@redises.hmget(@key)
|
32
|
-
end.should raise_error(
|
36
|
+
end.should raise_error(Redis::CommandError)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'raises an error if given an empty list of fields' do
|
40
|
+
lambda do
|
41
|
+
@redises.hmget(@key, [])
|
42
|
+
end.should raise_error(Redis::CommandError)
|
33
43
|
end
|
34
44
|
|
35
|
-
it_should_behave_like
|
45
|
+
it_should_behave_like 'a hash-only command'
|
36
46
|
end
|
data/spec/commands/hmset_spec.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#hmset(key, field, value [, field, value ...])' do
|
4
4
|
before do
|
5
|
-
@key =
|
5
|
+
@key = 'mock-redis-test:hmset'
|
6
6
|
end
|
7
7
|
|
8
8
|
it "returns 'OK'" do
|
9
|
-
@redises.hmset(@key, 'k1', 'v1', 'k2', 'v2').should ==
|
9
|
+
@redises.hmset(@key, 'k1', 'v1', 'k2', 'v2').should == 'OK'
|
10
10
|
end
|
11
11
|
|
12
|
-
it
|
12
|
+
it 'sets the values' do
|
13
13
|
@redises.hmset(@key, 'k1', 'v1', 'k2', 'v2')
|
14
14
|
@redises.hmget(@key, 'k1', 'k2').should == %w[v1 v2]
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'updates an existing hash' do
|
18
18
|
@redises.hset(@key, 'foo', 'bar')
|
19
19
|
@redises.hmset(@key, 'bert', 'ernie', 'diet', 'coke')
|
20
20
|
|
@@ -22,22 +22,48 @@ describe "#hmset(key, field, value [, field, value ...])" do
|
|
22
22
|
should == %w[bar ernie coke]
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
25
|
+
it 'stores the values as strings' do
|
26
26
|
@redises.hmset(@key, 'one', 1)
|
27
|
-
@redises.hget(@key, 'one').should ==
|
27
|
+
@redises.hget(@key, 'one').should == '1'
|
28
28
|
end
|
29
29
|
|
30
|
-
it
|
30
|
+
it 'raises an error if given no fields or values' do
|
31
31
|
lambda do
|
32
32
|
@redises.hmset(@key)
|
33
|
-
end.should raise_error(
|
33
|
+
end.should raise_error(Redis::CommandError)
|
34
34
|
end
|
35
35
|
|
36
|
-
it
|
36
|
+
it 'raises an error if given an odd number of fields+values' do
|
37
37
|
lambda do
|
38
38
|
@redises.hmset(@key, 'k1', 1, 'k2')
|
39
|
-
end.should raise_error(
|
39
|
+
end.should raise_error(Redis::CommandError)
|
40
40
|
end
|
41
41
|
|
42
|
-
|
42
|
+
# The following tests address https://github.com/sds/mock_redis/issues/170
|
43
|
+
context 'keys are stored as strings' do
|
44
|
+
before do
|
45
|
+
@redises.hmset(1, :foo, :bar)
|
46
|
+
@redises.hmset(:a_sym, :boo, :bas)
|
47
|
+
end
|
48
|
+
|
49
|
+
it { expect(@redises.hgetall('1')).to eq @redises.hgetall(1) }
|
50
|
+
it { expect(@redises.hgetall('a_sym')).to eq @redises.hgetall(:a_sym) }
|
51
|
+
it { expect(@redises.del('1')).to eq 1 }
|
52
|
+
it { expect(@redises.del(1)).to eq 1 }
|
53
|
+
it { expect(@redises.del('a_sym')).to eq 1 }
|
54
|
+
it { expect(@redises.del(:a_sym)).to eq 1 }
|
55
|
+
|
56
|
+
after do
|
57
|
+
@redises.del(1)
|
58
|
+
@redises.del(:a_sym)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# The following tests address https://github.com/sds/mock_redis/issues/134
|
63
|
+
context 'hmset accepts an array as its only argument' do
|
64
|
+
it { expect(@redises.hmset([@key, :bar, :bas])).to eq 'OK' }
|
65
|
+
it { lambda { @redises.hmset([:foo, :bar]) }.should raise_error(Redis::CommandError) }
|
66
|
+
end
|
67
|
+
|
68
|
+
it_should_behave_like 'a hash-only command'
|
43
69
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#hscan_each' do
|
4
|
+
subject { MockRedis::Database.new(self) }
|
5
|
+
|
6
|
+
let(:key) { 'mock-redis-test:hscan_each' }
|
7
|
+
|
8
|
+
before do
|
9
|
+
allow(subject).to receive(:hgetall).and_return(collection)
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'when no keys are found' do
|
13
|
+
let(:collection) { [] }
|
14
|
+
|
15
|
+
it 'does not iterate over any elements' do
|
16
|
+
results = subject.hscan_each(key).map do |k, v|
|
17
|
+
[k, v]
|
18
|
+
end
|
19
|
+
expect(results).to match_array(collection)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'when keys are found' do
|
24
|
+
context 'when no match filter is supplied' do
|
25
|
+
let(:collection) { Array.new(20) { |i| ["k#{i}", "v#{i}"] } }
|
26
|
+
|
27
|
+
it 'iterates over each item in the collection' do
|
28
|
+
results = subject.hscan_each(key).map do |k, v|
|
29
|
+
[k, v]
|
30
|
+
end
|
31
|
+
expect(results).to match_array(collection)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when giving a custom match filter' do
|
36
|
+
let(:match) { 'k1*' }
|
37
|
+
let(:collection) { Array.new(12) { |i| ["k#{i}", "v#{i}"] } }
|
38
|
+
let(:expected) { [%w[k1 v1], %w[k10 v10], %w[k11 v11]] }
|
39
|
+
|
40
|
+
it 'iterates over each item in the filtered collection' do
|
41
|
+
results = subject.hscan_each(key, match: match).map do |k, v|
|
42
|
+
[k, v]
|
43
|
+
end
|
44
|
+
expect(results).to match_array(expected)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#hscan' do
|
4
|
+
let(:count) { 10 }
|
5
|
+
let(:match) { '*' }
|
6
|
+
let(:key) { 'mock-redis-test:hscan' }
|
7
|
+
|
8
|
+
context 'when the hash does not exist' do
|
9
|
+
it 'returns a 0 cursor and an empty collection' do
|
10
|
+
expect(@redises.hscan(key, 0, count: count, match: match)).to eq(['0', []])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when the hash exists' do
|
15
|
+
before do
|
16
|
+
@redises.hset(key, 'k1', 'v1')
|
17
|
+
@redises.hset(key, 'k2', 'v2')
|
18
|
+
@redises.hset(key, 'k3', 'v3')
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:expected) { ['0', [%w[k1 v1], %w[k2 v2], %w[k3 v3]]] }
|
22
|
+
|
23
|
+
it 'returns a 0 cursor and the collection' do
|
24
|
+
expect(@redises.hscan(key, 0, count: 10)).to eq(expected)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/commands/hset_spec.rb
CHANGED
@@ -1,28 +1,42 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#hset(key, field)' do
|
4
4
|
before do
|
5
|
-
@key =
|
5
|
+
@key = 'mock-redis-test:hset'
|
6
6
|
end
|
7
7
|
|
8
|
-
it
|
9
|
-
@redises.hset(@key, 'k1', 'v1').should ==
|
8
|
+
it 'returns 1 if the key does not exist' do
|
9
|
+
@redises.hset(@key, 'k1', 'v1').should == 1
|
10
10
|
end
|
11
11
|
|
12
|
-
it
|
12
|
+
it 'returns 1 if the key exists but the field does not' do
|
13
|
+
@redises.hset(@key, 'k1', 'v1')
|
14
|
+
@redises.hset(@key, 'k2', 'v2').should == 1
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns 0 if the field already exists' do
|
18
|
+
@redises.hset(@key, 'k1', 'v1')
|
19
|
+
@redises.hset(@key, 'k1', 'v1').should == 0
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'creates a hash there is no such field' do
|
13
23
|
@redises.hset(@key, 'k1', 'v1')
|
14
24
|
@redises.hget(@key, 'k1').should == 'v1'
|
15
25
|
end
|
16
26
|
|
17
|
-
it
|
18
|
-
@redises.hset(@key,
|
19
|
-
@redises.hget(@key, 'num').should ==
|
27
|
+
it 'stores values as strings' do
|
28
|
+
@redises.hset(@key, 'num', 1)
|
29
|
+
@redises.hget(@key, 'num').should == '1'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'stores fields as strings' do
|
33
|
+
@redises.hset(@key, 1, 'one')
|
34
|
+
@redises.hget(@key, '1').should == 'one'
|
20
35
|
end
|
21
36
|
|
22
|
-
it
|
23
|
-
@redises.hset(@key,
|
24
|
-
@redises.hget(@key, "1").should == "one"
|
37
|
+
it 'stores fields sent in a hash' do
|
38
|
+
@redises.hset(@key, { 'k1' => 'v1', 'k2' => 'v2' }).should == 2
|
25
39
|
end
|
26
40
|
|
27
|
-
it_should_behave_like
|
41
|
+
it_should_behave_like 'a hash-only command'
|
28
42
|
end
|
@@ -1,44 +1,44 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#hsetnx(key, field)' do
|
4
4
|
before do
|
5
|
-
@key =
|
5
|
+
@key = 'mock-redis-test:hsetnx'
|
6
6
|
end
|
7
7
|
|
8
|
-
it
|
9
|
-
@redises.hsetnx(@key, 'field', 'val').should
|
8
|
+
it 'returns true if the field is absent' do
|
9
|
+
@redises.hsetnx(@key, 'field', 'val').should == true
|
10
10
|
end
|
11
11
|
|
12
|
-
it
|
12
|
+
it 'returns 0 if the field is present' do
|
13
13
|
@redises.hset(@key, 'field', 'val')
|
14
|
-
@redises.hsetnx(@key, 'field', 'val').should
|
14
|
+
@redises.hsetnx(@key, 'field', 'val').should == false
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'leaves the field unchanged if the field is present' do
|
18
18
|
@redises.hset(@key, 'field', 'old')
|
19
19
|
@redises.hsetnx(@key, 'field', 'new')
|
20
20
|
@redises.hget(@key, 'field').should == 'old'
|
21
21
|
end
|
22
22
|
|
23
|
-
it
|
23
|
+
it 'sets the field if the field is absent' do
|
24
24
|
@redises.hsetnx(@key, 'field', 'new')
|
25
25
|
@redises.hget(@key, 'field').should == 'new'
|
26
26
|
end
|
27
27
|
|
28
|
-
it
|
28
|
+
it 'creates a hash if there is no such field' do
|
29
29
|
@redises.hsetnx(@key, 'field', 'val')
|
30
30
|
@redises.hget(@key, 'field').should == 'val'
|
31
31
|
end
|
32
32
|
|
33
|
-
it
|
34
|
-
@redises.hsetnx(@key,
|
35
|
-
@redises.hget(@key, 'num').should ==
|
33
|
+
it 'stores values as strings' do
|
34
|
+
@redises.hsetnx(@key, 'num', 1)
|
35
|
+
@redises.hget(@key, 'num').should == '1'
|
36
36
|
end
|
37
37
|
|
38
|
-
it
|
39
|
-
@redises.hsetnx(@key, 1,
|
40
|
-
@redises.hget(@key,
|
38
|
+
it 'stores fields as strings' do
|
39
|
+
@redises.hsetnx(@key, 1, 'one')
|
40
|
+
@redises.hget(@key, '1').should == 'one'
|
41
41
|
end
|
42
42
|
|
43
|
-
it_should_behave_like
|
43
|
+
it_should_behave_like 'a hash-only command'
|
44
44
|
end
|
data/spec/commands/hvals_spec.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe '#hvals(key)' do
|
4
4
|
before do
|
5
|
-
@key =
|
5
|
+
@key = 'mock-redis-test:hvals'
|
6
6
|
@redises.hset(@key, 'k1', 'v1')
|
7
7
|
@redises.hset(@key, 'k2', 'v2')
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
10
|
+
it 'returns the values stored in the hash' do
|
11
11
|
@redises.hvals(@key).sort.should == %w[v1 v2]
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
14
|
+
it 'returns [] when there is no such key' do
|
15
15
|
@redises.hvals('mock-redis-test:nonesuch').should == []
|
16
16
|
end
|
17
17
|
|
18
|
-
it_should_behave_like
|
18
|
+
it_should_behave_like 'a hash-only command'
|
19
19
|
end
|