Pr0d1r2-geokit 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'lib/geokit'
3
+
4
+ class InflectorTest < Test::Unit::TestCase #:nodoc: all
5
+
6
+ def test_titleize
7
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('Sugar Grove')
8
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('Sugar grove')
9
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('sugar Grove')
10
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('sugar grove')
11
+ end
12
+
13
+ def test_titleize_with_unicode
14
+ assert_equal 'Borås', Geokit::Inflector.titleize('Borås')
15
+ assert_equal 'Borås', Geokit::Inflector.titleize('borås')
16
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('Borås (Abc)')
17
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('Borås (abc)')
18
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('borås (Abc)')
19
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('borås (abc)')
20
+ end
21
+
22
+ end
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
3
+
4
+ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
5
+
6
+ IP_FAILURE=<<-EOF
7
+ Country: (Private Address) (XX)
8
+ City: (Private Address)
9
+ Latitude:
10
+ Longitude:
11
+ EOF
12
+
13
+ IP_SUCCESS=<<-EOF
14
+ Country: UNITED STATES (US)
15
+ City: Sugar Grove, IL
16
+ Latitude: 41.7696
17
+ Longitude: -88.4588
18
+ EOF
19
+
20
+ IP_UNICODED=<<-EOF
21
+ Country: SWEDEN (SE)
22
+ City: Borås
23
+ Latitude: 57.7167
24
+ Longitude: 12.9167
25
+ EOF
26
+
27
+ def setup
28
+ super
29
+ @success.provider = "hostip"
30
+ end
31
+
32
+ def test_successful_lookup
33
+ success = MockSuccess.new
34
+ success.expects(:body).returns(IP_SUCCESS)
35
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
36
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
37
+ location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
38
+ assert_not_nil location
39
+ assert_equal 41.7696, location.lat
40
+ assert_equal(-88.4588, location.lng)
41
+ assert_equal "Sugar Grove", location.city
42
+ assert_equal "IL", location.state
43
+ assert_equal "US", location.country_code
44
+ assert_equal "hostip", location.provider
45
+ assert location.success?
46
+ end
47
+
48
+ def test_unicoded_lookup
49
+ success = MockSuccess.new
50
+ success.expects(:body).returns(IP_UNICODED)
51
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
52
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
53
+ location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
54
+ assert_not_nil location
55
+ assert_equal 57.7167, location.lat
56
+ assert_equal 12.9167, location.lng
57
+ assert_equal "Bor\303\245s", location.city
58
+ assert_nil location.state
59
+ assert_equal "SE", location.country_code
60
+ assert_equal "hostip", location.provider
61
+ assert location.success?
62
+ end
63
+
64
+ def test_failed_lookup
65
+ failure = MockSuccess.new
66
+ failure.expects(:body).returns(IP_FAILURE)
67
+ url = 'http://api.hostip.info/get_html.php?ip=10.10.10.10&position=true'
68
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
69
+ location = GeoKit::Geocoders::IpGeocoder.geocode("10.10.10.10")
70
+ assert_not_nil location
71
+ assert !location.success?
72
+ end
73
+
74
+ def test_invalid_ip
75
+ location = GeoKit::Geocoders::IpGeocoder.geocode("blah")
76
+ assert_not_nil location
77
+ assert !location.success?
78
+ end
79
+
80
+ def test_service_unavailable
81
+ failure = MockFailure.new
82
+ url = 'http://api.hostip.info/get_html.php?ip=10.10.10.10&position=true'
83
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
84
+ location = GeoKit::Geocoders::IpGeocoder.geocode("10.10.10.10")
85
+ assert_not_nil location
86
+ assert !location.success?
87
+ end
88
+ end
@@ -0,0 +1,148 @@
1
+ require 'test/unit'
2
+ require 'lib/geokit'
3
+
4
+ class LatLngTest < Test::Unit::TestCase #:nodoc: all
5
+
6
+ def setup
7
+ @loc_a = Geokit::LatLng.new(32.918593,-96.958444)
8
+ @loc_e = Geokit::LatLng.new(32.969527,-96.990159)
9
+ @point = Geokit::LatLng.new(@loc_a.lat, @loc_a.lng)
10
+ end
11
+
12
+ def test_distance_between_same_using_defaults
13
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a)
14
+ assert_equal 0, @loc_a.distance_to(@loc_a)
15
+ end
16
+
17
+ def test_distance_between_same_with_miles_and_flat
18
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :flat)
19
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :flat)
20
+ end
21
+
22
+ def test_distance_between_same_with_kms_and_flat
23
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :flat)
24
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :flat)
25
+ end
26
+
27
+ def test_distance_between_same_with_nms_and_flat
28
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :nms, :formula => :flat)
29
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :nms, :formula => :flat)
30
+ end
31
+
32
+ def test_distance_between_same_with_miles_and_sphere
33
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :sphere)
34
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :sphere)
35
+ end
36
+
37
+ def test_distance_between_same_with_kms_and_sphere
38
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :sphere)
39
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :sphere)
40
+ end
41
+
42
+ def test_distance_between_same_with_nms_and_sphere
43
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :nms, :formula => :sphere)
44
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :nms, :formula => :sphere)
45
+ end
46
+
47
+ def test_distance_between_diff_using_defaults
48
+ assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e), 0.01
49
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e), 0.01
50
+ end
51
+
52
+ def test_distance_between_diff_with_miles_and_flat
53
+ assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :flat), 0.2
54
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
55
+ end
56
+
57
+ def test_distance_between_diff_with_kms_and_flat
58
+ assert_in_delta 6.39, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :flat), 0.4
59
+ assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
60
+ end
61
+
62
+ def test_distance_between_diff_with_nms_and_flat
63
+ assert_in_delta 3.334, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :nms, :formula => :flat), 0.4
64
+ assert_in_delta 3.334, @loc_a.distance_to(@loc_e, :units => :nms, :formula => :flat), 0.4
65
+ end
66
+
67
+ def test_distance_between_diff_with_miles_and_sphere
68
+ assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :sphere), 0.01
69
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :sphere), 0.01
70
+ end
71
+
72
+ def test_distance_between_diff_with_kms_and_sphere
73
+ assert_in_delta 6.39, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :sphere), 0.01
74
+ assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :sphere), 0.01
75
+ end
76
+
77
+ def test_distance_between_diff_with_nms_and_sphere
78
+ assert_in_delta 3.454, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :nms, :formula => :sphere), 0.01
79
+ assert_in_delta 3.454, @loc_a.distance_to(@loc_e, :units => :nms, :formula => :sphere), 0.01
80
+ end
81
+
82
+ def test_manually_mixed_in
83
+ assert_equal 0, Geokit::LatLng.distance_between(@point, @point)
84
+ assert_equal 0, @point.distance_to(@point)
85
+ assert_equal 0, @point.distance_to(@loc_a)
86
+ assert_in_delta 3.97, @point.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
87
+ assert_in_delta 6.39, @point.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
88
+ assert_in_delta 3.334, @point.distance_to(@loc_e, :units => :nms, :formula => :flat), 0.4
89
+ end
90
+
91
+ def test_heading_between
92
+ assert_in_delta 332, Geokit::LatLng.heading_between(@loc_a,@loc_e), 0.5
93
+ end
94
+
95
+ def test_heading_to
96
+ assert_in_delta 332, @loc_a.heading_to(@loc_e), 0.5
97
+ end
98
+
99
+ def test_class_endpoint
100
+ endpoint=Geokit::LatLng.endpoint(@loc_a, 332, 3.97)
101
+ assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
102
+ assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
103
+ end
104
+
105
+ def test_instance_endpoint
106
+ endpoint=@loc_a.endpoint(332, 3.97)
107
+ assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
108
+ assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
109
+ end
110
+
111
+ def test_midpoint
112
+ midpoint=@loc_a.midpoint_to(@loc_e)
113
+ assert_in_delta 32.944061, midpoint.lat, 0.0005
114
+ assert_in_delta(-96.974296, midpoint.lng, 0.0005)
115
+ end
116
+
117
+ def test_normalize
118
+ lat=37.7690
119
+ lng=-122.443
120
+ res=Geokit::LatLng.normalize(lat,lng)
121
+ assert_equal res,Geokit::LatLng.new(lat,lng)
122
+ res=Geokit::LatLng.normalize("#{lat}, #{lng}")
123
+ assert_equal res,Geokit::LatLng.new(lat,lng)
124
+ res=Geokit::LatLng.normalize("#{lat} #{lng}")
125
+ assert_equal res,Geokit::LatLng.new(lat,lng)
126
+ res=Geokit::LatLng.normalize("#{lat.to_i} #{lng.to_i}")
127
+ assert_equal res,Geokit::LatLng.new(lat.to_i,lng.to_i)
128
+ res=Geokit::LatLng.normalize([lat,lng])
129
+ assert_equal res,Geokit::LatLng.new(lat,lng)
130
+ end
131
+
132
+ def test_hash
133
+ lat=37.7690
134
+ lng=-122.443
135
+ first = Geokit::LatLng.new(lat,lng)
136
+ second = Geokit::LatLng.new(lat,lng)
137
+ assert_equal first.hash, second.hash
138
+ end
139
+
140
+ def test_eql?
141
+ lat=37.7690
142
+ lng=-122.443
143
+ first = Geokit::LatLng.new(lat,lng)
144
+ second = Geokit::LatLng.new(lat,lng)
145
+ assert first.eql?(second)
146
+ assert second.eql?(first)
147
+ end
148
+ end
@@ -0,0 +1,54 @@
1
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
2
+
3
+ Geokit::Geocoders::provider_order=[:google,:yahoo,:us]
4
+
5
+ class MultiGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ def setup
8
+ super
9
+ @failure = Geokit::GeoLoc.new
10
+ end
11
+
12
+ def test_successful_first
13
+ Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address).returns(@success)
14
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@address)
15
+ end
16
+
17
+ def test_failover
18
+ Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address).returns(@failure)
19
+ Geokit::Geocoders::YahooGeocoder.expects(:geocode).with(@address).returns(@success)
20
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@address)
21
+ end
22
+
23
+ def test_double_failover
24
+ Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address).returns(@failure)
25
+ Geokit::Geocoders::YahooGeocoder.expects(:geocode).with(@address).returns(@failure)
26
+ Geokit::Geocoders::UsGeocoder.expects(:geocode).with(@address).returns(@success)
27
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@address)
28
+ end
29
+
30
+ def test_failure
31
+ Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address).returns(@failure)
32
+ Geokit::Geocoders::YahooGeocoder.expects(:geocode).with(@address).returns(@failure)
33
+ Geokit::Geocoders::UsGeocoder.expects(:geocode).with(@address).returns(@failure)
34
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@address)
35
+ end
36
+
37
+ def test_invalid_provider
38
+ temp = Geokit::Geocoders::provider_order
39
+ Geokit::Geocoders.provider_order = [:bogus]
40
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@address)
41
+ Geokit::Geocoders.provider_order = temp
42
+ end
43
+
44
+ def test_blank_address
45
+ t1, t2 = Geokit::Geocoders.provider_order, Geokit::Geocoders.ip_provider_order # will need to reset after
46
+ Geokit::Geocoders.provider_order = [:google]
47
+ Geokit::Geocoders.ip_provider_order = [:geo_plugin]
48
+ Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with("").returns(@failure)
49
+ Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).never
50
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode("")
51
+ Geokit::Geocoders.provider_order, Geokit::Geocoders.ip_provider_order = t1, t2 # reset to orig values
52
+ end
53
+
54
+ end
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
2
+
3
+ Geokit::Geocoders::ip_provider_order=[:geo_plugin,:ip]
4
+
5
+ class MultiIpGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ def setup
8
+ @ip_address = '10.10.10.10'
9
+ @success = Geokit::GeoLoc.new({:city=>"SAN FRANCISCO", :state=>"CA", :country_code=>"US", :lat=>37.7742, :lng=>-122.417068})
10
+ @success.success = true
11
+ @failure = Geokit::GeoLoc.new
12
+ end
13
+
14
+ def test_successful_first
15
+ Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address).returns(@success)
16
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
17
+ end
18
+
19
+ def test_failover
20
+ Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address).returns(@failure)
21
+ Geokit::Geocoders::IpGeocoder.expects(:geocode).with(@ip_address).returns(@success)
22
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
23
+ end
24
+
25
+ def test_failure
26
+ Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address).returns(@failure)
27
+ Geokit::Geocoders::IpGeocoder.expects(:geocode).with(@ip_address).returns(@failure)
28
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
29
+ end
30
+
31
+ def test_invalid_provider
32
+ temp = Geokit::Geocoders::ip_provider_order
33
+ Geokit::Geocoders.ip_provider_order = [:bogus]
34
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
35
+ Geokit::Geocoders.ip_provider_order = temp
36
+ end
37
+
38
+ end
@@ -0,0 +1,56 @@
1
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
2
+
3
+ Geokit::Geocoders::geocoder_us = nil
4
+
5
+ class UsGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ GEOCODER_US_FULL='37.792528,-122.393981,100 Spear St,San Francisco,CA,94105'
8
+
9
+ def setup
10
+ super
11
+ @us_full_hash = {:city=>"San Francisco", :state=>"CA"}
12
+ @us_full_loc = Geokit::GeoLoc.new(@us_full_hash)
13
+ end
14
+
15
+ def test_geocoder_us
16
+ response = MockSuccess.new
17
+ response.expects(:body).returns(GEOCODER_US_FULL)
18
+ url = "http://geocoder.us/service/csv/geocode?address=#{Geokit::Inflector.url_escape(@address)}"
19
+ Geokit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
20
+ verify(Geokit::Geocoders::UsGeocoder.geocode(@address))
21
+ end
22
+
23
+ def test_geocoder_with_geo_loc
24
+ response = MockSuccess.new
25
+ response.expects(:body).returns(GEOCODER_US_FULL)
26
+ url = "http://geocoder.us/service/csv/geocode?address=#{Geokit::Inflector.url_escape(@address)}"
27
+ Geokit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
28
+ verify(Geokit::Geocoders::UsGeocoder.geocode(@us_full_loc))
29
+ end
30
+
31
+ def test_service_unavailable
32
+ response = MockFailure.new
33
+ url = "http://geocoder.us/service/csv/geocode?address=#{Geokit::Inflector.url_escape(@address)}"
34
+ Geokit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
35
+ assert !Geokit::Geocoders::UsGeocoder.geocode(@us_full_loc).success
36
+ end
37
+
38
+ def test_all_method
39
+ response = MockSuccess.new
40
+ response.expects(:body).returns(GEOCODER_US_FULL)
41
+ url = "http://geocoder.us/service/csv/geocode?address=#{Geokit::Inflector.url_escape(@address)}"
42
+ Geokit::Geocoders::UsGeocoder.expects(:call_geocoder_service).with(url).returns(response)
43
+ res=Geokit::Geocoders::UsGeocoder.geocode(@address)
44
+ assert_equal 1, res.all.size
45
+ end
46
+
47
+ private
48
+
49
+ def verify(location)
50
+ assert_equal "CA", location.state
51
+ assert_equal "San Francisco", location.city
52
+ assert_equal "37.792528,-122.393981", location.ll
53
+ assert location.is_us?
54
+ assert_equal "100 Spear St, San Francisco, CA, 94105, US", location.full_address #slightly different from yahoo
55
+ end
56
+ end
@@ -0,0 +1,105 @@
1
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
2
+
3
+ Geokit::Geocoders::yahoo = 'Yahoo'
4
+
5
+ class YahooGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+ YAHOO_FULL=<<-EOF.strip
7
+ <?xml version="1.0"?>
8
+ <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd"><Result precision="address"><Latitude>37.792406</Latitude><Longitude>-122.39411</Longitude><Address>100 SPEAR ST</Address><City>SAN FRANCISCO</City><State>CA</State><Zip>94105-1522</Zip><Country>US</Country></Result></ResultSet>
9
+ <!-- ws01.search.scd.yahoo.com uncompressed/chunked Mon Jan 29 16:23:43 PST 2007 -->
10
+ EOF
11
+
12
+ YAHOO_CITY=<<-EOF.strip
13
+ <?xml version="1.0"?>
14
+ <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd"><Result precision="city"><Latitude>37.7742</Latitude><Longitude>-122.417068</Longitude><Address></Address><City>SAN FRANCISCO</City><State>CA</State><Zip></Zip><Country>US</Country></Result></ResultSet>
15
+ <!-- ws02.search.scd.yahoo.com uncompressed/chunked Mon Jan 29 18:00:28 PST 2007 -->
16
+ EOF
17
+
18
+ def setup
19
+ super
20
+ @yahoo_full_hash = {:street_address=>"100 Spear St", :city=>"San Francisco", :state=>"CA", :zip=>"94105-1522", :country_code=>"US"}
21
+ @yahoo_city_hash = {:city=>"San Francisco", :state=>"CA"}
22
+ @yahoo_full_loc = Geokit::GeoLoc.new(@yahoo_full_hash)
23
+ @yahoo_city_loc = Geokit::GeoLoc.new(@yahoo_city_hash)
24
+ end
25
+
26
+ # the testing methods themselves
27
+ def test_yahoo_full_address
28
+ response = MockSuccess.new
29
+ response.expects(:body).returns(YAHOO_FULL)
30
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{Geokit::Inflector.url_escape(@address)}"
31
+ Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
32
+ do_full_address_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@address))
33
+ end
34
+
35
+ def test_yahoo_full_address_accuracy
36
+ response = MockSuccess.new
37
+ response.expects(:body).returns(YAHOO_FULL)
38
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{Geokit::Inflector.url_escape(@address)}"
39
+ Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
40
+ res = Geokit::Geocoders::YahooGeocoder.geocode(@address)
41
+ assert_equal 8, res.accuracy
42
+ end
43
+
44
+ def test_yahoo_full_address_with_geo_loc
45
+ response = MockSuccess.new
46
+ response.expects(:body).returns(YAHOO_FULL)
47
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{Geokit::Inflector.url_escape(@full_address)}"
48
+ Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
49
+ do_full_address_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@yahoo_full_loc))
50
+ end
51
+
52
+ def test_yahoo_city
53
+ response = MockSuccess.new
54
+ response.expects(:body).returns(YAHOO_CITY)
55
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{Geokit::Inflector.url_escape(@address)}"
56
+ Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
57
+ do_city_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@address))
58
+ end
59
+
60
+ def test_yahoo_city_accuracy
61
+ response = MockSuccess.new
62
+ response.expects(:body).returns(YAHOO_CITY)
63
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{Geokit::Inflector.url_escape(@address)}"
64
+ Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
65
+ res = Geokit::Geocoders::YahooGeocoder.geocode(@address)
66
+ assert_equal 4, res.accuracy
67
+ end
68
+
69
+ def test_yahoo_city_with_geo_loc
70
+ response = MockSuccess.new
71
+ response.expects(:body).returns(YAHOO_CITY)
72
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{Geokit::Inflector.url_escape(@address)}"
73
+ Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
74
+ do_city_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@yahoo_city_loc))
75
+ end
76
+
77
+ def test_service_unavailable
78
+ response = MockFailure.new
79
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{Geokit::Inflector.url_escape(@address)}"
80
+ Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
81
+ assert !Geokit::Geocoders::YahooGeocoder.geocode(@yahoo_city_loc).success
82
+ end
83
+
84
+ private
85
+
86
+ # next two methods do the assertions for both address-level and city-level lookups
87
+ def do_full_address_assertions(res)
88
+ assert_equal "CA", res.state
89
+ assert_equal "San Francisco", res.city
90
+ assert_equal "37.792406,-122.39411", res.ll
91
+ assert res.is_us?
92
+ assert_equal "100 Spear St, San Francisco, CA, 94105-1522, US", res.full_address
93
+ assert_equal "yahoo", res.provider
94
+ end
95
+
96
+ def do_city_assertions(res)
97
+ assert_equal "CA", res.state
98
+ assert_equal "San Francisco", res.city
99
+ assert_equal "37.7742,-122.417068", res.ll
100
+ assert res.is_us?
101
+ assert_equal "San Francisco, CA, US", res.full_address
102
+ assert_nil res.street_address
103
+ assert_equal "yahoo", res.provider
104
+ end
105
+ end