Skip to content

Commit

Permalink
feat(set_exat_pxat): Add EXAT and PXAT arguments to the SET redis com…
Browse files Browse the repository at this point in the history
…mand
  • Loading branch information
jcagarcia committed Oct 13, 2023
1 parent f4a151d commit 2c31deb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# MockRedis Changelog

### 0.38.0

* Add support for `EXAT` AND `PXAT` arguments to `SET` command

### 0.37.0

* Require Ruby 2.7 or newer, since Ruby 2.6 and older are EOL
Expand Down
18 changes: 16 additions & 2 deletions lib/mock_redis/string_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ def mapped_msetnx(hash)
msetnx(*hash.to_a.flatten)
end

# Parameer list required to ensure the ArgumentError is returned correctly
# Parameter 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, get: nil)
def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
key = key.to_s
retval = self.get(key) if get

Expand Down Expand Up @@ -248,6 +248,20 @@ def set(key, value, ex: nil, px: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
pexpire(key, px)
end

if exat
if exat == 0
raise Redis::CommandError, 'ERR invalid expire time in set'
end
expireat(key, exat)
end

if pxat
if pxat == 0
raise Redis::CommandError, 'ERR invalid expire time in set'
end
pexpireat(key, pxat)
end

if get
retval
else
Expand Down
2 changes: 1 addition & 1 deletion lib/mock_redis/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

# Defines the gem version.
class MockRedis
VERSION = '0.37.0'
VERSION = '0.38.0'
end
38 changes: 38 additions & 0 deletions spec/commands/set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
end.to raise_error(Redis::CommandError, 'ERR invalid expire time in set')
end

it 'raises an error for EXAT seconds = 0' do
expect do
@redises.set('mock-redis-test', 1, exat: 0)
end.to raise_error(Redis::CommandError, 'ERR invalid expire time in set')
end

it 'raises an error for PXAT seconds = 0' do
expect do
@redises.set('mock-redis-test', 1, pxat: 0)
end.to raise_error(Redis::CommandError, 'ERR invalid expire time in set')
end

it 'accepts NX' do
@redises.del(key)
expect(@redises.set(key, 1, nx: true)).to eq(true)
Expand All @@ -33,6 +45,16 @@
expect(@redises.set(key, 1, xx: true)).to eq(true)
end

it 'accepts EXAT' do
@redises.del(key)
expect(@redises.set(key, 1, exat: 1697197606)).to eq('OK')
end

it 'accepts PXAT' do
@redises.del(key)
expect(@redises.set(key, 1, exat: 1697197589362)).to eq('OK')
end

it 'accepts GET on a string' do
expect(@redises.set(key, '1')).to eq('OK')
expect(@redises.set(key, '2', get: true)).to eq('1')
Expand Down Expand Up @@ -129,6 +151,22 @@
allow(Time).to receive(:now).and_return(@now + 600 / 1000.to_f)
expect(@mock.get(key)).to be_nil
end

it 'accepts EXAT seconds' do
expect(@mock.set(key, 1, exat: (@now + 1).to_i)).to eq('OK')
expect(@mock.get(key)).not_to be_nil
allow(Time).to receive(:now).and_return(@now + 2)
expect(@mock.get(key)).to be_nil
end

it 'accepts PXAT milliseconds' do
expect(@mock.set(key, 1, pxat: ((@now + 500).to_f * 1000).to_i)).to eq('OK')
expect(@mock.get(key)).not_to be_nil
allow(Time).to receive(:now).and_return(@now + 300)
expect(@mock.get(key)).not_to be_nil
allow(Time).to receive(:now).and_return(@now + 600)
expect(@mock.get(key)).to be_nil
end
end
end
end

0 comments on commit 2c31deb

Please sign in to comment.