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

- old
+ new

@@ -1,98 +1,79 @@ require "spec_helper" describe Mongoid::Contexts::Mongo do + describe "#geo_near" do - before do - Bar.delete_all - Bar.create_indexes - end - - let!(:munich) do - Bar.create(:location => [45, 11], :name => 'Munich') - end + before do + Bar.delete_all + Bar.create_indexes + end - let!(:berlin) do - Bar.create(:location => [46, 12], :name => 'Berlin') - end - describe "geo_near" do + let!(:jfk) do + Bar.create(:name => 'jfk', :location => [-73.77694444, 40.63861111 ]) + end + + let!(:lax) do + Bar.create(:name => 'lax', :location => [-118.40, 33.94]) + end + it "should work with specifying specific center and different location attribute on collction" do - location = [-47,23.5] - near = Bar.geo_near(location) - near.should == [munich,berlin] - near.first.geo[:distance].should > 0 + 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 - location = [-47,23.5] - Bar.geo_near(location, :num => 1).size.should == 1 + Bar.geo_near(jfk.location, :num => 1).size.should == 1 end end - + describe 'option :maxDistance' do - it "should limit on maximum distance" do - location = [45.1, 11.1] - # db.runCommand({ geo_near : "points", near :[45.1, 11.1]}).results; - # dis: is 0.14141869255648362 and 1.2727947855285668 - Bar.geo_near(location, :max_distance => 0.2).should == [munich] + 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 - - describe 'option :distanceMultiplier' do + + describe 'option :distance_multiplier' do it "should multiply returned distance with multiplier" do - location = [45.1, 11.1] - Bar.geo_near(location, :distance_multiplier => 4).first.geo[:distance].should > 0 + 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 - + describe 'option :unit' do it "should multiply returned distance with multiplier" do - location = [45.1, 11.1] - distance = Bar.geo_near(location, :unit => :mi).first.geo[:distance] - distance.should > 559 - distance.should < 560 + Bar.geo_near(lax.location, :spherical => true, :unit => :mi).second.geo[:distance].to_i.should be_within(1).of(2469) end it "should convert max_distance to radians with unit" do - location = [45.1, 11.1] - near = Bar.geo_near(location, :max_distance => 570, :unit => :mi) - near.size.should == 1 - near.first.should == munich + Bar.geo_near(lax.location, :spherical => true, :max_distance => 2465, :unit => :mi).size.should == 1 end - it "should convert max_distance to radians with unit" do - location = [45.1, 11.1] - Bar.geo_near(location, :max_distance => 570, :distance_multiplier => 4, :unit => :mi).first.should == munich - end - end describe 'option :query' do it "should filter using extra query option" do - location = [45.1, 11.1] # two record in the collection, only one's name is Munich - Bar.geo_near(location, :query => {:name => 'Munich'}).size.should == 1 + Bar.geo_near(jfk.location, :query => {:name => jfk.name}).should == [jfk] end end describe 'criteria chaining' do it "should filter by where" do - location = [45.1, 11.1] - # two record in the collection, only one's name is Munich - a = Bar.where(:name => 'Munich') - # p a.selector - # a.geo_near(location).size.should == 1 + 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 it 'should skip 1' do - location = [-47,23.5] - Bar.skip(1).geo_near(location).size.should == 1 + Bar.skip(1).geo_near(jfk.location).size.should == 1 end it 'should limit 1' do - location = [-47,23.5] - Bar.limit(1).geo_near(location).size.should == 1 + Bar.limit(1).geo_near(jfk.location).size.should == 1 end end end