bjeanes-geokit 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
2
+
3
+ Geokit::Geocoders::google = 'Google'
4
+
5
+ class GoogleReverseGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ GOOGLE_REVERSE_FULL=<<-EOF.strip
8
+ <?xml version="1.0" encoding="UTF-8" ?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>51.457833,7.016685</name><Status><code>200</code><request>geocode</request></Status><Placemark id="p1"><address>Porscheplatz 1, 45127 Essen, Deutschland</address><AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><DependentLocality><DependentLocalityName>Stadtkern</DependentLocalityName><Thoroughfare><ThoroughfareName>Porscheplatz 1</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>45127</PostalCodeNumber></PostalCode></DependentLocality></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4609805" south="51.4546853" east="7.0198324" west="7.0135372" /></ExtendedData><Point><coordinates>7.0166848,51.4578329,0</coordinates></Point></Placemark><Placemark id="p2"><address>Stadtkern, Essen, Deutschland</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><DependentLocality><DependentLocalityName>Stadtkern</DependentLocalityName></DependentLocality></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4630710" south="51.4506320" east="7.0193200" west="7.0026170" /></ExtendedData><Point><coordinates>7.0124328,51.4568201,0</coordinates></Point></Placemark><Placemark id="p3"><address>45127 Essen, Deutschland</address><AddressDetails Accuracy="5" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><PostalCode><PostalCodeNumber>45127</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4637808" south="51.4503125" east="7.0231080" west="6.9965454" /></ExtendedData><Point><coordinates>7.0104543,51.4556194,0</coordinates></Point></Placemark><Placemark id="p4"><address>Essen, Deutschland</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.5342070" south="51.3475730" east="7.1376530" west="6.8943470" /></ExtendedData><Point><coordinates>7.0147614,51.4580686,0</coordinates></Point></Placemark><Placemark id="p5"><address>Essen, Deutschland</address><AddressDetails Accuracy="3" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.5342070" south="51.3475730" east="7.1376530" west="6.8943470" /></ExtendedData><Point><coordinates>7.0461136,51.4508381,0</coordinates></Point></Placemark><Placemark id="p6"><address>Nordrhein-Westfalen, Deutschland</address><AddressDetails Accuracy="2" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="52.5314170" south="50.3225720" east="9.4615950" west="5.8663566" /></ExtendedData><Point><coordinates>7.6615938,51.4332367,0</coordinates></Point></Placemark><Placemark id="p7"><address>Deutschland</address><AddressDetails Accuracy="1" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName></Country></AddressDetails><ExtendedData><LatLonBox north="55.0568230" south="47.2701270" east="15.0418536" west="5.8663566" /></ExtendedData><Point><coordinates>10.4515260,51.1656910,0</coordinates></Point></Placemark></Response></kml>
9
+ EOF
10
+
11
+
12
+ def test_google_full_address
13
+ response = MockSuccess.new
14
+ response.expects(:body).returns(GOOGLE_REVERSE_FULL)
15
+
16
+
17
+ # http://maps.google.com/maps/geo?output=xml&oe=utf-8&ll=51.4578329,7.0166848&key=asdad
18
+
19
+ # #<Geokit::GeoLoc:0x10ec7ec
20
+ # @city="Essen",
21
+ # @country_code="DE",
22
+ # @full_address="Porscheplatz 1, 45127 Essen, Germany",
23
+ # @lat=51.4578329,
24
+ # @lng=7.0166848,
25
+ # @precision="address",
26
+ # @provider="google",
27
+ # @state="Nordrhein-Westfalen",
28
+ # @street_address="Porscheplatz 1",
29
+ # @success=true,
30
+ # @zip="45127">
31
+ #
32
+
33
+
34
+ @latlng = "51.4578329,7.0166848"
35
+
36
+ url = "http://maps.google.com/maps/geo?ll=#{Geokit::Inflector.url_escape(@latlng)}&output=xml&key=Google&oe=utf-8"
37
+ Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
38
+ res=Geokit::Geocoders::GoogleGeocoder.reverse_geocode(@latlng)
39
+ assert_equal "Nordrhein-Westfalen", res.state
40
+ assert_equal "Essen", res.city
41
+ assert_equal "45127", res.zip
42
+ assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
43
+ assert res.is_us? == false
44
+ assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
45
+ assert_equal "google", res.provider
46
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test/unit'
4
+ require 'lib/geokit'
5
+
6
+ class InflectorTest < Test::Unit::TestCase #:nodoc: all
7
+
8
+ def test_titleize
9
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('Sugar Grove')
10
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('Sugar grove')
11
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('sugar Grove')
12
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('sugar grove')
13
+ end
14
+
15
+ def test_titleize_with_unicode
16
+ assert_equal 'Borås', Geokit::Inflector.titleize('Borås')
17
+ assert_equal 'Borås', Geokit::Inflector.titleize('borås')
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
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('borås (Abc)')
21
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('borås (abc)')
22
+ end
23
+
24
+ end
@@ -0,0 +1,110 @@
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: SWITZERLAND (CH)
8
+ City: (Unknown City)
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
+ PRIVATE_IPS_TO_TEST = [
28
+ '10.10.10.10',
29
+ '172.16.1.3',
30
+ '172.22.3.42',
31
+ '172.30.254.164',
32
+ '192.168.1.1',
33
+ '0.0.0.0',
34
+ '127.0.0.1',
35
+ '240.3.4.5',
36
+ '225.1.6.55'
37
+ ].freeze
38
+
39
+ def setup
40
+ super
41
+ @success.provider = "hostip"
42
+ end
43
+
44
+ def test_successful_lookup
45
+ success = MockSuccess.new
46
+ success.expects(:body).returns(IP_SUCCESS)
47
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
48
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
49
+ location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
50
+ assert_not_nil location
51
+ assert_equal 41.7696, location.lat
52
+ assert_equal(-88.4588, location.lng)
53
+ assert_equal "Sugar Grove", location.city
54
+ assert_equal "IL", location.state
55
+ assert_equal "US", location.country_code
56
+ assert_equal "hostip", location.provider
57
+ assert location.success?
58
+ end
59
+
60
+ def test_unicoded_lookup
61
+ success = MockSuccess.new
62
+ success.expects(:body).returns(IP_UNICODED)
63
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
64
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
65
+ location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
66
+ assert_not_nil location
67
+ assert_equal 57.7167, location.lat
68
+ assert_equal 12.9167, location.lng
69
+ assert_equal "Bor\303\245s", location.city
70
+ assert_nil location.state
71
+ assert_equal "SE", location.country_code
72
+ assert_equal "hostip", location.provider
73
+ assert location.success?
74
+ end
75
+
76
+ def test_failed_lookup
77
+ failure = MockSuccess.new
78
+ failure.expects(:body).returns(IP_FAILURE)
79
+ url = 'http://api.hostip.info/get_html.php?ip=128.178.0.0&position=true'
80
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
81
+ location = GeoKit::Geocoders::IpGeocoder.geocode("128.178.0.0")
82
+ assert_not_nil location
83
+ assert !location.success?
84
+ end
85
+
86
+ def test_private_ips
87
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).never
88
+ PRIVATE_IPS_TO_TEST.each do |ip|
89
+ location = GeoKit::Geocoders::IpGeocoder.geocode(ip)
90
+ assert_not_nil location
91
+ assert !location.success?
92
+ end
93
+ end
94
+
95
+ def test_invalid_ip
96
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).never
97
+ location = GeoKit::Geocoders::IpGeocoder.geocode("blah")
98
+ assert_not_nil location
99
+ assert !location.success?
100
+ end
101
+
102
+ def test_service_unavailable
103
+ failure = MockFailure.new
104
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
105
+ GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
106
+ location = GeoKit::Geocoders::IpGeocoder.geocode("12.215.42.19")
107
+ assert_not_nil location
108
+ assert !location.success?
109
+ end
110
+ end
@@ -0,0 +1,209 @@
1
+ require 'test/unit'
2
+ require 'lib/geokit'
3
+ require 'mocha'
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 valid_reverse_geocoding_result
14
+ location = Geokit::GeoLoc.new({
15
+ :city => "Essen",
16
+ :country_code => "DE",
17
+ :lat => 51.4578329,
18
+ :lng => 7.0166848,
19
+ :provider => "google",
20
+ :state => "Nordrhein-Westfalen",
21
+ :street_address => "Porscheplatz 1",
22
+ :zip => "45127"
23
+ })
24
+
25
+ location.full_address = "Porscheplatz 1, 45127 Essen, Deutschland"
26
+ location.precision = 'address'
27
+ location.provider = 'google'
28
+ location.success = true
29
+ location
30
+ end
31
+
32
+ def test_distance_between_same_using_defaults
33
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a)
34
+ assert_equal 0, @loc_a.distance_to(@loc_a)
35
+ end
36
+
37
+ def test_distance_between_same_with_miles_and_flat
38
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :flat)
39
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :flat)
40
+ end
41
+
42
+ def test_distance_between_same_with_kms_and_flat
43
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :flat)
44
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :flat)
45
+ end
46
+
47
+ def test_distance_between_same_with_nms_and_flat
48
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :nms, :formula => :flat)
49
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :nms, :formula => :flat)
50
+ end
51
+
52
+ def test_distance_between_same_with_miles_and_sphere
53
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :sphere)
54
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :sphere)
55
+ end
56
+
57
+ def test_distance_between_same_with_kms_and_sphere
58
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :sphere)
59
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :sphere)
60
+ end
61
+
62
+ def test_distance_between_same_with_nms_and_sphere
63
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :nms, :formula => :sphere)
64
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :nms, :formula => :sphere)
65
+ end
66
+
67
+ def test_distance_between_diff_using_defaults
68
+ assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e), 0.01
69
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e), 0.01
70
+ end
71
+
72
+ def test_distance_between_diff_with_miles_and_flat
73
+ assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :flat), 0.2
74
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
75
+ end
76
+
77
+ def test_distance_between_diff_with_kms_and_flat
78
+ assert_in_delta 6.39, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :flat), 0.4
79
+ assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
80
+ end
81
+
82
+ def test_distance_between_diff_with_nms_and_flat
83
+ assert_in_delta 3.334, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :nms, :formula => :flat), 0.4
84
+ assert_in_delta 3.334, @loc_a.distance_to(@loc_e, :units => :nms, :formula => :flat), 0.4
85
+ end
86
+
87
+ def test_distance_between_diff_with_miles_and_sphere
88
+ assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :sphere), 0.01
89
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :sphere), 0.01
90
+ end
91
+
92
+ def test_distance_between_diff_with_kms_and_sphere
93
+ assert_in_delta 6.39, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :sphere), 0.01
94
+ assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :sphere), 0.01
95
+ end
96
+
97
+ def test_distance_between_diff_with_nms_and_sphere
98
+ assert_in_delta 3.454, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :nms, :formula => :sphere), 0.01
99
+ assert_in_delta 3.454, @loc_a.distance_to(@loc_e, :units => :nms, :formula => :sphere), 0.01
100
+ end
101
+
102
+ def test_manually_mixed_in
103
+ assert_equal 0, Geokit::LatLng.distance_between(@point, @point)
104
+ assert_equal 0, @point.distance_to(@point)
105
+ assert_equal 0, @point.distance_to(@loc_a)
106
+ assert_in_delta 3.97, @point.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
107
+ assert_in_delta 6.39, @point.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
108
+ assert_in_delta 3.334, @point.distance_to(@loc_e, :units => :nms, :formula => :flat), 0.4
109
+ end
110
+
111
+ def test_heading_between
112
+ assert_in_delta 332, Geokit::LatLng.heading_between(@loc_a,@loc_e), 0.5
113
+ end
114
+
115
+ def test_heading_to
116
+ assert_in_delta 332, @loc_a.heading_to(@loc_e), 0.5
117
+ end
118
+
119
+ def test_class_endpoint
120
+ endpoint=Geokit::LatLng.endpoint(@loc_a, 332, 3.97)
121
+ assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
122
+ assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
123
+ end
124
+
125
+ def test_instance_endpoint
126
+ endpoint=@loc_a.endpoint(332, 3.97)
127
+ assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
128
+ assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
129
+ end
130
+
131
+ def test_midpoint
132
+ midpoint=@loc_a.midpoint_to(@loc_e)
133
+ assert_in_delta 32.944061, midpoint.lat, 0.0005
134
+ assert_in_delta(-96.974296, midpoint.lng, 0.0005)
135
+ end
136
+
137
+ def test_normalize
138
+ lat=37.7690
139
+ lng=-122.443
140
+ res=Geokit::LatLng.normalize(lat,lng)
141
+ assert_equal res,Geokit::LatLng.new(lat,lng)
142
+ res=Geokit::LatLng.normalize("#{lat}, #{lng}")
143
+ assert_equal res,Geokit::LatLng.new(lat,lng)
144
+ res=Geokit::LatLng.normalize("#{lat} #{lng}")
145
+ assert_equal res,Geokit::LatLng.new(lat,lng)
146
+ res=Geokit::LatLng.normalize("#{lat.to_i} #{lng.to_i}")
147
+ assert_equal res,Geokit::LatLng.new(lat.to_i,lng.to_i)
148
+ res=Geokit::LatLng.normalize([lat,lng])
149
+ assert_equal res,Geokit::LatLng.new(lat,lng)
150
+ end
151
+
152
+ def test_hash
153
+ lat=37.7690
154
+ lng=-122.443
155
+ first = Geokit::LatLng.new(lat,lng)
156
+ second = Geokit::LatLng.new(lat,lng)
157
+ assert_equal first.hash, second.hash
158
+ end
159
+
160
+ def test_eql?
161
+ lat=37.7690
162
+ lng=-122.443
163
+ first = Geokit::LatLng.new(lat,lng)
164
+ second = Geokit::LatLng.new(lat,lng)
165
+ assert first.eql?(second)
166
+ assert second.eql?(first)
167
+ end
168
+
169
+ def test_reverse_geocode
170
+ point = Geokit::LatLng.new(51.4578329, 7.0166848)
171
+ Geokit::Geocoders::MultiGeocoder.expects(:reverse_geocode).with(point).returns(valid_reverse_geocoding_result)
172
+ res = point.reverse_geocode
173
+
174
+ assert_equal "Nordrhein-Westfalen", res.state
175
+ assert_equal "Essen", res.city
176
+ assert_equal "45127", res.zip
177
+ assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
178
+ assert res.is_us? == false
179
+ assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
180
+ end
181
+
182
+ def test_reverse_geocoding_using_specific_geocoder
183
+ point = Geokit::LatLng.new(51.4578329, 7.0166848)
184
+ Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(point).returns(valid_reverse_geocoding_result)
185
+ res = point.reverse_geocode(:using => Geokit::Geocoders::GoogleGeocoder)
186
+
187
+ assert_equal "Nordrhein-Westfalen", res.state
188
+ assert_equal "Essen", res.city
189
+ assert_equal "45127", res.zip
190
+ assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
191
+ assert res.is_us? == false
192
+ assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
193
+ assert_equal "google", res.provider
194
+ end
195
+
196
+ def test_reverse_geocoding_using_specific_geocoder_short_syntax
197
+ point = Geokit::LatLng.new(51.4578329, 7.0166848)
198
+ Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(point).returns(valid_reverse_geocoding_result)
199
+ res = point.reverse_geocode(:using => :google)
200
+
201
+ assert_equal "Nordrhein-Westfalen", res.state
202
+ assert_equal "Essen", res.city
203
+ assert_equal "45127", res.zip
204
+ assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
205
+ assert res.is_us? == false
206
+ assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
207
+ assert_equal "google", res.provider
208
+ end
209
+ end
@@ -0,0 +1,93 @@
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
+ def test_reverse_geocode_successful_first
55
+ Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(@latlng).returns(@success)
56
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.reverse_geocode(@latlng)
57
+ end
58
+
59
+ def test_reverse_geocode_failover
60
+ Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
61
+ Geokit::Geocoders::YahooGeocoder.expects(:reverse_geocode).with(@latlng).returns(@success)
62
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.reverse_geocode(@latlng)
63
+ end
64
+
65
+ def test_reverse_geocode_double_failover
66
+ Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
67
+ Geokit::Geocoders::YahooGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
68
+ Geokit::Geocoders::UsGeocoder.expects(:reverse_geocode).with(@latlng).returns(@success)
69
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.reverse_geocode(@latlng)
70
+ end
71
+
72
+ def test_reverse_geocode_failure
73
+ Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
74
+ Geokit::Geocoders::YahooGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
75
+ Geokit::Geocoders::UsGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
76
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.reverse_geocode(@latlng)
77
+ end
78
+
79
+ def test_reverse_geocode_with_invalid_provider
80
+ temp = Geokit::Geocoders::provider_order
81
+ Geokit::Geocoders.provider_order = [:bogus]
82
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.reverse_geocode(@latlng)
83
+ Geokit::Geocoders.provider_order = temp
84
+ end
85
+
86
+ def test_reverse_geocode_with_blank_latlng
87
+ t1 = Geokit::Geocoders.provider_order # will need to reset after
88
+ Geokit::Geocoders.provider_order = [:google]
89
+ Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with("").returns(@failure)
90
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.reverse_geocode("")
91
+ Geokit::Geocoders.provider_order = t1 # reset to orig values
92
+ end
93
+ end