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 US zipcodes
266 def test_identify_us_postcode
271 search_check code, %w[osm_nominatim]
276 # Test identification of UK postcodes using examples from
277 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
278 def test_identify_uk_postcode
287 search_check code, %w[osm_nominatim]
292 # Test identification of Canadian postcodes
293 def test_identify_ca_postcode
294 search_check "A1B 2C3", %w[osm_nominatim]
298 # Test identification fall through to the default case
299 def test_identify_default
300 search_check "foo bar baz", %w[osm_nominatim]
304 # Test the builtin latitude+longitude search
305 def test_search_latlon
306 post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16), :xhr => true
307 results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
309 post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16), :xhr => true
310 results_check_error "Latitude -91.23 out of range"
312 post geocoder_search_latlon_path(:lat => 91.23, :lon => 4.56, :zoom => 16), :xhr => true
313 results_check_error "Latitude 91.23 out of range"
315 post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16), :xhr => true
316 results_check_error "Longitude -180.23 out of range"
318 post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16), :xhr => true
319 results_check_error "Longitude 180.23 out of range"
322 def test_search_latlon_digits
323 post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
324 results_check({ :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 },
325 { :name => "4.56, 1.23", :lat => 4.56, :lon => 1.23, :zoom => 16 })
327 post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
328 results_check :name => "4.56, -91.23", :lat => 4.56, :lon => -91.23, :zoom => 16
330 post geocoder_search_latlon_path(:lat => -1.23, :lon => 170.23, :zoom => 16, :latlon_digits => true), :xhr => true
331 results_check :name => "-1.23, 170.23", :lat => -1.23, :lon => 170.23, :zoom => 16
333 post geocoder_search_latlon_path(:lat => 91.23, :lon => 94.56, :zoom => 16, :latlon_digits => true), :xhr => true
334 results_check_error "Latitude or longitude are out of range"
336 post geocoder_search_latlon_path(:lat => -91.23, :lon => -94.56, :zoom => 16, :latlon_digits => true), :xhr => true
337 results_check_error "Latitude or longitude are out of range"
339 post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16, :latlon_digits => true), :xhr => true
340 results_check_error "Latitude or longitude are out of range"
342 post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16, :latlon_digits => true), :xhr => true
343 results_check_error "Latitude or longitude are out of range"
347 # Test the nominatim forward search
348 def test_search_osm_nominatim
349 with_http_stubs "nominatim" do
350 post geocoder_search_osm_nominatim_path(:query => "Hoddesdon", :zoom => 10,
351 :minlon => -0.559, :minlat => 51.217,
352 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
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 post geocoder_search_osm_nominatim_path(:query => "Broxbourne", :zoom => 10,
359 :minlon => -0.559, :minlat => 51.217,
360 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
361 results_check({ "prefix" => "Suburb",
362 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
363 "min-lat" => 51.7265723, "max-lat" => 51.7665723,
364 "min-lon" => -0.0390782, "max-lon" => 0.0009218,
365 "type" => "node", "id" => 28825933 },
366 { "prefix" => "Village",
367 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
368 "min-lat" => 51.6808751, "max-lat" => 51.7806237,
369 "min-lon" => -0.114204, "max-lon" => 0.0145267,
370 "type" => "relation", "id" => 2677978 },
371 { "prefix" => "Railway Station",
372 "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
373 "min-lat" => 51.7418469, "max-lat" => 51.7518469,
374 "min-lon" => -0.0156773, "max-lon" => -0.0056773,
375 "type" => "node", "id" => 17044599 })
380 # Test the nominatim reverse search
381 def test_search_osm_nominatim_reverse
382 with_http_stubs "nominatim" do
383 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15), :xhr => true
384 results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
385 :lat => 51.7465723, :lon => -0.0190782,
386 :type => "node", :id => 28825933, :zoom => 15
388 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17), :xhr => true
389 results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
390 :lat => 51.7634883, :lon => -0.0088373,
391 :type => "way", :id => 3489841, :zoom => 17
393 post geocoder_search_osm_nominatim_reverse_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19), :xhr => true
394 results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
395 :lat => 13.7708691, :lon => 100.505073233221,
396 :type => "way", :id => 542901374, :zoom => 19
402 def latlon_check(query, lat, lon)
403 get search_path(:query => query)
404 assert_response :success
405 assert_template :search
406 assert_template :layout => "map"
407 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
408 assert_nil @controller.params[:query]
409 assert_in_delta lat, @controller.params[:lat].to_f
410 assert_in_delta lon, @controller.params[:lon].to_f
412 get search_path(:query => query), :xhr => true
413 assert_response :success
414 assert_template :search
415 assert_template :layout => "xhr"
416 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
417 assert_nil @controller.params[:query]
418 assert_in_delta lat, @controller.params[:lat].to_f
419 assert_in_delta lon, @controller.params[:lon].to_f
422 def search_check(query, sources)
423 get search_path(:query => query)
424 assert_response :success
425 assert_template :search
426 assert_template :layout => "map"
427 assert_equal sources, assigns(:sources).pluck(:name)
429 get search_path(:query => query), :xhr => true
430 assert_response :success
431 assert_template :search
432 assert_template :layout => "xhr"
433 assert_equal sources, assigns(:sources).pluck(:name)
436 def results_check(*results)
437 assert_response :success
438 assert_template :results
439 assert_template :layout => nil
441 assert_select "ul.results-list", 0
443 assert_select "ul.results-list", 1 do
444 assert_select "li.search_results_entry", results.count
446 results.each do |result|
447 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join
448 assert_select "li.search_results_entry a.set_position#{attrs}", result[:name]
454 def results_check_error(error)
455 assert_response :success
456 assert_template :error
457 assert_template :layout => nil
458 assert_select ".alert.alert-danger", error