Skip to content

Commit

Permalink
Implement call method in pipelined block (#283)
Browse files Browse the repository at this point in the history
Resolves #278

Seems like everything is working; however, I'm not sure if moving
`#call` from `MockRedis` to `MockRedis::Database` has some side-effects
-- is there a reason `call` was separated from other commands?

I also changed `send` to `public_send` in `call` implementation (seems
to be OK, I'm not sure if there are any edge cases when calling private
is a usecase).

Also, I think we should allow mix-cased variants of Redis methods
(`RedisMock.new.call(["GET", 123])` does not work right now, but redis
commands are case insensitive).

Usage example:

```ruby
redis = MockRedis.new

redis.pipelined do |pipeline|
  pipeline.call(["get", "foo"])
  pipeline.call(["get", "bar"])
end
```

Co-authored-by: ya2k <yk@wfolio.com>
  • Loading branch information
viralpraxis and ya2k committed Oct 29, 2023
1 parent 36435ec commit 29961e0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 0 additions & 4 deletions lib/mock_redis.rb
Expand Up @@ -49,10 +49,6 @@ def id
end
alias location id

def call(command, &_block)
send(*command)
end

def host
options[:host]
end
Expand Down
4 changes: 4 additions & 0 deletions lib/mock_redis/database.rb
Expand Up @@ -43,6 +43,10 @@ def initialize_copy(_source)

# Redis commands go below this line and above 'private'

def call(command, &_block)
public_send(*command)
end

def auth(_)
'OK'
end
Expand Down
22 changes: 22 additions & 0 deletions spec/commands/pipelined_spec.rb
Expand Up @@ -111,4 +111,26 @@
expect(results).to eq([value1, value2])
end
end

context 'with `call` method' do
let(:key1) { 'hello' }
let(:key2) { 'world' }
let(:value1) { 'foo' }
let(:value2) { 'bar' }

before do
@redises.set key1, value1
@redises.set key2, value2
end

it 'returns results of pipelined operations' do
results = @redises.pipelined do |redis|
redis.call(['get', key1])
redis.call(['set', key2, 'foobar'])
redis.call(['get', key2])
end

expect(results).to eq([value1, 'OK', 'foobar'])
end
end
end

0 comments on commit 29961e0

Please sign in to comment.