From fe1e274fbf1f0ae4efd2f7330c7db2d381691a1e Mon Sep 17 00:00:00 2001 From: Hieu Nguyen Date: Sun, 26 Apr 2020 03:31:36 +0800 Subject: [PATCH 1/2] Handle block within multi block --- lib/mock_redis/future.rb | 2 +- lib/mock_redis/transaction_wrapper.rb | 4 ++-- spec/commands/future_spec.rb | 12 +++++++++++- spec/transactions_spec.rb | 16 ++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/mock_redis/future.rb b/lib/mock_redis/future.rb index 29c35dae..e2ee8ba3 100644 --- a/lib/mock_redis/future.rb +++ b/lib/mock_redis/future.rb @@ -17,7 +17,7 @@ def value def store_result(result) @result_set = true - @result = result + @result = @block ? @block.call(result) : result end end end diff --git a/lib/mock_redis/transaction_wrapper.rb b/lib/mock_redis/transaction_wrapper.rb index 228ae034..3cbfbc82 100644 --- a/lib/mock_redis/transaction_wrapper.rb +++ b/lib/mock_redis/transaction_wrapper.rb @@ -17,7 +17,7 @@ def initialize(db) def method_missing(method, *args, &block) if in_multi? - future = MockRedis::Future.new([method, *args]) + future = MockRedis::Future.new([method, *args], block) @transaction_futures << future if @multi_block_given @@ -60,7 +60,7 @@ def exec begin result = send(*future.command) future.store_result(result) - result + future.value rescue StandardError => e e end diff --git a/spec/commands/future_spec.rb b/spec/commands/future_spec.rb index e89c50f0..0f1d81ca 100644 --- a/spec/commands/future_spec.rb +++ b/spec/commands/future_spec.rb @@ -3,7 +3,12 @@ describe MockRedis::Future do let(:command) { [:get, 'foo'] } let(:result) { 'bar' } - before { @future = MockRedis::Future.new(command) } + let(:block) { ->(value) { value.upcase } } + + before do + @future = MockRedis::Future.new(command) + @future_2 = MockRedis::Future.new(command, block) + end it 'remembers the command' do @future.command.should eq(command) @@ -17,4 +22,9 @@ @future.store_result(result) @future.value.should eq(result) end + + it 'executes the block on the value if block is passed in' do + @future_2.store_result(result) + @future_2.value.should eq('BAR') + end end diff --git a/spec/transactions_spec.rb b/spec/transactions_spec.rb index 4ce70a5c..f1ff05fe 100644 --- a/spec/transactions_spec.rb +++ b/spec/transactions_spec.rb @@ -67,6 +67,22 @@ @redises.get('counter').should eq '6' @redises.get('test').should eq '1' end + + it 'allows blocks within multi blocks' do + @redises.set('foo', 'bar') + @redises.set('fuu', 'baz') + + result = nil + + @redises.multi do |r| + result = r.mget('foo', 'fuu') { |reply| reply.map(&:upcase) } + r.del('foo', 'fuu') + end + + result.value.should eq ['BAR', 'BAZ'] + @redises.get('foo').should eq nil + @redises.get('fuu').should eq nil + end end context '#discard' do From 618a6d4fda0682f6dcb9c7eb4290980799c8da33 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Sun, 26 Apr 2020 21:03:43 -0700 Subject: [PATCH 2/2] Fix RuboCop warnings --- spec/commands/future_spec.rb | 6 +++--- spec/transactions_spec.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/commands/future_spec.rb b/spec/commands/future_spec.rb index 0f1d81ca..3669b058 100644 --- a/spec/commands/future_spec.rb +++ b/spec/commands/future_spec.rb @@ -7,7 +7,7 @@ before do @future = MockRedis::Future.new(command) - @future_2 = MockRedis::Future.new(command, block) + @future2 = MockRedis::Future.new(command, block) end it 'remembers the command' do @@ -24,7 +24,7 @@ end it 'executes the block on the value if block is passed in' do - @future_2.store_result(result) - @future_2.value.should eq('BAR') + @future2.store_result(result) + @future2.value.should eq('BAR') end end diff --git a/spec/transactions_spec.rb b/spec/transactions_spec.rb index f1ff05fe..b4a2ea3b 100644 --- a/spec/transactions_spec.rb +++ b/spec/transactions_spec.rb @@ -79,7 +79,7 @@ r.del('foo', 'fuu') end - result.value.should eq ['BAR', 'BAZ'] + result.value.should eq %w[BAR BAZ] @redises.get('foo').should eq nil @redises.get('fuu').should eq nil end