mock_redis 0.19.0 → 0.44.0

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.
Files changed (191) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +161 -0
  3. data/LICENSE.md +21 -0
  4. data/README.md +41 -17
  5. data/lib/mock_redis/connection_method.rb +13 -0
  6. data/lib/mock_redis/database.rb +116 -41
  7. data/lib/mock_redis/expire_wrapper.rb +1 -1
  8. data/lib/mock_redis/future.rb +1 -1
  9. data/lib/mock_redis/geospatial_methods.rb +13 -21
  10. data/lib/mock_redis/hash_methods.rb +34 -15
  11. data/lib/mock_redis/indifferent_hash.rb +0 -8
  12. data/lib/mock_redis/info_method.rb +2 -2
  13. data/lib/mock_redis/list_methods.rb +39 -4
  14. data/lib/mock_redis/memory_method.rb +11 -0
  15. data/lib/mock_redis/multi_db_wrapper.rb +4 -4
  16. data/lib/mock_redis/pipelined_wrapper.rb +32 -15
  17. data/lib/mock_redis/set_methods.rb +34 -6
  18. data/lib/mock_redis/stream/id.rb +58 -0
  19. data/lib/mock_redis/stream.rb +88 -0
  20. data/lib/mock_redis/stream_methods.rb +102 -0
  21. data/lib/mock_redis/string_methods.rb +81 -30
  22. data/lib/mock_redis/transaction_wrapper.rb +32 -21
  23. data/lib/mock_redis/utility_methods.rb +9 -4
  24. data/lib/mock_redis/version.rb +1 -1
  25. data/lib/mock_redis/zset.rb +5 -8
  26. data/lib/mock_redis/zset_methods.rb +64 -12
  27. data/lib/mock_redis.rb +38 -10
  28. metadata +40 -336
  29. data/.gitignore +0 -5
  30. data/.mailmap +0 -2
  31. data/.overcommit.yml +0 -21
  32. data/.rspec +0 -1
  33. data/.rubocop.yml +0 -121
  34. data/.rubocop_todo.yml +0 -35
  35. data/.simplecov +0 -4
  36. data/.travis.yml +0 -33
  37. data/Gemfile +0 -12
  38. data/LICENSE +0 -19
  39. data/Rakefile +0 -2
  40. data/mock_redis.gemspec +0 -30
  41. data/spec/client_spec.rb +0 -17
  42. data/spec/cloning_spec.rb +0 -95
  43. data/spec/commands/append_spec.rb +0 -24
  44. data/spec/commands/auth_spec.rb +0 -7
  45. data/spec/commands/bgrewriteaof_spec.rb +0 -7
  46. data/spec/commands/bgsave_spec.rb +0 -7
  47. data/spec/commands/bitcount_spec.rb +0 -25
  48. data/spec/commands/bitfield_spec.rb +0 -169
  49. data/spec/commands/blpop_spec.rb +0 -59
  50. data/spec/commands/brpop_spec.rb +0 -58
  51. data/spec/commands/brpoplpush_spec.rb +0 -52
  52. data/spec/commands/connected_spec.rb +0 -7
  53. data/spec/commands/dbsize_spec.rb +0 -18
  54. data/spec/commands/decr_spec.rb +0 -34
  55. data/spec/commands/decrby_spec.rb +0 -34
  56. data/spec/commands/del_spec.rb +0 -35
  57. data/spec/commands/disconnect_spec.rb +0 -7
  58. data/spec/commands/echo_spec.rb +0 -11
  59. data/spec/commands/eval_spec.rb +0 -7
  60. data/spec/commands/evalsha_spec.rb +0 -10
  61. data/spec/commands/exists_spec.rb +0 -14
  62. data/spec/commands/expire_spec.rb +0 -111
  63. data/spec/commands/expireat_spec.rb +0 -47
  64. data/spec/commands/flushall_spec.rb +0 -38
  65. data/spec/commands/flushdb_spec.rb +0 -38
  66. data/spec/commands/future_spec.rb +0 -20
  67. data/spec/commands/geoadd_spec.rb +0 -58
  68. data/spec/commands/geodist_spec.rb +0 -114
  69. data/spec/commands/geohash_spec.rb +0 -52
  70. data/spec/commands/geopos_spec.rb +0 -55
  71. data/spec/commands/get_spec.rb +0 -30
  72. data/spec/commands/getbit_spec.rb +0 -34
  73. data/spec/commands/getrange_spec.rb +0 -22
  74. data/spec/commands/getset_spec.rb +0 -23
  75. data/spec/commands/hash_operator_spec.rb +0 -21
  76. data/spec/commands/hdel_spec.rb +0 -54
  77. data/spec/commands/hexists_spec.rb +0 -27
  78. data/spec/commands/hget_spec.rb +0 -28
  79. data/spec/commands/hgetall_spec.rb +0 -32
  80. data/spec/commands/hincrby_spec.rb +0 -58
  81. data/spec/commands/hincrbyfloat_spec.rb +0 -58
  82. data/spec/commands/hkeys_spec.rb +0 -19
  83. data/spec/commands/hlen_spec.rb +0 -19
  84. data/spec/commands/hmget_spec.rb +0 -40
  85. data/spec/commands/hmset_spec.rb +0 -43
  86. data/spec/commands/hscan_each_spec.rb +0 -48
  87. data/spec/commands/hscan_spec.rb +0 -27
  88. data/spec/commands/hset_spec.rb +0 -38
  89. data/spec/commands/hsetnx_spec.rb +0 -44
  90. data/spec/commands/hvals_spec.rb +0 -19
  91. data/spec/commands/incr_spec.rb +0 -34
  92. data/spec/commands/incrby_spec.rb +0 -44
  93. data/spec/commands/incrbyfloat_spec.rb +0 -44
  94. data/spec/commands/info_spec.rb +0 -62
  95. data/spec/commands/keys_spec.rb +0 -122
  96. data/spec/commands/lastsave_spec.rb +0 -8
  97. data/spec/commands/lindex_spec.rb +0 -49
  98. data/spec/commands/linsert_spec.rb +0 -68
  99. data/spec/commands/llen_spec.rb +0 -16
  100. data/spec/commands/lpop_spec.rb +0 -34
  101. data/spec/commands/lpush_spec.rb +0 -43
  102. data/spec/commands/lpushx_spec.rb +0 -46
  103. data/spec/commands/lrange_spec.rb +0 -51
  104. data/spec/commands/lrem_spec.rb +0 -80
  105. data/spec/commands/lset_spec.rb +0 -43
  106. data/spec/commands/ltrim_spec.rb +0 -45
  107. data/spec/commands/mapped_hmget_spec.rb +0 -29
  108. data/spec/commands/mapped_hmset_spec.rb +0 -47
  109. data/spec/commands/mapped_mget_spec.rb +0 -22
  110. data/spec/commands/mapped_mset_spec.rb +0 -19
  111. data/spec/commands/mapped_msetnx_spec.rb +0 -26
  112. data/spec/commands/mget_spec.rb +0 -34
  113. data/spec/commands/move_spec.rb +0 -147
  114. data/spec/commands/mset_spec.rb +0 -29
  115. data/spec/commands/msetnx_spec.rb +0 -40
  116. data/spec/commands/persist_spec.rb +0 -48
  117. data/spec/commands/pexpire_spec.rb +0 -86
  118. data/spec/commands/pexpireat_spec.rb +0 -48
  119. data/spec/commands/ping_spec.rb +0 -7
  120. data/spec/commands/pipelined_spec.rb +0 -42
  121. data/spec/commands/pttl_spec.rb +0 -41
  122. data/spec/commands/quit_spec.rb +0 -7
  123. data/spec/commands/randomkey_spec.rb +0 -20
  124. data/spec/commands/rename_spec.rb +0 -42
  125. data/spec/commands/renamenx_spec.rb +0 -41
  126. data/spec/commands/rpop_spec.rb +0 -34
  127. data/spec/commands/rpoplpush_spec.rb +0 -50
  128. data/spec/commands/rpush_spec.rb +0 -43
  129. data/spec/commands/rpushx_spec.rb +0 -46
  130. data/spec/commands/sadd_spec.rb +0 -45
  131. data/spec/commands/save_spec.rb +0 -7
  132. data/spec/commands/scan_each_spec.rb +0 -39
  133. data/spec/commands/scan_spec.rb +0 -55
  134. data/spec/commands/scard_spec.rb +0 -18
  135. data/spec/commands/script_spec.rb +0 -9
  136. data/spec/commands/sdiff_spec.rb +0 -47
  137. data/spec/commands/sdiffstore_spec.rb +0 -58
  138. data/spec/commands/select_spec.rb +0 -61
  139. data/spec/commands/set_spec.rb +0 -63
  140. data/spec/commands/setbit_spec.rb +0 -54
  141. data/spec/commands/setex_spec.rb +0 -22
  142. data/spec/commands/setnx_spec.rb +0 -25
  143. data/spec/commands/setrange_spec.rb +0 -30
  144. data/spec/commands/sinter_spec.rb +0 -39
  145. data/spec/commands/sinterstore_spec.rb +0 -53
  146. data/spec/commands/sismember_spec.rb +0 -29
  147. data/spec/commands/smembers_spec.rb +0 -28
  148. data/spec/commands/smove_spec.rb +0 -41
  149. data/spec/commands/sort_list_spec.rb +0 -21
  150. data/spec/commands/sort_set_spec.rb +0 -21
  151. data/spec/commands/sort_zset_spec.rb +0 -21
  152. data/spec/commands/spop_spec.rb +0 -25
  153. data/spec/commands/srandmember_spec.rb +0 -49
  154. data/spec/commands/srem_spec.rb +0 -40
  155. data/spec/commands/sscan_each_spec.rb +0 -48
  156. data/spec/commands/sscan_spec.rb +0 -39
  157. data/spec/commands/strlen_spec.rb +0 -18
  158. data/spec/commands/sunion_spec.rb +0 -42
  159. data/spec/commands/sunionstore_spec.rb +0 -59
  160. data/spec/commands/ttl_spec.rb +0 -40
  161. data/spec/commands/type_spec.rb +0 -36
  162. data/spec/commands/unwatch_spec.rb +0 -7
  163. data/spec/commands/watch_spec.rb +0 -16
  164. data/spec/commands/zadd_spec.rb +0 -123
  165. data/spec/commands/zcard_spec.rb +0 -19
  166. data/spec/commands/zcount_spec.rb +0 -39
  167. data/spec/commands/zincrby_spec.rb +0 -31
  168. data/spec/commands/zinterstore_spec.rb +0 -96
  169. data/spec/commands/zrange_spec.rb +0 -80
  170. data/spec/commands/zrangebyscore_spec.rb +0 -83
  171. data/spec/commands/zrank_spec.rb +0 -29
  172. data/spec/commands/zrem_spec.rb +0 -43
  173. data/spec/commands/zremrangebyrank_spec.rb +0 -27
  174. data/spec/commands/zremrangebyscore_spec.rb +0 -35
  175. data/spec/commands/zrevrange_spec.rb +0 -56
  176. data/spec/commands/zrevrangebyscore_spec.rb +0 -58
  177. data/spec/commands/zrevrank_spec.rb +0 -29
  178. data/spec/commands/zscan_each_spec.rb +0 -48
  179. data/spec/commands/zscan_spec.rb +0 -26
  180. data/spec/commands/zscore_spec.rb +0 -22
  181. data/spec/commands/zunionstore_spec.rb +0 -104
  182. data/spec/mock_redis_spec.rb +0 -86
  183. data/spec/spec_helper.rb +0 -63
  184. data/spec/support/redis_multiplexer.rb +0 -106
  185. data/spec/support/shared_examples/only_operates_on_hashes.rb +0 -13
  186. data/spec/support/shared_examples/only_operates_on_lists.rb +0 -13
  187. data/spec/support/shared_examples/only_operates_on_sets.rb +0 -13
  188. data/spec/support/shared_examples/only_operates_on_strings.rb +0 -13
  189. data/spec/support/shared_examples/only_operates_on_zsets.rb +0 -57
  190. data/spec/support/shared_examples/sorts_enumerables.rb +0 -56
  191. data/spec/transactions_spec.rb +0 -159
