require File.join(File.dirname(__FILE__), 'test_base_geocoder') Geokit::Geocoders::google = 'Google' class GoogleGeocoderTest < BaseGeocoderTest #:nodoc: all GOOGLE_FULL=<<-EOF.strip 100 spear st, san francisco, ca200geocode
100 Spear St, San Francisco, CA 94105, USA
USCASan FranciscoSan Francisco100 Spear St94105-122.393985,37.792501,0
EOF GOOGLE_CITY=<<-EOF.strip San Francisco200geocode
San Francisco, CA, USA
USCASan Francisco-122.418333,37.775000,0
EOF GOOGLE_MULTI="\n\n via Sandro Pertini 8, Ossona, MI\n \n 200\n geocode\n \n \n
Via Sandro Pertini, 8, 20010 Mesero MI, Italy
\n ITItalyLombardyMilanMesero8 Via Sandro Pertini20010\n 8.8527131,45.4966243,0\n
\n \n
Via Sandro Pertini, 20010 Ossona MI, Italy
\n ITItalyLombardyMilanOssonaVia Sandro Pertini20010\n 8.9023200,45.5074444,0\n
\n
\n" def setup super @google_full_hash = {:street_address=>"100 Spear St", :city=>"San Francisco", :state=>"CA", :zip=>"94105", :country_code=>"US"} @google_city_hash = {:city=>"San Francisco", :state=>"CA"} @google_full_loc = Geokit::GeoLoc.new(@google_full_hash) @google_city_loc = Geokit::GeoLoc.new(@google_city_hash) end def test_google_full_address response = MockSuccess.new response.expects(:body).returns(GOOGLE_FULL) url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8" Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response) res=Geokit::Geocoders::GoogleGeocoder.geocode(@address) assert_equal "CA", res.state assert_equal "San Francisco", res.city assert_equal "37.792501,-122.393985", res.ll # slightly dif from yahoo assert res.is_us? assert_equal "100 Spear St, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo assert_equal "google", res.provider end def test_google_full_address_with_geo_loc response = MockSuccess.new response.expects(:body).returns(GOOGLE_FULL) url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}&output=xml&key=Google&oe=utf-8" Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response) res=Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc) assert_equal "CA", res.state assert_equal "San Francisco", res.city assert_equal "37.792501,-122.393985", res.ll # slightly dif from yahoo assert res.is_us? assert_equal "100 Spear St, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo assert_equal "google", res.provider end def test_google_full_address_accuracy response = MockSuccess.new response.expects(:body).returns(GOOGLE_FULL) url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}&output=xml&key=Google&oe=utf-8" Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response) res=Geokit::Geocoders::GoogleGeocoder.geocode(@google_full_loc) assert_equal 8, res.accuracy end def test_google_city response = MockSuccess.new response.expects(:body).returns(GOOGLE_CITY) url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8" Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response) res=Geokit::Geocoders::GoogleGeocoder.geocode(@address) assert_equal "CA", res.state assert_equal "San Francisco", res.city assert_equal "37.775,-122.418333", res.ll assert res.is_us? assert_equal "San Francisco, CA, USA", res.full_address assert_nil res.street_address assert_equal "google", res.provider end def test_google_city_accuracy response = MockSuccess.new response.expects(:body).returns(GOOGLE_CITY) url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8" Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response) res=Geokit::Geocoders::GoogleGeocoder.geocode(@address) assert_equal 4, res.accuracy end def test_google_city_with_geo_loc response = MockSuccess.new response.expects(:body).returns(GOOGLE_CITY) url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8" Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response) res=Geokit::Geocoders::GoogleGeocoder.geocode(@google_city_loc) assert_equal "CA", res.state assert_equal "San Francisco", res.city assert_equal "37.775,-122.418333", res.ll assert res.is_us? assert_equal "San Francisco, CA, USA", res.full_address assert_nil res.street_address assert_equal "google", res.provider end def test_service_unavailable response = MockFailure.new url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8" Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response) assert !Geokit::Geocoders::GoogleGeocoder.geocode(@google_city_loc).success end def test_multiple_results #Geokit::Geocoders::GoogleGeocoder.do_geocode('via Sandro Pertini 8, Ossona, MI') response = MockSuccess.new response.expects(:body).returns(GOOGLE_MULTI) url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape('via Sandro Pertini 8, Ossona, MI')}&output=xml&key=Google&oe=utf-8" Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response) res=Geokit::Geocoders::GoogleGeocoder.geocode('via Sandro Pertini 8, Ossona, MI') assert_equal "Lombardy", res.state assert_equal "Mesero", res.city assert_equal "45.4966243,8.8527131", res.ll assert !res.is_us? assert_equal "Via Sandro Pertini, 8, 20010 Mesero MI, Italy", res.full_address assert_equal "8 Via Sandro Pertini", res.street_address assert_equal "google", res.provider assert_equal 2, res.all.size res = res.all[1] assert_equal "Lombardy", res.state assert_equal "Ossona", res.city assert_equal "45.5074444,8.90232", res.ll assert !res.is_us? assert_equal "Via Sandro Pertini, 20010 Ossona MI, Italy", res.full_address assert_equal "Via Sandro Pertini", res.street_address assert_equal "google", res.provider end end