Add Google Map of voters
[selectricity-live] / vendor / plugins / geokit / test / google_geocoder_test.rb
diff --git a/vendor/plugins/geokit/test/google_geocoder_test.rb b/vendor/plugins/geokit/test/google_geocoder_test.rb
new file mode 100644 (file)
index 0000000..dfa8a0c
--- /dev/null
@@ -0,0 +1,88 @@
+require File.join(File.dirname(__FILE__), 'base_geocoder_test')
+
+GeoKit::Geocoders::google = 'Google'
+
+class GoogleGeocoderTest < BaseGeocoderTest #:nodoc: all
+  
+  GOOGLE_FULL=<<-EOF.strip
+  <?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>
+  EOF
+
+  GOOGLE_CITY=<<-EOF.strip
+  <?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>
+  EOF
+  
+  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=#{CGI.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=#{CGI.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_city
+    response = MockSuccess.new
+    response.expects(:body).returns(GOOGLE_CITY)
+    url = "http://maps.google.com/maps/geo?q=#{CGI.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_with_geo_loc
+    response = MockSuccess.new
+    response.expects(:body).returns(GOOGLE_CITY)
+    url = "http://maps.google.com/maps/geo?q=#{CGI.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=#{CGI.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 
+end
\ No newline at end of file

Benjamin Mako Hill || Want to submit a patch?