1 require File.join(File.dirname(__FILE__), 'base_geocoder_test')
3 GeoKit::Geocoders::google = 'Google'
5 class GoogleGeocoderTest < BaseGeocoderTest #:nodoc: all
7 GOOGLE_FULL=<<-EOF.strip
8 <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>100 spear st, san francisco, ca</name><Status><code>200</code><request>geocode</request></Status><Placemark><address>100 Spear St, San Francisco, CA 94105, USA</address><AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><AdministrativeArea><AdministrativeAreaName>CA</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>San Francisco</SubAdministrativeAreaName><Locality><LocalityName>San Francisco</LocalityName><Thoroughfare><ThoroughfareName>100 Spear St</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>94105</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><Point><coordinates>-122.393985,37.792501,0</coordinates></Point></Placemark></Response></kml>
11 GOOGLE_CITY=<<-EOF.strip
12 <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>San Francisco</name><Status><code>200</code><request>geocode</request></Status><Placemark><address>San Francisco, CA, USA</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><AdministrativeArea><AdministrativeAreaName>CA</AdministrativeAreaName><Locality><LocalityName>San Francisco</LocalityName></Locality></AdministrativeArea></Country></AddressDetails><Point><coordinates>-122.418333,37.775000,0</coordinates></Point></Placemark></Response></kml>
17 @google_full_hash = {:street_address=>"100 Spear St", :city=>"San Francisco", :state=>"CA", :zip=>"94105", :country_code=>"US"}
18 @google_city_hash = {:city=>"San Francisco", :state=>"CA"}
20 @google_full_loc = GeoKit::GeoLoc.new(@google_full_hash)
21 @google_city_loc = GeoKit::GeoLoc.new(@google_city_hash)
24 def test_google_full_address
25 response = MockSuccess.new
26 response.expects(:body).returns(GOOGLE_FULL)
27 url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
28 GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
29 res=GeoKit::Geocoders::GoogleGeocoder.geocode(@address)
30 assert_equal "CA", res.state
31 assert_equal "San Francisco", res.city
32 assert_equal "37.792501,-122.393985", res.ll # slightly dif from yahoo
34 assert_equal "100 Spear St, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
35 assert_equal "google", res.provider
38 def test_google_full_address_with_geo_loc
39 response = MockSuccess.new
40 response.expects(:body).returns(GOOGLE_FULL)
41 url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@full_address_short_zip)}&output=xml&key=Google&oe=utf-8"
42 GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
43 res=GeoKit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
44 assert_equal "CA", res.state
45 assert_equal "San Francisco", res.city
46 assert_equal "37.792501,-122.393985", res.ll # slightly dif from yahoo
48 assert_equal "100 Spear St, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
49 assert_equal "google", res.provider
53 response = MockSuccess.new
54 response.expects(:body).returns(GOOGLE_CITY)
55 url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
56 GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
57 res=GeoKit::Geocoders::GoogleGeocoder.geocode(@address)
58 assert_equal "CA", res.state
59 assert_equal "San Francisco", res.city
60 assert_equal "37.775,-122.418333", res.ll
62 assert_equal "San Francisco, CA, USA", res.full_address
63 assert_nil res.street_address
64 assert_equal "google", res.provider
67 def test_google_city_with_geo_loc
68 response = MockSuccess.new
69 response.expects(:body).returns(GOOGLE_CITY)
70 url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
71 GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
72 res=GeoKit::Geocoders::GoogleGeocoder.geocode(@google_city_loc)
73 assert_equal "CA", res.state
74 assert_equal "San Francisco", res.city
75 assert_equal "37.775,-122.418333", res.ll
77 assert_equal "San Francisco, CA, USA", res.full_address
78 assert_nil res.street_address
79 assert_equal "google", res.provider
82 def test_service_unavailable
83 response = MockFailure.new
84 url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
85 GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
86 assert !GeoKit::Geocoders::GoogleGeocoder.geocode(@google_city_loc).success