Skip to content
Snippets Groups Projects
Commit 5c36e1e1 authored by Thu Trang Pham's avatar Thu Trang Pham Committed by GitHub
Browse files

Adding more mongoid model tests (#1232)

parent 59fd214d
No related branches found
No related tags found
No related merge requests found
......@@ -41,3 +41,62 @@ class PlaceUsingMongoidWithoutIndex
field :location, :type => Array
geocoded_by :location, :skip_index => true
end
class PlaceUsingMongoidReverseGeocoded
include Mongoid::Document
include Geocoder::Model::Mongoid
field :address
field :coordinates, :type => Array
reverse_geocoded_by :coordinates
def initialize(name, latitude, longitude)
super()
write_attribute :name, name
write_attribute :coordinates, [latitude, longitude]
end
end
class PlaceUsingMongoidWithCustomResultsHandling
include Mongoid::Document
include Geocoder::Model::Mongoid
field :location, :type => Array
field :coords_string
field :name
field :address
geocoded_by :address, :coordinates => :location do |obj,results|
if result = results.first
obj.coords_string = "#{result.latitude},#{result.longitude}"
else
obj.coords_string = "NOT FOUND"
end
end
def initialize(name, address)
super()
write_attribute :name, name
write_attribute :address, address
end
end
class PlaceUsingMongoidReverseGeocodedWithCustomResultsHandling
include Mongoid::Document
include Geocoder::Model::Mongoid
field :name
field :country
field :coordinates, :type => Array
reverse_geocoded_by :coordinates do |obj,results|
if result = results.first
obj.country = result.country_code
end
end
def initialize(name, latitude, longitude)
super()
write_attribute :name, name
write_attribute :coordinates, [latitude, longitude]
end
end
......@@ -39,4 +39,23 @@ class MongoidTest < GeocoderTestCase
end
assert !result
end
def test_geocoded_with_custom_handling
p = PlaceUsingMongoidWithCustomResultsHandling.new(*geocoded_object_params(:msg))
p.location = [40.750354, -73.993371]
p.geocode
assert p.coords_string == "40.750354,-73.993371"
end
def test_reverse_geocoded
p = PlaceUsingMongoidReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
p.reverse_geocode
assert p.address == "4 Penn Plaza, New York, NY 10001, USA"
end
def test_reverse_geocoded_with_custom_handling
p = PlaceUsingMongoidReverseGeocodedWithCustomResultsHandling.new(*reverse_geocoded_object_params(:msg))
p.reverse_geocode
assert p.country == "US"
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment