4 require "geocoder_controller"
6 class GeocoderControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/search", :method => :get },
12 { :controller => "geocoder", :action => "search" }
15 { :path => "/geocoder/search_latlon", :method => :get },
16 { :controller => "geocoder", :action => "search_latlon" }
19 { :path => "/geocoder/search_us_postcode", :method => :get },
20 { :controller => "geocoder", :action => "search_us_postcode" }
23 { :path => "/geocoder/search_uk_postcode", :method => :get },
24 { :controller => "geocoder", :action => "search_uk_postcode" }
27 { :path => "/geocoder/search_ca_postcode", :method => :get },
28 { :controller => "geocoder", :action => "search_ca_postcode" }
31 { :path => "/geocoder/search_osm_nominatim", :method => :get },
32 { :controller => "geocoder", :action => "search_osm_nominatim" }
35 { :path => "/geocoder/search_geonames", :method => :get },
36 { :controller => "geocoder", :action => "search_geonames" }
39 { :path => "/geocoder/search_osm_nominatim_reverse", :method => :get },
40 { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
43 { :path => "/geocoder/search_geonames_reverse", :method => :get },
44 { :controller => "geocoder", :action => "search_geonames_reverse" }
49 # Test identification with no arguments
50 def test_identify_error
52 assert_response :bad_request
55 assert_response :bad_request
59 # Test identification of basic lat/lon pairs
60 def test_identify_latlon_basic
64 "+50.06773 +14.37742",
65 "+50.06773, +14.37742"
67 latlon_check code, 50.06773, 14.37742
72 # Test identification of lat/lon pairs using N/E with degrees
73 def test_identify_latlon_ne_d
75 "N50.06773 E14.37742",
76 "N50.06773, E14.37742",
77 "50.06773N 14.37742E",
78 "50.06773N, 14.37742E"
80 latlon_check code, 50.06773, 14.37742
85 # Test identification of lat/lon pairs using N/W with degrees
86 def test_identify_latlon_nw_d
88 "N50.06773 W14.37742",
89 "N50.06773, W14.37742",
90 "50.06773N 14.37742W",
91 "50.06773N, 14.37742W"
93 latlon_check code, 50.06773, -14.37742
98 # Test identification of lat/lon pairs using S/E with degrees
99 def test_identify_latlon_se_d
101 "S50.06773 E14.37742",
102 "S50.06773, E14.37742",
103 "50.06773S 14.37742E",
104 "50.06773S, 14.37742E"
106 latlon_check code, -50.06773, 14.37742
111 # Test identification of lat/lon pairs using S/W with degrees
112 def test_identify_latlon_sw_d
114 "S50.06773 W14.37742",
115 "S50.06773, W14.37742",
116 "50.06773S 14.37742W",
117 "50.06773S, 14.37742W"
119 latlon_check code, -50.06773, -14.37742
124 # Test identification of lat/lon pairs using N/E with degrees/mins
125 def test_identify_latlon_ne_dm
127 "N 50° 04.064 E 014° 22.645",
128 "N 50° 04.064' E 014° 22.645",
129 "N 50° 04.064', E 014° 22.645'",
130 "N50° 04.064 E14° 22.645",
131 "N 50 04.064 E 014 22.645",
132 "N50 4.064 E14 22.645",
133 "50° 04.064' N, 014° 22.645' E"
135 latlon_check code, 50.06773, 14.37742
140 # Test identification of lat/lon pairs using N/W with degrees/mins
141 def test_identify_latlon_nw_dm
143 "N 50° 04.064 W 014° 22.645",
144 "N 50° 04.064' W 014° 22.645",
145 "N 50° 04.064', W 014° 22.645'",
146 "N50° 04.064 W14° 22.645",
147 "N 50 04.064 W 014 22.645",
148 "N50 4.064 W14 22.645",
149 "50° 04.064' N, 014° 22.645' W"
151 latlon_check code, 50.06773, -14.37742
156 # Test identification of lat/lon pairs using S/E with degrees/mins
157 def test_identify_latlon_se_dm
159 "S 50° 04.064 E 014° 22.645",
160 "S 50° 04.064' E 014° 22.645",
161 "S 50° 04.064', E 014° 22.645'",
162 "S50° 04.064 E14° 22.645",
163 "S 50 04.064 E 014 22.645",
164 "S50 4.064 E14 22.645",
165 "50° 04.064' S, 014° 22.645' E"
167 latlon_check code, -50.06773, 14.37742
172 # Test identification of lat/lon pairs using S/W with degrees/mins
173 def test_identify_latlon_sw_dm
175 "S 50° 04.064 W 014° 22.645",
176 "S 50° 04.064' W 014° 22.645",
177 "S 50° 04.064', W 014° 22.645'",
178 "S50° 04.064 W14° 22.645",
179 "S 50 04.064 W 014 22.645",
180 "S50 4.064 W14 22.645",
181 "50° 04.064' S, 014° 22.645' W"
183 latlon_check code, -50.06773, -14.37742
188 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
189 def test_identify_latlon_ne_dms
191 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
192 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
193 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
194 "N50 4 03.828 E14 22 38.712",
195 "N50 4 03.828, E14 22 38.712",
196 "50°4'3.828\"N 14°22'38.712\"E"
198 latlon_check code, 50.06773, 14.37742
203 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
204 def test_identify_latlon_nw_dms
206 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
207 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
208 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
209 "N50 4 03.828 W14 22 38.712",
210 "N50 4 03.828, W14 22 38.712",
211 "50°4'3.828\"N 14°22'38.712\"W"
213 latlon_check code, 50.06773, -14.37742
218 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
219 def test_identify_latlon_se_dms
221 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
222 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
223 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
224 "S50 4 03.828 E14 22 38.712",
225 "S50 4 03.828, E14 22 38.712",
226 "50°4'3.828\"S 14°22'38.712\"E"
228 latlon_check code, -50.06773, 14.37742
233 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
234 def test_identify_latlon_sw_dms
236 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
237 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
238 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
239 "S50 4 03.828 W14 22 38.712",
240 "S50 4 03.828, W14 22 38.712",
241 "50°4'3.828\"S 14°22'38.712\"W"
243 latlon_check code, -50.06773, -14.37742
248 # Test identification of US zipcodes
249 def test_identify_us_postcode
254 post :search, :query => code
255 assert_response :success
256 assert_equal %w(us_postcode osm_nominatim), assigns(:sources)
261 # Test identification of UK postcodes using examples from
262 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
263 def test_identify_uk_postcode
272 search_check code, %w(uk_postcode osm_nominatim)
277 # Test identification of Canadian postcodes
278 def test_identify_ca_postcode
279 search_check "A1B 2C3", %w(ca_postcode osm_nominatim)
283 # Test identification fall through to the default case
284 def test_identify_default
285 search_check "foo bar baz", %w(osm_nominatim geonames)
289 # Test the builtin latitude+longitude search
290 def test_search_latlon
291 xhr :get, :search_latlon, :lat => 1.23, :lon => 4.56, :zoom => 16
292 results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
294 xhr :get, :search_latlon, :lat => -91.23, :lon => 4.56, :zoom => 16
295 results_check_error "Latitude -91.23 out of range"
297 xhr :get, :search_latlon, :lat => 91.23, :lon => 4.56, :zoom => 16
298 results_check_error "Latitude 91.23 out of range"
300 xhr :get, :search_latlon, :lat => 1.23, :lon => -180.23, :zoom => 16
301 results_check_error "Longitude -180.23 out of range"
303 xhr :get, :search_latlon, :lat => 1.23, :lon => 180.23, :zoom => 16
304 results_check_error "Longitude 180.23 out of range"
308 # Test the US postcode search
309 def test_search_us_postcode
310 with_http_stubs "geocoder_us" do
311 xhr :get, :search_us_postcode,
312 :query => "90210", :zoom => 10,
313 :minlon => -0.559, :minlat => 51.217,
314 :maxlon => 0.836, :maxlat => 51.766
315 results_check :prefix => "Beverly Hills, CA,", :name => "90210",
316 :lat => 34.088808, :lon => -118.40612
318 xhr :get, :search_us_postcode,
319 :query => "00000", :zoom => 10,
320 :minlon => -0.559, :minlat => 51.217,
321 :maxlon => 0.836, :maxlat => 51.766
327 # Test the UK postcode search
328 def test_search_uk_postcode
329 with_http_stubs "npemap" do
330 xhr :get, :search_uk_postcode,
331 :query => "CV4 7AL", :zoom => 10,
332 :minlon => -0.559, :minlat => 51.217,
333 :maxlon => 0.836, :maxlat => 51.766
334 results_check :name => "CV4 7AL", :lat => 52.381748701968, :lon => -1.56176420939232
336 xhr :get, :search_uk_postcode,
337 :query => "XX9 9XX", :zoom => 10,
338 :minlon => -0.559, :minlat => 51.217,
339 :maxlon => 0.836, :maxlat => 51.766
345 # Test the Canadian postcode search
346 def test_search_ca_postcode
347 with_http_stubs "geocoder_ca" do
348 xhr :get, :search_ca_postcode,
349 :query => "A1B 2C3", :zoom => 10,
350 :minlon => -0.559, :minlat => 51.217,
351 :maxlon => 0.836, :maxlat => 51.766
352 results_check :name => "A1B 2C3", :lat => "47.172520", :lon => "-55.440515"
354 xhr :get, :search_ca_postcode,
355 :query => "k1a 0b1", :zoom => 10,
356 :minlon => -0.559, :minlat => 51.217,
357 :maxlon => 0.836, :maxlat => 51.766
358 results_check :name => "K1A 0B1", :lat => "45.375437", :lon => "-75.691041"
360 xhr :get, :search_ca_postcode,
361 :query => "Q0Q 0Q0", :zoom => 10,
362 :minlon => -0.559, :minlat => 51.217,
363 :maxlon => 0.836, :maxlat => 51.766
369 # Test the nominatim forward search
370 def test_search_osm_nominatim
371 with_http_stubs "nominatim" do
372 xhr :get, :search_osm_nominatim,
373 :query => "Hoddesdon", :zoom => 10,
374 :minlon => -0.559, :minlat => 51.217,
375 :maxlon => 0.836, :maxlat => 51.766
376 results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
377 "min-lat" => 51.7216709, "max-lat" => 51.8016709,
378 "min-lon" => -0.0512898, "max-lon" => 0.0287102,
379 "type" => "node", "id" => 18007599
381 xhr :get, :search_osm_nominatim,
382 :query => "Broxbourne", :zoom => 10,
383 :minlon => -0.559, :minlat => 51.217,
384 :maxlon => 0.836, :maxlat => 51.766
385 results_check({ "prefix" => "Suburb",
386 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
387 "min-lat" => 51.7265723, "max-lat" => 51.7665723,
388 "min-lon" => -0.0390782, "max-lon" => 0.0009218,
389 "type" => "node", "id" => 28825933 },
390 { "prefix" => "City Boundary",
391 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
392 "min-lat" => 51.6808751, "max-lat" => 51.7806237,
393 "min-lon" => -0.114204, "max-lon" => 0.0145267,
394 "type" => "relation", "id" => 2677978 },
395 { "prefix" => "Railway Station",
396 "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
397 "min-lat" => 51.7418469, "max-lat" => 51.7518469,
398 "min-lon" => -0.0156773, "max-lon" => -0.0056773,
399 "type" => "node", "id" => 17044599 })
404 # Test the geonames forward search
405 def test_search_geonames
406 with_http_stubs "geonames" do
407 xhr :get, :search_geonames,
408 :query => "Hoddesdon", :zoom => 10,
409 :minlon => -0.559, :minlat => 51.217,
410 :maxlon => 0.836, :maxlat => 51.766
411 results_check :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144
413 xhr :get, :search_geonames,
414 :query => "Broxbourne", :zoom => 10,
415 :minlon => -0.559, :minlat => 51.217,
416 :maxlon => 0.836, :maxlat => 51.766
417 results_check({ :name => "Broxbourne", :lat => 51.74712, :lon => -0.01923 },
418 { :name => "Broxbourne District", :lat => 51.73026, :lon => -0.04821 },
419 { :name => "Cheshunt", :lat => 51.70791, :lon => -0.03739 },
420 { :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144 },
421 { :name => "Waltham Cross", :lat => 51.68905, :lon => -0.0333 },
422 { :name => "Goffs Oak", :lat => 51.71015, :lon => -0.0872 },
423 { :name => "Wormley", :lat => 51.7324, :lon => -0.0242 },
424 { :name => "Broxbourne", :lat => -27.50314, :lon => 151.378 },
425 { :name => "Lee Valley White Water Centre", :lat => 51.68814, :lon => -0.01682 },
426 { :name => "Cheshunt Railway Station", :lat => 51.703, :lon => -0.024 },
427 { :name => "Theobalds Grove Railway Station", :lat => 51.692, :lon => -0.035 },
428 { :name => "Waltham Cross Railway Station", :lat => 51.685, :lon => -0.027 },
429 { :name => "Rye House Station", :lat => 51.76938, :lon => 0.00562 },
430 { :name => "Broxbourne Station", :lat => 51.74697, :lon => -0.01105 },
431 { :name => "Broxbornebury Park", :lat => 51.75252, :lon => -0.03839 },
432 { :name => "Marriott Cheshunt", :lat => 51.7208, :lon => -0.0324 },
433 { :name => "Cheshunt Community Hospital", :lat => 51.68396, :lon => -0.03951 })
438 # Test the nominatim reverse search
439 def test_search_osm_nominatim_reverse
440 with_http_stubs "nominatim" do
441 xhr :get, :search_osm_nominatim_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 15
442 results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
443 :lat => 51.7465723, :lon => -0.0190782,
444 :type => "node", :id => 28825933, :zoom => 15
446 xhr :get, :search_osm_nominatim_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 17
447 results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
448 :lat => 51.7634883, :lon => -0.0088373,
449 :type => "way", :id => 3489841, :zoom => 17
454 # Test the geonames reverse search
455 def test_search_geonames_reverse
456 with_http_stubs "geonames" do
457 xhr :get, :search_geonames_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 15
458 results_check :name => "England", :suffix => ", United Kingdom",
459 :lat => 51.7632, :lon => -0.0076
465 def latlon_check(query, lat, lon)
466 get :search, :query => query
467 assert_response :success
468 assert_template :search
469 assert_template :layout => "map"
470 assert_equal %w(latlon osm_nominatim_reverse geonames_reverse), assigns(:sources)
471 assert_nil @controller.params[:query]
472 assert_in_delta lat, @controller.params[:lat]
473 assert_in_delta lon, @controller.params[:lon]
475 xhr :get, :search, :query => query
476 assert_response :success
477 assert_template :search
478 assert_template :layout => "xhr"
479 assert_equal %w(latlon osm_nominatim_reverse geonames_reverse), assigns(:sources)
480 assert_nil @controller.params[:query]
481 assert_in_delta lat, @controller.params[:lat]
482 assert_in_delta lon, @controller.params[:lon]
485 def search_check(query, sources)
486 get :search, :query => query
487 assert_response :success
488 assert_template :search
489 assert_template :layout => "map"
490 assert_equal sources, assigns(:sources)
492 xhr :get, :search, :query => query
493 assert_response :success
494 assert_template :search
495 assert_template :layout => "xhr"
496 assert_equal sources, assigns(:sources)
499 def results_check(*results)
500 assert_response :success
501 assert_template :results
502 assert_template :layout => nil
504 assert_select "ul.results-list", 0
506 assert_select "ul.results-list", 1 do
507 assert_select "p.search_results_entry", results.count
509 results.each do |result|
510 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join("")
511 assert_select "p.search_results_entry a.set_position#{attrs}", result[:name]
517 def results_check_error(error)
518 assert_response :success
519 assert_template :error
520 assert_template :layout => nil
521 assert_select "p.search_results_error", error