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_uk_postcode", :method => :get },
20 { :controller => "geocoder", :action => "search_uk_postcode" }
23 { :path => "/geocoder/search_ca_postcode", :method => :get },
24 { :controller => "geocoder", :action => "search_ca_postcode" }
27 { :path => "/geocoder/search_osm_nominatim", :method => :get },
28 { :controller => "geocoder", :action => "search_osm_nominatim" }
31 { :path => "/geocoder/search_geonames", :method => :get },
32 { :controller => "geocoder", :action => "search_geonames" }
35 { :path => "/geocoder/search_osm_nominatim_reverse", :method => :get },
36 { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
39 { :path => "/geocoder/search_geonames_reverse", :method => :get },
40 { :controller => "geocoder", :action => "search_geonames_reverse" }
45 # Test identification with no arguments
46 def test_identify_error
48 assert_response :bad_request
50 get :search, :xhr => true
51 assert_response :bad_request
55 # Test identification of basic lat/lon pairs
56 def test_identify_latlon_basic
60 "+50.06773 +14.37742",
61 "+50.06773, +14.37742"
63 latlon_check code, 50.06773, 14.37742
68 # Test identification of lat/lon pairs using N/E with degrees
69 def test_identify_latlon_ne_d
71 "N50.06773 E14.37742",
72 "N50.06773, E14.37742",
73 "50.06773N 14.37742E",
74 "50.06773N, 14.37742E"
76 latlon_check code, 50.06773, 14.37742
81 # Test identification of lat/lon pairs using N/W with degrees
82 def test_identify_latlon_nw_d
84 "N50.06773 W14.37742",
85 "N50.06773, W14.37742",
86 "50.06773N 14.37742W",
87 "50.06773N, 14.37742W"
89 latlon_check code, 50.06773, -14.37742
94 # Test identification of lat/lon pairs using S/E with degrees
95 def test_identify_latlon_se_d
97 "S50.06773 E14.37742",
98 "S50.06773, E14.37742",
99 "50.06773S 14.37742E",
100 "50.06773S, 14.37742E"
102 latlon_check code, -50.06773, 14.37742
107 # Test identification of lat/lon pairs using S/W with degrees
108 def test_identify_latlon_sw_d
110 "S50.06773 W14.37742",
111 "S50.06773, W14.37742",
112 "50.06773S 14.37742W",
113 "50.06773S, 14.37742W"
115 latlon_check code, -50.06773, -14.37742
120 # Test identification of lat/lon pairs using N/E with degrees/mins
121 def test_identify_latlon_ne_dm
123 "N 50° 04.064 E 014° 22.645",
124 "N 50° 04.064' E 014° 22.645",
125 "N 50° 04.064', E 014° 22.645'",
126 "N50° 04.064 E14° 22.645",
127 "N 50 04.064 E 014 22.645",
128 "N50 4.064 E14 22.645",
129 "50° 04.064' N, 014° 22.645' E"
131 latlon_check code, 50.06773, 14.37742
136 # Test identification of lat/lon pairs using N/W with degrees/mins
137 def test_identify_latlon_nw_dm
139 "N 50° 04.064 W 014° 22.645",
140 "N 50° 04.064' W 014° 22.645",
141 "N 50° 04.064', W 014° 22.645'",
142 "N50° 04.064 W14° 22.645",
143 "N 50 04.064 W 014 22.645",
144 "N50 4.064 W14 22.645",
145 "50° 04.064' N, 014° 22.645' W"
147 latlon_check code, 50.06773, -14.37742
152 # Test identification of lat/lon pairs using S/E with degrees/mins
153 def test_identify_latlon_se_dm
155 "S 50° 04.064 E 014° 22.645",
156 "S 50° 04.064' E 014° 22.645",
157 "S 50° 04.064', E 014° 22.645'",
158 "S50° 04.064 E14° 22.645",
159 "S 50 04.064 E 014 22.645",
160 "S50 4.064 E14 22.645",
161 "50° 04.064' S, 014° 22.645' E"
163 latlon_check code, -50.06773, 14.37742
168 # Test identification of lat/lon pairs using S/W with degrees/mins
169 def test_identify_latlon_sw_dm
171 "S 50° 04.064 W 014° 22.645",
172 "S 50° 04.064' W 014° 22.645",
173 "S 50° 04.064', W 014° 22.645'",
174 "S50° 04.064 W14° 22.645",
175 "S 50 04.064 W 014 22.645",
176 "S50 4.064 W14 22.645",
177 "50° 04.064' S, 014° 22.645' W"
179 latlon_check code, -50.06773, -14.37742
184 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
185 def test_identify_latlon_ne_dms
187 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
188 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
189 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
190 "N50 4 03.828 E14 22 38.712",
191 "N50 4 03.828, E14 22 38.712",
192 "50°4'3.828\"N 14°22'38.712\"E"
194 latlon_check code, 50.06773, 14.37742
199 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
200 def test_identify_latlon_nw_dms
202 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
203 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
204 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
205 "N50 4 03.828 W14 22 38.712",
206 "N50 4 03.828, W14 22 38.712",
207 "50°4'3.828\"N 14°22'38.712\"W"
209 latlon_check code, 50.06773, -14.37742
214 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
215 def test_identify_latlon_se_dms
217 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
218 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
219 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
220 "S50 4 03.828 E14 22 38.712",
221 "S50 4 03.828, E14 22 38.712",
222 "50°4'3.828\"S 14°22'38.712\"E"
224 latlon_check code, -50.06773, 14.37742
229 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
230 def test_identify_latlon_sw_dms
232 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
233 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
234 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
235 "S50 4 03.828 W14 22 38.712",
236 "S50 4 03.828, W14 22 38.712",
237 "50°4'3.828\"S 14°22'38.712\"W"
239 latlon_check code, -50.06773, -14.37742
244 # Test identification of US zipcodes
245 def test_identify_us_postcode
250 post :search, :params => { :query => code }
251 assert_response :success
252 assert_equal %w[osm_nominatim], assigns(:sources)
257 # Test identification of UK postcodes using examples from
258 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
259 def test_identify_uk_postcode
268 search_check code, %w[uk_postcode osm_nominatim]
273 # Test identification of Canadian postcodes
274 def test_identify_ca_postcode
275 search_check "A1B 2C3", %w[ca_postcode osm_nominatim]
279 # Test identification fall through to the default case
280 def test_identify_default
281 search_check "foo bar baz", %w[osm_nominatim geonames]
285 # Test the builtin latitude+longitude search
286 def test_search_latlon
287 get :search_latlon, :params => { :lat => 1.23, :lon => 4.56, :zoom => 16 }, :xhr => true
288 results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
290 get :search_latlon, :params => { :lat => -91.23, :lon => 4.56, :zoom => 16 }, :xhr => true
291 results_check_error "Latitude -91.23 out of range"
293 get :search_latlon, :params => { :lat => 91.23, :lon => 4.56, :zoom => 16 }, :xhr => true
294 results_check_error "Latitude 91.23 out of range"
296 get :search_latlon, :params => { :lat => 1.23, :lon => -180.23, :zoom => 16 }, :xhr => true
297 results_check_error "Longitude -180.23 out of range"
299 get :search_latlon, :params => { :lat => 1.23, :lon => 180.23, :zoom => 16 }, :xhr => true
300 results_check_error "Longitude 180.23 out of range"
304 # Test the UK postcode search
305 def test_search_uk_postcode
306 with_http_stubs "npemap" do
307 get :search_uk_postcode, :xhr => true,
308 :params => { :query => "CV4 7AL", :zoom => 10,
309 :minlon => -0.559, :minlat => 51.217,
310 :maxlon => 0.836, :maxlat => 51.766 }
311 results_check :name => "CV4 7AL", :lat => 52.381748701968, :lon => -1.56176420939232
313 get :search_uk_postcode, :xhr => true,
314 :params => { :query => "XX9 9XX", :zoom => 10,
315 :minlon => -0.559, :minlat => 51.217,
316 :maxlon => 0.836, :maxlat => 51.766 }
322 # Test the Canadian postcode search
323 def test_search_ca_postcode
324 with_http_stubs "geocoder_ca" do
325 get :search_ca_postcode, :xhr => true,
326 :params => { :query => "A1B 2C3", :zoom => 10,
327 :minlon => -0.559, :minlat => 51.217,
328 :maxlon => 0.836, :maxlat => 51.766 }
329 results_check :name => "A1B 2C3", :lat => "47.172520", :lon => "-55.440515"
331 get :search_ca_postcode, :xhr => true,
332 :params => { :query => "k1a 0b1", :zoom => 10,
333 :minlon => -0.559, :minlat => 51.217,
334 :maxlon => 0.836, :maxlat => 51.766 }
335 results_check :name => "K1A 0B1", :lat => "45.375437", :lon => "-75.691041"
337 get :search_ca_postcode, :xhr => true,
338 :params => { :query => "Q0Q 0Q0", :zoom => 10,
339 :minlon => -0.559, :minlat => 51.217,
340 :maxlon => 0.836, :maxlat => 51.766 }
346 # Test the nominatim forward search
347 def test_search_osm_nominatim
348 with_http_stubs "nominatim" do
349 get :search_osm_nominatim, :xhr => true,
350 :params => { :query => "Hoddesdon", :zoom => 10,
351 :minlon => -0.559, :minlat => 51.217,
352 :maxlon => 0.836, :maxlat => 51.766 }
353 results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
354 "min-lat" => 51.7216709, "max-lat" => 51.8016709,
355 "min-lon" => -0.0512898, "max-lon" => 0.0287102,
356 "type" => "node", "id" => 18007599
358 get :search_osm_nominatim, :xhr => true,
359 :params => { :query => "Broxbourne", :zoom => 10,
360 :minlon => -0.559, :minlat => 51.217,
361 :maxlon => 0.836, :maxlat => 51.766 }
362 results_check({ "prefix" => "Suburb",
363 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
364 "min-lat" => 51.7265723, "max-lat" => 51.7665723,
365 "min-lon" => -0.0390782, "max-lon" => 0.0009218,
366 "type" => "node", "id" => 28825933 },
367 { "prefix" => "Village",
368 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
369 "min-lat" => 51.6808751, "max-lat" => 51.7806237,
370 "min-lon" => -0.114204, "max-lon" => 0.0145267,
371 "type" => "relation", "id" => 2677978 },
372 { "prefix" => "Railway Station",
373 "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
374 "min-lat" => 51.7418469, "max-lat" => 51.7518469,
375 "min-lon" => -0.0156773, "max-lon" => -0.0056773,
376 "type" => "node", "id" => 17044599 })
381 # Test the geonames forward search
382 def test_search_geonames
383 with_http_stubs "geonames" do
384 get :search_geonames, :xhr => true,
385 :params => { :query => "Hoddesdon", :zoom => 10,
386 :minlon => -0.559, :minlat => 51.217,
387 :maxlon => 0.836, :maxlat => 51.766 }
388 results_check :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144
390 get :search_geonames, :xhr => true,
391 :params => { :query => "Broxbourne", :zoom => 10,
392 :minlon => -0.559, :minlat => 51.217,
393 :maxlon => 0.836, :maxlat => 51.766 }
394 results_check({ :name => "Broxbourne", :lat => 51.74712, :lon => -0.01923 },
395 { :name => "Broxbourne District", :lat => 51.73026, :lon => -0.04821 },
396 { :name => "Cheshunt", :lat => 51.70791, :lon => -0.03739 },
397 { :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144 },
398 { :name => "Waltham Cross", :lat => 51.68905, :lon => -0.0333 },
399 { :name => "Goffs Oak", :lat => 51.71015, :lon => -0.0872 },
400 { :name => "Wormley", :lat => 51.7324, :lon => -0.0242 },
401 { :name => "Broxbourne", :lat => -27.50314, :lon => 151.378 },
402 { :name => "Lee Valley White Water Centre", :lat => 51.68814, :lon => -0.01682 },
403 { :name => "Cheshunt Railway Station", :lat => 51.703, :lon => -0.024 },
404 { :name => "Theobalds Grove Railway Station", :lat => 51.692, :lon => -0.035 },
405 { :name => "Waltham Cross Railway Station", :lat => 51.685, :lon => -0.027 },
406 { :name => "Rye House Station", :lat => 51.76938, :lon => 0.00562 },
407 { :name => "Broxbourne Station", :lat => 51.74697, :lon => -0.01105 },
408 { :name => "Broxbornebury Park", :lat => 51.75252, :lon => -0.03839 },
409 { :name => "Marriott Cheshunt", :lat => 51.7208, :lon => -0.0324 },
410 { :name => "Cheshunt Community Hospital", :lat => 51.68396, :lon => -0.03951 })
415 # Test the nominatim reverse search
416 def test_search_osm_nominatim_reverse
417 with_http_stubs "nominatim" do
418 get :search_osm_nominatim_reverse, :xhr => true,
419 :params => { :lat => 51.7632, :lon => -0.0076, :zoom => 15 }
420 results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
421 :lat => 51.7465723, :lon => -0.0190782,
422 :type => "node", :id => 28825933, :zoom => 15
424 get :search_osm_nominatim_reverse, :xhr => true,
425 :params => { :lat => 51.7632, :lon => -0.0076, :zoom => 17 }
426 results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
427 :lat => 51.7634883, :lon => -0.0088373,
428 :type => "way", :id => 3489841, :zoom => 17
433 # Test the geonames reverse search
434 def test_search_geonames_reverse
435 with_http_stubs "geonames" do
436 get :search_geonames_reverse, :xhr => true,
437 :params => { :lat => 51.7632, :lon => -0.0076, :zoom => 15 }
438 results_check :name => "England", :suffix => ", United Kingdom",
439 :lat => 51.7632, :lon => -0.0076
445 def latlon_check(query, lat, lon)
446 get :search, :params => { :query => query }
447 assert_response :success
448 assert_template :search
449 assert_template :layout => "map"
450 assert_equal %w[latlon osm_nominatim_reverse geonames_reverse], assigns(:sources)
451 assert_nil @controller.params[:query]
452 assert_in_delta lat, @controller.params[:lat]
453 assert_in_delta lon, @controller.params[:lon]
455 get :search, :params => { :query => query }, :xhr => true
456 assert_response :success
457 assert_template :search
458 assert_template :layout => "xhr"
459 assert_equal %w[latlon osm_nominatim_reverse geonames_reverse], assigns(:sources)
460 assert_nil @controller.params[:query]
461 assert_in_delta lat, @controller.params[:lat]
462 assert_in_delta lon, @controller.params[:lon]
465 def search_check(query, sources)
466 get :search, :params => { :query => query }
467 assert_response :success
468 assert_template :search
469 assert_template :layout => "map"
470 assert_equal sources, assigns(:sources)
472 get :search, :params => { :query => query }, :xhr => true
473 assert_response :success
474 assert_template :search
475 assert_template :layout => "xhr"
476 assert_equal sources, assigns(:sources)
479 def results_check(*results)
480 assert_response :success
481 assert_template :results
482 assert_template :layout => nil
484 assert_select "ul.results-list", 0
486 assert_select "ul.results-list", 1 do
487 assert_select "p.search_results_entry", results.count
489 results.each do |result|
490 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join("")
491 assert_select "p.search_results_entry a.set_position#{attrs}", result[:name]
497 def results_check_error(error)
498 assert_response :success
499 assert_template :error
500 assert_template :layout => nil
501 assert_select "p.search_results_error", error