From b16060ed144c75efc28a738860d975155bb2d5f2 Mon Sep 17 00:00:00 2001 From: Adnan Ali <adnan.ali@gmail.com> Date: Sat, 28 May 2011 19:26:10 -0400 Subject: [PATCH] adding mongoid test for customer coordinate field in near scope. --- test/mongoid_test.rb | 39 +++++++++++++++++++++++++++++++++++++ test/mongoid_test_helper.rb | 28 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 test/mongoid_test.rb create mode 100644 test/mongoid_test_helper.rb diff --git a/test/mongoid_test.rb b/test/mongoid_test.rb new file mode 100644 index 00000000..4b2bed0c --- /dev/null +++ b/test/mongoid_test.rb @@ -0,0 +1,39 @@ +# encoding: utf-8 +require 'test_helper' + +begin +require 'mongoid' +require 'mongoid_test_helper' + +class MongoidTest < Test::Unit::TestCase + + def setup + Geocoder::Configuration.set_defaults + end + + def test_geocoded_check + p = Place.new(*venue_params(:msg)) + p.location = [40.750354, -73.993371] + + assert p.geocoded? + end + + def test_distance_to_returns_float + p = Place.new(*venue_params(:msg)) + p.location = [40.750354, -73.993371] + + assert p.distance_to([30, -94]).is_a?(Float) + end + + def test_custom_coordinate_field_near_scope + location = [40.750354, -73.993371] + p = Place.near(location) + + assert p.selector[:location] + assert_equal p.selector[:location]['$nearSphere'], location.reverse + end +end + +rescue LoadError => crash + warn 'Mongoid not installed, not tested.' +end diff --git a/test/mongoid_test_helper.rb b/test/mongoid_test_helper.rb new file mode 100644 index 00000000..159f8a22 --- /dev/null +++ b/test/mongoid_test_helper.rb @@ -0,0 +1,28 @@ +require 'rubygems' +require 'test/unit' + +$LOAD_PATH.unshift(File.dirname(__FILE__)) +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) + +Mongoid.configure do |config| + config.logger = Logger.new($stderr, :debug) +end + +## +# Geocoded model. +# +class Place + include Mongoid::Document + include Geocoder::Model::Mongoid + + geocoded_by :address, :coordinates => :location + field :name + field :address + field :location, :type => Array + + def initialize(name, address) + super() + write_attribute :name, name + write_attribute :address, address + end +end -- GitLab