mock_redis 0.34.0 → 0.36.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85e71d6b9f2fa3400db015c2c29a7f8c0d017c1c6c0f44f45c404fd1eb8d6fd6
4
- data.tar.gz: b61b3a420aa7dc9232fffbddd263c82719665cc738e1e36834d95fcd9566cff3
3
+ metadata.gz: dac18dd7d43fb11953bf9988a89d64d8f82a0d6b909836d215880b5fc6c75e98
4
+ data.tar.gz: e34af1ec074a43fbdeefa25e27543c57a146e0e22900842875adfca1fed3a698
5
5
  SHA512:
6
- metadata.gz: ce57715bda59804aeed319d49a76d81ac9cd1e4d9093005402dd256b804b20245c2a723051eef79c7da883896e75b72b91fe0066ae6e830dd1d24130a751b965
7
- data.tar.gz: b4a4d237d9705b64b973fd5959ed2544fd8fbf037f5525b1080d647c1b4a1743fb22110e8d5af4cf4077ba7a185e880f4cfbb303ff23346db3611adc64d5aab3
6
+ metadata.gz: a47e3c68ad32983cd4017ba6541dbfe3004d197468179713d5102bbe094a9978256831f9ee3d3c9a7abf8ec0c07b18ee8c04b911e9ca326e8d04da8288b8131e
7
+ data.tar.gz: 119d337effa67690de4754bfa9bfe37bcceade74089b73d8328fd7818877b14afb708ba432ea7b52c6299186d7dad736b14de95acde7bb51909ad6096eb1a143
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # MockRedis Changelog
2
2
 
3
+ ### 0.36.0
4
+
5
+ * Add support for `smismember`
6
+ * Add support for `lmove` and `blmove`
7
+ * Fix `zrem` to support passing array of integers
8
+
9
+ ### 0.35.0
10
+
11
+ * Add support for `getdel`
12
+
3
13
  ### 0.34.0
4
14
 
5
15
  * Add support for `with`
data/README.md CHANGED
@@ -55,8 +55,8 @@ since it's an in-memory object confined to a single process. MockRedis
55
55
  makes every attempt to be Redis-compatible, but there are some
56
56
  necessary exceptions.
57
57
 
58
- * Blocking list commands (`#blpop`, `#brpop`, and `#brpoplpush`) work
59
- as expected if there is data for them to retrieve. If you use one of
58
+ * Blocking list commands (`#blmove`, `#blpop`, `#brpop`, and `#brpoplpush`)
59
+ work as expected if there is data for them to retrieve. If you use one of
60
60
  these commands with a nonzero timeout and there is no data for it to
61
61
  retrieve, then the command returns immediately. However, if you ask
