From 29961e0f7e13c2e5d5da7e43a96ab04704f8f224 Mon Sep 17 00:00:00 2001 From: Yaroslav <39729785+viralpraxis@users.noreply.github.com> Date: Sun, 29 Oct 2023 06:24:48 +0200 Subject: [PATCH] Implement `call` method in `pipelined` block (#283) Resolves https://github.com/sds/mock_redis/issues/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 --- lib/mock_redis.rb | 4 ---- lib/mock_redis/database.rb | 4 ++++ spec/commands/pipelined_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/mock_redis.rb b/lib/mock_redis.rb index 0d9c10d..d747d9b 100644 --- a/lib/mock_redis.rb +++ b/lib/mock_redis.rb @@ -49,10 +49,6 @@ def id end alias location id - def call(command, &_block) - send(*command) - end - def host options[:host] end diff --git a/lib/mock_redis/database.rb b/lib/mock_redis/database.rb index 4691313..fd65bdc 100644 --- a/lib/mock_redis/database.rb +++ b/lib/mock_redis/database.rb @@ -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 diff --git a/spec/commands/pipelined_spec.rb b/spec/commands/pipelined_spec.rb index 1e0e2cb..224d136 100644 --- a/spec/commands/pipelined_spec.rb +++ b/spec/commands/pipelined_spec.rb @@ -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