From dd985747f93bc25da40523c7d5e458e135df0c80 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Tue, 6 Oct 2009 00:42:10 -0400
Subject: [PATCH] Add fixture for Google XML response, ActiveRecord::Base
 simulator, and two basic tests.

---
 test/fixtures/madison_square_garden.xml | 16 +++++++
 test/geocoder_test.rb                   | 13 ++++--
 test/test_helper.rb                     | 59 +++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 3 deletions(-)
 create mode 100644 test/fixtures/madison_square_garden.xml

diff --git a/test/fixtures/madison_square_garden.xml b/test/fixtures/madison_square_garden.xml
new file mode 100644
index 00000000..bc81bd76
--- /dev/null
+++ b/test/fixtures/madison_square_garden.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<kml xmlns="http://earth.google.com/kml/2.0"><Response>
+  <name>4 Penn Plaza, New York, NY</name>
+  <Status>
+    <code>200</code>
+    <request>geocode</request>
+  </Status>
+  <Placemark id="p1">
+    <address>4 Penn Plaza, New York, NY 10001, USA</address>
+    <AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><CountryName>USA</CountryName><AdministrativeArea><AdministrativeAreaName>NY</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>New York</SubAdministrativeAreaName><Locality><LocalityName>New York</LocalityName><Thoroughfare><ThoroughfareName>4 Penn Plaza</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>10001</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails>
+    <ExtendedData>
+      <LatLonBox north="40.7527158" south="40.7464206" east="-73.9885076" west="-73.9948028" />
+    </ExtendedData>
+    <Point><coordinates>-73.9916733,40.7495760,0</coordinates></Point>
+  </Placemark>
+</Response></kml>
diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb
index 2dee9ea2..f340ea9b 100644
--- a/test/geocoder_test.rb
+++ b/test/geocoder_test.rb
@@ -1,8 +1,15 @@
 require 'test_helper'
 
 class GeocoderTest < Test::Unit::TestCase
-  # Replace this with your real tests.
-  def test_this_plugin
-    flunk
+
+  def test_fetch_coordinates
+    v = Venue.new(*venue_params(:msg))
+    assert_equal [40.7495760, -73.9916733], v.fetch_coordinates
+  end
+
+  def test_fetch_coordinates!
+    v = Venue.new(*venue_params(:msg))
+    v.fetch_coordinates!
+    assert_equal [40.7495760, -73.9916733], [v.latitude, v.longitude]
   end
 end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 0d94d705..f8572b2c 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -1,9 +1,68 @@
 require 'rubygems'
 require 'test/unit'
+require 'activesupport'
 
 $LOAD_PATH.unshift(File.dirname(__FILE__))
 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+##
+# Simulate enough of ActiveRecord::Base that objects can be used for testing.
+#
+module ActiveRecord
+  class Base
+    
+    def initialize
+      @attributes = {}
+    end
+    
+    def read_attribute(attr_name)
+      @attributes[attr_name.to_s]
+    end
+    
+    def write_attribute(attr_name, value)
+      attr_name = attr_name.to_s
+      @attributes[attr_name] = value
+    end
+    
+    def self.named_scope(*args); end
+  end
+end
+
+# Require Geocoder after ActiveRecord simulator.
 require 'geocoder'
 
+##
+# Mock HTTP request to Google.
+#
+module Geocoder
+  def self._fetch_xml(query)
+    filename = File.join("test", "fixtures", "madison_square_garden.xml")
+    File.read(filename)
+  end
+end
+
+##
+# Geocoded model.
+#
+class Venue < ActiveRecord::Base
+  geocoded_by :address
+  
+  def initialize(name, address)
+    super()
+    write_attribute :name, name
+    write_attribute :address, address
+  end
+  
+  # could implement these with method_missing
+  # to simulate custom lat/lon methods
+  def latitude; read_attribute(:latitude); end
+  def longitude; read_attribute(:longitude); end
+end
+
 class Test::Unit::TestCase
+  def venue_params(abbrev)
+    {
+      :msg => ["Madison Square Garden", "4 Penn Plaza, New York, NY"]
+    }[abbrev]
+  end
 end
-- 
GitLab