From 622cc4603f4b8f1e78beafb680f45dd512965655 Mon Sep 17 00:00:00 2001
From: Matthew Landauer <matthew@openaustralia.org>
Date: Mon, 22 Oct 2012 11:48:06 +1100
Subject: [PATCH] Added regression test for near_scope_options

---
 lib/geocoder/stores/active_record.rb |  4 ++--
 test/near_test.rb                    | 17 +++++++++++++++++
 test/test_helper.rb                  | 10 ++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 test/near_test.rb

diff --git a/lib/geocoder/stores/active_record.rb b/lib/geocoder/stores/active_record.rb
index b49a4a97..3bd17d08 100644
--- a/lib/geocoder/stores/active_record.rb
+++ b/lib/geocoder/stores/active_record.rb
@@ -77,8 +77,6 @@ module Geocoder::Store
         end
       end
 
-      private # ----------------------------------------------------------------
-
       ##
       # Get options hash suitable for passing to ActiveRecord.find to get
       # records within a radius (in kilometers) of the given point.
@@ -119,6 +117,8 @@ module Geocoder::Store
         }
       end
 
+      private # ----------------------------------------------------------------
+
       ##
       # SQL for calculating distance based on the current database's
       # capabilities (trig functions?).
diff --git a/test/near_test.rb b/test/near_test.rb
new file mode 100644
index 00000000..5a2dc9fa
--- /dev/null
+++ b/test/near_test.rb
@@ -0,0 +1,17 @@
+require 'test_helper'
+
+class NearTest < Test::Unit::TestCase
+
+  def test_near_scope_options
+    result = Event.near_scope_options(1.0, 2.0, 5)
+    expected = {
+      :select =>
+        "test_table_name.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((1.0 - test_table_name.latitude) * PI() / 180 / 2), 2) + COS(1.0 * PI() / 180) * COS(test_table_name.latitude * PI() / 180) * POWER(SIN((2.0 - test_table_name.longitude) * PI() / 180 / 2), 2))) AS distance, CAST(DEGREES(ATAN2( RADIANS(test_table_name.longitude - 2.0), RADIANS(test_table_name.latitude - 1.0))) + 360 AS decimal) % 360 AS bearing",
+      :conditions =>
+        ["3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((1.0 - test_table_name.latitude) * PI() / 180 / 2), 2) + COS(1.0 * PI() / 180) * COS(test_table_name.latitude * PI() / 180) * POWER(SIN((2.0 - test_table_name.longitude) * PI() / 180 / 2), 2))) <= ?", 5],
+      :order => "distance ASC"
+    }
+
+    assert_equal expected, result
+  end
+end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index e390f0db..1f592ecf 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -4,6 +4,12 @@ require 'test/unit'
 $LOAD_PATH.unshift(File.dirname(__FILE__))
 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
 
+class MysqlConnection
+  def adapter_name
+    "mysql"
+  end
+end
+
 ##
 # Simulate enough of ActiveRecord::Base that objects can be used for testing.
 #
@@ -28,6 +34,10 @@ module ActiveRecord
 
     def self.scope(*args); end
 
+    def self.connection
+      MysqlConnection.new
+    end
+
     def method_missing(name, *args, &block)
       if name.to_s[-1..-1] == "="
         write_attribute name.to_s[0...-1], *args
-- 
GitLab