3 class GeocoderControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/search", :method => :get },
9 { :controller => "geocoder", :action => "search" }
12 { :path => "/geocoder/search_latlon", :method => :post },
13 { :controller => "geocoder", :action => "search_latlon" }
16 { :path => "/geocoder/search_osm_nominatim", :method => :post },
17 { :controller => "geocoder", :action => "search_osm_nominatim" }
20 { :path => "/geocoder/search_osm_nominatim_reverse", :method => :post },
21 { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
26 # Test identification with no arguments
27 def test_identify_error
29 assert_response :bad_request
31 get search_path, :xhr => true
32 assert_response :bad_request
36 # Test identification of basic lat/lon pairs
37 def test_identify_latlon_basic
42 "+50.06773 +14.37742",
43 "+50.06773, +14.37742",
46 latlon_check code, 50.06773, 14.37742
51 # Test identification of lat/lon pairs using N/E with degrees
52 def test_identify_latlon_ne_d
54 "N50.06773 E14.37742",
55 "N50.06773, E14.37742",
56 "50.06773N 14.37742E",
57 "50.06773N, 14.37742E"
59 latlon_check code, 50.06773, 14.37742
64 # Test identification of integer lat/lon pairs using N/E with degrees
65 def test_identify_latlon_ne_d_int_deg
72 latlon_check code, 50, 14
77 # Test identification of lat/lon pairs using N/W with degrees
78 def test_identify_latlon_nw_d
80 "N50.06773 W14.37742",
81 "N50.06773, W14.37742",
82 "50.06773N 14.37742W",
83 "50.06773N, 14.37742W"
85 latlon_check code, 50.06773, -14.37742
90 # Test identification of lat/lon pairs using S/E with degrees
91 def test_identify_latlon_se_d
93 "S50.06773 E14.37742",
94 "S50.06773, E14.37742",
95 "50.06773S 14.37742E",
96 "50.06773S, 14.37742E"
98 latlon_check code, -50.06773, 14.37742
103 # Test identification of lat/lon pairs using S/W with degrees
104 def test_identify_latlon_sw_d
106 "S50.06773 W14.37742",
107 "S50.06773, W14.37742",
108 "50.06773S 14.37742W",
109 "50.06773S, 14.37742W"
111 latlon_check code, -50.06773, -14.37742
116 # Test identification of lat/lon pairs using N/E with degrees/mins
117 def test_identify_latlon_ne_dm
119 "N 50° 04.064 E 014° 22.645",
120 "N 50° 04.064' E 014° 22.645",
121 "N 50° 04.064', E 014° 22.645'",
122 "N50° 04.064 E14° 22.645",
123 "N 50 04.064 E 014 22.645",
124 "N50 4.064 E14 22.645",
125 "50° 04.064' N, 014° 22.645' E"
127 latlon_check code, 50.06773, 14.37742
132 # Test identification of lat/lon pairs using N/W with degrees/mins
133 def test_identify_latlon_nw_dm
135 "N 50° 04.064 W 014° 22.645",
136 "N 50° 04.064' W 014° 22.645",
137 "N 50° 04.064', W 014° 22.645'",
138 "N50° 04.064 W14° 22.645",
139 "N 50 04.064 W 014 22.645",
140 "N50 4.064 W14 22.645",
141 "50° 04.064' N, 014° 22.645' W"
143 latlon_check code, 50.06773, -14.37742
148 # Test identification of lat/lon pairs using S/E with degrees/mins
149 def test_identify_latlon_se_dm
151 "S 50° 04.064 E 014° 22.645",
152 "S 50° 04.064' E 014° 22.645",
153 "S 50° 04.064', E 014° 22.645'",
154 "S50° 04.064 E14° 22.645",
155 "S 50 04.064 E 014 22.645",
156 "S50 4.064 E14 22.645",
157 "50° 04.064' S, 014° 22.645' E"
159 latlon_check code, -50.06773, 14.37742
164 # Test identification of lat/lon pairs using S/W with degrees/mins
165 def test_identify_latlon_sw_dm
167 "S 50° 04.064 W 014° 22.645",
168 "S 50° 04.064' W 014° 22.645",
169 "S 50° 04.064', W 014° 22.645'",
170 "S50° 04.064 W14° 22.645",
171 "S 50 04.064 W 014 22.645",
172 "S50 4.064 W14 22.645",
173 "50° 04.064' S, 014° 22.645' W"
175 latlon_check code, -50.06773, -14.37742
180 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
181 def test_identify_latlon_ne_dms
183 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
184 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
185 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
186 "N50 4 03.828 E14 22 38.712",
187 "N50 4 03.828, E14 22 38.712",
188 "50°4'3.828\"N 14°22'38.712\"E"
190 latlon_check code, 50.06773, 14.37742
195 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
196 def test_identify_latlon_nw_dms
198 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
199 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
200 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
201 "N50 4 03.828 W14 22 38.712",
202 "N50 4 03.828, W14 22 38.712",
203 "50°4'3.828\"N 14°22'38.712\"W"
205 latlon_check code, 50.06773, -14.37742
210 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
211 def test_identify_latlon_se_dms
213 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
214 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
215 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
216 "S50 4 03.828 E14 22 38.712",
217 "S50 4 03.828, E14 22 38.712",
218 "50°4'3.828\"S 14°22'38.712\"E"
220 latlon_check code, -50.06773, 14.37742
225 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
226 def test_identify_latlon_sw_dms
228 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
229 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
230 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
231 "S50 4 03.828 W14 22 38.712",
232 "S50 4 03.828, W14 22 38.712",
233 "50°4'3.828\"S 14°22'38.712\"W"
235 latlon_check code, -50.06773, -14.37742
240 # Test identification of lat/lon pairs with missing fractions
241 def test_no_identify_latlon_ne_missing_fraction_part
251 "N50 1' 3,\" E14 2' 4.\"",
252 "N50° 1' 3.\" E14° 2' 4.\"",
253 "50N 1' 3.\" 14 2' 4.\"E",
254 "50° 1' 3.\"N 14° 2' 4.\"E"
256 get search_path(:query => code)
257 assert_response :success
258 assert_template :search
259 assert_template :layout => "map"
260 assert_equal %w[osm_nominatim], assigns(:sources).pluck(:name)
265 # Test identification of lat/lon pairs with mixed precision
266 def test_identify_latlon_ne_mixed_precision
267 latlon_check "N1 5 E15", 1.083333, 15
268 latlon_check "N1 5 9 E15", 1.085833, 15
269 latlon_check "N1 5 9 E1 5", 1.085833, 1.083333
270 latlon_check "N15 E1 5", 15, 1.083333
271 latlon_check "N15 E1 5 9", 15, 1.085833
272 latlon_check "N1 5 E1 5 9", 1.083333, 1.085833
276 # Test identification of lat/lon pairs with values close to zero
277 def test_identify_latlon_close_to_zero
279 "0.0000123 -0.0000456",
280 "+0.0000123 -0.0000456",
281 "N 0° 0' 0.4428\", W 0° 0' 1.6416\""
283 latlon_check code, 0.0000123, -0.0000456
288 # Test identification of US zipcodes
289 def test_identify_us_postcode
294 search_check code, %w[osm_nominatim]
299 # Test identification of UK postcodes using examples from
300 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
301 def test_identify_uk_postcode
310 search_check code, %w[osm_nominatim]
315 # Test identification of Canadian postcodes
316 def test_identify_ca_postcode
317 search_check "A1B 2C3", %w[osm_nominatim]
321 # Test identification fall through to the default case
322 def test_identify_default
323 search_check "foo bar baz", %w[osm_nominatim]
327 # Test the builtin latitude+longitude search
328 def test_search_latlon
329 post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16), :xhr => true
330 results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
332 post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16), :xhr => true
333 results_check_error "Latitude -91.23 out of range"
335 post geocoder_search_latlon_path(:lat => 91.23, :lon => 4.56, :zoom => 16), :xhr => true
336 results_check_error "Latitude 91.23 out of range"
338 post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16), :xhr => true
339 results_check_error "Longitude -180.23 out of range"
341 post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16), :xhr => true
342 results_check_error "Longitude 180.23 out of range"
345 def test_search_latlon_digits
346 post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
347 results_check({ :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 },
348 { :name => "4.56, 1.23", :lat => 4.56, :lon => 1.23, :zoom => 16 })
350 post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
351 results_check :name => "4.56, -91.23", :lat => 4.56, :lon => -91.23, :zoom => 16
353 post geocoder_search_latlon_path(:lat => -1.23, :lon => 170.23, :zoom => 16, :latlon_digits => true), :xhr => true
354 results_check :name => "-1.23, 170.23", :lat => -1.23, :lon => 170.23, :zoom => 16
356 post geocoder_search_latlon_path(:lat => 91.23, :lon => 94.56, :zoom => 16, :latlon_digits => true), :xhr => true
357 results_check_error "Latitude or longitude are out of range"
359 post geocoder_search_latlon_path(:lat => -91.23, :lon => -94.56, :zoom => 16, :latlon_digits => true), :xhr => true
360 results_check_error "Latitude or longitude are out of range"
362 post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16, :latlon_digits => true), :xhr => true
363 results_check_error "Latitude or longitude are out of range"
365 post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16, :latlon_digits => true), :xhr => true
366 results_check_error "Latitude or longitude are out of range"
370 # Test the nominatim forward search
371 def test_search_osm_nominatim
372 with_http_stubs "nominatim" do
373 post geocoder_search_osm_nominatim_path(:query => "Hoddesdon", :zoom => 10,
374 :minlon => -0.559, :minlat => 51.217,
375 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
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 post geocoder_search_osm_nominatim_path(:query => "Broxbourne", :zoom => 10,
382 :minlon => -0.559, :minlat => 51.217,
383 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
384 results_check({ "prefix" => "Suburb",
385 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
386 "min-lat" => 51.7265723, "max-lat" => 51.7665723,
387 "min-lon" => -0.0390782, "max-lon" => 0.0009218,
388 "type" => "node", "id" => 28825933 },
389 { "prefix" => "Village",
390 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
391 "min-lat" => 51.6808751, "max-lat" => 51.7806237,
392 "min-lon" => -0.114204, "max-lon" => 0.0145267,
393 "type" => "relation", "id" => 2677978 },
394 { "prefix" => "Railway Station",
395 "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
396 "min-lat" => 51.7418469, "max-lat" => 51.7518469,
397 "min-lon" => -0.0156773, "max-lon" => -0.0056773,
398 "type" => "node", "id" => 17044599 })
403 # Test the nominatim reverse search
404 def test_search_osm_nominatim_reverse
405 with_http_stubs "nominatim" do
406 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15), :xhr => true
407 results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
408 :lat => 51.7465723, :lon => -0.0190782,
409 :type => "node", :id => 28825933, :zoom => 15
411 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17), :xhr => true
412 results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
413 :lat => 51.7634883, :lon => -0.0088373,
414 :type => "way", :id => 3489841, :zoom => 17
416 post geocoder_search_osm_nominatim_reverse_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19), :xhr => true
417 results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
418 :lat => 13.7708691, :lon => 100.505073233221,
419 :type => "way", :id => 542901374, :zoom => 19
425 def latlon_check(query, lat, lon)
426 get search_path(:query => query)
427 assert_response :success
428 assert_template :search
429 assert_template :layout => "map"
430 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
431 assert_nil @controller.params[:query]
432 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lat])
433 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lon])
434 assert_in_delta lat, @controller.params[:lat].to_f
435 assert_in_delta lon, @controller.params[:lon].to_f
437 get search_path(:query => query), :xhr => true
438 assert_response :success
439 assert_template :search
440 assert_template :layout => "xhr"
441 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
442 assert_nil @controller.params[:query]
443 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lat])
444 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lon])
445 assert_in_delta lat, @controller.params[:lat].to_f
446 assert_in_delta lon, @controller.params[:lon].to_f
449 def search_check(query, sources)
450 get search_path(:query => query)
451 assert_response :success
452 assert_template :search
453 assert_template :layout => "map"
454 assert_equal sources, assigns(:sources).pluck(:name)
456 get search_path(:query => query), :xhr => true
457 assert_response :success
458 assert_template :search
459 assert_template :layout => "xhr"
460 assert_equal sources, assigns(:sources).pluck(:name)
463 def results_check(*results)
464 assert_response :success
465 assert_template :results
466 assert_template :layout => nil
468 assert_select "ul.results-list", 0
470 assert_select "ul.results-list", 1 do
471 assert_select "li.search_results_entry", results.count
473 results.each do |result|
474 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join
475 assert_select "li.search_results_entry a.set_position#{attrs}", result[:name]
481 def results_check_error(error)
482 assert_response :success
483 assert_template :error
484 assert_template :layout => nil
485 assert_select ".alert.alert-danger", error