Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor specs of date/datetime filters

  • Loading branch information...
commit d3ca17aefa27e07aa998d43eabe071616b1ba23d 1 parent 957599e
@mshibuya mshibuya authored
View
5 spec/integration/basic/update/rails_admin_basic_update_spec.rb
@@ -64,13 +64,14 @@
before(:each) do
@player = FactoryGirl.create :player
@draft = FactoryGirl.create :draft
- page.driver.put edit_path(:model_name => "player", :id => @player.id, :player => {:name => "Jackie Robinson", :draft_id => @draft.id, :number => 42, :position => "Second baseman"})
+ @number = @draft.player.number + 1 # to avoid collision
+ page.driver.put edit_path(:model_name => "player", :id => @player.id, :player => {:name => "Jackie Robinson", :draft_id => @draft.id, :number => @number, :position => "Second baseman"})
@player.reload
end
it "should update an object with correct attributes" do
@player.name.should eql("Jackie Robinson")
- @player.number.should eql(42)
+ @player.number.should eql(@number)
@player.position.should eql("Second baseman")
end
View
45 spec/unit/abstract_model_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+describe RailsAdmin::AbstractModel do
+ describe 'filters' do
+ before do
+ @abstract_model = RailsAdmin::AbstractModel.new('FieldTest')
+ end
+
+ context 'on dates' do
+ it 'lists elements within outbound limits' do
+ date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
+
+ FactoryGirl.create(:field_test, :date_field => Date.strptime("01/01/2012", date_format))
+ FactoryGirl.create(:field_test, :date_field => Date.strptime("01/02/2012", date_format))
+ FactoryGirl.create(:field_test, :date_field => Date.strptime("01/03/2012", date_format))
+ FactoryGirl.create(:field_test, :date_field => Date.strptime("01/04/2012", date_format))
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
+
+ end
+ end
+
+ context 'on datetimes' do
+ it 'lists elements within outbound limits' do
+ FactoryGirl.create(:field_test, :datetime_field => Time.local(2012, 1, 1, 23, 59, 59))
+ FactoryGirl.create(:field_test, :datetime_field => Time.local(2012, 1, 2, 0, 0, 0))
+ FactoryGirl.create(:field_test, :datetime_field => Time.local(2012, 1, 3, 23, 59, 59))
+ if defined?(Mongoid) && Mongoid::VERSION >= '3.0.0'
+ # TODO: Mongoid 3.0.0 mysteriously expands the range of inclusion slightly...
+ FactoryGirl.create(:field_test, :datetime_field => Time.local(2012, 1, 4, 0, 0, 1))
+ else
+ FactoryGirl.create(:field_test, :datetime_field => Time.local(2012, 1, 4, 0, 0, 0))
+ end
+ @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
+ @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
+ end
+ end
+ end
+end
View
37 spec/unit/adapters/active_record_spec.rb
@@ -329,34 +329,19 @@ class ARComment < ActiveRecord::Base
@abstract_model.send(:build_statement, :field, :string, "foo", "is").should == ["(field #{@like} ?)", "foo"]
end
- context 'filters on dates' do
- it 'lists elements within outbound limits' do
- date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
-
- FieldTest.create!(:date_field => Date.strptime("01/01/2012", date_format))
- FieldTest.create!(:date_field => Date.strptime("01/02/2012", date_format))
- FieldTest.create!(:date_field => Date.strptime("01/03/2012", date_format))
- FieldTest.create!(:date_field => Date.strptime("01/04/2012", date_format))
- @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
- @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
- end
+
+ it 'supports date type query' do
+ @abstract_model.send(:filter_conditions, { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } }).should == ["((field_tests.date_field BETWEEN ? AND ?))", Date.new(2012,1,2), Date.new(2012,1,3)]
+ @abstract_model.send(:filter_conditions, { "date_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).should == ["((field_tests.date_field >= ?))", Date.new(2012,1,3)]
+ @abstract_model.send(:filter_conditions, { "date_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).should == ["((field_tests.date_field <= ?))", Date.new(2012,1,2)]
+ @abstract_model.send(:filter_conditions, { "date_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).should == ["((field_tests.date_field BETWEEN ? AND ?))", Date.new(2012,1,2), Date.new(2012,1,2)]
end
- context 'filters on datetimes' do
- it 'lists elements within outbound limits' do
- FieldTest.create!(:datetime_field => Time.local(2012, 1, 1, 23, 59, 59))
- FieldTest.create!(:datetime_field => Time.local(2012, 1, 2, 0, 0, 0))
- FieldTest.create!(:datetime_field => Time.local(2012, 1, 3, 23, 59, 59))
- FieldTest.create!(:datetime_field => Time.local(2012, 1, 4, 0, 0, 0))
- @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
- @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
- end
+ it 'supports datetime type query' do
+ @abstract_model.send(:filter_conditions, { "datetime_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).should == ["((field_tests.datetime_field BETWEEN ? AND ?))", Time.local(2012,1,2), Time.local(2012,1,3).end_of_day]
+ @abstract_model.send(:filter_conditions, { "datetime_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).should == ["((field_tests.datetime_field >= ?))", Time.local(2012,1,3)]
+ @abstract_model.send(:filter_conditions, { "datetime_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).should == ["((field_tests.datetime_field <= ?))", Time.local(2012,1,2).end_of_day]
+ @abstract_model.send(:filter_conditions, { "datetime_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).should == ["((field_tests.datetime_field BETWEEN ? AND ?))", Time.local(2012,1,2), Time.local(2012,1,2).end_of_day]
end
it "supports enum type query" do
View
41 spec/unit/adapters/mongoid_spec.rb
@@ -628,35 +628,18 @@ class MongoEmbedded
@abstract_model.send(:build_statement, :field, :string, "foo", "is").should == {:field=>'foo'}
end
- context 'filters on dates' do
- it 'lists elements within outbound limits' do
- date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
-
- FieldTest.create!(:date_field => Date.strptime("01/01/2012", date_format))
- FieldTest.create!(:date_field => Date.strptime("01/02/2012", date_format))
- FieldTest.create!(:date_field => Date.strptime("01/03/2012", date_format))
- FieldTest.create!(:date_field => Date.strptime("01/04/2012", date_format))
- @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
- @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
-
- end
- end
-
- context 'filters on datetimes' do
- it 'lists elements within outbound limits' do
- FieldTest.create!(:datetime_field => Time.local(2012, 1, 1, 23, 59, 59))
- FieldTest.create!(:datetime_field => Time.local(2012, 1, 2, 0, 0, 0))
- FieldTest.create!(:datetime_field => Time.local(2012, 1, 3, 23, 59, 59))
- FieldTest.create!(:datetime_field => Time.local(2012, 1, 4, 0, 0, 0))
- @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
- @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
- @abstract_model.all(:filters => { "datetime_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
- end
+ it 'supports date type query' do
+ @abstract_model.send(:filter_conditions, { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } }).should == {"$and"=>[{"date_field"=>{"$gte"=>Date.new(2012,1,2), "$lte"=>Date.new(2012,1,3)}}]}
+ @abstract_model.send(:filter_conditions, { "date_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).should == {"$and"=>[{"date_field"=>{"$gte"=>Date.new(2012,1,3)}}]}
+ @abstract_model.send(:filter_conditions, { "date_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).should == {"$and"=>[{"date_field"=>{"$lte"=>Date.new(2012,1,2)}}]}
+ @abstract_model.send(:filter_conditions, { "date_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).should == {"$and"=>[{"date_field"=>{"$gte"=>Date.new(2012,1,2), "$lte"=>Date.new(2012,1,2)}}]}
+ end
+
+ it 'supports datetime type query' do
+ @abstract_model.send(:filter_conditions, { "datetime_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).should == {"$and"=>[{"datetime_field"=>{"$gte"=>Time.local(2012,1,2), "$lte"=>Time.local(2012,1,3).end_of_day}}]}
+ @abstract_model.send(:filter_conditions, { "datetime_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).should == {"$and"=>[{"datetime_field"=>{"$gte"=>Time.local(2012,1,3)}}]}
+ @abstract_model.send(:filter_conditions, { "datetime_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).should == {"$and"=>[{"datetime_field"=>{"$lte"=>Time.local(2012,1,2).end_of_day}}]}
+ @abstract_model.send(:filter_conditions, { "datetime_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).should == {"$and"=>[{"datetime_field"=>{"$gte"=>Time.local(2012,1,2), "$lte"=>Time.local(2012,1,2).end_of_day}}]}
end
it "supports enum type query" do
Please sign in to comment.
Something went wrong with that request. Please try again.