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 values close to zero
266 def test_identify_latlon_close_to_zero
268 "0.0000123 -0.0000456",
269 "+0.0000123 -0.0000456",
270 "N 0° 0' 0.4428\", W 0° 0' 1.6416\""
272 latlon_check code, 0.0000123, -0.0000456
277 # Test identification of US zipcodes
278 def test_identify_us_postcode
283 search_check code, %w[osm_nominatim]
288 # Test identification of UK postcodes using examples from
289 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
290 def test_identify_uk_postcode
299 search_check code, %w[osm_nominatim]
304 # Test identification of Canadian postcodes
305 def test_identify_ca_postcode
306 search_check "A1B 2C3", %w[osm_nominatim]
310 # Test identification fall through to the default case
311 def test_identify_default
312 search_check "foo bar baz", %w[osm_nominatim]
316 # Test the builtin latitude+longitude search
317 def test_search_latlon
318 post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16), :xhr => true
319 results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
321 post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16), :xhr => true
322 results_check_error "Latitude -91.23 out of range"
324 post geocoder_search_latlon_path(:lat => 91.23, :lon => 4.56, :zoom => 16), :xhr => true
325 results_check_error "Latitude 91.23 out of range"
327 post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16), :xhr => true
328 results_check_error "Longitude -180.23 out of range"
330 post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16), :xhr => true
331 results_check_error "Longitude 180.23 out of range"
334 def test_search_latlon_digits
335 post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
336 results_check({ :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 },
337 { :name => "4.56, 1.23", :lat => 4.56, :lon => 1.23, :zoom => 16 })
339 post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
340 results_check :name => "4.56, -91.23", :lat => 4.56, :lon => -91.23, :zoom => 16
342 post geocoder_search_latlon_path(:lat => -1.23, :lon => 170.23, :zoom => 16, :latlon_digits => true), :xhr => true
343 results_check :name => "-1.23, 170.23", :lat => -1.23, :lon => 170.23, :zoom => 16
345 post geocoder_search_latlon_path(:lat => 91.23, :lon => 94.56, :zoom => 16, :latlon_digits => true), :xhr => true
346 results_check_error "Latitude or longitude are out of range"
348 post geocoder_search_latlon_path(:lat => -91.23, :lon => -94.56, :zoom => 16, :latlon_digits => true), :xhr => true
349 results_check_error "Latitude or longitude are out of range"
351 post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16, :latlon_digits => true), :xhr => true
352 results_check_error "Latitude or longitude are out of range"
354 post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16, :latlon_digits => true), :xhr => true
355 results_check_error "Latitude or longitude are out of range"
359 # Test the nominatim forward search
360 def test_search_osm_nominatim
361 with_http_stubs "nominatim" do
362 post geocoder_search_osm_nominatim_path(:query => "Hoddesdon", :zoom => 10,
363 :minlon => -0.559, :minlat => 51.217,
364 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
365 results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
366 "min-lat" => 51.7216709, "max-lat" => 51.8016709,
367 "min-lon" => -0.0512898, "max-lon" => 0.0287102,
368 "type" => "node", "id" => 18007599
370 post geocoder_search_osm_nominatim_path(:query => "Broxbourne", :zoom => 10,
371 :minlon => -0.559, :minlat => 51.217,
372 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
373 results_check({ "prefix" => "Suburb",
374 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
375 "min-lat" => 51.7265723, "max-lat" => 51.7665723,
376 "min-lon" => -0.0390782, "max-lon" => 0.0009218,
377 "type" => "node", "id" => 28825933 },
378 { "prefix" => "Village",
379 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
380 "min-lat" => 51.6808751, "max-lat" => 51.7806237,
381 "min-lon" => -0.114204, "max-lon" => 0.0145267,
382 "type" => "relation", "id" => 2677978 },
383 { "prefix" => "Railway Station",
384 "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
385 "min-lat" => 51.7418469, "max-lat" => 51.7518469,
386 "min-lon" => -0.0156773, "max-lon" => -0.0056773,
387 "type" => "node", "id" => 17044599 })
392 # Test the nominatim reverse search
393 def test_search_osm_nominatim_reverse
394 with_http_stubs "nominatim" do
395 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15), :xhr => true
396 results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
397 :lat => 51.7465723, :lon => -0.0190782,
398 :type => "node", :id => 28825933, :zoom => 15
400 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17), :xhr => true
401 results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
402 :lat => 51.7634883, :lon => -0.0088373,
403 :type => "way", :id => 3489841, :zoom => 17
405 post geocoder_search_osm_nominatim_reverse_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19), :xhr => true
406 results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
407 :lat => 13.7708691, :lon => 100.505073233221,
408 :type => "way", :id => 542901374, :zoom => 19
414 def latlon_check(query, lat, lon)
415 get search_path(:query => query)
416 assert_response :success
417 assert_template :search
418 assert_template :layout => "map"
419 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
420 assert_nil @controller.params[:query]
421 assert_match(/^[+-]?\d+(?:\.\d*)?$/, @controller.params[:lat])
422 assert_match(/^[+-]?\d+(?:\.\d*)?$/, @controller.params[:lon])
423 assert_in_delta lat, @controller.params[:lat].to_f
424 assert_in_delta lon, @controller.params[:lon].to_f
426 get search_path(:query => query), :xhr => true
427 assert_response :success
428 assert_template :search
429 assert_template :layout => "xhr"
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
438 def search_check(query, sources)
439 get search_path(:query => query)
440 assert_response :success
441 assert_template :search
442 assert_template :layout => "map"
443 assert_equal sources, assigns(:sources).pluck(:name)
445 get search_path(:query => query), :xhr => true
446 assert_response :success
447 assert_template :search
448 assert_template :layout => "xhr"
449 assert_equal sources, assigns(:sources).pluck(:name)
452 def results_check(*results)
453 assert_response :success
454 assert_template :results
455 assert_template :layout => nil
457 assert_select "ul.results-list", 0
459 assert_select "ul.results-list", 1 do
460 assert_select "li.search_results_entry", results.count
462 results.each do |result|
463 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join
464 assert_select "li.search_results_entry a.set_position#{attrs}", result[:name]
470 def results_check_error(error)
471 assert_response :success
472 assert_template :error
473 assert_template :layout => nil
474 assert_select ".alert.alert-danger", error