geocoder 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of geocoder might be problematic. Click here for more details.

@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ConfigurationTest < Test::Unit::TestCase
5
+
6
+ def test_exception_raised_on_bad_lookup_config
7
+ Geocoder::Configuration.lookup = :stoopid
8
+ assert_raises Geocoder::ConfigurationError do
9
+ Geocoder.search "something dumb"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class CustomBlockTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ Geocoder::Configuration.set_defaults
8
+ end
9
+
10
+ def test_geocode_with_block_runs_block
11
+ e = Event.new(*venue_params(:msg))
12
+ coords = [40.750354, -73.993371]
13
+ e.geocode
14
+ assert_equal coords.map{ |c| c.to_s }.join(','), e.coords_string
15
+ end
16
+
17
+ def test_geocode_with_block_doesnt_auto_assign_coordinates
18
+ e = Event.new(*venue_params(:msg))
19
+ e.geocode
20
+ assert_nil e.latitude
21
+ assert_nil e.longitude
22
+ end
23
+
24
+ def test_reverse_geocode_with_block_runs_block
25
+ e = Party.new(*landmark_params(:msg))
26
+ e.reverse_geocode
27
+ assert_equal "US", e.country
28
+ end
29
+
30
+ def test_reverse_geocode_with_block_doesnt_auto_assign_address
31
+ e = Party.new(*landmark_params(:msg))
32
+ e.reverse_geocode
33
+ assert_nil e.address
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ErrorHandlingTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ Geocoder::Configuration.set_defaults
8
+ end
9
+
10
+ def test_does_not_choke_on_timeout
11
+ # keep test output clean: suppress timeout warning
12
+ orig = $VERBOSE; $VERBOSE = nil
13
+ all_lookups.each do |l|
14
+ Geocoder::Configuration.lookup = l
15
+ assert_nothing_raised { Geocoder.search("timeout") }
16
+ end
17
+ $VERBOSE = orig
18
+ end
19
+
20
+ def test_always_raise_timeout_error
21
+ Geocoder::Configuration.always_raise = [TimeoutError]
22
+ assert_raise(TimeoutError) { Geocoder.search("timeout") }
23
+ Geocoder::Configuration.always_raise = []
24
+ end
25
+
26
+ def test_always_raise_socket_error
27
+ Geocoder::Configuration.always_raise = [SocketError]
28
+ assert_raise(SocketError) { Geocoder.search("socket_error") }
29
+ Geocoder::Configuration.always_raise = []
30
+ end
31
+ end
@@ -7,79 +7,28 @@ class GeocoderTest < Test::Unit::TestCase
7
7
  Geocoder::Configuration.set_defaults
8
8
  end
9
9
 
10
-
11
- # --- configuration ---
12
- #
13
- def test_exception_raised_on_bad_lookup_config
14
- Geocoder::Configuration.lookup = :stoopid
15
- assert_raises Geocoder::ConfigurationError do
16
- Geocoder.search "something dumb"
17
- end
18
- end
19
-
20
-
21
- # --- sanity checks ---
22
-
23
- def test_uses_proxy_when_specified
24
- Geocoder::Configuration.http_proxy = 'localhost'
25
- lookup = Geocoder::Lookup::Google.new
26
- assert lookup.send(:http_client).proxy_class?
27
- end
28
-
29
- def test_doesnt_use_proxy_when_not_specified
30
- lookup = Geocoder::Lookup::Google.new
31
- assert !lookup.send(:http_client).proxy_class?
32
- end
33
-
34
- def test_exception_raised_on_bad_proxy_url
35
- Geocoder::Configuration.http_proxy = ' \\_O< Quack Quack'
36
- assert_raise Geocoder::ConfigurationError do
37
- Geocoder::Lookup::Google.new.send(:http_client)
38
- end
39
- end
40
-
41
- def test_uses_https_for_secure_query
42
- Geocoder::Configuration.use_https = true
43
- g = Geocoder::Lookup::Google.new
44
- assert_match /^https:/, g.send(:query_url, {:a => 1, :b => 2})
45
- end
46
-
47
- def test_uses_http_by_default
48
- g = Geocoder::Lookup::Google.new
49
- assert_match /^http:/, g.send(:query_url, {:a => 1, :b => 2})
50
- end
51
-
52
10
  def test_distance_to_returns_float
53
11
  v = Venue.new(*venue_params(:msg))
54
12
  v.latitude, v.longitude = [40.750354, -73.993371]
55
13
  assert (d = v.distance_to([30, -94])).is_a?(Float)
