From c85a14be50f6130d2a395e970b58b888dec959ca Mon Sep 17 00:00:00 2001 From: Stefan Date: Sun, 16 Jun 2013 23:21:34 +0300 Subject: [PATCH 1/3] Fix range_hash sym to string conversion. Add tests. --- lib/dynamoid/criteria/chain.rb | 5 +++- spec/dynamoid/criteria/chain_spec.rb | 37 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/dynamoid/criteria/chain.rb b/lib/dynamoid/criteria/chain.rb index 50e8fc0..d94886d 100644 --- a/lib/dynamoid/criteria/chain.rb +++ b/lib/dynamoid/criteria/chain.rb @@ -247,12 +247,15 @@ def index_query end end + # Comparison Operator for evaluating Key Conditions. + # + # @return [Hash] a hash with the comparison operator for the query. def range_hash(key) val = query[key] return { :range_value => query[key] } if query[key].is_a?(Range) - case key.split('.').last + case key.to_s.split('.').last when 'gt' { :range_greater_than => val.to_f } when 'lt' diff --git a/spec/dynamoid/criteria/chain_spec.rb b/spec/dynamoid/criteria/chain_spec.rb index 199793c..d56937f 100644 --- a/spec/dynamoid/criteria/chain_spec.rb +++ b/spec/dynamoid/criteria/chain_spec.rb @@ -138,6 +138,43 @@ @chain.send(:range?).should be_false end + context 'range hash' do + before do + @chain = Dynamoid::Criteria::Chain.new(Message) + end + + it 'uses gt comparator' do + @chain.query = { 'time.gt' => 2 } + @chain.send(:range_hash, 'time.gt').to_a.should == {:range_greater_than => 2.to_f}.to_a + end + + it 'uses lt comparator' do + @chain.query = { 'time.lt' => 2 } + @chain.send(:range_hash, 'time.lt').to_a.should == {:range_less_than => 2.to_f}.to_a + end + + it 'uses gte comparator' do + @chain.query = { 'time.gte' => 2 } + @chain.send(:range_hash, 'time.gte').to_a.should == {:range_gte => 2.to_f}.to_a + end + + it 'uses lte comparator' do + @chain.query = { 'time.lte' => 2 } + @chain.send(:range_hash, 'time.lte').to_a.should == {:range_lte => 2.to_f}.to_a + end + + it 'uses begins_with comparator' do + @chain.query = { 'time.begins_with' => 2 } + @chain.send(:range_hash, 'time.begins_with').to_a.should == {:range_begins_with => 2.to_f}.to_a + end + + it 'tests right convertion from sym to string' do + @chain.query = { 'time.begins_with'.to_sym => 2 } + @chain.send(:range_hash, 'time.begins_with').to_a.should == {:range_begins_with => 2.to_f}.to_a + end + + end + context 'range queries' do before do @tweet1 = Tweet.create(:tweet_id => "x", :group => "one") From 74693dabd8715f6148a504c28d8986b6e419793c Mon Sep 17 00:00:00 2001 From: Stefan Date: Sun, 16 Jun 2013 23:49:26 +0300 Subject: [PATCH 2/3] Improve range_hash. Fix tests failing. --- lib/dynamoid/criteria/chain.rb | 4 ++-- spec/dynamoid/criteria/chain_spec.rb | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/dynamoid/criteria/chain.rb b/lib/dynamoid/criteria/chain.rb index d94886d..47952e0 100644 --- a/lib/dynamoid/criteria/chain.rb +++ b/lib/dynamoid/criteria/chain.rb @@ -251,9 +251,9 @@ def index_query # # @return [Hash] a hash with the comparison operator for the query. def range_hash(key) - val = query[key] + val = query[key.to_sym] - return { :range_value => query[key] } if query[key].is_a?(Range) + return { :range_value => val } if val.is_a?(Range) case key.to_s.split('.').last when 'gt' diff --git a/spec/dynamoid/criteria/chain_spec.rb b/spec/dynamoid/criteria/chain_spec.rb index d56937f..04bf565 100644 --- a/spec/dynamoid/criteria/chain_spec.rb +++ b/spec/dynamoid/criteria/chain_spec.rb @@ -143,34 +143,36 @@ @chain = Dynamoid::Criteria::Chain.new(Message) end + # All keys from query are converted automatically to_sym in where method. + it 'uses gt comparator' do - @chain.query = { 'time.gt' => 2 } + @chain.query = { 'time.gt'.to_sym => 2 } @chain.send(:range_hash, 'time.gt').to_a.should == {:range_greater_than => 2.to_f}.to_a end it 'uses lt comparator' do - @chain.query = { 'time.lt' => 2 } + @chain.query = { 'time.lt'.to_sym => 2 } @chain.send(:range_hash, 'time.lt').to_a.should == {:range_less_than => 2.to_f}.to_a end it 'uses gte comparator' do - @chain.query = { 'time.gte' => 2 } + @chain.query = { 'time.gte'.to_sym => 2 } @chain.send(:range_hash, 'time.gte').to_a.should == {:range_gte => 2.to_f}.to_a end it 'uses lte comparator' do - @chain.query = { 'time.lte' => 2 } + @chain.query = { 'time.lte'.to_sym => 2 } @chain.send(:range_hash, 'time.lte').to_a.should == {:range_lte => 2.to_f}.to_a end it 'uses begins_with comparator' do - @chain.query = { 'time.begins_with' => 2 } - @chain.send(:range_hash, 'time.begins_with').to_a.should == {:range_begins_with => 2.to_f}.to_a + @chain.query = { 'time.begins_with'.to_sym => 'test' } + @chain.send(:range_hash, 'time.begins_with').to_a.should == {:range_begins_with => 'test'}.to_a end it 'tests right convertion from sym to string' do - @chain.query = { 'time.begins_with'.to_sym => 2 } - @chain.send(:range_hash, 'time.begins_with').to_a.should == {:range_begins_with => 2.to_f}.to_a + @chain.query = { 'time.begins_with'.to_sym => 'test' } + @chain.send(:range_hash, 'time.begins_with').to_a.should == {:range_begins_with => 'test'}.to_a end end From 5b618cbbf2d30f7a299cf7d938e90639c73854bc Mon Sep 17 00:00:00 2001 From: Stefan Date: Mon, 17 Jun 2013 00:08:15 +0300 Subject: [PATCH 3/3] Update tests to match symbols. --- spec/dynamoid/criteria/chain_spec.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/spec/dynamoid/criteria/chain_spec.rb b/spec/dynamoid/criteria/chain_spec.rb index 04bf565..96e25d9 100644 --- a/spec/dynamoid/criteria/chain_spec.rb +++ b/spec/dynamoid/criteria/chain_spec.rb @@ -20,15 +20,15 @@ end it 'makes string symbol for query keys' do - @chain.query = {'name' => 'Josh'} + @chain.query = {:name => 'Josh'} @chain.send(:index).should == User.indexes[[:name]] end it 'finds matching index for a range query' do - @chain.query = {"created_at.gt" => @time - 1.day} + @chain.query = {"created_at.gt".to_sym => @time - 1.day} @chain.send(:index).should == User.indexes[[:created_at]] - @chain.query = {:name => 'Josh', "created_at.lt" => @time - 1.day} + @chain.query = {:name => 'Josh', "created_at.lt".to_sym => @time - 1.day} @chain.send(:index).should == User.indexes[[:created_at, :name]] end @@ -50,7 +50,8 @@ @chain.query = {:name => 'Josh', :email => 'josh@joshsymonds.com'} @chain.send(:index_query).should == {:hash_value => 'josh@joshsymonds.com.Josh'} - @chain.query = {:name => 'Josh', 'created_at.gt' => @time} + # All keys are converted to_sym in the where method, so we should pass them in query as Symbols. + @chain.query = {:name => 'Josh', 'created_at.gt'.to_sym => @time} @chain.send(:index_query).should == {:hash_value => 'Josh', :range_greater_than => @time.to_f} end @@ -66,10 +67,10 @@ end it 'returns records with an index for a ranged query' do - @chain.query = {:name => 'Josh', "created_at.gt" => @time - 1.day} + @chain.query = {:name => 'Josh', "created_at.gt".to_sym => @time - 1.day} @chain.send(:records_with_index).should == @user - @chain.query = {:name => 'Josh', "created_at.lt" => @time + 1.day} + @chain.query = {:name => 'Josh', "created_at.lt".to_sym => @time + 1.day} @chain.send(:records_with_index).should == @user end @@ -79,7 +80,7 @@ end it "doesn't crash if it finds a nil id in the index" do - @chain.query = {:name => 'Josh', "created_at.gt" => @time - 1.day} + @chain.query = {:name => 'Josh', "created_at.gt".to_sym => @time - 1.day} Dynamoid::Adapter.expects(:query). with("dynamoid_tests_index_user_created_ats_and_names", kind_of(Hash)). returns([{ids: nil}, {ids: Set.new([42])}])