From 5c36e1e18a92cf158157111f4f7f9f9a35321a49 Mon Sep 17 00:00:00 2001
From: Thu Trang Pham <thuutrangpham@gmail.com>
Date: Wed, 11 Oct 2017 18:16:47 -0700
Subject: [PATCH] Adding more mongoid model tests (#1232)

---
 test/mongoid_test_helper.rb | 59 +++++++++++++++++++++++++++++++++++++
 test/unit/mongoid_test.rb   | 19 ++++++++++++
 2 files changed, 78 insertions(+)

diff --git a/test/mongoid_test_helper.rb b/test/mongoid_test_helper.rb
index 903ff554..ef072fb2 100644
--- a/test/mongoid_test_helper.rb
+++ b/test/mongoid_test_helper.rb
@@ -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
diff --git a/test/unit/mongoid_test.rb b/test/unit/mongoid_test.rb
index f98afa19..cea0fa73 100644
--- a/test/unit/mongoid_test.rb
+++ b/test/unit/mongoid_test.rb
@@ -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
-- 
GitLab