56
14
  end
57
15
 
58
- def test_distance_from_is_alias_for_distance_to
59
- v = Venue.new(*venue_params(:msg))
60
- v.latitude, v.longitude = [40.750354, -73.993371]
61
- assert_equal v.distance_from([30, -94]), v.distance_to([30, -94])
62
- end
63
-
64
- def test_coordinates_method
16
+ def test_coordinates_method_returns_array
65
17
  assert Geocoder.coordinates("Madison Square Garden, New York, NY").is_a?(Array)
66
18
  end
67
19
 
68
- def test_address_method
20
+ def test_address_method_returns_string
69
21
  assert Geocoder.address([40.750354, -73.993371]).is_a?(String)
70
22
  end
71
23
 
72
24
  def test_geographic_center_doesnt_overwrite_argument_value
73
- # this tests for the presence of a bug that was introduced in version 0.9.11
25
+ # test for the presence of a bug that was introduced in version 0.9.11
74
26
  orig_points = [[52,8], [46,9], [42,5]]
75
27
  points = orig_points.clone
76
28
  Geocoder::Calculations.geographic_center(points)
77
29
  assert_equal orig_points, points
78
30
  end
79
31
 
80
-
81
- # --- general ---
82
-
83
32
  def test_geocode_assigns_and_returns_coordinates
84
33
  v = Venue.new(*venue_params(:msg))
85
34
  coords = [40.750354, -73.993371]
@@ -94,33 +43,7 @@ class GeocoderTest < Test::Unit::TestCase
94
43
  assert_equal address, v.address
95
44
  end
96
45
 
97
- def test_geocode_with_block_runs_block
98
- e = Event.new(*venue_params(:msg))
99
- coords = [40.750354, -73.993371]
100
- e.geocode
101
- assert_equal coords.map{ |c| c.to_s }.join(','), e.coords_string
102
- end
103
-
104
- def test_geocode_with_block_doesnt_auto_assign_coordinates
105
- e = Event.new(*venue_params(:msg))
106
- e.geocode
107
- assert_nil e.latitude
108
- assert_nil e.longitude
109
- end
110
-
111
- def test_reverse_geocode_with_block_runs_block
112
- e = Party.new(*landmark_params(:msg))
113
- e.reverse_geocode
114
- assert_equal "US", e.country
115
- end
116
-
117
- def test_reverse_geocode_with_block_doesnt_auto_assign_address
118
- e = Party.new(*landmark_params(:msg))
119
- e.reverse_geocode
120
- assert_nil e.address
121
- end
122
-
123
- def test_forward_and_reverse_geocoding_on_same_model
46
+ def test_forward_and_reverse_geocoding_on_same_model_works
124
47
  g = GasStation.new("Exxon")
125
48
  g.address = "404 New St, Middletown, CT"
126
49
  g.geocode
@@ -131,365 +54,4 @@ class GeocoderTest < Test::Unit::TestCase
131
54
  g.reverse_geocode
132
55
  assert_not_nil g.location
133
56
  end
