artofmission-Geokit 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
1
+ require File.join(File.dirname(__FILE__), '../../../../config/environment')
2
+ require 'action_controller/test_process'
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mocha'
6
+
7
+
8
+ class LocationAwareController < ActionController::Base #:nodoc: all
9
+ geocode_ip_address
10
+
11
+ def index
12
+ render :nothing => true
13
+ end
14
+ end
15
+
16
+ class ActionController::TestRequest #:nodoc: all
17
+ attr_accessor :remote_ip
18
+ end
19
+
20
+ # Re-raise errors caught by the controller.
21
+ class LocationAwareController #:nodoc: all
22
+ def rescue_action(e) raise e end;
23
+ end
24
+
25
+ class IpGeocodeLookupTest < Test::Unit::TestCase #:nodoc: all
26
+
27
+ def setup
28
+ @success = GeoKit::GeoLoc.new
29
+ @success.provider = "hostip"
30
+ @success.lat = 41.7696
31
+ @success.lng = -88.4588
32
+ @success.city = "Sugar Grove"
33
+ @success.state = "IL"
34
+ @success.country_code = "US"
35
+ @success.success = true
36
+
37
+ @failure = GeoKit::GeoLoc.new
38
+ @failure.provider = "hostip"
39
+ @failure.city = "(Private Address)"
40
+ @failure.success = false
41
+
42
+ @controller = LocationAwareController.new
43
+ @request = ActionController::TestRequest.new
44
+ @response = ActionController::TestResponse.new
45
+ end
46
+
47
+ def test_no_location_in_cookie_or_session
48
+ GeoKit::Geocoders::IpGeocoder.expects(:geocode).with("good ip").returns(@success)
49
+ @request.remote_ip = "good ip"
50
+ get :index
51
+ verify
52
+ end
53
+
54
+ def test_location_in_cookie
55
+ @request.remote_ip = "good ip"
56
+ @request.cookies['geo_location'] = CGI::Cookie.new('geo_location', @success.to_yaml)
57
+ get :index
58
+ verify
59
+ end
60
+
61
+ def test_location_in_session
62
+ @request.remote_ip = "good ip"
63
+ @request.session[:geo_location] = @success
64
+ @request.cookies['geo_location'] = CGI::Cookie.new('geo_location', @success.to_yaml)
65
+ get :index
66
+ verify
67
+ end
68
+
69
+ def test_ip_not_located
70
+ GeoKit::Geocoders::IpGeocoder.expects(:geocode).with("bad ip").returns(@failure)
71
+ @request.remote_ip = "bad ip"
72
+ get :index
73
+ assert_nil @request.session[:geo_location]
74
+ end
75
+
76
+ private
77
+
78
+ def verify
79
+ assert_response :success
80
+ assert_equal @success, @request.session[:geo_location]
81
+ assert_not_nil cookies['geo_location']
82
+ assert_equal @success, YAML.load(cookies['geo_location'].join)
83
+ end
84
+ end
@@ -0,0 +1,87 @@
1
+ require File.join(File.dirname(__FILE__), 'base_geocoder_test')
2
+
3
+ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
4
+
5
+ IP_FAILURE=<<-EOF
6
+ Country: (Private Address) (XX)
7
+ City: (Private Address)
8
+ Latitude:
9
+ Longitude:
10
+ EOF
11
+
12
+ IP_SUCCESS=<<-EOF
13
+ Country: UNITED STATES (US)
14
+ City: Sugar Grove, IL
15
+ Latitude: 41.7696
16
+ Longitude: -88.4588
17
+ EOF
18
+
19
+ IP_UNICODED=<<-EOF
20
+ Country: SWEDEN (SE)
21
+ City: Borås
22
+ Latitude: 57.7167
23
+ Longitude: 12.9167
24
+ EOF
25
+
26
+ def setup
27
+ super
28
+ @success.provider = "hostip"
29
+ end
30
+
31
+ def test_successful_lookup
32
+ success = MockSuccess.new
33
+ success.expects(:body).returns(IP_SUCCESS)
34
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
35
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
36
+ location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
37
+ assert_not_nil location
38
+ assert_equal 41.7696, location.lat
39
+ assert_equal -88.4588, location.lng
40
+ assert_equal "Sugar Grove", location.city
41
+ assert_equal "IL", location.state
42
+ assert_equal "US", location.country_code
43
+ assert_equal "hostip", location.provider
44
+ assert location.success
45
+ end
46
+
47
+ def test_unicoded_lookup
48
+ success = MockSuccess.new
49
+ success.expects(:body).returns(IP_UNICODED)
50
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
51
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
52
+ location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
53
+ assert_not_nil location
54
+ assert_equal 57.7167, location.lat
55
+ assert_equal 12.9167, location.lng
56
+ assert_equal "Borås", location.city
57
+ assert_nil location.state
58
+ assert_equal "SE", location.country_code
59
+ assert_equal "hostip", location.provider
60
+ assert location.success
61
+ end
62
+
63
+ def test_failed_lookup
64
+ failure = MockSuccess.new
65
+ failure.expects(:body).returns(IP_FAILURE)
66
+ url = 'http://api.hostip.info/get_html.php?ip=0.0.0.0&position=true'
67
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
68
+ location = GeoKit::Geocoders::IpGeocoder.geocode("0.0.0.0")
69
+ assert_not_nil location
70
+ assert !location.success
71
+ end
72
+
73
+ def test_invalid_ip
74
+ location = GeoKit::Geocoders::IpGeocoder.geocode("blah")
75
+ assert_not_nil location
76
+ assert !location.success
77
+ end
78
+
79
+ def test_service_unavailable
80
+ failure = MockFailure.new
81
+ url = 'http://api.hostip.info/get_html.php?ip=0.0.0.0&position=true'
82
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
83
+ location = GeoKit::Geocoders::IpGeocoder.geocode("0.0.0.0")
84
+ assert_not_nil location
85
+ assert !location.success
86
+ end
87
+ end
@@ -0,0 +1,112 @@
1
+ $LOAD_PATH.unshift File.join('..', 'lib')
2
+ require 'geo_kit/mappable'
3
+ require 'test/unit'
4
+
5
+ class LatLngTest < Test::Unit::TestCase #:nodoc: all
6
+
7
+ def setup
8
+ @loc_a = GeoKit::LatLng.new(32.918593,-96.958444)
9
+ @loc_e = GeoKit::LatLng.new(32.969527,-96.990159)
10
+ @point = GeoKit::LatLng.new(@loc_a.lat, @loc_a.lng)
11
+ end
12
+
13
+ def test_distance_between_same_using_defaults
14
+ assert_equal 0, GeoKit::LatLng.distance_between(@loc_a, @loc_a)
15
+ assert_equal 0, @loc_a.distance_to(@loc_a)
16
+ end
17
+
18
+ def test_distance_between_same_with_miles_and_flat
19
+ assert_equal 0, GeoKit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :flat)
20
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :flat)
21
+ end
22
+
23
+ def test_distance_between_same_with_kms_and_flat
24
+ assert_equal 0, GeoKit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :flat)
25
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :flat)
26
+ end
27
+
28
+ def test_distance_between_same_with_miles_and_sphere
29
+ assert_equal 0, GeoKit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :sphere)
30
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :sphere)
31
+ end
32
+
33
+ def test_distance_between_same_with_kms_and_sphere
34
+ assert_equal 0, GeoKit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :sphere)
35
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :sphere)
36
+ end
37
+
38
+ def test_distance_between_diff_using_defaults
39
+ assert_in_delta 3.97, GeoKit::LatLng.distance_between(@loc_a, @loc_e), 0.01
40
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e), 0.01
41
+ end
42
+
43
+ def test_distance_between_diff_with_miles_and_flat
44
+ assert_in_delta 3.97, GeoKit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :flat), 0.2
45
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
46
+ end
47
+
48
+ def test_distance_between_diff_with_kms_and_flat
49
+ assert_in_delta 6.39, GeoKit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :flat), 0.4
50
+ assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
51
+ end
52
+
53
+ def test_distance_between_diff_with_miles_and_sphere
54
+ assert_in_delta 3.97, GeoKit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :sphere), 0.01
55
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :sphere), 0.01
56
+ end
57
+
58
+ def test_distance_between_diff_with_kms_and_sphere
59
+ assert_in_delta 6.39, GeoKit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :sphere), 0.01
60
+ assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :sphere), 0.01
61
+ end
62
+
63
+ def test_manually_mixed_in
64
+ assert_equal 0, GeoKit::LatLng.distance_between(@point, @point)
65
+ assert_equal 0, @point.distance_to(@point)
66
+ assert_equal 0, @point.distance_to(@loc_a)
67
+ assert_in_delta 3.97, @point.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
68
+ assert_in_delta 6.39, @point.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
69
+ end
70
+
71
+ def test_heading_between
72
+ assert_in_delta 332, GeoKit::LatLng.heading_between(@loc_a,@loc_e), 0.5
73
+ end
74
+
75
+ def test_heading_to
76
+ assert_in_delta 332, @loc_a.heading_to(@loc_e), 0.5
77
+ end
78
+
79
+ def test_class_endpoint
80
+ endpoint=GeoKit::LatLng.endpoint(@loc_a, 332, 3.97)
81
+ assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
82
+ assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
83
+ end
84
+
85
+ def test_instance_endpoint
86
+ endpoint=@loc_a.endpoint(332, 3.97)
87
+ assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
88
+ assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
89
+ end
90
+
91
+ def test_midpoint
92
+ midpoint=@loc_a.midpoint_to(@loc_e)
93
+ assert_in_delta 32.944061, midpoint.lat, 0.0005
94
+ assert_in_delta -96.974296, midpoint.lng, 0.0005
95
+ end
96
+
97
+ def test_normalize
98
+ lat=37.7690
99
+ lng=-122.443
100
+ res=GeoKit::LatLng.normalize(lat,lng)
101
+ assert_equal res,GeoKit::LatLng.new(lat,lng)
102
+ res=GeoKit::LatLng.normalize("#{lat}, #{lng}")
103
+ assert_equal res,GeoKit::LatLng.new(lat,lng)
104
+ res=GeoKit::LatLng.normalize("#{lat} #{lng}")
105
+ assert_equal res,GeoKit::LatLng.new(lat,lng)
106
+ res=GeoKit::LatLng.normalize("#{lat.to_i} #{lng.to_i}")
107
+ assert_equal res,GeoKit::LatLng.new(lat.to_i,lng.to_i)
108
+ res=GeoKit::LatLng.normalize([lat,lng])
109
+ assert_equal res,GeoKit::LatLng.new(lat,lng)
110
+ end
111
+
112
+ end
@@ -0,0 +1,44 @@
1
+ require File.join(File.dirname(__FILE__), 'base_geocoder_test')
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
+ end
@@ -0,0 +1,31 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :companies, :force => true do |t|
3
+ t.column :name, :string
4
+ end
5
+
6
+ create_table :locations, :force => true do |t|
7
+ t.column :company_id, :integer, :default => 0, :null => false
8
+ t.column :street, :string, :limit => 60
9
+ t.column :city, :string, :limit => 60
10
+ t.column :state, :string, :limit => 2
11
+ t.column :postal_code, :string, :limit => 16
12
+ t.column :lat, :decimal, :precision => 15, :scale => 10
13
+ t.column :lng, :decimal, :precision => 15, :scale => 10
14
+ end
15
+
16
+ create_table :custom_locations, :force => true do |t|
17
+ t.column :company_id, :integer, :default => 0, :null => false
18
+ t.column :street, :string, :limit => 60
19
+ t.column :city, :string, :limit => 60
20
+ t.column :state, :string, :limit => 2
21
+ t.column :postal_code, :string, :limit => 16
22
+ t.column :latitude, :decimal, :precision => 15, :scale => 10
23
+ t.column :longitude, :decimal, :precision => 15, :scale => 10
24
+ end
25
+
26
+ create_table :stores, :force=> true do |t|
27
+ t.column :address, :string
28
+ t.column :lat, :decimal, :precision => 15, :scale => 10
29
+ t.column :lng, :decimal, :precision => 15, :scale => 10
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ require 'test/unit'
2
+
3
+ plugin_test_dir = File.dirname(__FILE__)
4
+
5
+ # Load the Rails environment
6
+ require File.join(plugin_test_dir, '../../../../config/environment')
7
+ require 'active_record/fixtures'
8
+ databases = YAML::load(IO.read(plugin_test_dir + '/database.yml'))
9
+ ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
10
+
11
+ # A specific database can be used by setting the DB environment variable
12
+ ActiveRecord::Base.establish_connection(databases[ENV['DB'] || 'mysql'])
13
+
14
+ # Load the test schema into the database
15
+ load(File.join(plugin_test_dir, 'schema.rb'))
16
+
17
+ # Load fixtures from the plugin
18
+ Test::Unit::TestCase.fixture_path = File.join(plugin_test_dir, 'fixtures/')
@@ -0,0 +1,47 @@
1
+ require File.join(File.dirname(__FILE__), 'base_geocoder_test')
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=#{CGI.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=#{CGI.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=#{CGI.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
+ private
39
+
40
+ def verify(location)
41
+ assert_equal "CA", location.state
42
+ assert_equal "San Francisco", location.city
43
+ assert_equal "37.792528,-122.393981", location.ll
44
+ assert location.is_us?
45
+ assert_equal "100 Spear St, San Francisco, CA, 94105, US", location.full_address #slightly different from yahoo
46
+ end
47
+ end
@@ -0,0 +1,87 @@
1
+ require File.join(File.dirname(__FILE__), 'base_geocoder_test')
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=#{CGI.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_with_geo_loc
36
+ response = MockSuccess.new
37
+ response.expects(:body).returns(YAHOO_FULL)
38
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@full_address)}"
39
+ GeoKit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
40
+ do_full_address_assertions(GeoKit::Geocoders::YahooGeocoder.geocode(@yahoo_full_loc))
41
+ end
42
+
43
+ def test_yahoo_city
44
+ response = MockSuccess.new
45
+ response.expects(:body).returns(YAHOO_CITY)
46
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}"
47
+ GeoKit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
48
+ do_city_assertions(GeoKit::Geocoders::YahooGeocoder.geocode(@address))
49
+ end
50
+
51
+ def test_yahoo_city_with_geo_loc
52
+ response = MockSuccess.new
53
+ response.expects(:body).returns(YAHOO_CITY)
54
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}"
55
+ GeoKit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
56
+ do_city_assertions(GeoKit::Geocoders::YahooGeocoder.geocode(@yahoo_city_loc))
57
+ end
58
+
59
+ def test_service_unavailable
60
+ response = MockFailure.new
61
+ url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=Yahoo&location=#{CGI.escape(@address)}"
62
+ GeoKit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response)
63
+ assert !GeoKit::Geocoders::YahooGeocoder.geocode(@yahoo_city_loc).success
64
+ end
65
+
66
+ private
67
+
68
+ # next two methods do the assertions for both address-level and city-level lookups
69
+ def do_full_address_assertions(res)
70
+ assert_equal "CA", res.state
71
+ assert_equal "San Francisco", res.city
72
+ assert_equal "37.792406,-122.39411", res.ll
73
+ assert res.is_us?
74
+ assert_equal "100 Spear St, San Francisco, CA, 94105-1522, US", res.full_address
75
+ assert_equal "yahoo", res.provider
76
+ end
77
+
78
+ def do_city_assertions(res)
79
+ assert_equal "CA", res.state
80
+ assert_equal "San Francisco", res.city
81
+ assert_equal "37.7742,-122.417068", res.ll
82
+ assert res.is_us?
83
+ assert_equal "San Francisco, CA, US", res.full_address
84
+ assert_nil res.street_address
85
+ assert_equal "yahoo", res.provider
86
+ end
87
+ end