mock_redis 0.32.0 → 0.44.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +59 -0
  3. data/README.md +4 -4
  4. data/lib/mock_redis/database.rb +42 -13
  5. data/lib/mock_redis/geospatial_methods.rb +2 -2
  6. data/lib/mock_redis/hash_methods.rb +1 -1
  7. data/lib/mock_redis/list_methods.rb +37 -2
  8. data/lib/mock_redis/memory_method.rb +11 -0
  9. data/lib/mock_redis/multi_db_wrapper.rb +1 -1
  10. data/lib/mock_redis/pipelined_wrapper.rb +12 -14
  11. data/lib/mock_redis/set_methods.rb +18 -2
  12. data/lib/mock_redis/stream.rb +3 -3
  13. data/lib/mock_redis/string_methods.rb +33 -6
  14. data/lib/mock_redis/transaction_wrapper.rb +5 -7
  15. data/lib/mock_redis/utility_methods.rb +4 -2
  16. data/lib/mock_redis/version.rb +1 -1
  17. data/lib/mock_redis/zset.rb +4 -2
  18. data/lib/mock_redis/zset_methods.rb +13 -4
  19. data/lib/mock_redis.rb +5 -6
  20. metadata +20 -352
  21. data/.github/workflows/lint.yml +0 -31
  22. data/.github/workflows/tests.yml +0 -63
  23. data/.gitignore +0 -5
  24. data/.mailmap +0 -2
  25. data/.overcommit.yml +0 -21
  26. data/.rspec +0 -1
  27. data/.rubocop.yml +0 -148
  28. data/.rubocop_todo.yml +0 -35
  29. data/.simplecov +0 -4
  30. data/Gemfile +0 -13
  31. data/Rakefile +0 -2
  32. data/mock_redis.gemspec +0 -32
  33. data/spec/client_spec.rb +0 -29
  34. data/spec/cloning_spec.rb +0 -95
  35. data/spec/commands/append_spec.rb +0 -24
  36. data/spec/commands/auth_spec.rb +0 -7
  37. data/spec/commands/bgrewriteaof_spec.rb +0 -7
  38. data/spec/commands/bgsave_spec.rb +0 -7
  39. data/spec/commands/bitcount_spec.rb +0 -25
  40. data/spec/commands/bitfield_spec.rb +0 -169
  41. data/spec/commands/blpop_spec.rb +0 -53
  42. data/spec/commands/brpop_spec.rb +0 -59
  43. data/spec/commands/brpoplpush_spec.rb +0 -52
  44. data/spec/commands/connected_spec.rb +0 -7
  45. data/spec/commands/connection_spec.rb +0 -15
  46. data/spec/commands/dbsize_spec.rb +0 -18
  47. data/spec/commands/decr_spec.rb +0 -34
  48. data/spec/commands/decrby_spec.rb +0 -34
  49. data/spec/commands/del_spec.rb +0 -52
  50. data/spec/commands/disconnect_spec.rb +0 -7
  51. data/spec/commands/dump_spec.rb +0 -19
  52. data/spec/commands/echo_spec.rb +0 -11
  53. data/spec/commands/eval_spec.rb +0 -7
  54. data/spec/commands/evalsha_spec.rb +0 -10
  55. data/spec/commands/exists_spec.rb +0 -43
  56. data/spec/commands/expire_spec.rb +0 -111
  57. data/spec/commands/expireat_spec.rb +0 -47
  58. data/spec/commands/flushall_spec.rb +0 -38
  59. data/spec/commands/flushdb_spec.rb +0 -38
  60. data/spec/commands/future_spec.rb +0 -30
  61. data/spec/commands/geoadd_spec.rb +0 -58
  62. data/spec/commands/geodist_spec.rb +0 -118
  63. data/spec/commands/geohash_spec.rb +0 -52
  64. data/spec/commands/geopos_spec.rb +0 -55
  65. data/spec/commands/get_spec.rb +0 -31
  66. data/spec/commands/getbit_spec.rb +0 -34
  67. data/spec/commands/getrange_spec.rb +0 -22
  68. data/spec/commands/getset_spec.rb +0 -23
  69. data/spec/commands/hdel_spec.rb +0 -77
  70. data/spec/commands/hexists_spec.rb +0 -27
  71. data/spec/commands/hget_spec.rb +0 -28
  72. data/spec/commands/hgetall_spec.rb +0 -32
  73. data/spec/commands/hincrby_spec.rb +0 -58
  74. data/spec/commands/hincrbyfloat_spec.rb +0 -58
  75. data/spec/commands/hkeys_spec.rb +0 -19
  76. data/spec/commands/hlen_spec.rb +0 -19
  77. data/spec/commands/hmget_spec.rb +0 -46
  78. data/spec/commands/hmset_spec.rb +0 -69
  79. data/spec/commands/hscan_each_spec.rb +0 -48
  80. data/spec/commands/hscan_spec.rb +0 -27
  81. data/spec/commands/hset_spec.rb +0 -42
  82. data/spec/commands/hsetnx_spec.rb +0 -44
  83. data/spec/commands/hvals_spec.rb +0 -19
  84. data/spec/commands/incr_spec.rb +0 -34
  85. data/spec/commands/incrby_spec.rb +0 -44
  86. data/spec/commands/incrbyfloat_spec.rb +0 -44
  87. data/spec/commands/info_spec.rb +0 -62
  88. data/spec/commands/keys_spec.rb +0 -139
  89. data/spec/commands/lastsave_spec.rb +0 -8
  90. data/spec/commands/lindex_spec.rb +0 -49
  91. data/spec/commands/linsert_spec.rb +0 -68
  92. data/spec/commands/llen_spec.rb +0 -16
  93. data/spec/commands/lpop_spec.rb +0 -34
  94. data/spec/commands/lpush_spec.rb +0 -43
  95. data/spec/commands/lpushx_spec.rb +0 -46
  96. data/spec/commands/lrange_spec.rb +0 -51
  97. data/spec/commands/lrem_spec.rb +0 -80
  98. data/spec/commands/lset_spec.rb +0 -43
  99. data/spec/commands/ltrim_spec.rb +0 -45
  100. data/spec/commands/mapped_hmget_spec.rb +0 -29
  101. data/spec/commands/mapped_hmset_spec.rb +0 -47
  102. data/spec/commands/mapped_mget_spec.rb +0 -22
  103. data/spec/commands/mapped_mset_spec.rb +0 -19
  104. data/spec/commands/mapped_msetnx_spec.rb +0 -26
  105. data/spec/commands/mget_spec.rb +0 -65
  106. data/spec/commands/move_spec.rb +0 -147
  107. data/spec/commands/mset_spec.rb +0 -43
  108. data/spec/commands/msetnx_spec.rb +0 -40
  109. data/spec/commands/persist_spec.rb +0 -48
  110. data/spec/commands/pexpire_spec.rb +0 -86
  111. data/spec/commands/pexpireat_spec.rb +0 -48
  112. data/spec/commands/ping_spec.rb +0 -11
  113. data/spec/commands/pipelined_spec.rb +0 -114
  114. data/spec/commands/psetex_spec.rb +0 -44
  115. data/spec/commands/pttl_spec.rb +0 -41
  116. data/spec/commands/quit_spec.rb +0 -7
  117. data/spec/commands/randomkey_spec.rb +0 -20
  118. data/spec/commands/rename_spec.rb +0 -42
  119. data/spec/commands/renamenx_spec.rb +0 -41
  120. data/spec/commands/restore_spec.rb +0 -47
  121. data/spec/commands/rpop_spec.rb +0 -34
  122. data/spec/commands/rpoplpush_spec.rb +0 -50
  123. data/spec/commands/rpush_spec.rb +0 -43
  124. data/spec/commands/rpushx_spec.rb +0 -46
  125. data/spec/commands/sadd_spec.rb +0 -45
  126. data/spec/commands/save_spec.rb +0 -7
  127. data/spec/commands/scan_each_spec.rb +0 -39
  128. data/spec/commands/scan_spec.rb +0 -64
  129. data/spec/commands/scard_spec.rb +0 -18
  130. data/spec/commands/script_spec.rb +0 -9
  131. data/spec/commands/sdiff_spec.rb +0 -47
  132. data/spec/commands/sdiffstore_spec.rb +0 -58
  133. data/spec/commands/select_spec.rb +0 -61
  134. data/spec/commands/set_spec.rb +0 -119
  135. data/spec/commands/setbit_spec.rb +0 -55
  136. data/spec/commands/setex_spec.rb +0 -38
  137. data/spec/commands/setnx_spec.rb +0 -25
  138. data/spec/commands/setrange_spec.rb +0 -30
  139. data/spec/commands/sinter_spec.rb +0 -39
  140. data/spec/commands/sinterstore_spec.rb +0 -53
  141. data/spec/commands/sismember_spec.rb +0 -29
  142. data/spec/commands/smembers_spec.rb +0 -28
  143. data/spec/commands/smove_spec.rb +0 -41
  144. data/spec/commands/sort_list_spec.rb +0 -21
  145. data/spec/commands/sort_set_spec.rb +0 -21
  146. data/spec/commands/sort_zset_spec.rb +0 -21
  147. data/spec/commands/spop_spec.rb +0 -40
  148. data/spec/commands/srandmember_spec.rb +0 -49
  149. data/spec/commands/srem_spec.rb +0 -45
  150. data/spec/commands/sscan_each_spec.rb +0 -48
  151. data/spec/commands/sscan_spec.rb +0 -39
  152. data/spec/commands/strlen_spec.rb +0 -18
  153. data/spec/commands/sunion_spec.rb +0 -42
  154. data/spec/commands/sunionstore_spec.rb +0 -59
  155. data/spec/commands/ttl_spec.rb +0 -40
  156. data/spec/commands/type_spec.rb +0 -36
  157. data/spec/commands/unwatch_spec.rb +0 -7
  158. data/spec/commands/watch_spec.rb +0 -21
  159. data/spec/commands/xadd_spec.rb +0 -122
  160. data/spec/commands/xlen_spec.rb +0 -22
  161. data/spec/commands/xrange_spec.rb +0 -164
  162. data/spec/commands/xread_spec.rb +0 -66
  163. data/spec/commands/xrevrange_spec.rb +0 -130
  164. data/spec/commands/xtrim_spec.rb +0 -36
  165. data/spec/commands/zadd_spec.rb +0 -129
  166. data/spec/commands/zcard_spec.rb +0 -19
  167. data/spec/commands/zcount_spec.rb +0 -39
  168. data/spec/commands/zincrby_spec.rb +0 -31
  169. data/spec/commands/zinterstore_spec.rb +0 -130
  170. data/spec/commands/zpopmax_spec.rb +0 -60
  171. data/spec/commands/zpopmin_spec.rb +0 -60
  172. data/spec/commands/zrange_spec.rb +0 -80
  173. data/spec/commands/zrangebyscore_spec.rb +0 -83
  174. data/spec/commands/zrank_spec.rb +0 -29
  175. data/spec/commands/zrem_spec.rb +0 -43
  176. data/spec/commands/zremrangebyrank_spec.rb +0 -27
  177. data/spec/commands/zremrangebyscore_spec.rb +0 -35
  178. data/spec/commands/zrevrange_spec.rb +0 -56
  179. data/spec/commands/zrevrangebyscore_spec.rb +0 -58
  180. data/spec/commands/zrevrank_spec.rb +0 -29
  181. data/spec/commands/zscan_each_spec.rb +0 -48
  182. data/spec/commands/zscan_spec.rb +0 -26
  183. data/spec/commands/zscore_spec.rb +0 -22
  184. data/spec/commands/zunionstore_spec.rb +0 -137
  185. data/spec/mock_redis_spec.rb +0 -93
  186. data/spec/spec_helper.rb +0 -71
  187. data/spec/support/redis_multiplexer.rb +0 -123
  188. data/spec/support/shared_examples/does_not_cleanup_empty_strings.rb +0 -14
  189. data/spec/support/shared_examples/only_operates_on_hashes.rb +0 -15
  190. data/spec/support/shared_examples/only_operates_on_lists.rb +0 -15
  191. data/spec/support/shared_examples/only_operates_on_sets.rb +0 -15
  192. data/spec/support/shared_examples/only_operates_on_strings.rb +0 -13
  193. data/spec/support/shared_examples/only_operates_on_zsets.rb +0 -59
  194. data/spec/support/shared_examples/sorts_enumerables.rb +0 -56
  195. data/spec/transactions_spec.rb +0 -163