62
62
  one of these commands for data with a 0 timeout (means "wait
@@ -6,6 +6,20 @@ class MockRedis
6
6
  include Assertions
7
7
  include UtilityMethods
8
8
 
9
+ def blmove(source, destination, wherefrom, whereto, options = {})
10
+ options = { :timeout => options } if options.is_a?(Integer)
11
+ timeout = options.is_a?(Hash) && options[:timeout] || 0
12
+ assert_valid_timeout(timeout)
13
+
14
+ if llen(source) > 0
15
+ lmove(source, destination, wherefrom, whereto)
16
+ elsif timeout > 0
17
+ nil
18
+ else
19
+ raise MockRedis::WouldBlock, "Can't block forever"
20
+ end
21
+ end
22
+
9
23
  def blpop(*args)
10
24
  lists, timeout = extract_timeout(args)
11
25
  nonempty_list = first_nonempty_list(lists)
@@ -78,6 +92,22 @@ class MockRedis
78
92
  with_list_at(key, &:length)
79
93
  end
80
94
 
95
+ def lmove(source, destination, wherefrom, whereto)
96
+ assert_listy(source)
97
+ assert_listy(destination)
98
+
99
+ wherefrom = wherefrom.to_s.downcase
100
+ whereto = whereto.to_s.downcase
101
+
102
+ unless %w[left right].include?(wherefrom) && %w[left right].include?(whereto)
103
+ raise Redis::CommandError, 'ERR syntax error'
104
+ end
105
+
106
+ value = wherefrom == 'left' ? lpop(source) : rpop(source)
107
+ (whereto == 'left' ? lpush(destination, value) : rpush(destination, value)) unless value.nil?
108
+ value
109
+ end
110
+
81
111
  def lpop(key)
82
112
  with_list_at(key, &:shift)
83
113
  end
@@ -64,6 +64,12 @@ class MockRedis
64
64
  with_set_at(key) { |s| s.include?(member.to_s) }
65
65
  end
66
66
 
67
+ def smismember(key, *members)
68
+ with_set_at(key) do |set|
69
+ members.flatten.map { |m| set.include?(m.to_s) }
70
+ end
71
+ end
72
+
67
73
  def smembers(key)
68
74
  with_set_at(key, &:to_a).map(&:dup).reverse
69
75
  end
@@ -107,6 +107,12 @@ class MockRedis
107
107
  end
108
108
  end
109
109
 
110
+ def getdel(key)
111
+ value = get(key)
112
+ del(key)
113
+ value
114
+ end
115
+
110
116
  def getrange(key, start, stop)
111
117
  assert_stringy(key)
112
118
  (data[key] || '')[start..stop]
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  class MockRedis
5
- VERSION = '0.34.0'
5
+ VERSION = '0.36.0'
6
6
  end
@@ -147,7 +147,7 @@ class MockRedis
147
147
  else
148
148
  retval = args.map { |member| !!zscore(key, member.to_s) }.count(true)
149
149
  with_zset_at(key) do |z|
150
- args.each { |member| z.delete?(member) }
150
+ args.each { |member| z.delete?(member.to_s) }
151
151
  end
152
152
  end
153
153
  end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#blmove(source, destination, wherefrom, whereto, timeout)' do
4
+ before do
5
+ @list1 = 'mock-redis-test:blmove-list1'
6
+ @list2 = 'mock-redis-test:blmove-list2'
7
+
8
+ @redises.lpush(@list1, 'b')
9
+ @redises.lpush(@list1, 'a')
10
+
11
+ @redises.lpush(@list2, 'y')
12
+ @redises.lpush(@list2, 'x')
13
+ end
14
+
15
+ it 'returns the value moved' do
16
+ @redises.blmove(@list1, @list2, 'left', 'right').should == 'a'
17
+ end
18
+
19
+ it 'takes the first element of source and appends it to destination' do
20
+ @redises.blmove(@list1, @list2, 'left', 'right')
21
+
22
+ @redises.lrange(@list1, 0, -1).should == %w[b]
23
+ @redises.lrange(@list2, 0, -1).should == %w[x y a]
24
+ end
25
+
26
+ it 'raises an error on negative timeout' do
27
+ lambda do
28
+ @redises.blmove(@list1, @list2, 'left', 'right', :timeout => -1)
29
+ end.should raise_error(Redis::CommandError)
30
+ end
31
+
32
+ let(:default_error) { RedisMultiplexer::MismatchedResponse }
33
+ it_should_behave_like 'a list-only command'
34
+
35
+ context '[mock only]' do
36
+ it 'ignores positive timeouts and returns nil' do
37
+ @redises.mock.blmove('mock-redis-test:not-here', @list1, 'left', 'right', :timeout => 1).
38
+ should be_nil
39
+ end
40
+
41
+ it 'ignores positive legacy timeouts and returns nil' do
42
+ @redises.mock.blmove('mock-redis-test:not-here', @list1, 'left', 'right', 1).
43
+ should be_nil
44
+ end
45
+
46
+ it 'raises WouldBlock on zero timeout (no blocking in the mock)' do
47
+ lambda do
48
+ @redises.mock.blmove('mock-redis-test:not-here', @list1, 'left', 'right', :timeout => 0)
49
+ end.should raise_error(MockRedis::WouldBlock)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#getdel(key)' do
4
+ before do
5
+ @key = 'mock-redis-test:73288'
6
+ end
7
+
8
+ it 'returns nil for a nonexistent value' do
9
+ @redises.getdel('mock-redis-test:does-not-exist').should be_nil
10
+ end
11
+
12
+ it 'returns a stored string value' do
13
+ @redises.set(@key, 'forsooth')
14
+ @redises.getdel(@key).should == 'forsooth'
15
+ end
16
+
17
+ it 'deletes the key after returning it' do
18
+ @redises.set(@key, 'forsooth')
19
+ @redises.getdel(@key)
20
+ @redises.get(@key).should be_nil
21
+ end
22
+
23
+ it_should_behave_like 'a string-only command'
24
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#lmove(source, destination, wherefrom, whereto)' do
4
+ before do
5
+ @list1 = 'mock-redis-test:lmove-list1'
6
+ @list2 = 'mock-redis-test:lmove-list2'
7
+
8
+ @redises.lpush(@list1, 'b')
9
+ @redises.lpush(@list1, 'a')
10
+
11
+ @redises.lpush(@list2, 'y')
12
+ @redises.lpush(@list2, 'x')
13
+ end
14
+
15
+ it 'returns the value moved' do
16
+ @redises.lmove(@list1, @list2, 'left', 'right').should == 'a'
17
+ end
18
+
19
+ it "returns nil and doesn't append if source empty" do
20
+ @redises.lmove('empty', @list1, 'left', 'right').should be_nil
21
+ @redises.lrange(@list1, 0, -1).should == %w[a b]
22
+ end
23
+
24
+ it 'takes the first element of source and prepends it to destination' do
25
+ @redises.lmove(@list1, @list2, 'left', 'left')
26
+
27
+ @redises.lrange(@list1, 0, -1).should == %w[b]
28
+ @redises.lrange(@list2, 0, -1).should == %w[a x y]
29
+ end
30
+
31
+ it 'takes the first element of source and appends it to destination' do
32
+ @redises.lmove(@list1, @list2, 'left', 'right')
33
+
34
+ @redises.lrange(@list1, 0, -1).should == %w[b]
35
+ @redises.lrange(@list2, 0, -1).should == %w[x y a]
36
+ end
37
+
38
+ it 'takes the last element of source and prepends it to destination' do
39
+ @redises.lmove(@list1, @list2, 'right', 'left')
40
+
41
+ @redises.lrange(@list1, 0, -1).should == %w[a]
42
+ @redises.lrange(@list2, 0, -1).should == %w[b x y]
43
+ end
44
+
45
+ it 'takes the last element of source and appends it to destination' do
46
+ @redises.lmove(@list1, @list2, 'right', 'right')
47
+
48
+ @redises.lrange(@list1, 0, -1).should == %w[a]
49
+ @redises.lrange(@list2, 0, -1).should == %w[x y b]
50
+ end
51
+
52
+ it 'rotates a list when source and destination are the same' do
53
+ @redises.lmove(@list1, @list1, 'left', 'right')
54
+ @redises.lrange(@list1, 0, -1).should == %w[b a]
55
+ end
56
+
57
+ it 'removes empty lists' do
58
+ @redises.llen(@list1).times { @redises.lmove(@list1, @list2, 'left', 'right') }
59
+ @redises.get(@list1).should be_nil
60
+ end
61
+
62
+ it 'raises an error for non-list source value' do
63
+ @redises.set(@list1, 'string value')
64
+
65
+ lambda do
66
+ @redises.lmove(@list1, @list2, 'left', 'right')
67
+ end.should raise_error(Redis::CommandError)
68
+ end
69
+
70
+ let(:default_error) { RedisMultiplexer::MismatchedResponse }
71
+ it_should_behave_like 'a list-only command'
72
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#smismember(key, *members)' do
4
+ before do
5
+ @key = 'mock-redis-test:smismember'
6
+ @redises.sadd(@key, 'whiskey')
7
+ @redises.sadd(@key, 'beer')
8
+ end
9
+
10
+ it 'returns true if member is in set' do
11
+ @redises.smismember(@key, 'whiskey').should == [true]
12
+ @redises.smismember(@key, 'beer').should == [true]
13
+ @redises.smismember(@key, 'whiskey', 'beer').should == [true, true]
14
+ @redises.smismember(@key, %w[whiskey beer]).should == [true, true]
15
+ end
16
+
17
+ it 'returns false if member is not in set' do
18
+ @redises.smismember(@key, 'cola').should == [false]
19
+ @redises.smismember(@key, 'whiskey', 'cola').should == [true, false]
20
+ @redises.smismember(@key, %w[whiskey beer cola]).should == [true, true, false]
21
+ end
22
+
23
+ it 'stringifies member' do
24
+ @redises.sadd(@key, '1')
25
+ @redises.smismember(@key, 1).should == [true]
26
+ @redises.smismember(@key, [1]).should == [true]
27
+ end
28
+
29
+ it 'treats a nonexistent value as an empty set' do
30
+ @redises.smismember('mock-redis-test:nonesuch', 'beer').should == [false]
31
+ end
32
+
33
+ it_should_behave_like 'a set-only command'
34
+ end
@@ -28,6 +28,13 @@ describe '#zrem(key, member)' do
28
28
  @redises.zrange(@key, 0, -1).should == %w[one two]
29
29
  end
30
30
 
31
+ it 'removes integer members inside an array from the set' do
32
+ member = 11
33
+ @redises.zadd(@key, 3, member)
34
+ @redises.zrem(@key, [member]).should == 1
35
+ @redises.zrange(@key, 0, -1).should == %w[one two]
36
+ end
37
+
31
38
  it 'supports a variable number of arguments' do
32
39
  @redises.zrem(@key, %w[one two])
33
40
  @redises.zrange(@key, 0, -1).should be_empty
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock_redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.34.0
4
+ version: 0.36.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane da Silva
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-09-19 00:00:00.000000000 Z
12
+ date: 2023-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby2_keywords
@@ -138,6 +138,7 @@ files:
138
138
  - spec/commands/bgsave_spec.rb
139
139
  - spec/commands/bitcount_spec.rb
140
140
  - spec/commands/bitfield_spec.rb
141
+ - spec/commands/blmove_spec.rb
141
142
  - spec/commands/blpop_spec.rb
142
143
  - spec/commands/brpop_spec.rb
143
144
  - spec/commands/brpoplpush_spec.rb
@@ -164,6 +165,7 @@ files:
164
165
  - spec/commands/geopos_spec.rb
165
166
  - spec/commands/get_spec.rb
166
167
  - spec/commands/getbit_spec.rb
168
+ - spec/commands/getdel.rb
167
169
  - spec/commands/getrange_spec.rb
168
170
  - spec/commands/getset_spec.rb
169
171
  - spec/commands/hdel_spec.rb
@@ -190,6 +192,7 @@ files:
190
192
  - spec/commands/lindex_spec.rb
191
193
  - spec/commands/linsert_spec.rb
192
194
  - spec/commands/llen_spec.rb
195
+ - spec/commands/lmove_spec.rb
193
196
  - spec/commands/lpop_spec.rb
194
197
  - spec/commands/lpush_spec.rb
195
198
  - spec/commands/lpushx_spec.rb
@@ -240,6 +243,7 @@ files:
240
243
  - spec/commands/sinterstore_spec.rb
241
244
  - spec/commands/sismember_spec.rb
242
245
  - spec/commands/smembers_spec.rb
246
+ - spec/commands/smismember_spec.rb
243
247
  - spec/commands/smove_spec.rb
244
248
  - spec/commands/sort_list_spec.rb
245
249
  - spec/commands/sort_set_spec.rb
@@ -325,6 +329,7 @@ test_files:
325
329
  - spec/commands/bgsave_spec.rb
326
330
  - spec/commands/bitcount_spec.rb
327
331
  - spec/commands/bitfield_spec.rb
332
+ - spec/commands/blmove_spec.rb
328
333
  - spec/commands/blpop_spec.rb
329
334
  - spec/commands/brpop_spec.rb
330
335
  - spec/commands/brpoplpush_spec.rb
@@ -351,6 +356,7 @@ test_files:
351
356
  - spec/commands/geopos_spec.rb
352
357
  - spec/commands/get_spec.rb
353
358
  - spec/commands/getbit_spec.rb
359
+ - spec/commands/getdel.rb
354
360
  - spec/commands/getrange_spec.rb
355
361
  - spec/commands/getset_spec.rb
356
362
  - spec/commands/hdel_spec.rb
@@ -377,6 +383,7 @@ test_files:
377
383
  - spec/commands/lindex_spec.rb
378
384
  - spec/commands/linsert_spec.rb
379
385
  - spec/commands/llen_spec.rb
386
+ - spec/commands/lmove_spec.rb
380
387
  - spec/commands/lpop_spec.rb
381
388
  - spec/commands/lpush_spec.rb
382
389
  - spec/commands/lpushx_spec.rb
@@ -427,6 +434,7 @@ test_files:
427
434
  - spec/commands/sinterstore_spec.rb
428
435
  - spec/commands/sismember_spec.rb
429
436
  - spec/commands/smembers_spec.rb
437
+ - spec/commands/smismember_spec.rb
430
438
  - spec/commands/smove_spec.rb
431
439
  - spec/commands/sort_list_spec.rb
432
440
  - spec/commands/sort_set_spec.rb