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_uk_postcode", :method => :get },
18 { :controller => "geocoder", :action => "search_uk_postcode" }
21 { :path => "/geocoder/search_ca_postcode", :method => :get },
22 { :controller => "geocoder", :action => "search_ca_postcode" }
25 { :path => "/geocoder/search_osm_nominatim", :method => :get },
26 { :controller => "geocoder", :action => "search_osm_nominatim" }
29 { :path => "/geocoder/search_geonames", :method => :get },
30 { :controller => "geocoder", :action => "search_geonames" }
33 { :path => "/geocoder/search_osm_nominatim_reverse", :method => :get },
34 { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
37 { :path => "/geocoder/search_geonames_reverse", :method => :get },
38 { :controller => "geocoder", :action => "search_geonames_reverse" }
43 # Test identification with no arguments
44 def test_identify_error
46 assert_response :bad_request
48 get :search, :xhr => true
49 assert_response :bad_request
53 # Test identification of basic lat/lon pairs
54 def test_identify_latlon_basic
58 "+50.06773 +14.37742",
59 "+50.06773, +14.37742"
61 latlon_check code, 50.06773, 14.37742
66 # Test identification of lat/lon pairs using N/E with degrees
67 def test_identify_latlon_ne_d
69 "N50.06773 E14.37742",
70 "N50.06773, E14.37742",
71 "50.06773N 14.37742E",
72 "50.06773N, 14.37742E"
74 latlon_check code, 50.06773, 14.37742
79 # Test identification of lat/lon pairs using N/W with degrees
80 def test_identify_latlon_nw_d
82 "N50.06773 W14.37742",
83 "N50.06773, W14.37742",
84 "50.06773N 14.37742W",
85 "50.06773N, 14.37742W"
87 latlon_check code, 50.06773, -14.37742
92 # Test identification of lat/lon pairs using S/E with degrees
93 def test_identify_latlon_se_d
95 "S50.06773 E14.37742",
96 "S50.06773, E14.37742",
97 "50.06773S 14.37742E",
98 "50.06773S, 14.37742E"
100 latlon_check code, -50.06773, 14.37742
105 # Test identification of lat/lon pairs using S/W with degrees
106 def test_identify_latlon_sw_d
108 "S50.06773 W14.37742",
109 "S50.06773, W14.37742",
110 "50.06773S 14.37742W",
111 "50.06773S, 14.37742W"
113 latlon_check code, -50.06773, -14.37742
118 # Test identification of lat/lon pairs using N/E with degrees/mins
119 def test_identify_latlon_ne_dm
121 "N 50° 04.064 E 014° 22.645",
122 "N 50° 04.064' E 014° 22.645",
123 "N 50° 04.064', E 014° 22.645'",
124 "N50° 04.064 E14° 22.645",
125 "N 50 04.064 E 014 22.645",
126 "N50 4.064 E14 22.645",
127 "50° 04.064' N, 014° 22.645' E"
129 latlon_check code, 50.06773, 14.37742
134 # Test identification of lat/lon pairs using N/W with degrees/mins
135 def test_identify_latlon_nw_dm
137 "N 50° 04.064 W 014° 22.645",
138 "N 50° 04.064' W 014° 22.645",
139 "N 50° 04.064', W 014° 22.645'",
140 "N50° 04.064 W14° 22.645",
141 "N 50 04.064 W 014 22.645",
142 "N50 4.064 W14 22.645",
143 "50° 04.064' N, 014° 22.645' W"
145 latlon_check code, 50.06773, -14.37742
150 # Test identification of lat/lon pairs using S/E with degrees/mins
151 def test_identify_latlon_se_dm
153 "S 50° 04.064 E 014° 22.645",
154 "S 50° 04.064' E 014° 22.645",
155 "S 50° 04.064', E 014° 22.645'",
156 "S50° 04.064 E14° 22.645",
157 "S 50 04.064 E 014 22.645",
158 "S50 4.064 E14 22.645",
159 "50° 04.064' S, 014° 22.645' E"
161 latlon_check code, -50.06773, 14.37742
166 # Test identification of lat/lon pairs using S/W with degrees/mins
167 def test_identify_latlon_sw_dm
169 "S 50° 04.064 W 014° 22.645",
170 "S 50° 04.064' W 014° 22.645",
171 "S 50° 04.064', W 014° 22.645'",
172 "S50° 04.064 W14° 22.645",
173 "S 50 04.064 W 014 22.645",
174 "S50 4.064 W14 22.645",
175 "50° 04.064' S, 014° 22.645' W"
177 latlon_check code, -50.06773, -14.37742
182 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
183 def test_identify_latlon_ne_dms
185 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
186 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
187 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
188 "N50 4 03.828 E14 22 38.712",
189 "N50 4 03.828, E14 22 38.712",
190 "50°4'3.828\"N 14°22'38.712\"E"
192 latlon_check code, 50.06773, 14.37742
197 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
198 def test_identify_latlon_nw_dms
200 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
201 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
202 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
203 "N50 4 03.828 W14 22 38.712",
204 "N50 4 03.828, W14 22 38.712",
205 "50°4'3.828\"N 14°22'38.712\"W"
207 latlon_check code, 50.06773, -14.37742
212 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
213 def test_identify_latlon_se_dms
215 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
216 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
217 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
218 "S50 4 03.828 E14 22 38.712",
219 "S50 4 03.828, E14 22 38.712",
220 "50°4'3.828\"S 14°22'38.712\"E"
222 latlon_check code, -50.06773, 14.37742
227 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
228 def test_identify_latlon_sw_dms
230 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
231 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
232 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
233 "S50 4 03.828 W14 22 38.712",
234 "S50 4 03.828, W14 22 38.712",
235 "50°4'3.828\"S 14°22'38.712\"W"
237 latlon_check code, -50.06773, -14.37742
242 # Test identification of US zipcodes
243 def test_identify_us_postcode
248 post :search, :params => { :query => code }
249 assert_response :success
250 assert_equal %w[osm_nominatim], assigns(:sources)
255 # Test identification of UK postcodes using examples from
256 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
257 def test_identify_uk_postcode
266 search_check code, %w[uk_postcode osm_nominatim]
271 # Test identification of Canadian postcodes
272 def test_identify_ca_postcode
273 search_check "A1B 2C3", %w[ca_postcode osm_nominatim]
277 # Test identification fall through to the default case
278 def test_identify_default
279 search_check "foo bar baz", %w[osm_nominatim geonames]
283 # Test the builtin latitude+longitude search
284 def test_search_latlon
285 get :search_latlon, :params => { :lat => 1.23, :lon => 4.56, :zoom => 16 }, :xhr => true
286 results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
288 get :search_latlon, :params => { :lat => -91.23, :lon => 4.56, :zoom => 16 }, :xhr => true
289 results_check_error "Latitude -91.23 out of range"
291 get :search_latlon, :params => { :lat => 91.23, :lon => 4.56, :zoom => 16 }, :xhr => true
292 results_check_error "Latitude 91.23 out of range"
294 get :search_latlon, :params => { :lat => 1.23, :lon => -180.23, :zoom => 16 }, :xhr => true
295 results_check_error "Longitude -180.23 out of range"
297 get :search_latlon, :params => { :lat => 1.23, :lon => 180.23, :zoom => 16 }, :xhr => true
298 results_check_error "Longitude 180.23 out of range"
302 # Test the UK postcode search
303 def test_search_uk_postcode
304 with_http_stubs "npemap" do
305 get :search_uk_postcode, :xhr => true,
306 :params => { :query => "CV4 7AL", :zoom => 10,
307 :minlon => -0.559, :minlat => 51.217,
308 :maxlon => 0.836, :maxlat => 51.766 }
309 results_check :name => "CV4 7AL", :lat => 52.381748701968, :lon => -1.56176420939232
311 get :search_uk_postcode, :xhr => true,
312 :params => { :query => "XX9 9XX", :zoom => 10,
313 :minlon => -0.559, :minlat => 51.217,
314 :maxlon => 0.836, :maxlat => 51.766 }
320 # Test the Canadian postcode search
321 def test_search_ca_postcode
322 with_http_stubs "geocoder_ca" do
323 get :search_ca_postcode, :xhr => true,
324 :params => { :query => "A1B 2C3", :zoom => 10,
325 :minlon => -0.559, :minlat => 51.217,
326 :maxlon => 0.836, :maxlat => 51.766 }
327 results_check :name => "A1B 2C3", :lat => "47.172520", :lon => "-55.440515"
329 get :search_ca_postcode, :xhr => true,
330 :params => { :query => "k1a 0b1", :zoom => 10,
331 :minlon => -0.559, :minlat => 51.217,
332 :maxlon => 0.836, :maxlat => 51.766 }
333 results_check :name => "K1A 0B1", :lat => "45.375437", :lon => "-75.691041"
335 get :search_ca_postcode, :xhr => true,
336 :params => { :query => "Q0Q 0Q0", :zoom => 10,
337 :minlon => -0.559, :minlat => 51.217,
338 :maxlon => 0.836, :maxlat => 51.766 }
344 # Test the nominatim forward search
345 def test_search_osm_nominatim
346 with_http_stubs "nominatim" do
347 get :search_osm_nominatim, :xhr => true,
348 :params => { :query => "Hoddesdon", :zoom => 10,
349 :minlon => -0.559, :minlat => 51.217,
350 :maxlon => 0.836, :maxlat => 51.766 }
351 results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
352 "min-lat" => 51.7216709, "max-lat" => 51.8016709,
353 "min-lon" => -0.0512898, "max-lon" => 0.0287102,
354 "type" => "node", "id" => 18007599
356 get :search_osm_nominatim, :xhr => true,
357 :params => { :query => "Broxbourne", :zoom => 10,
358 :minlon => -0.559, :minlat => 51.217,
359 :maxlon => 0.836, :maxlat => 51.766 }
360 results_check({ "prefix" => "Suburb",
361 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
362 "min-lat" => 51.7265723, "max-lat" => 51.7665723,
363 "min-lon" => -0.0390782, "max-lon" => 0.0009218,
364 "type" => "node", "id" => 28825933 },
365 { "prefix" => "Village",
366 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
367 "min-lat" => 51.6808751, "max-lat" => 51.7806237,
368 "min-lon" => -0.114204, "max-lon" => 0.0145267,
369 "type" => "relation", "id" => 2677978 },
370 { "prefix" => "Railway Station",
371 "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
372 "min-lat" => 51.7418469, "max-lat" => 51.7518469,
373 "min-lon" => -0.0156773, "max-lon" => -0.0056773,
374 "type" => "node", "id" => 17044599 })
379 # Test the geonames forward search
380 def test_search_geonames
381 with_http_stubs "geonames" do
382 get :search_geonames, :xhr => true,
383 :params => { :query => "Hoddesdon", :zoom => 10,
384 :minlon => -0.559, :minlat => 51.217,
385 :maxlon => 0.836, :maxlat => 51.766 }
386 results_check :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144
388 get :search_geonames, :xhr => true,
389 :params => { :query => "Broxbourne", :zoom => 10,
390 :minlon => -0.559, :minlat => 51.217,
391 :maxlon => 0.836, :maxlat => 51.766 }
392 results_check({ :name => "Broxbourne", :lat => 51.74712, :lon => -0.01923 },
393 { :name => "Broxbourne District", :lat => 51.73026, :lon => -0.04821 },
394 { :name => "Cheshunt", :lat => 51.70791, :lon => -0.03739 },
395 { :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144 },
396 { :name => "Waltham Cross", :lat => 51.68905, :lon => -0.0333 },
397 { :name => "Goffs Oak", :lat => 51.71015, :lon => -0.0872 },
398 { :name => "Wormley", :lat => 51.7324, :lon => -0.0242 },
399 { :name => "Broxbourne", :lat => -27.50314, :lon => 151.378 },
400 { :name => "Lee Valley White Water Centre", :lat => 51.68814, :lon => -0.01682 },
401 { :name => "Cheshunt Railway Station", :lat => 51.703, :lon => -0.024 },
402 { :name => "Theobalds Grove Railway Station", :lat => 51.692, :lon => -0.035 },
403 { :name => "Waltham Cross Railway Station", :lat => 51.685, :lon => -0.027 },
404 { :name => "Rye House Station", :lat => 51.76938, :lon => 0.00562 },
405 { :name => "Broxbourne Station", :lat => 51.74697, :lon => -0.01105 },
406 { :name => "Broxbornebury Park", :lat => 51.75252, :lon => -0.03839 },
407 { :name => "Marriott Cheshunt", :lat => 51.7208, :lon => -0.0324 },
408 { :name => "Cheshunt Community Hospital", :lat => 51.68396, :lon => -0.03951 })
413 # Test the nominatim reverse search
414 def test_search_osm_nominatim_reverse
415 with_http_stubs "nominatim" do
416 get :search_osm_nominatim_reverse, :xhr => true,
417 :params => { :lat => 51.7632, :lon => -0.0076, :zoom => 15 }
418 results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
419 :lat => 51.7465723, :lon => -0.0190782,
420 :type => "node", :id => 28825933, :zoom => 15
422 get :search_osm_nominatim_reverse, :xhr => true,
423 :params => { :lat => 51.7632, :lon => -0.0076, :zoom => 17 }
424 results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
425 :lat => 51.7634883, :lon => -0.0088373,
426 :type => "way", :id => 3489841, :zoom => 17
428 get :search_osm_nominatim_reverse, :xhr => true,
429 :params => { :lat => 13.7709, :lon => 100.50507, :zoom => 19 }
430 results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
431 :lat => 13.7708691, :lon => 100.505073233221,
432 :type => "way", :id => 542901374, :zoom => 19
437 # Test the geonames reverse search
438 def test_search_geonames_reverse
439 with_http_stubs "geonames" do
440 get :search_geonames_reverse, :xhr => true,
441 :params => { :lat => 51.7632, :lon => -0.0076, :zoom => 15 }
442 results_check :name => "England", :suffix => ", United Kingdom",
443 :lat => 51.7632, :lon => -0.0076
449 def latlon_check(query, lat, lon)
450 get :search, :params => { :query => query }
451 assert_response :success
452 assert_template :search
453 assert_template :layout => "map"
454 assert_equal %w[latlon osm_nominatim_reverse geonames_reverse], assigns(:sources)
455 assert_nil @controller.params[:query]
456 assert_in_delta lat, @controller.params[:lat]
457 assert_in_delta lon, @controller.params[:lon]
459 get :search, :params => { :query => query }, :xhr => true
460 assert_response :success
461 assert_template :search
462 assert_template :layout => "xhr"
463 assert_equal %w[latlon osm_nominatim_reverse geonames_reverse], assigns(:sources)
464 assert_nil @controller.params[:query]
465 assert_in_delta lat, @controller.params[:lat]
466 assert_in_delta lon, @controller.params[:lon]
469 def search_check(query, sources)
470 get :search, :params => { :query => query }
471 assert_response :success
472 assert_template :search
473 assert_template :layout => "map"
474 assert_equal sources, assigns(:sources)
476 get :search, :params => { :query => query }, :xhr => true
477 assert_response :success
478 assert_template :search
479 assert_template :layout => "xhr"
480 assert_equal sources, assigns(:sources)
483 def results_check(*results)
484 assert_response :success
485 assert_template :results
486 assert_template :layout => nil
488 assert_select "ul.results-list", 0
490 assert_select "ul.results-list", 1 do
491 assert_select "p.search_results_entry", results.count
493 results.each do |result|
494 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join("")
495 assert_select "p.search_results_entry a.set_position#{attrs}", result[:name]
501 def results_check_error(error)
502 assert_response :success
503 assert_template :error
504 assert_template :layout => nil
505 assert_select "p.search_results_error", error