134
-
135
- def test_fetch_coordinates_is_alias_for_geocode
136
- v = Venue.new(*venue_params(:msg))
137
- coords = [40.750354, -73.993371]
138
- assert_equal coords, v.fetch_coordinates
139
- assert_equal coords, [v.latitude, v.longitude]
140
- end
141
-
142
- def test_fetch_address_is_alias_for_reverse_geocode
143
- v = Landmark.new(*landmark_params(:msg))
144
- address = "4 Penn Plaza, New York, NY 10001, USA"
145
- assert_equal address, v.fetch_address
146
- assert_equal address, v.address
147
- end
148
-
149
- def test_search_returns_empty_array_when_no_results
150
- street_lookups.each do |l|
151
- Geocoder::Configuration.lookup = l
152
- assert_equal [], Geocoder.search("no results"),
153
- "Lookup #{l} does not return empty array when no results."
154
- end
155
- end
156
-
157
- def test_result_has_required_attributes
158
- all_lookups.each do |l|
159
- Geocoder::Configuration.lookup = l
160
- result = Geocoder.search([45.423733, -75.676333]).first
161
- assert_result_has_required_attributes(result)
162
- end
163
- end
164
-
165
-
166
- # --- calculations: degree distance ---
167
-
168
- def test_longitude_degree_distance_at_equator
169
- assert_equal 69, Geocoder::Calculations.longitude_degree_distance(0).round
170
- end
171
-
172
- def test_longitude_degree_distance_at_new_york
173
- assert_equal 53, Geocoder::Calculations.longitude_degree_distance(40).round
174
- end
175
-
176
- def test_longitude_degree_distance_at_north_pole
177
- assert_equal 0, Geocoder::Calculations.longitude_degree_distance(89.98).round
178
- end
179
-
180
-
181
- # --- calculations: distance between ---
182
-
183
- def test_distance_between_in_miles
184
- assert_equal 69, Geocoder::Calculations.distance_between([0,0], [0,1]).round
185
- la_to_ny = Geocoder::Calculations.distance_between([34.05,-118.25], [40.72,-74]).round
186
- assert (la_to_ny - 2444).abs < 10
187
- end
188
-
189
- def test_distance_between_in_kilometers
190
- assert_equal 111, Geocoder::Calculations.distance_between([0,0], [0,1], :units => :km).round
191
- la_to_ny = Geocoder::Calculations.distance_between([34.05,-118.25], [40.72,-74], :units => :km).round
192
- assert (la_to_ny - 3942).abs < 10
193
- end
194
-
195
-
196
- # --- calculations: geographic center ---
197
-
198
- def test_geographic_center_with_arrays
199
- assert_equal [0.0, 0.5],
200
- Geocoder::Calculations.geographic_center([[0,0], [0,1]])
201
- assert_equal [0.0, 1.0],
202
- Geocoder::Calculations.geographic_center([[0,0], [0,1], [0,2]])
203
- end
204
-
205
- def test_geographic_center_with_mixed_arguments
206
- p1 = [0, 0]
207
- p2 = Landmark.new("Some Cold Place", 0, 1)
208
- assert_equal [0.0, 0.5], Geocoder::Calculations.geographic_center([p1, p2])
209
- end
210
-
211
-
212
- # --- calculations: bounding box ---
213
-
214
- def test_bounding_box_calculation_in_miles
215
- center = [51, 7] # Cologne, DE
216
- radius = 10 # miles
217
- dlon = radius / Geocoder::Calculations.latitude_degree_distance
218
- dlat = radius / Geocoder::Calculations.longitude_degree_distance(center[0])
219
- corners = [50.86, 6.77, 51.14, 7.23]
220
- assert_equal corners.map{ |i| (i * 100).round },
221
- Geocoder::Calculations.bounding_box(center, radius).map{ |i| (i * 100).round }
222
- end
223
-
224
- def test_bounding_box_calculation_in_kilometers
225
- center = [51, 7] # Cologne, DE
226
- radius = 111 # kilometers (= 1 degree latitude)
227
- dlon = radius / Geocoder::Calculations.latitude_degree_distance(:km)
228
- dlat = radius / Geocoder::Calculations.longitude_degree_distance(center[0], :km)
229
- corners = [50, 5.41, 52, 8.59]
230
- assert_equal corners.map{ |i| (i * 100).round },
231
- Geocoder::Calculations.bounding_box(center, radius, :units => :km).map{ |i| (i * 100).round }
232
- end
233
-
234
- def test_bounding_box_calculation_with_object
235
- center = [51, 7] # Cologne, DE
236
- radius = 10 # miles
237
- dlon = radius / Geocoder::Calculations.latitude_degree_distance
238
- dlat = radius / Geocoder::Calculations.longitude_degree_distance(center[0])
239
- corners = [50.86, 6.77, 51.14, 7.23]
240
- obj = Landmark.new("Cologne", center[0], center[1])
241
- assert_equal corners.map{ |i| (i * 100).round },
242
- Geocoder::Calculations.bounding_box(obj, radius).map{ |i| (i * 100).round }
243
- end
244
-
245
- def test_bounding_box_calculation_with_address_string
246
- assert_nothing_raised do
247
- Geocoder::Calculations.bounding_box("4893 Clay St, San Francisco, CA", 50)
248
- end
249
- end
250
-
251
-
252
- # --- calculations: bearing ---
253
-
254
- def test_compass_points
255
- assert_equal "N", Geocoder::Calculations.compass_point(0)
256
- assert_equal "N", Geocoder::Calculations.compass_point(1.0)
257
- assert_equal "N", Geocoder::Calculations.compass_point(360)
258
- assert_equal "N", Geocoder::Calculations.compass_point(361)
259
- assert_equal "N", Geocoder::Calculations.compass_point(-22)
260
- assert_equal "NW", Geocoder::Calculations.compass_point(-23)
261
- assert_equal "S", Geocoder::Calculations.compass_point(180)
262
- assert_equal "S", Geocoder::Calculations.compass_point(181)
263
- end
264
-
265
- def test_bearing_between
266
- bearings = {
267
- :n => 0,
268
- :e => 90,
269
- :s => 180,
270
- :w => 270
271
- }
272
- points = {
273
- :n => [41, -75],
274
- :e => [40, -74],
275
- :s => [39, -75],
276
- :w => [40, -76]
277
- }
278
- directions = [:n, :e, :s, :w]
279
- methods = [:linear, :spherical]
280
-
281
- methods.each do |m|
282
- directions.each_with_index do |d,i|
283
- opp = directions[(i + 2) % 4] # opposite direction
284
- b = Geocoder::Calculations.bearing_between(
285
- points[d], points[opp], :method => m)
286
- assert (b - bearings[opp]).abs < 1,
287
- "Bearing (#{m}) should be close to #{bearings[opp]} but was #{b}."
288
- end
289
- end
290
- end
291
-
292
- def test_spherical_bearing_to
293
- l = Landmark.new(*landmark_params(:msg))
294
- assert_equal 324, l.bearing_to([50,-85], :method => :spherical).round
295
- end
296
-
297
- def test_spherical_bearing_from
298
- l = Landmark.new(*landmark_params(:msg))
299
- assert_equal 136, l.bearing_from([50,-85], :method => :spherical).round
300
- end
301
-
302
- def test_linear_bearing_from_and_to_are_exactly_opposite
303
- l = Landmark.new(*landmark_params(:msg))
304
- assert_equal l.bearing_from([50,-86.1]), l.bearing_to([50,-86.1]) - 180
305
- end
306
-
307
-
308
- # --- input handling ---
309
-
310
- def test_ip_address_detection
311
- assert Geocoder.send(:ip_address?, "232.65.123.94")
312
- assert Geocoder.send(:ip_address?, "666.65.123.94") # technically invalid
313
- assert !Geocoder.send(:ip_address?, "232.65.123.94.43")
314
- assert !Geocoder.send(:ip_address?, "232.65.123")
315
- end
316
-
317
- def test_blank_query_detection
318
- assert Geocoder.send(:blank_query?, nil)
319
- assert Geocoder.send(:blank_query?, "")
320
- assert Geocoder.send(:blank_query?, "\t ")
321
- assert !Geocoder.send(:blank_query?, "a")
322
- assert !Geocoder.send(:blank_query?, "Москва") # no ASCII characters
323
- end
324
-
325
- def test_coordinates_detection
326
- lookup = Geocoder::Lookup::Google.new
327
- assert lookup.send(:coordinates?, "51.178844,5")
328
- assert lookup.send(:coordinates?, "51.178844, -1.826189")
329
- assert !lookup.send(:coordinates?, "232.65.123")
330
- end
331
-
332
- def test_does_not_choke_on_nil_address
333
- all_lookups.each do |l|
334
- Geocoder::Configuration.lookup = l
335
- assert_nothing_raised { Venue.new("Venue", nil).geocode }
336
- end
337
- end
338
-
339
- def test_extract_coordinates
340
- coords = [-23,47]
341
- l = Landmark.new("Madagascar", coords[0], coords[1])
342
- assert_equal coords, Geocoder::Calculations.extract_coordinates(l)
343
- assert_equal coords, Geocoder::Calculations.extract_coordinates(coords)
344
- end
345
-
346
-
347
- # --- error handling ---
348
-
349
- def test_does_not_choke_on_timeout
350
- # keep test output clean: suppress timeout warning
351
- orig = $VERBOSE; $VERBOSE = nil
352
- all_lookups.each do |l|
353
- Geocoder::Configuration.lookup = l
354
- assert_nothing_raised { Geocoder.search("timeout") }
355
- end
356
- $VERBOSE = orig
357
- end
358
-
359
-
360
- def test_always_raise_timeout_error
361
- Geocoder::Configuration.always_raise = [TimeoutError]
362
- assert_raise(TimeoutError) { Geocoder.search("timeout") }
363
- Geocoder::Configuration.always_raise = []
364
- end
365
-
366
-
367
- def test_always_raise_socket_error
368
- Geocoder::Configuration.always_raise = [SocketError]
369
- assert_raise(SocketError) { Geocoder.search("socket_error") }
370
- Geocoder::Configuration.always_raise = []
371
- end
372
-
373
- # --- Google ---
374
-
375
- def test_google_result_components
376
- result = Geocoder.search("Madison Square Garden, New York, NY").first
377
- assert_equal "Manhattan",
378
- result.address_components_of_type(:sublocality).first['long_name']
379
- end
380
-
381
- def test_google_returns_city_when_no_locality_in_result
382
- result = Geocoder.search("no locality").first
383
- assert_equal "Haram", result.city
384
- end
385
-
386
- def test_google_city_results_returns_nil_if_no_matching_component_types
387
- result = Geocoder.search("no city data").first
388
- assert_equal nil, result.city
389
- end
390
-
391
- # --- Yahoo ---
392
-
393
- def test_yahoo_result_components
394
- Geocoder::Configuration.lookup = :yahoo
395
- result = Geocoder.search("Madison Square Garden, New York, NY").first
396
- assert_equal "10001", result.postal_code
397
- end
398
-
399
- def test_yahoo_address_formatting
400
- Geocoder::Configuration.lookup = :yahoo
401
- result = Geocoder.search("Madison Square Garden, New York, NY").first
402
- assert_equal "Madison Square Garden, New York, NY 10001, United States",
403
- result.address
404
- end
405
-
406
-
407
- # --- Yandex ---
408
-
409
- def test_yandex_with_invalid_key
410
- # keep test output clean: suppress timeout warning
411
- orig = $VERBOSE; $VERBOSE = nil
412
- Geocoder::Configuration.lookup = :yandex
413
- assert_equal [], Geocoder.search("invalid key")
414
- $VERBOSE = orig
415
- end
416
-
417
-
418
- # --- Geocoder.ca ---
419
-
420
- def test_geocoder_ca_result_components
421
- Geocoder::Configuration.lookup = :geocoder_ca
422
- result = Geocoder.search([45.423733, -75.676333]).first
423
- assert_equal "CA", result.country_code
424
- assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address
425
- end
426
-
427
-
428
- # --- FreeGeoIp ---
429
-
430
- def test_freegeoip_result_on_ip_address_search
431
- result = Geocoder.search("74.200.247.59").first
432
- assert result.is_a?(Geocoder::Result::Freegeoip)
433
- end
434
-
435
- def test_freegeoip_result_components
436
- result = Geocoder.search("74.200.247.59").first
437
- assert_equal "Plano, TX 75093, United States", result.address
438
- end
439
-
440
-
441
- # --- Bing ---
442
-
443
- def test_bing_result_components
444
- Geocoder::Configuration.lookup = :bing
445
- result = Geocoder.search("Madison Square Garden, New York, NY").first
446
- assert_equal "Madison Square Garden, NY", result.address
447
- assert_equal "NY", result.state
448
- assert_equal "New York", result.city
449
- end
450
-
451
- def test_bing_no_results
452
- Geocoder::Configuration.lookup = :bing
453
- results = Geocoder.search("no results")
454
- assert_equal 0, results.length
455
- end
456
-
457
-
458
- # --- search queries ---
459
-
460
- def test_hash_to_query
461
- g = Geocoder::Lookup::Google.new
462
- assert_equal "a=1&b=2", g.send(:hash_to_query, {:a => 1, :b => 2})
463
- end
464
-
465
- def test_google_api_key
466
- Geocoder::Configuration.api_key = "MY_KEY"
467
- g = Geocoder::Lookup::Google.new
468
- assert_match "key=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
469
- end
470
-
471
- def test_yahoo_app_id
472
- Geocoder::Configuration.api_key = "MY_KEY"
473
- g = Geocoder::Lookup::Yahoo.new
474
- assert_match "appid=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
475
- end
476
-
477
-
478
- private # ------------------------------------------------------------------
479
-
480
- def assert_result_has_required_attributes(result)
481
- m = "Lookup #{Geocoder::Configuration.lookup} does not support %s attribute."
482
- assert result.coordinates.is_a?(Array), m % "coordinates"
483
- assert result.latitude.is_a?(Float), m % "latitude"
484
- assert result.longitude.is_a?(Float), m % "longitude"
485
- assert result.city.is_a?(String), m % "city"
486
- assert result.state.is_a?(String), m % "state"
487
- assert result.state_code.is_a?(String), m % "state_code"
488
- assert result.province.is_a?(String), m % "province"
489
- assert result.province_code.is_a?(String), m % "province_code"
490
- assert result.postal_code.is_a?(String), m % "postal_code"
491
- assert result.country.is_a?(String), m % "country"
492
- assert result.country_code.is_a?(String), m % "country_code"
493
- assert_not_nil result.address, m % "address"
494
- end
495
57
  end