spec/functional/mongoid/contexts/mongo_spec.rb in mongoid_spacial-0.1.0 vs spec/functional/mongoid/contexts/mongo_spec.rb in mongoid_spacial-0.1.1

- old
+ new

@@ -18,52 +18,54 @@ it "should work with specifying specific center and different location attribute on collction" do Bar.geo_near(lax.location, :spherical => true).should == [lax, jfk] Bar.geo_near(jfk.location, :spherical => true).should == [jfk, lax] end - - describe 'option :num' do - it "should limit number of results to 1" do - Bar.geo_near(jfk.location, :num => 1).size.should == 1 + context 'option' do + context ':num' do + it "should limit number of results to 1" do + Bar.geo_near(jfk.location, :num => 1).size.should == 1 + end end - end - describe 'option :maxDistance' do - it "should get 1 item" do - Bar.geo_near(lax.location, :spherical => true, :max_distance => 2465/Mongoid::Spacial.earth_radius[:mi]).size.should == 1 + context ':maxDistance' do + it "should get 1 item" do + Bar.geo_near(lax.location, :spherical => true, :max_distance => 2465/Mongoid::Spacial.earth_radius[:mi]).size.should == 1 + end + it "should get 2 items" do + Bar.geo_near(lax.location, :spherical => true, :max_distance => 2480/Mongoid::Spacial.earth_radius[:mi]).size.should == 2 + end + end - it "should get 2 items" do - Bar.geo_near(lax.location, :spherical => true, :max_distance => 2480/Mongoid::Spacial.earth_radius[:mi]).size.should == 2 - end - end - - describe 'option :distance_multiplier' do - it "should multiply returned distance with multiplier" do + context ':distance_multiplier' do + it "should multiply returned distance with multiplier" do Bar.geo_near(lax.location, :spherical => true, :distance_multiplier=> Mongoid::Spacial.earth_radius[:mi]).second.geo[:distance].to_i.should be_within(1).of(2469) + end end - end - describe 'option :unit' do - it "should multiply returned distance with multiplier" do + context ':unit' do + it "should multiply returned distance with multiplier" do Bar.geo_near(lax.location, :spherical => true, :unit => :mi).second.geo[:distance].to_i.should be_within(1).of(2469) - end + end - it "should convert max_distance to radians with unit" do + it "should convert max_distance to radians with unit" do Bar.geo_near(lax.location, :spherical => true, :max_distance => 2465, :unit => :mi).size.should == 1 + end + end - end - - describe 'option :query' do - it "should filter using extra query option" do - # two record in the collection, only one's name is Munich - Bar.geo_near(jfk.location, :query => {:name => jfk.name}).should == [jfk] + context ':query' do + it "should filter using extra query option" do + # two record in the collection, only one's name is Munich + Bar.geo_near(jfk.location, :query => {:name => jfk.name}).should == [jfk] + end end + end - describe 'criteria chaining' do + context 'criteria chaining' do it "should filter by where" do Bar.where(:name => jfk.name).geo_near(jfk.location).should == [jfk] Bar.any_of({:name => jfk.name},{:name => lax.name}).geo_near(jfk.location).should == [jfk,lax] end @@ -76,7 +78,34 @@ end end end + + context ':page' do + before(:all) do + Bar.delete_all + Bar.create_indexes + + 100.times do + Bar.create({:location => [rand(360)-180,rand(360)-180]}) + end + end + context ":paginator :array" do + [nil,1,2,3,4].each do |page| + it "page=#{page} should have 25" do + Bar.geo_near([1,1], :page => page).size.should == 25 + end + end + + it "page=20 should have 0" do + Bar.geo_near([1,1], :page => 20).size.should == 0 + end + + it "per_page=5" do + Bar.geo_near([1,1], :page => 1, :per_page => 5).size.should == 5 + end + end + end + end