google_geocoder_test.rb
5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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