@@ -1,58 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe '#zrevrangebyscore(key, start, stop [:with_scores => true] [:limit => [offset count]])' do
4
- before do
5
- @key = 'mock-redis-test:zrevrangebyscore'
6
- @redises.zadd(@key, 1, 'Washington')
7
- @redises.zadd(@key, 2, 'Adams')
8
- @redises.zadd(@key, 3, 'Jefferson')
9
- @redises.zadd(@key, 4, 'Madison')
10
- end
11
-
12
- context 'when the zset is empty' do
13
- before do
14
- @redises.del(@key)
15
- end
16
-
17
- it 'should return an empty array' do
18
- @redises.exists(@key).should == false
19
- @redises.zrevrangebyscore(@key, 0, 4).should == []
20
- end
21
- end
22
-
23
- it 'returns the elements in order by score' do
24
- @redises.zrevrangebyscore(@key, 4, 3).should == %w[Madison Jefferson]
25
- end
26
-
27
- it 'returns the scores when :with_scores is specified' do
28
- @redises.zrevrangebyscore(@key, 4, 3, :with_scores => true).
29
- should == [['Madison', 4.0], ['Jefferson', 3.0]]
30
- end
31
-
32
- it 'returns the scores when :withscores is specified' do
33
- @redises.zrevrangebyscore(@key, 4, 3, :withscores => true).
34
- should == [['Madison', 4.0], ['Jefferson', 3.0]]
35
- end
36
-
37
- it 'treats +inf as positive infinity' do
38
- @redises.zrevrangebyscore(@key, '+inf', 3).
39
- should == %w[Madison Jefferson]
40
- end
41
-
42
- it 'honors the :limit => [offset count] argument' do
43
- @redises.zrevrangebyscore(@key, 100, -100, :limit => [1, 2]).
44
- should == %w[Jefferson Adams]
45
- end
46
-
47
- it "raises an error if :limit isn't a 2-tuple" do
48
- lambda do
49
- @redises.zrevrangebyscore(@key, 100, -100, :limit => [1, 2, 3])
50
- end.should raise_error(Redis::CommandError)
51
-
52
- lambda do
53
- @redises.zrevrangebyscore(@key, 100, -100, :limit => '1, 2')
54
- end.should raise_error(RedisMultiplexer::MismatchedResponse)
55
- end
56
-
57
- it_should_behave_like 'a zset-only command'
58
- end
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe '#zrevrank(key, member)' do
4
- before do
5
- @key = 'mock-redis-test:zrevrank'
6
-
7
- @redises.zadd(@key, 1, 'one')
8
- @redises.zadd(@key, 2, 'two')
9
- @redises.zadd(@key, 3, 'three')
10
- end
11
-
12
- it "returns nil if member wasn't present in the set" do
13
- @redises.zrevrank(@key, 'foo').should be_nil
14
- end
15
-
16
- it 'returns the index of the member in the set (ordered by -score)' do
17
- @redises.zrevrank(@key, 'one').should == 2
18
- @redises.zrevrank(@key, 'two').should == 1
19
- @redises.zrevrank(@key, 'three').should == 0
20
- end
21
-
22
- it 'handles integer members correctly' do
23
- member = 11
24
- @redises.zadd(@key, 4, member)
25
- @redises.zrevrank(@key, member).should == 0
26
- end
27
-
28
- it_should_behave_like 'a zset-only command'
29
- end
@@ -1,48 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe '#zscan_each' do
4
- subject { MockRedis::Database.new(self) }
5
-
6
- let(:key) { 'mock-redis-test:zscan_each' }
7
-
8
- before do
9
- allow(subject).to receive(:zrange).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.zscan_each(key).map do |m, s|
17
- [m, s]
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| ["m#{i}", i] } }
26
-
27
- it 'iterates over each item in the collection' do
28
- results = subject.zscan_each(key).map do |m, s|
29
- [m, s]
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) { 'm1*' }
37
- let(:collection) { Array.new(12) { |i| ["m#{i}", i] } }
38
- let(:expected) { [['m1', 1], ['m10', 10], ['m11', 11]] }
39
-
40
- it 'iterates over each item in the filtered collection' do
41
- results = subject.zscan_each(key, match: match).map do |m, s|
42
- [m, s]
43
- end
44
- expect(results).to match_array(expected)
45
- end
46
- end
47
- end
48
- end
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe '#zscan' do
4
- let(:count) { 10 }
5
- let(:match) { '*' }
6
- let(:key) { 'mock-redis-test:zscan' }
7
-
8
- context 'when the zset does not exist' do
9
- it 'returns a 0 cursor and an empty collection' do
10
- expect(@redises.zscan(key, 0, count: count, match: match)).to eq(['0', []])
11
- end
12
- end
13
-
14
- context 'when the zset exists' do
15
- before do
16
- @redises.zadd(key, 1.0, 'member1')
17
- @redises.zadd(key, 2.0, 'member2')
18
- end
19
-
20
- it 'returns a 0 cursor and the collection' do
21
- result = @redises.zscan(key, 0)
22
- expect(result[0]).to eq('0')
23
- expect(result[1]).to match_array([['member1', 1.0], ['member2', 2.0]])
24
- end
25
- end
26
- end
@@ -1,22 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe '#zscore(key, member)' do
4
- before { @key = 'mock-redis-test:zscore' }
5
-
6
- it 'returns the score as a string' do
7
- @redises.zadd(@key, 0.25, 'foo').should == true
8
- @redises.zscore(@key, 'foo').should == 0.25
9
- end
10
-
11
- it 'handles integer members correctly' do
12
- member = 11
13
- @redises.zadd(@key, 0.25, member).should == true
14
- @redises.zscore(@key, member).should == 0.25
15
- end
16
-
17
- it 'returns nil if member is not present in the set' do
18
- @redises.zscore(@key, 'foo').should be_nil
19
- end
20
-
21
- it_should_behave_like 'a zset-only command'
22
- end
@@ -1,104 +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 'the :weights argument' do
45
- it 'multiplies the scores by the weights while aggregating' do
46
- @redises.zunionstore(@dest, [@set1, @set2, @set3], :weights => [2, 3, 5])
47
- @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
48
- [['one', 10.0], ['three', 15.0], ['two', 16.0]]
49
- end
50
-
51
- it 'raises an error if the number of weights != the number of keys' do
52
- lambda do
53
- @redises.zunionstore(@dest, [@set1, @set2, @set3], :weights => [1, 2])
54
- end.should raise_error(Redis::CommandError)
55
- end
56
- end
57
-
58
- context 'the :aggregate argument' do
59
- before do
60
- @smalls = 'mock-redis-test:zunionstore:smalls'
61
- @bigs = 'mock-redis-test:zunionstore:bigs'
62
-
63
- @redises.zadd(@smalls, 1, 'bert')
64
- @redises.zadd(@smalls, 2, 'ernie')
65
- @redises.zadd(@bigs, 100, 'bert')
66
- @redises.zadd(@bigs, 200, 'ernie')
67
- end
68
-
69
- it 'aggregates scores with min when :aggregate => :min is specified' do
70
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :min)
71
- @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
72
- [['bert', 1.0], ['ernie', 2.0]]
73
- end
74
-
75
- it 'aggregates scores with max when :aggregate => :max is specified' do
76
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :max)
77
- @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
78
- [['bert', 100.0], ['ernie', 200.0]]
79
- end
80
-
81
- it 'ignores scores for missing members' do
82
- @redises.zadd(@smalls, 3, 'grover')
83
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :min)
84
- @redises.zscore(@dest, 'grover').should == 3.0
85
-
86
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :max)
87
- @redises.zscore(@dest, 'grover').should == 3.0
88
- end
89
-
90
- it "allows 'min', 'MIN', etc. as aliases for :min" do
91
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => 'min')
92
- @redises.zscore(@dest, 'bert').should == 1.0
93
-
94
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => 'MIN')
95
- @redises.zscore(@dest, 'bert').should == 1.0
96
- end
97
-
98
- it 'raises an error for unknown aggregation function' do
99
- lambda do
100
- @redises.zunionstore(@dest, [@bigs, @smalls], :aggregate => :mix)
101
- end.should raise_error(Redis::CommandError)
102
- end
103
- end
104
- end
@@ -1,86 +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(:now) { 'Now' }
53
- let(:time_stub) { double 'Time', :now => now }
54
- let(:options) { { :time_class => time_stub } }
55
-
56
- subject { MockRedis.new(options) }
57
-
58
- its(:now) { should == now }
59
- end
60
-
61
- describe '.time' do
62
- let(:now) { 'Now' }
63
- let(:time_stub) { double 'Time', :now => now }
64
- let(:options) { { :time_class => time_stub } }
65
-
66
- subject { MockRedis.new(options) }
67
-
68
- its(:time) { should == now }
69
- end
70
-
71
- describe '.expireat' do
72
- let(:time_at) { 'expireat' }
73
- let(:time_stub) { double 'Time' }
74
- let(:options) { { :time_class => time_stub } }
75
- let(:timestamp) { 123_456 }
76
-
77
- subject { MockRedis.new(options) }
78
-
79
- it 'Forwards time_at to the time_class' do
80
- time_stub.should_receive(:at).with(timestamp).and_return(time_at)
81
-
82
- subject.time_at(timestamp).should == time_at
83
- end
84
- end
85
- end
86
- end
data/spec/spec_helper.rb DELETED
@@ -1,63 +0,0 @@
1
- if ENV['TRAVIS']
2
- # When running in Travis, report coverage stats to Coveralls.
3
- require 'coveralls'
4
- Coveralls.wear!
5
- else
6
- # Otherwise render coverage information in coverage/index.html and display
7
- # coverage percentage in the console.
8
- require 'simplecov'
9
- end
10
-
11
- require 'rspec/its'
12
- require 'redis'
13
- $LOAD_PATH.unshift(File.expand_path(File.join(__FILE__, '..', '..', 'lib')))
14
- require 'mock_redis'
15
-
16
- $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..')))
17
- Dir['spec/support/**/*.rb'].each { |x| require x }
18
-
19
- module TypeCheckingHelper
20
- def method_from_description(example)
21
- # extracting this from the RSpec description string may or may not
22
- # be a good idea. On the one hand, it enforces the convention of
23
- # putting the method name in the right place; on the other hand,
24
- # it's pretty magic-looking.
25
- example.full_description.match(/#(\w+)/).captures.first
26
- end
27
-
28
- def args_for_method(method)
29
- return [] if method.to_s == 'spop'
30
- method_arity = @redises.real.method(method).arity
31
- if method_arity < 0 # -1 comes from def foo(*args)
32
- [1, 2] # probably good enough
33
- else
34
- 1.upto(method_arity - 1).to_a
35
- end
36
- end
37
- end
38
-
39
- RSpec.configure do |config|
40
- config.expect_with :rspec do |c|
41
- c.syntax = [:expect, :should]
42
- end
43
-
44
- config.mock_with :rspec do |c|
45
- c.syntax = [:expect, :should]
46
- end
47
-
48
- config.include(TypeCheckingHelper)
49
-
50
- config.before(:all) do
51
- @redises = RedisMultiplexer.new
52
- end
53
-
54
- config.before(:each) do
55
- # databases mentioned in our tests
56
- [1, 0].each do |db|
57
- @redises.send_without_checking(:select, db)
58
- @redises.send_without_checking(:keys, 'mock-redis-test:*').each do |key|
59
- @redises.send_without_checking(:del, key)
60
- end
61
- end
62
- end
63
- end
@@ -1,106 +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
- @real_redis = Redis.new(*a)
13
- end
14
-
15
- def method_missing(method, *args, &blk)
16
- # If we're in a Redis command that accepts a block, and we execute more
17
- # redis commands, ONLY execute them on the Redis implementation that the
18
- # block came from.
19
- # e.g. if a pipelined command is started on a MockRedis object, DON'T send
20
- # commands inside the pipelined block to the real Redis object, as that one
21
- # WON'T be inside a pipelined command, and we'll see weird behaviour
22
- if blk
23
- @in_mock_block = false
24
- @in_redis_block = true
25
- end
26
- real_retval, real_error =
27
- catch_errors { @in_mock_block ? :no_op : @real_redis.send(method, *args, &blk) }
28
-
29
- if blk
30
- @in_mock_block = true
31
- @in_redis_block = false
32
- end
33
- mock_retval, mock_error =
34
- catch_errors { @in_redis_block ? :no_op : @mock_redis.send(method, *args, &blk) }
35
-
36
- if blk
37
- @in_mock_block = false
38
- @in_redis_block = false
39
- end
40
-
41
- if mock_retval == :no_op || real_retval == :no_op
42
- # ignore, we were inside a block (like pipelined)
43
- elsif !equalish?(mock_retval, real_retval) && !mock_error && !real_error
44
- # no exceptions, just different behavior
45
- raise MismatchedResponse,
46
- "Mock failure: responses not equal.\n" \
47
- "Redis.#{method}(#{args.inspect}) returned #{real_retval.inspect}\n" \
48
- "MockRedis.#{method}(#{args.inspect}) returned #{mock_retval.inspect}\n"
49
- elsif !mock_error && real_error
50
- raise MismatchedResponse,
51
- "Mock failure: didn't raise an error when it should have.\n" \
52
- "Redis.#{method}(#{args.inspect}) raised #{real_error.inspect}\n" \
53
- "MockRedis.#{method}(#{args.inspect}) raised nothing " \
54
- "and returned #{mock_retval.inspect}"
55
- elsif !real_error && mock_error
56
- raise MismatchedResponse,
57
- "Mock failure: raised an error when it shouldn't have.\n" \
58
- "Redis.#{method}(#{args.inspect}) returned #{real_retval.inspect}\n" \
59
- "MockRedis.#{method}(#{args.inspect}) raised #{mock_error.inspect}"
60
- elsif mock_error && real_error && !equalish?(mock_error, real_error)
61
- raise MismatchedResponse,
62
- "Mock failure: raised the wrong error.\n" \
63
- "Redis.#{method}(#{args.inspect}) raised #{real_error.inspect}\n" \
64
- "MockRedis.#{method}(#{args.inspect}) raised #{mock_error.inspect}"
65
- end
66
-
67
- raise mock_error if mock_error
68
- mock_retval
69
- end
70
-
71
- def equalish?(a, b)
72
- if a == b
73
- true
74
- elsif a.is_a?(Array) && b.is_a?(Array)
75
- a.collect(&:to_s).sort == b.collect(&:to_s).sort
76
- elsif a.is_a?(Exception) && b.is_a?(Exception)
77
- a.class == b.class && a.message == b.message
78
- elsif a.is_a?(Float) && b.is_a?(Float)
79
- delta = [a.abs, b.abs].min / 1_000_000.0
80
- (a - b).abs < delta
81
- else
82
- false
83
- end
84
- end
85
-
86
- def mock
87
- @mock_redis
88
- end
89
-
90
- def real
91
- @real_redis
92
- end
93
-
94
- # Used in cleanup before() blocks.
95
- def send_without_checking(method, *args)
96
- @mock_redis.send(method, *args)
97
- @real_redis.send(method, *args)
98
- end
99
-
100
- def catch_errors
101
- retval = yield
102
- [retval, nil]
103
- rescue StandardError => e
104
- [nil, e]
105
- end
106
- end
@@ -1,13 +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
- end
@@ -1,13 +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
- end
@@ -1,13 +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
- 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,57 +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
- end
14
-
15
- shared_examples_for 'arg 1 is a score' do
16
- before { @_arg_index = 1 }
17
- it_should_behave_like 'arg N is a score'
18
- end
19
-
20
- shared_examples_for 'arg 2 is a score' do
21
- before { @_arg_index = 2 }
22
- it_should_behave_like 'arg N is a score'
23
- end
24
-
25
- shared_examples_for 'arg N is a score' do
26
- before do |example|
27
- key = 'mock-redis-test:zset-only'
28
-
29
- @method = method_from_description(example)
30
- @args = args_for_method(@method).unshift(key)
31
- end
32
-
33
- it 'is okay with positive ints' do
34
- @args[@_arg_index] = 1
35
- lambda { @redises.send(@method, *@args) }.should_not raise_error
36
- end
37
-
38
- it 'is okay with negative ints' do
39
- @args[@_arg_index] = -1
40
- lambda { @redises.send(@method, *@args) }.should_not raise_error
41
- end
42
-
43
- it 'is okay with positive floats' do
44
- @args[@_arg_index] = 1.5
45
- lambda { @redises.send(@method, *@args) }.should_not raise_error
46
- end
47
-
48
- it 'is okay with negative floats' do
49
- @args[@_arg_index] = -1.5
50
- lambda { @redises.send(@method, *@args) }.should_not raise_error
51
- end
52
-
53
- it 'rejects non-numbers' do
54
- @args[@_arg_index] = 'foo'
55
- lambda { @redises.send(@method, *@args) }.should raise_error(RuntimeError)
56
- end
57
- end