Skip to content

Commit

Permalink
feat(set_get): Add GET argument to the SET redis command (#243)
Browse files Browse the repository at this point in the history
* documentation here: https://redis.io/commands/set/

Co-authored-by: Theodore Hanson <ted@Theodores-MBP.attlocal.net>
  • Loading branch information
ted-hanson and Theodore Hanson committed Aug 20, 2022
1 parent b101299 commit 8f01ab4
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/mock_redis/database.rb
Expand Up @@ -71,7 +71,7 @@ def dbsize

def del(*keys)
keys = keys.flatten.map(&:to_s)
assert_has_args(keys, 'del')
# assert_has_args(keys, 'del') # no longer errors in redis > v4.5

keys.
find_all { |key| data[key] }.
Expand Down
10 changes: 8 additions & 2 deletions lib/mock_redis/string_methods.rb
Expand Up @@ -206,8 +206,10 @@ def mapped_msetnx(hash)

# Parameer list required to ensure the ArgumentError is returned correctly
# rubocop:disable Metrics/ParameterLists
def set(key, value, ex: nil, px: nil, nx: nil, xx: nil, keepttl: nil)
def set(key, value, ex: nil, px: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
key = key.to_s
retval = self.get(key) if get

return_true = false
if nx
if exists?(key)
Expand Down Expand Up @@ -240,7 +242,11 @@ def set(key, value, ex: nil, px: nil, nx: nil, xx: nil, keepttl: nil)
pexpire(key, px)
end

return_true ? true : 'OK'
if get
retval
else
return_true ? true : 'OK'
end
end
# rubocop:enable Metrics/ParameterLists

Expand Down
2 changes: 1 addition & 1 deletion mock_redis.gemspec
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'ruby2_keywords'

s.add_development_dependency 'redis', '~> 4.2.0'
s.add_development_dependency 'redis', '~> 4.5.0'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'rspec-its', '~> 1.0'
s.add_development_dependency 'timecop', '~> 0.9.1'
Expand Down
2 changes: 1 addition & 1 deletion spec/commands/del_spec.rb
Expand Up @@ -40,7 +40,7 @@
end

it 'raises an error if an empty array is given' do
expect { @redises.del [] }.to raise_error Redis::CommandError
expect { @redises.del [] }.not_to raise_error Redis::CommandError
end

it 'removes a stream key' do
Expand Down
1 change: 1 addition & 0 deletions spec/commands/lpop_spec.rb
Expand Up @@ -30,5 +30,6 @@
@redises.get(@key).should be_nil
end

let(:default_error) { RedisMultiplexer::MismatchedResponse }
it_should_behave_like 'a list-only command'
end
1 change: 1 addition & 0 deletions spec/commands/rpop_spec.rb
Expand Up @@ -30,5 +30,6 @@
@redises.get(@key).should be_nil
end

let(:default_error) { RedisMultiplexer::MismatchedResponse }
it_should_behave_like 'a list-only command'
end
15 changes: 15 additions & 0 deletions spec/commands/set_spec.rb
Expand Up @@ -33,6 +33,21 @@
@redises.set(key, 1, xx: true).should == true
end

it 'accepts GET on a string' do
@redises.set(key, '1').should == 'OK'
@redises.set(key, '2', get: true).should == '1'
@redises.set(key, '3', get: true).should == '2'
end

context 'when set key is not a String' do
it 'should error with Redis::CommandError' do
@redises.lpush(key, '1').should == 1
expect do
@redises.set(key, '2', get: true)
end.to raise_error(Redis::CommandError)
end
end

it 'sets the ttl to -1' do
@redises.set(key, 1)
expect(@redises.ttl(key)).to eq(-1)
Expand Down
5 changes: 4 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -62,7 +62,10 @@ def args_for_method(method)
# databases mentioned in our tests
[1, 0].each do |db|
@redises.send_without_checking(:select, db)
@redises.send_without_checking(:keys, 'mock-redis-test:*').each do |key|
keys = @redises.send_without_checking(:keys, 'mock-redis-test:*')
next unless keys.is_a?(Enumerable)

keys.each do |key|
@redises.send_without_checking(:del, key)
end
end
Expand Down
Expand Up @@ -8,7 +8,7 @@
@redises.set(key, '')
lambda do
@redises.send(method, *args)
end.should raise_error(RuntimeError)
end.should raise_error(defined?(default_error) ? default_error : RuntimeError)
@redises.get(key).should == ''
end
end
2 changes: 1 addition & 1 deletion spec/support/shared_examples/only_operates_on_lists.rb
Expand Up @@ -8,7 +8,7 @@
@redises.set(key, 1)
lambda do
@redises.send(method, *args)
end.should raise_error(RuntimeError)
end.should raise_error(defined?(default_error) ? default_error : RuntimeError)
end

it_should_behave_like 'does not remove empty strings on error'
Expand Down

0 comments on commit 8f01ab4

Please sign in to comment.