-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix range_hash sym to string conversion. Add tests. #128
Open
stefanneculai
wants to merge
3
commits into
Veraticus:master
Choose a base branch
from
stefanneculai:range_hash
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 => '[email protected]'} | ||
@chain.send(:index_query).should == {:hash_value => '[email protected]'} | ||
|
||
@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])}]) | ||
|
@@ -138,6 +139,45 @@ | |
@chain.send(:range?).should be_false | ||
end | ||
|
||
context 'range hash' do | ||
before do | ||
@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'.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'.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'.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'.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'.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 => 'test' } | ||
@chain.send(:range_hash, 'time.begins_with').to_a.should == {:range_begins_with => 'test'}.to_a | ||
end | ||
|
||
end | ||
|
||
context 'range queries' do | ||
before do | ||
@tweet1 = Tweet.create(:tweet_id => "x", :group => "one") | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While passing 'name' as symbol instead of a string seems natural, "created_at.gt" seems like an extra burden on the caller. I would expect most people would just call it with "created_at.gt" in string form, which is probably what the test should express.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only that for the tests it has to be called this way because it writes it directly on the query object. When you use Model.where("created_at.gt" => 'whatever') it will make "created_at.gt" symbol in the where method. No one said that it has to be sym when people use them.