Skip to content

Commit

Permalink
#292 extend set method to accept a third positional argument (#299)
Browse files Browse the repository at this point in the history
# Summary
closes #292 

This pull request is for issue #292.
I extend set method to accept a third positional argument.


## Purpose of This Change

The primary goal of this change is to enhance the compatibility of the
`mock_redis` `set` method with the `redis-store` `set` method. By
introducing a third positional argument, we aim to align more closely
with the `redis-store` implementation, facilitating easier integration
and use in environments where `redis-store` is prevalent.

## Compatibility Concerns with Standard Redis

While the `redis-store` `set` method accepts three positional arguments,
the standard Redis `set` method traditionally takes only two. This
discrepancy could raise concerns about compatibility. However, this
change ensures that compatibility with the standard Redis API is
maintained and verified through existing tests in `set_spec.rb`.


## Before and After the Change

### Before
The `set` method in `mock_redis` was defined as follows:
```ruby
def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
```
This definition aligns with the standard Redis API but lacks
compatibility with the `redis-store` version.

### After
The revised `set` method is now defined with an additional third
positional argument:
```ruby
def set(key, value, _hash = nil, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
```
This addition enables compatibility with `redis-store` without
compromising the existing functionality with standard Redis.
  • Loading branch information
sho-work committed Jan 17, 2024
1 parent 3b6849e commit 9518740
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/mock_redis/string_methods.rb
Expand Up @@ -212,8 +212,8 @@ def mapped_msetnx(hash)

# Parameter list required to ensure the ArgumentError is returned correctly
# rubocop:disable Metrics/ParameterLists
def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil,
get: nil)
def set(key, value, _hash = nil, 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
16 changes: 15 additions & 1 deletion spec/commands/set_spec.rb
@@ -1,12 +1,26 @@
require 'spec_helper'

RSpec.describe '#set(key, value)' do
RSpec.describe '#set(key, value, _hash)' do
let(:key) { 'mock-redis-test' }

it "responds with 'OK'" do
expect(@redises.set('mock-redis-test', 1)).to eq('OK')
end

context 'when the positional argument _hash exists' do
it "responds with 'OK'" do
# Testing MockRedis.new.set instead of @redises.set
# because the latter doesn't align with Redis's set method.
# In mock_redis, set accepts a third positional argument, _hash,
# to accommodate the difference with redis-store's set method,
# which takes three positional arguments, unlike the standard Redis set.
# Reference:
# Redis: https://github.com/redis/redis-rb/blob/09acb9026ab2deaca4c851e0bcdb0ef9318b1ee0/lib/redis/commands/strings.rb#L83
# Redis-store: https://github.com/redis-store/redis-store/blob/5c3fee1b8fba672eb2bd5bfaedb973b68d12b773/lib/redis/store/ttl.rb#L4
expect(MockRedis.new.set('mock-redis-test', 1, { key: 'value' })).to eq('OK')
end
end

context 'options' do
it 'raises an error for EX seconds = 0' do
expect do
Expand Down

0 comments on commit 9518740

Please sign in to comment.