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 lat/lon pairs using N/W with degrees
65 def test_identify_latlon_nw_d
67 "N50.06773 W14.37742",
68 "N50.06773, W14.37742",
69 "50.06773N 14.37742W",
70 "50.06773N, 14.37742W"
72 latlon_check code, 50.06773, -14.37742
77 # Test identification of lat/lon pairs using S/E with degrees
78 def test_identify_latlon_se_d
80 "S50.06773 E14.37742",
81 "S50.06773, E14.37742",
82 "50.06773S 14.37742E",
83 "50.06773S, 14.37742E"
85 latlon_check code, -50.06773, 14.37742
90 # Test identification of lat/lon pairs using S/W with degrees
91 def test_identify_latlon_sw_d
93 "S50.06773 W14.37742",
94 "S50.06773, W14.37742",
95 "50.06773S 14.37742W",
96 "50.06773S, 14.37742W"
98 latlon_check code, -50.06773, -14.37742
103 # Test identification of lat/lon pairs using N/E with degrees/mins
104 def test_identify_latlon_ne_dm
106 "N 50° 04.064 E 014° 22.645",
107 "N 50° 04.064' E 014° 22.645",
108 "N 50° 04.064', E 014° 22.645'",
109 "N50° 04.064 E14° 22.645",
110 "N 50 04.064 E 014 22.645",
111 "N50 4.064 E14 22.645",
112 "50° 04.064' N, 014° 22.645' E"
114 latlon_check code, 50.06773, 14.37742
119 # Test identification of lat/lon pairs using N/W with degrees/mins
120 def test_identify_latlon_nw_dm
122 "N 50° 04.064 W 014° 22.645",
123 "N 50° 04.064' W 014° 22.645",
124 "N 50° 04.064', W 014° 22.645'",
125 "N50° 04.064 W14° 22.645",
126 "N 50 04.064 W 014 22.645",
127 "N50 4.064 W14 22.645",
128 "50° 04.064' N, 014° 22.645' W"
130 latlon_check code, 50.06773, -14.37742
135 # Test identification of lat/lon pairs using S/E with degrees/mins
136 def test_identify_latlon_se_dm
138 "S 50° 04.064 E 014° 22.645",
139 "S 50° 04.064' E 014° 22.645",
140 "S 50° 04.064', E 014° 22.645'",
141 "S50° 04.064 E14° 22.645",
142 "S 50 04.064 E 014 22.645",
143 "S50 4.064 E14 22.645",
144 "50° 04.064' S, 014° 22.645' E"
146 latlon_check code, -50.06773, 14.37742
151 # Test identification of lat/lon pairs using S/W with degrees/mins
152 def test_identify_latlon_sw_dm
154 "S 50° 04.064 W 014° 22.645",
155 "S 50° 04.064' W 014° 22.645",
156 "S 50° 04.064', W 014° 22.645'",
157 "S50° 04.064 W14° 22.645",
158 "S 50 04.064 W 014 22.645",
159 "S50 4.064 W14 22.645",
160 "50° 04.064' S, 014° 22.645' W"
162 latlon_check code, -50.06773, -14.37742
167 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
168 def test_identify_latlon_ne_dms
170 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
171 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
172 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
173 "N50 4 03.828 E14 22 38.712",
174 "N50 4 03.828, E14 22 38.712",
175 "50°4'3.828\"N 14°22'38.712\"E"
177 latlon_check code, 50.06773, 14.37742
182 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
183 def test_identify_latlon_nw_dms
185 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
186 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
187 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
188 "N50 4 03.828 W14 22 38.712",
189 "N50 4 03.828, W14 22 38.712",
190 "50°4'3.828\"N 14°22'38.712\"W"
192 latlon_check code, 50.06773, -14.37742
197 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
198 def test_identify_latlon_se_dms
200 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
201 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
202 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
203 "S50 4 03.828 E14 22 38.712",
204 "S50 4 03.828, E14 22 38.712",
205 "50°4'3.828\"S 14°22'38.712\"E"
207 latlon_check code, -50.06773, 14.37742
212 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
213 def test_identify_latlon_sw_dms
215 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
216 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
217 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
218 "S50 4 03.828 W14 22 38.712",
219 "S50 4 03.828, W14 22 38.712",
220 "50°4'3.828\"S 14°22'38.712\"W"
222 latlon_check code, -50.06773, -14.37742
227 # Test identification of US zipcodes
228 def test_identify_us_postcode
233 search_check code, %w[osm_nominatim]
238 # Test identification of UK postcodes using examples from
239 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
240 def test_identify_uk_postcode
249 search_check code, %w[osm_nominatim]
254 # Test identification of Canadian postcodes
255 def test_identify_ca_postcode
256 search_check "A1B 2C3", %w[osm_nominatim]
260 # Test identification fall through to the default case
261 def test_identify_default
262 search_check "foo bar baz", %w[osm_nominatim]
266 # Test the builtin latitude+longitude search
267 def test_search_latlon
268 post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16), :xhr => true
269 results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
271 post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16), :xhr => true
272 results_check_error "Latitude -91.23 out of range"
274 post geocoder_search_latlon_path(:lat => 91.23, :lon => 4.56, :zoom => 16), :xhr => true
275 results_check_error "Latitude 91.23 out of range"
277 post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16), :xhr => true
278 results_check_error "Longitude -180.23 out of range"
280 post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16), :xhr => true
281 results_check_error "Longitude 180.23 out of range"
284 def test_search_latlon_digits
285 post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
286 results_check({ :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 },
287 { :name => "4.56, 1.23", :lat => 4.56, :lon => 1.23, :zoom => 16 })
289 post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
290 results_check :name => "4.56, -91.23", :lat => 4.56, :lon => -91.23, :zoom => 16
292 post geocoder_search_latlon_path(:lat => -1.23, :lon => 170.23, :zoom => 16, :latlon_digits => true), :xhr => true
293 results_check :name => "-1.23, 170.23", :lat => -1.23, :lon => 170.23, :zoom => 16
295 post geocoder_search_latlon_path(:lat => 91.23, :lon => 94.56, :zoom => 16, :latlon_digits => true), :xhr => true
296 results_check_error "Latitude or longitude are out of range"
298 post geocoder_search_latlon_path(:lat => -91.23, :lon => -94.56, :zoom => 16, :latlon_digits => true), :xhr => true
299 results_check_error "Latitude or longitude are out of range"
301 post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16, :latlon_digits => true), :xhr => true
302 results_check_error "Latitude or longitude are out of range"
304 post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16, :latlon_digits => true), :xhr => true
305 results_check_error "Latitude or longitude are out of range"
309 # Test the nominatim forward search
310 def test_search_osm_nominatim
311 with_http_stubs "nominatim" do
312 post geocoder_search_osm_nominatim_path(:query => "Hoddesdon", :zoom => 10,
313 :minlon => -0.559, :minlat => 51.217,
314 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
315 results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
316 "min-lat" => 51.7216709, "max-lat" => 51.8016709,
317 "min-lon" => -0.0512898, "max-lon" => 0.0287102,
318 "type" => "node", "id" => 18007599
320 post geocoder_search_osm_nominatim_path(:query => "Broxbourne", :zoom => 10,
321 :minlon => -0.559, :minlat => 51.217,
322 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
323 results_check({ "prefix" => "Suburb",
324 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
325 "min-lat" => 51.7265723, "max-lat" => 51.7665723,
326 "min-lon" => -0.0390782, "max-lon" => 0.0009218,
327 "type" => "node", "id" => 28825933 },
328 { "prefix" => "Village",
329 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
330 "min-lat" => 51.6808751, "max-lat" => 51.7806237,
331 "min-lon" => -0.114204, "max-lon" => 0.0145267,
332 "type" => "relation", "id" => 2677978 },
333 { "prefix" => "Railway Station",
334 "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
335 "min-lat" => 51.7418469, "max-lat" => 51.7518469,
336 "min-lon" => -0.0156773, "max-lon" => -0.0056773,
337 "type" => "node", "id" => 17044599 })
342 # Test the nominatim reverse search
343 def test_search_osm_nominatim_reverse
344 with_http_stubs "nominatim" do
345 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15), :xhr => true
346 results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
347 :lat => 51.7465723, :lon => -0.0190782,
348 :type => "node", :id => 28825933, :zoom => 15
350 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17), :xhr => true
351 results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
352 :lat => 51.7634883, :lon => -0.0088373,
353 :type => "way", :id => 3489841, :zoom => 17
355 post geocoder_search_osm_nominatim_reverse_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19), :xhr => true
356 results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
357 :lat => 13.7708691, :lon => 100.505073233221,
358 :type => "way", :id => 542901374, :zoom => 19
364 def latlon_check(query, lat, lon)
365 get search_path(:query => query)
366 assert_response :success
367 assert_template :search
368 assert_template :layout => "map"
369 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
370 assert_nil @controller.params[:query]
371 assert_in_delta lat, @controller.params[:lat]
372 assert_in_delta lon, @controller.params[:lon]
374 get search_path(:query => query), :xhr => true
375 assert_response :success
376 assert_template :search
377 assert_template :layout => "xhr"
378 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
379 assert_nil @controller.params[:query]
380 assert_in_delta lat, @controller.params[:lat]
381 assert_in_delta lon, @controller.params[:lon]
384 def search_check(query, sources)
385 get search_path(:query => query)
386 assert_response :success
387 assert_template :search
388 assert_template :layout => "map"
389 assert_equal sources, assigns(:sources).pluck(:name)
391 get search_path(:query => query), :xhr => true
392 assert_response :success
393 assert_template :search
394 assert_template :layout => "xhr"
395 assert_equal sources, assigns(:sources).pluck(:name)
398 def results_check(*results)
399 assert_response :success
400 assert_template :results
401 assert_template :layout => nil
403 assert_select "ul.results-list", 0
405 assert_select "ul.results-list", 1 do
406 assert_select "li.search_results_entry", results.count
408 results.each do |result|
409 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join
410 assert_select "li.search_results_entry a.set_position#{attrs}", result[:name]
416 def results_check_error(error)
417 assert_response :success
418 assert_template :error
419 assert_template :layout => nil
420 assert_select ".alert.alert-danger", error