@@ -1,137 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe '#zunionstore(destination, keys, [:weights => [w,w,], [:aggregate => :sum|:min|:max])' do
4
- before do
5
- @set1 = 'mock-redis-test:zunionstore1'
6
- @set2 = 'mock-redis-test:zunionstore2'
7
- @set3 = 'mock-redis-test:zunionstore3'
8
- @dest = 'mock-redis-test:zunionstoredest'
9
-
10
- @redises.zadd(@set1, 1, 'one')
11
-
12
- @redises.zadd(@set2, 1, 'one')
13
- @redises.zadd(@set2, 2, 'two')
14
-
15
- @redises.zadd(@set3, 1, 'one')
16
- @redises.zadd(@set3, 2, 'two')
17
- @redises.zadd(@set3, 3, 'three')
18
- end
19
-
20
- it 'returns the number of elements in the new set' do
21
- @redises.zunionstore(@dest, [@set1, @set2, @set3]).should == 3
22
- end
23
-
24
- it "sums the members' scores by default" do
25
- @redises.zunionstore(@dest, [@set1, @set2, @set3])
26
- @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
27
- [['one', 3.0], ['three', 3.0], ['two', 4.0]]
28
- end
29
-
30
- it 'removes existing elements in destination' do
31
- @redises.zadd(@dest, 10, 'ten')
32
-
33
- @redises.zunionstore(@dest, [@set1])
34
- @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
35
- [['one', 1.0]]
36
- end
37
-
38
- it 'raises an error if keys is empty' do
39
- lambda do
40
- @redises.zunionstore(@dest, [])
41
- end.should raise_error(Redis::CommandError)
42
- end
43
-
44
- context 'when used with a set' do
45
- before do
46
- @set4 = 'mock-redis-test:zunionstore4'
47
-
48
- @redises.sadd(@set4, 'two')
49
- @redises.sadd(@set4, 'three')
50
- @redises.sadd(@set4, 'four')
51
- end
52
-
53
- it 'returns the number of elements in the new set' do
54
- @redises.zunionstore(@dest, [@set3, @set4]).should == 4
55
- end
56
-
57
- it 'sums the scores, substituting 1.0 for set values' do
58
- @redises.zunionstore(@dest, [@set3, @set4])
59
- @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
60
- [['four', 1.0], ['one', 1.0], ['two', 3.0], ['three', 4.0]]
61
- end
62
- end
63
-
64
- context 'when used with a non-coercible structure' do
65
- before do
66
- @non_set = 'mock-redis-test:zunionstore4'
67
-
68
- @redises.set(@non_set, 'one')
69
- end
70
- it 'raises an error for wrong value type' do
71
- lambda do
72
- @redises.zunionstore(@dest, [@set1, @non_set])
73
- end.should raise_error(Redis::CommandError)
74
- end
75
- end
76
-
77
- context 'the :weights argument' do
78
- it 'multiplies the scores by the weights while aggregating' do
79
- @redises.zunionstore(@dest, [@set1, @set2, @set3], :weights => [2, 3, 5])
80
- @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
81
- [['one', 10.0], ['three', 15.0], ['two', 16.0]]
82
- end
83
-
84
- it 'raises an error if the number of weights != the number of keys' do
85
- lambda do
86
- @redises.zunionstore(@dest, [@set1, @set2, @set3], :weights => [1, 2])
87
- end.should raise_error(Redis::CommandError)
88
- end
89
- end
90
-
91
- context 'the :aggregate argument' do
92
- before do
93
- @smalls = 'mock-redis-test:zunionstore:smalls'
94
- @bigs = 'mock-redis-test:zunionstore:bigs'
95
-
96
- @redises.zadd(@smalls, 1, 'bert')
97
- @redises.zadd(@smalls, 2, 'ernie')
98
- @redises.zadd(@bigs, 100, 'bert')
99
- @redises.zadd(@bigs, 200, 'ernie')
100
- end
101
-
102
- it 'aggregates scores with min when :aggregate => :min is specified' do
103
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :min)
104
- @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
105
- [['bert', 1.0], ['ernie', 2.0]]
106
- end
107
-
108
- it 'aggregates scores with max when :aggregate => :max is specified' do
109
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :max)
110
- @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
111
- [['bert', 100.0], ['ernie', 200.0]]
112
- end
113
-
114
- it 'ignores scores for missing members' do
115
- @redises.zadd(@smalls, 3, 'grover')
116
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :min)
117
- @redises.zscore(@dest, 'grover').should == 3.0
118
-
119
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :max)
120
- @redises.zscore(@dest, 'grover').should == 3.0
121
- end
122
-
123
- it "allows 'min', 'MIN', etc. as aliases for :min" do
124
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => 'min')
125
- @redises.zscore(@dest, 'bert').should == 1.0
126
-
127
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => 'MIN')
128
- @redises.zscore(@dest, 'bert').should == 1.0
129
- end
130
-
131
- it 'raises an error for unknown aggregation function' do
132
- lambda do
133
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :mix)
134
- end.should raise_error(Redis::CommandError)
135
- end
136
- end
137
- end
@@ -1,93 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe MockRedis do
4
- let(:url) { 'redis://127.0.0.1:6379/1' }
5
-
6
- describe '.new' do
7
- subject { MockRedis.new(:url => url) }
8
-
9
- it 'correctly parses options' do
10
- subject.host.should == '127.0.0.1'
11
- subject.port.should == 6379
12
- subject.db.should == 1
13
- end
14
-
15
- its(:id) { should == url }
16
-
17
- its(:location) { should == url }
18
- end
19
-
20
- describe '.connect' do
21
- let(:url) { 'redis://127.0.0.1:6379/0' }
22
- subject { MockRedis.connect(:url => url) }
23
-
24
- it 'correctly parses options' do
25
- subject.host.should == '127.0.0.1'
26
- subject.port.should == 6379
27
- subject.db.should == 0
28
- end
29
-
30
- its(:id) { should == url }
31
- end
32
-
33
- describe 'Injecting a time class' do
34
- describe '.options' do
35
- let(:time_stub) { double 'Time' }
36
- let(:options) { { :time_class => time_stub } }
37
-
38
- it 'defaults to Time' do
39
- mock_redis = MockRedis.new
40
-
41
- mock_redis.options[:time_class].should == Time
42
- end
43
-
44
- it 'has a configurable Time class' do
45
- mock_redis = MockRedis.new(options)
46
-
47
- mock_redis.options[:time_class].should == time_stub
48
- end
49
- end
50
-
51
- describe '.now' do
52
- let(:time_stub) { double 'Time', :now => Time.new(2019, 1, 2, 3, 4, 6, '+00:00') }
53
- let(:options) { { :time_class => time_stub } }
54
-
55
- subject { MockRedis.new(options) }
56
-
57
- its(:now) { should == [1_546_398_246, 0] }
58
- end
59
-
60
- describe '.time' do
61
- let(:time_stub) { double 'Time', :now => Time.new(2019, 1, 2, 3, 4, 6, '+00:00') }
62
- let(:options) { { :time_class => time_stub } }
63
-
64
- subject { MockRedis.new(options) }
65
-
66
- its(:time) { should == [1_546_398_246, 0] }
67
- end
68
-
69
- describe '.expireat' do
70
- let(:time_at) { 'expireat' }
71
- let(:time_stub) { double 'Time' }
72
- let(:options) { { :time_class => time_stub } }
73
- let(:timestamp) { 123_456 }
74
-
75
- subject { MockRedis.new(options) }
76
-
77
- it 'Forwards time_at to the time_class' do
78
- time_stub.should_receive(:at).with(timestamp).and_return(time_at)
79
-
80
- subject.time_at(timestamp).should == time_at
81
- end
82
- end
83
- end
84
-
85
- describe 'supplying a logger' do
86
- it 'logs redis commands' do
87
- logger = double('Logger', debug?: true, debug: nil)
88
- mock_redis = MockRedis.new(logger: logger)
89
- expect(logger).to receive(:debug).with(/command=HMGET args="hash" "key1" "key2"/)
90
- mock_redis.hmget('hash', 'key1', 'key2')
91
- end
92
- end
93
- end
data/spec/spec_helper.rb DELETED
@@ -1,71 +0,0 @@
1
- require 'simplecov'
2
- SimpleCov.start do
3
- add_filter 'spec/'
4
-
5
- if ENV['CI']
6
- require 'simplecov-lcov'
7
-
8
- SimpleCov::Formatter::LcovFormatter.config do |c|
9
- c.report_with_single_file = true
10
- c.single_report_path = 'coverage/lcov.info'
11
- end
12
-
13
- formatter SimpleCov::Formatter::LcovFormatter
14
- end
15
- end
16
-
17
- require 'rspec/its'
18
- require 'redis'
19
- $LOAD_PATH.unshift(File.expand_path(File.join(__FILE__, '..', '..', 'lib')))
20
- require 'mock_redis'
21
- require 'timecop'
22
-
23
- $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..')))
24
- Dir['spec/support/**/*.rb'].sort.each { |x| require x }
25
-
26
- module TypeCheckingHelper
27
- def method_from_description(example)
28
- # extracting this from the RSpec description string may or may not
29
- # be a good idea. On the one hand, it enforces the convention of
30
- # putting the method name in the right place; on the other hand,
31
- # it's pretty magic-looking.
32
- example.full_description.match(/#(\w+)/).captures.first
33
- end
34
-
35
- def args_for_method(method)
36
- return [] if %w[spop zpopmin zpopmax].include?(method.to_s)
37
- method_arity = @redises.real.method(method).arity
38
- if method_arity < 0 # -1 comes from def foo(*args)
39
- [1, 2] # probably good enough
40
- else
41
- 1.upto(method_arity - 1).to_a
42
- end
43
- end
44
- end
45
-
46
- RSpec.configure do |config|
47
- config.expect_with :rspec do |c|
48
- c.syntax = [:expect, :should]
49
- end
50
-
51
- config.mock_with :rspec do |c|
52
- c.syntax = [:expect, :should]
53
- end
54
-
55
- config.include(TypeCheckingHelper)
56
-
57
- config.before(:all) do
58
- @redises = RedisMultiplexer.new
59
- end
60
-
61
- config.before(:each) do
62
- # databases mentioned in our tests
63
- [1, 0].each do |db|
64
- @redises.send_without_checking(:select, db)
65
- @redises.send_without_checking(:keys, 'mock-redis-test:*').each do |key|
66
- @redises.send_without_checking(:del, key)
67
- end
68
- end
69
- @redises._gsub_clear
70
- end
71
- end
@@ -1,123 +0,0 @@
1
- class BlankSlate
2
- instance_methods.each do |m|
3
- undef_method(m) unless m =~ /^__/ || %w[inspect object_id].include?(m.to_s)
4
- end
5
- end
6
-
7
- class RedisMultiplexer < BlankSlate
8
- MismatchedResponse = Class.new(StandardError)
9
-
10
- def initialize(*a)
11
- @mock_redis = MockRedis.new(*a)
12
- Redis.exists_returns_integer = true
13
- @real_redis = Redis.new(*a)
14
- _gsub_clear
15
- end
16
-
17
- def _gsub(from, to)
18
- @gsub_from = from
19
- @gsub_to = to
20
- end
21
-
22
- def _gsub_clear
23
- @gsub_from = @gsub_to = ''
24
- end
25
-
26
- ruby2_keywords def method_missing(method, *args, &blk)
27
- # If we're in a Redis command that accepts a block, and we execute more
28
- # redis commands, ONLY execute them on the Redis implementation that the
29
- # block came from.
30
- # e.g. if a pipelined command is started on a MockRedis object, DON'T send
31
- # commands inside the pipelined block to the real Redis object, as that one
32
- # WON'T be inside a pipelined command, and we'll see weird behaviour
33
- if blk
34
- @in_mock_block = false
35
- @in_redis_block = true
36
- end
37
- real_retval, real_error =
38
- catch_errors { @in_mock_block ? :no_op : @real_redis.send(method, *args, &blk) }
39
-
40
- if blk
41
- @in_mock_block = true
42
- @in_redis_block = false
43
- end
44
- mock_retval, mock_error =
45
- catch_errors { @in_redis_block ? :no_op : @mock_redis.send(method, *args, &blk) }
46
-
47
- if blk
48
- @in_mock_block = false
49
- @in_redis_block = false
50
- end
51
-
52
- if mock_retval == :no_op || real_retval == :no_op
53
- # ignore, we were inside a block (like pipelined)
54
- elsif !equalish?(mock_retval, real_retval) && !mock_error && !real_error
55
- # no exceptions, just different behavior
56
- raise MismatchedResponse,
57
- "Mock failure: responses not equal.\n" \
58
- "Redis.#{method}(#{args.inspect}) returned #{real_retval.inspect}\n" \
59
- "MockRedis.#{method}(#{args.inspect}) returned #{mock_retval.inspect}\n"
60
- elsif !mock_error && real_error
61
- raise MismatchedResponse,
62
- "Mock failure: didn't raise an error when it should have.\n" \
63
- "Redis.#{method}(#{args.inspect}) raised #{real_error.inspect}\n" \
64
- "MockRedis.#{method}(#{args.inspect}) raised nothing " \
65
- "and returned #{mock_retval.inspect}"
66
- elsif !real_error && mock_error
67
- raise MismatchedResponse,
68
- "Mock failure: raised an error when it shouldn't have.\n" \
69
- "Redis.#{method}(#{args.inspect}) returned #{real_retval.inspect}\n" \
70
- "MockRedis.#{method}(#{args.inspect}) raised #{mock_error.inspect}"
71
- elsif mock_error && real_error && !equalish?(mock_error, real_error)
72
- raise MismatchedResponse,
73
- "Mock failure: raised the wrong error.\n" \
74
- "Redis.#{method}(#{args.inspect}) raised #{real_error.inspect}\n" \
75
- "MockRedis.#{method}(#{args.inspect}) raised #{mock_error.inspect}"
76
- end
77
-
78
- raise mock_error if mock_error
79
- mock_retval
80
- end
81
-
82
- def equalish?(a, b)
83
- if a == b
84
- true
85
- elsif a.is_a?(String) && b.is_a?(String)
86
- masked(a) == masked(b)
87
- elsif a.is_a?(Array) && b.is_a?(Array)
88
- a.collect { |c| masked(c.to_s) }.sort == b.collect { |c| masked(c.to_s) }.sort
89
- elsif a.is_a?(Exception) && b.is_a?(Exception)
90
- a.class == b.class && a.message == b.message
91
- elsif a.is_a?(Float) && b.is_a?(Float)
92
- delta = [a.abs, b.abs].min / 1_000_000.0
93
- (a - b).abs < delta
94
- else
95
- false
96
- end
97
- end
98
-
99
- def masked(str)
100
- str.gsub(@gsub_from, @gsub_to)
101
- end
102
-
103
- def mock
104
- @mock_redis
105
- end
106
-
107
- def real
108
- @real_redis
109
- end
110
-
111
- # Used in cleanup before() blocks.
112
- def send_without_checking(method, *args)
113
- @mock_redis.send(method, *args)
114
- @real_redis.send(method, *args)
115
- end
116
-
117
- def catch_errors
118
- retval = yield
119
- [retval, nil]
120
- rescue StandardError => e
121
- [nil, e]
122
- end
123
- end
@@ -1,14 +0,0 @@
1
- shared_examples_for 'does not remove empty strings on error' do
2
- it 'does not remove empty strings on error' do |example|
3
- key = 'mock-redis-test:not-a-string'
4
-
5
- method = method_from_description(example)
6
- args = args_for_method(method).unshift(key)
7
-
8
- @redises.set(key, '')
9
- lambda do
10
- @redises.send(method, *args)
11
- end.should raise_error(RuntimeError)
12
- @redises.get(key).should == ''
13
- end
14
- end
@@ -1,15 +0,0 @@
1
- shared_examples_for 'a hash-only command' do
2
- it 'raises an error for non-hash values' do |example|
3
- key = 'mock-redis-test:hash-only'
4
-
5
- method = method_from_description(example)
6
- args = args_for_method(method).unshift(key)
7
-
8
- @redises.set(key, 1)
9
- lambda do
10
- @redises.send(method, *args)
11
- end.should raise_error(RuntimeError)
12
- end
13
-
14
- it_should_behave_like 'does not remove empty strings on error'
15
- end
@@ -1,15 +0,0 @@
1
- shared_examples_for 'a list-only command' do
2
- it 'raises an error for non-list values' do |example|
3
- key = 'mock-redis-test:list-only'
4
-
5
- method = method_from_description(example)
6
- args = args_for_method(method).unshift(key)
7
-
8
- @redises.set(key, 1)
9
- lambda do
10
- @redises.send(method, *args)
11
- end.should raise_error(RuntimeError)
12
- end
13
-
14
- it_should_behave_like 'does not remove empty strings on error'
15
- end
@@ -1,15 +0,0 @@
1
- shared_examples_for 'a set-only command' do
2
- it 'raises an error for non-set values' do |example|
3
- key = 'mock-redis-test:set-only'
4
-
5
- method = method_from_description(example)
6
- args = args_for_method(method).unshift(key)
7
-
8
- @redises.set(key, 1)
9
- lambda do
10
- @redises.send(method, *args)
11
- end.should raise_error(RuntimeError)
12
- end
13
-
14
- it_should_behave_like 'does not remove empty strings on error'
15
- end
@@ -1,13 +0,0 @@
1
- shared_examples_for 'a string-only command' do
2
- it 'raises an error for non-string values' do |example|
3
- key = 'mock-redis-test:string-only-command'
4
-
5
- method = method_from_description(example)
6
- args = args_for_method(method).unshift(key)
7
-
8
- @redises.lpush(key, 1)
9
- lambda do
10
- @redises.send(method, *args)
11
- end.should raise_error(RuntimeError)
12
- end
13
- end
@@ -1,59 +0,0 @@
1
- shared_examples_for 'a zset-only command' do
2
- it 'raises an error for non-zset values' do |example|
3
- key = 'mock-redis-test:zset-only'
4
-
5
- method = method_from_description(example)
6
- args = args_for_method(method).unshift(key)
7
-
8
- @redises.set(key, 1)
9
- lambda do
10
- @redises.send(method, *args)
11
- end.should raise_error(RuntimeError)
12
- end
13
-
14
- it_should_behave_like 'does not remove empty strings on error'
15
- end
16
-
17
- shared_examples_for 'arg 1 is a score' do
18
- before { @_arg_index = 1 }
19
- it_should_behave_like 'arg N is a score'
20
- end
21
-
22
- shared_examples_for 'arg 2 is a score' do
23
- before { @_arg_index = 2 }
24
- it_should_behave_like 'arg N is a score'
25
- end
26
-
27
- shared_examples_for 'arg N is a score' do
28
- before do |example|
29
- key = 'mock-redis-test:zset-only'
30
-
31
- @method = method_from_description(example)
32
- @args = args_for_method(@method).unshift(key)
33
- end
34
-
35
- it 'is okay with positive ints' do
36
- @args[@_arg_index] = 1
37
- lambda { @redises.send(@method, *@args) }.should_not raise_error
38
- end
39
-
40
- it 'is okay with negative ints' do
41
- @args[@_arg_index] = -1
42
- lambda { @redises.send(@method, *@args) }.should_not raise_error
43
- end
44
-
45
- it 'is okay with positive floats' do
46
- @args[@_arg_index] = 1.5
47
- lambda { @redises.send(@method, *@args) }.should_not raise_error
48
- end
49
-
50
- it 'is okay with negative floats' do
51
- @args[@_arg_index] = -1.5
52
- lambda { @redises.send(@method, *@args) }.should_not raise_error
53
- end
54
-
55
- it 'rejects non-numbers' do
56
- @args[@_arg_index] = 'foo'
57
- lambda { @redises.send(@method, *@args) }.should raise_error(RuntimeError)
58
- end
59
- end
@@ -1,56 +0,0 @@
1
- shared_examples_for 'a sortable' do
2
- it 'returns empty array on nil' do
3
- @redises.sort(nil).should == []
4
- end
5
-
6
- context 'ordering' do
7
- it 'orders ascending by default' do
8
- @redises.sort(@key).should == %w[1 2]
9
- end
10
-
11
- it 'orders by descending when specified' do
12
- @redises.sort(@key, :order => 'DESC').should == %w[2 1]
13
- end
14
- end
15
-
16
- context 'projections' do
17
- it 'projects element when :get is "#"' do
18
- @redises.sort(@key, :get => '#').should == %w[1 2]
19
- end
20
-
21
- it 'projects through a key pattern' do
22
- @redises.sort(@key, :get => 'mock-redis-test:values_*').should == %w[a b]
23
- end
24
-
25
- it 'projects through a key pattern and reflects element' do
26
- @redises.sort(@key, :get => ['#', 'mock-redis-test:values_*']).should == [%w[1 a], %w[2 b]]
27
- end
28
-
29
- it 'projects through a hash key pattern' do
30
- @redises.sort(@key, :get => 'mock-redis-test:hash_*->key').should == %w[x y]
31
- end
32
- end
33
-
34
- context 'weights' do
35
- it 'weights by projecting through a key pattern' do
36
- @redises.sort(@key, :by => 'mock-redis-test:weight_*').should == %w[2 1]
37
- end
38
-
39
- it 'weights by projecting through a key pattern and a specific order' do
40
- @redises.sort(@key, :order => 'DESC', :by => 'mock-redis-test:weight_*').should == %w[1 2]
41
- end
42
- end
43
-
44
- context 'limit' do
45
- it 'only returns requested window in the enumerable' do
46
- @redises.sort(@key, :limit => [0, 1]).should == ['1']
47
- end
48
- end
49
-
50
- context 'store' do
51
- it 'stores into another key' do
52
- @redises.sort(@key, :store => 'mock-redis-test:some_bucket').should == 2
53
- @redises.lrange('mock-redis-test:some_bucket', 0, -1).should == %w[1 2]
54
- end
55
- end
56
- end