diff --git a/test/fixtures/madison_square_garden.xml b/test/fixtures/madison_square_garden.xml new file mode 100644 index 0000000000000000000000000000000000000000..bc81bd76264ec3bdbc9cb12e3d4bd722812f961d --- /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 2dee9ea20653a4337ed7176164819432ef8fa3f0..f340ea9b24756accf018156f9f829ea29f721789 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 0d94d705b9702c8fedc537bb91c877cbf645d381..f8572b2c2c3453a2ee485a89d27b06b220b38c0f 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