2 require "geocoder_controller"
4 class GeocoderControllerTest < ActionController::TestCase
6 # test all routes which lead to this controller
9 { :path => "/search", :method => :get },
10 { :controller => "geocoder", :action => "search" }
13 { :path => "/geocoder/search_latlon", :method => :get },
14 { :controller => "geocoder", :action => "search_latlon" }
17 { :path => "/geocoder/search_ca_postcode", :method => :get },
18 { :controller => "geocoder", :action => "search_ca_postcode" }
21 { :path => "/geocoder/search_osm_nominatim", :method => :get },
22 { :controller => "geocoder", :action => "search_osm_nominatim" }
25 { :path => "/geocoder/search_geonames", :method => :get },
26 { :controller => "geocoder", :action => "search_geonames" }
29 { :path => "/geocoder/search_osm_nominatim_reverse", :method => :get },
30 { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
33 { :path => "/geocoder/search_geonames_reverse", :method => :get },
34 { :controller => "geocoder", :action => "search_geonames_reverse" }
39 # Test identification with no arguments
40 def test_identify_error
42 assert_response :bad_request
44 get :search, :xhr => true
45 assert_response :bad_request
49 # Test identification of basic lat/lon pairs
50 def test_identify_latlon_basic
54 "+50.06773 +14.37742",
55 "+50.06773, +14.37742"
57 latlon_check code, 50.06773, 14.37742
62 # Test identification of lat/lon pairs using N/E with degrees
63 def test_identify_latlon_ne_d
65 "N50.06773 E14.37742",
66 "N50.06773, E14.37742",
67 "50.06773N 14.37742E",
68 "50.06773N, 14.37742E"
70 latlon_check code, 50.06773, 14.37742
75 # Test identification of lat/lon pairs using N/W with degrees
76 def test_identify_latlon_nw_d
78 "N50.06773 W14.37742",
79 "N50.06773, W14.37742",
80 "50.06773N 14.37742W",
81 "50.06773N, 14.37742W"
83 latlon_check code, 50.06773, -14.37742
88 # Test identification of lat/lon pairs using S/E with degrees
89 def test_identify_latlon_se_d
91 "S50.06773 E14.37742",
92 "S50.06773, E14.37742",
93 "50.06773S 14.37742E",
94 "50.06773S, 14.37742E"
96 latlon_check code, -50.06773, 14.37742
101 # Test identification of lat/lon pairs using S/W with degrees
102 def test_identify_latlon_sw_d
104 "S50.06773 W14.37742",
105 "S50.06773, W14.37742",
106 "50.06773S 14.37742W",
107 "50.06773S, 14.37742W"
109 latlon_check code, -50.06773, -14.37742
114 # Test identification of lat/lon pairs using N/E with degrees/mins
115 def test_identify_latlon_ne_dm
117 "N 50° 04.064 E 014° 22.645",
118 "N 50° 04.064' E 014° 22.645",
119 "N 50° 04.064', E 014° 22.645'",
120 "N50° 04.064 E14° 22.645",
121 "N 50 04.064 E 014 22.645",
122 "N50 4.064 E14 22.645",
123 "50° 04.064' N, 014° 22.645' E"
125 latlon_check code, 50.06773, 14.37742
130 # Test identification of lat/lon pairs using N/W with degrees/mins
131 def test_identify_latlon_nw_dm
133 "N 50° 04.064 W 014° 22.645",
134 "N 50° 04.064' W 014° 22.645",
135 "N 50° 04.064', W 014° 22.645'",
136 "N50° 04.064 W14° 22.645",
137 "N 50 04.064 W 014 22.645",
138 "N50 4.064 W14 22.645",
139 "50° 04.064' N, 014° 22.645' W"
141 latlon_check code, 50.06773, -14.37742
146 # Test identification of lat/lon pairs using S/E with degrees/mins
147 def test_identify_latlon_se_dm
149 "S 50° 04.064 E 014° 22.645",
150 "S 50° 04.064' E 014° 22.645",
151 "S 50° 04.064', E 014° 22.645'",
152 "S50° 04.064 E14° 22.645",
153 "S 50 04.064 E 014 22.645",
154 "S50 4.064 E14 22.645",
155 "50° 04.064' S, 014° 22.645' E"
157 latlon_check code, -50.06773, 14.37742
162 # Test identification of lat/lon pairs using S/W with degrees/mins
163 def test_identify_latlon_sw_dm
165 "S 50° 04.064 W 014° 22.645",
166 "S 50° 04.064' W 014° 22.645",
167 "S 50° 04.064', W 014° 22.645'",
168 "S50° 04.064 W14° 22.645",
169 "S 50 04.064 W 014 22.645",
170 "S50 4.064 W14 22.645",
171 "50° 04.064' S, 014° 22.645' W"
173 latlon_check code, -50.06773, -14.37742
178 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
179 def test_identify_latlon_ne_dms
181 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
182 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
183 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
184 "N50 4 03.828 E14 22 38.712",
185 "N50 4 03.828, E14 22 38.712",
186 "50°4'3.828\"N 14°22'38.712\"E"
188 latlon_check code, 50.06773, 14.37742
193 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
194 def test_identify_latlon_nw_dms
196 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
197 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
198 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
199 "N50 4 03.828 W14 22 38.712",
200 "N50 4 03.828, W14 22 38.712",
201 "50°4'3.828\"N 14°22'38.712\"W"
203 latlon_check code, 50.06773, -14.37742
208 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
209 def test_identify_latlon_se_dms
211 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
212 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
213 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
214 "S50 4 03.828 E14 22 38.712",
215 "S50 4 03.828, E14 22 38.712",
216 "50°4'3.828\"S 14°22'38.712\"E"
218 latlon_check code, -50.06773, 14.37742
223 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
224 def test_identify_latlon_sw_dms
226 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
227 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
228 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
229 "S50 4 03.828 W14 22 38.712",
230 "S50 4 03.828, W14 22 38.712",
231 "50°4'3.828\"S 14°22'38.712\"W"
233 latlon_check code, -50.06773, -14.37742
238 # Test identification of US zipcodes
239 def test_identify_us_postcode
244 post :search, :params => { :query => code }
245 assert_response :success
246 assert_equal %w[osm_nominatim], assigns(:sources)
251 # Test identification of UK postcodes using examples from
252 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
253 def test_identify_uk_postcode
262 search_check code, %w[osm_nominatim]
267 # Test identification of Canadian postcodes
268 def test_identify_ca_postcode
269 search_check "A1B 2C3", %w[ca_postcode osm_nominatim]
273 # Test identification fall through to the default case
274 def test_identify_default
275 search_check "foo bar baz", %w[osm_nominatim geonames]
279 # Test the builtin latitude+longitude search
280 def test_search_latlon
281 get :search_latlon, :params => { :lat => 1.23, :lon => 4.56, :zoom => 16 }, :xhr => true
282 results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
284 get :search_latlon, :params => { :lat => -91.23, :lon => 4.56, :zoom => 16 }, :xhr => true
285 results_check_error "Latitude -91.23 out of range"
287 get :search_latlon, :params => { :lat => 91.23, :lon => 4.56, :zoom => 16 }, :xhr => true
288 results_check_error "Latitude 91.23 out of range"
290 get :search_latlon, :params => { :lat => 1.23, :lon => -180.23, :zoom => 16 }, :xhr => true
291 results_check_error "Longitude -180.23 out of range"
293 get :search_latlon, :params => { :lat => 1.23, :lon => 180.23, :zoom => 16 }, :xhr => true
294 results_check_error "Longitude 180.23 out of range"
297 def test_search_latlon_digits
298 get :search_latlon, :params => { :lat => 1.23, :lon => 4.56, :zoom => 16, :latlon_digits => true }, :xhr => true
299 results_check({ :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 },
300 { :name => "4.56, 1.23", :lat => 4.56, :lon => 1.23, :zoom => 16 })
302 get :search_latlon, :params => { :lat => -91.23, :lon => 4.56, :zoom => 16, :latlon_digits => true }, :xhr => true
303 results_check :name => "4.56, -91.23", :lat => 4.56, :lon => -91.23, :zoom => 16
305 get :search_latlon, :params => { :lat => -1.23, :lon => 170.23, :zoom => 16, :latlon_digits => true }, :xhr => true
306 results_check :name => "-1.23, 170.23", :lat => -1.23, :lon => 170.23, :zoom => 16
308 get :search_latlon, :params => { :lat => 91.23, :lon => 94.56, :zoom => 16, :latlon_digits => true }, :xhr => true
309 results_check_error "Latitude or longitude are out of range"
311 get :search_latlon, :params => { :lat => -91.23, :lon => -94.56, :zoom => 16, :latlon_digits => true }, :xhr => true
312 results_check_error "Latitude or longitude are out of range"
314 get :search_latlon, :params => { :lat => 1.23, :lon => -180.23, :zoom => 16, :latlon_digits => true }, :xhr => true
315 results_check_error "Latitude or longitude are out of range"
317 get :search_latlon, :params => { :lat => 1.23, :lon => 180.23, :zoom => 16, :latlon_digits => true }, :xhr => true
318 results_check_error "Latitude or longitude are out of range"
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
430 get :search_osm_nominatim_reverse, :xhr => true,
431 :params => { :lat => 13.7709, :lon => 100.50507, :zoom => 19 }
432 results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
433 :lat => 13.7708691, :lon => 100.505073233221,
434 :type => "way", :id => 542901374, :zoom => 19
439 # Test the geonames reverse search
440 def test_search_geonames_reverse
441 with_http_stubs "geonames" do
442 get :search_geonames_reverse, :xhr => true,
443 :params => { :lat => 51.7632, :lon => -0.0076, :zoom => 15 }
444 results_check :name => "England", :suffix => ", United Kingdom",
445 :lat => 51.7632, :lon => -0.0076
451 def latlon_check(query, lat, lon)
452 get :search, :params => { :query => query }
453 assert_response :success
454 assert_template :search
455 assert_template :layout => "map"
456 assert_equal %w[latlon osm_nominatim_reverse geonames_reverse], assigns(:sources)
457 assert_nil @controller.params[:query]
458 assert_in_delta lat, @controller.params[:lat]
459 assert_in_delta lon, @controller.params[:lon]
461 get :search, :params => { :query => query }, :xhr => true
462 assert_response :success
463 assert_template :search
464 assert_template :layout => "xhr"
465 assert_equal %w[latlon osm_nominatim_reverse geonames_reverse], assigns(:sources)
466 assert_nil @controller.params[:query]
467 assert_in_delta lat, @controller.params[:lat]
468 assert_in_delta lon, @controller.params[:lon]
471 def search_check(query, sources)
472 get :search, :params => { :query => query }
473 assert_response :success
474 assert_template :search
475 assert_template :layout => "map"
476 assert_equal sources, assigns(:sources)
478 get :search, :params => { :query => query }, :xhr => true
479 assert_response :success
480 assert_template :search
481 assert_template :layout => "xhr"
482 assert_equal sources, assigns(:sources)
485 def results_check(*results)
486 assert_response :success
487 assert_template :results
488 assert_template :layout => nil
490 assert_select "ul.results-list", 0
492 assert_select "ul.results-list", 1 do
493 assert_select "p.search_results_entry", results.count
495 results.each do |result|
496 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join("")
497 assert_select "p.search_results_entry a.set_position#{attrs}", result[:name]
503 def results_check_error(error)
504 assert_response :success
505 assert_template :error
506 assert_template :layout => nil
507 assert_select "p.search_results_error", error