]> projects.mako.cc - selectricity/blob - vendor/plugins/geokit/test/google_geocoder_test.rb
added quick documentation about XML-RPC interface
[selectricity] / vendor / plugins / geokit / test / google_geocoder_test.rb
1 require File.join(File.dirname(__FILE__), 'base_geocoder_test')
2
3 GeoKit::Geocoders::google = 'Google'
4
5 class GoogleGeocoderTest < BaseGeocoderTest #:nodoc: all
6   
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>
9   EOF
10
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>
13   EOF
14   
15   def setup
16     super
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"}
19
20     @google_full_loc = GeoKit::GeoLoc.new(@google_full_hash)
21     @google_city_loc = GeoKit::GeoLoc.new(@google_city_hash)
22   end  
23
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
33     assert res.is_us?
34     assert_equal "100 Spear St, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
35     assert_equal "google", res.provider
36   end
37   
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
47     assert res.is_us?
48     assert_equal "100 Spear St, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
49     assert_equal "google", res.provider
50   end  
51
52   def test_google_city
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
61     assert res.is_us?
62     assert_equal "San Francisco, CA, USA", res.full_address
63     assert_nil res.street_address
64     assert_equal "google", res.provider
65   end  
66   
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
76     assert res.is_us?
77     assert_equal "San Francisco, CA, USA", res.full_address
78     assert_nil res.street_address
79     assert_equal "google", res.provider
80   end  
81   
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
87   end 
88 end

Benjamin Mako Hill || Want to submit a patch?