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
41 "+50.06773 +14.37742",
42 "+50.06773, +14.37742"
44 latlon_check code, 50.06773, 14.37742
49 # Test identification of lat/lon pairs using N/E with degrees
50 def test_identify_latlon_ne_d
52 "N50.06773 E14.37742",
53 "N50.06773, E14.37742",
54 "50.06773N 14.37742E",
55 "50.06773N, 14.37742E"
57 latlon_check code, 50.06773, 14.37742
62 # Test identification of lat/lon pairs using N/W with degrees
63 def test_identify_latlon_nw_d
65 "N50.06773 W14.37742",
66 "N50.06773, W14.37742",
67 "50.06773N 14.37742W",
68 "50.06773N, 14.37742W"
70 latlon_check code, 50.06773, -14.37742
75 # Test identification of lat/lon pairs using S/E with degrees
76 def test_identify_latlon_se_d
78 "S50.06773 E14.37742",
79 "S50.06773, E14.37742",
80 "50.06773S 14.37742E",
81 "50.06773S, 14.37742E"
83 latlon_check code, -50.06773, 14.37742
88 # Test identification of lat/lon pairs using S/W with degrees
89 def test_identify_latlon_sw_d
91 "S50.06773 W14.37742",
92 "S50.06773, W14.37742",
93 "50.06773S 14.37742W",
94 "50.06773S, 14.37742W"
96 latlon_check code, -50.06773, -14.37742
101 # Test identification of lat/lon pairs using N/E with degrees/mins
102 def test_identify_latlon_ne_dm
104 "N 50° 04.064 E 014° 22.645",
105 "N 50° 04.064' E 014° 22.645",
106 "N 50° 04.064', E 014° 22.645'",
107 "N50° 04.064 E14° 22.645",
108 "N 50 04.064 E 014 22.645",
109 "N50 4.064 E14 22.645",
110 "50° 04.064' N, 014° 22.645' E"
112 latlon_check code, 50.06773, 14.37742
117 # Test identification of lat/lon pairs using N/W with degrees/mins
118 def test_identify_latlon_nw_dm
120 "N 50° 04.064 W 014° 22.645",
121 "N 50° 04.064' W 014° 22.645",
122 "N 50° 04.064', W 014° 22.645'",
123 "N50° 04.064 W14° 22.645",
124 "N 50 04.064 W 014 22.645",
125 "N50 4.064 W14 22.645",
126 "50° 04.064' N, 014° 22.645' W"
128 latlon_check code, 50.06773, -14.37742
133 # Test identification of lat/lon pairs using S/E with degrees/mins
134 def test_identify_latlon_se_dm
136 "S 50° 04.064 E 014° 22.645",
137 "S 50° 04.064' E 014° 22.645",
138 "S 50° 04.064', E 014° 22.645'",
139 "S50° 04.064 E14° 22.645",
140 "S 50 04.064 E 014 22.645",
141 "S50 4.064 E14 22.645",
142 "50° 04.064' S, 014° 22.645' E"
144 latlon_check code, -50.06773, 14.37742
149 # Test identification of lat/lon pairs using S/W with degrees/mins
150 def test_identify_latlon_sw_dm
152 "S 50° 04.064 W 014° 22.645",
153 "S 50° 04.064' W 014° 22.645",
154 "S 50° 04.064', W 014° 22.645'",
155 "S50° 04.064 W14° 22.645",
156 "S 50 04.064 W 014 22.645",
157 "S50 4.064 W14 22.645",
158 "50° 04.064' S, 014° 22.645' W"
160 latlon_check code, -50.06773, -14.37742
165 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
166 def test_identify_latlon_ne_dms
168 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
169 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
170 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
171 "N50 4 03.828 E14 22 38.712",
172 "N50 4 03.828, E14 22 38.712",
173 "50°4'3.828\"N 14°22'38.712\"E"
175 latlon_check code, 50.06773, 14.37742
180 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
181 def test_identify_latlon_nw_dms
183 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
184 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
185 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
186 "N50 4 03.828 W14 22 38.712",
187 "N50 4 03.828, W14 22 38.712",
188 "50°4'3.828\"N 14°22'38.712\"W"
190 latlon_check code, 50.06773, -14.37742
195 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
196 def test_identify_latlon_se_dms
198 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
199 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
200 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
201 "S50 4 03.828 E14 22 38.712",
202 "S50 4 03.828, E14 22 38.712",
203 "50°4'3.828\"S 14°22'38.712\"E"
205 latlon_check code, -50.06773, 14.37742
210 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
211 def test_identify_latlon_sw_dms
213 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
214 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
215 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
216 "S50 4 03.828 W14 22 38.712",
217 "S50 4 03.828, W14 22 38.712",
218 "50°4'3.828\"S 14°22'38.712\"W"
220 latlon_check code, -50.06773, -14.37742
225 # Test identification of US zipcodes
226 def test_identify_us_postcode
231 search_check code, %w[osm_nominatim]
236 # Test identification of UK postcodes using examples from
237 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
238 def test_identify_uk_postcode
247 search_check code, %w[osm_nominatim]
252 # Test identification of Canadian postcodes
253 def test_identify_ca_postcode
254 search_check "A1B 2C3", %w[osm_nominatim]
258 # Test identification fall through to the default case
259 def test_identify_default
260 search_check "foo bar baz", %w[osm_nominatim]
264 # Test the builtin latitude+longitude search
265 def test_search_latlon
266 post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16), :xhr => true
267 results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
269 post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16), :xhr => true
270 results_check_error "Latitude -91.23 out of range"
272 post geocoder_search_latlon_path(:lat => 91.23, :lon => 4.56, :zoom => 16), :xhr => true
273 results_check_error "Latitude 91.23 out of range"
275 post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16), :xhr => true
276 results_check_error "Longitude -180.23 out of range"
278 post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16), :xhr => true
279 results_check_error "Longitude 180.23 out of range"
282 def test_search_latlon_digits
283 post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
284 results_check({ :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 },
285 { :name => "4.56, 1.23", :lat => 4.56, :lon => 1.23, :zoom => 16 })
287 post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
288 results_check :name => "4.56, -91.23", :lat => 4.56, :lon => -91.23, :zoom => 16
290 post geocoder_search_latlon_path(:lat => -1.23, :lon => 170.23, :zoom => 16, :latlon_digits => true), :xhr => true
291 results_check :name => "-1.23, 170.23", :lat => -1.23, :lon => 170.23, :zoom => 16
293 post geocoder_search_latlon_path(:lat => 91.23, :lon => 94.56, :zoom => 16, :latlon_digits => true), :xhr => true
294 results_check_error "Latitude or longitude are out of range"
296 post geocoder_search_latlon_path(:lat => -91.23, :lon => -94.56, :zoom => 16, :latlon_digits => true), :xhr => true
297 results_check_error "Latitude or longitude are out of range"
299 post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16, :latlon_digits => true), :xhr => true
300 results_check_error "Latitude or longitude are out of range"
302 post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16, :latlon_digits => true), :xhr => true
303 results_check_error "Latitude or longitude are out of range"
307 # Test the nominatim forward search
308 def test_search_osm_nominatim
309 with_http_stubs "nominatim" do
310 post geocoder_search_osm_nominatim_path(:query => "Hoddesdon", :zoom => 10,
311 :minlon => -0.559, :minlat => 51.217,
312 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
313 results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
314 "min-lat" => 51.7216709, "max-lat" => 51.8016709,
315 "min-lon" => -0.0512898, "max-lon" => 0.0287102,
316 "type" => "node", "id" => 18007599
318 post geocoder_search_osm_nominatim_path(:query => "Broxbourne", :zoom => 10,
319 :minlon => -0.559, :minlat => 51.217,
320 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
321 results_check({ "prefix" => "Suburb",
322 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
323 "min-lat" => 51.7265723, "max-lat" => 51.7665723,
324 "min-lon" => -0.0390782, "max-lon" => 0.0009218,
325 "type" => "node", "id" => 28825933 },
326 { "prefix" => "Village",
327 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
328 "min-lat" => 51.6808751, "max-lat" => 51.7806237,
329 "min-lon" => -0.114204, "max-lon" => 0.0145267,
330 "type" => "relation", "id" => 2677978 },
331 { "prefix" => "Railway Station",
332 "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
333 "min-lat" => 51.7418469, "max-lat" => 51.7518469,
334 "min-lon" => -0.0156773, "max-lon" => -0.0056773,
335 "type" => "node", "id" => 17044599 })
340 # Test the nominatim reverse search
341 def test_search_osm_nominatim_reverse
342 with_http_stubs "nominatim" do
343 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15), :xhr => true
344 results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
345 :lat => 51.7465723, :lon => -0.0190782,
346 :type => "node", :id => 28825933, :zoom => 15
348 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17), :xhr => true
349 results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
350 :lat => 51.7634883, :lon => -0.0088373,
351 :type => "way", :id => 3489841, :zoom => 17
353 post geocoder_search_osm_nominatim_reverse_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19), :xhr => true
354 results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
355 :lat => 13.7708691, :lon => 100.505073233221,
356 :type => "way", :id => 542901374, :zoom => 19
362 def latlon_check(query, lat, lon)
363 get search_path(:query => query)
364 assert_response :success
365 assert_template :search
366 assert_template :layout => "map"
367 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
368 assert_nil @controller.params[:query]
369 assert_in_delta lat, @controller.params[:lat]
370 assert_in_delta lon, @controller.params[:lon]
372 get search_path(:query => query), :xhr => true
373 assert_response :success
374 assert_template :search
375 assert_template :layout => "xhr"
376 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
377 assert_nil @controller.params[:query]
378 assert_in_delta lat, @controller.params[:lat]
379 assert_in_delta lon, @controller.params[:lon]
382 def search_check(query, sources)
383 get search_path(:query => query)
384 assert_response :success
385 assert_template :search
386 assert_template :layout => "map"
387 assert_equal sources, assigns(:sources).pluck(:name)
389 get search_path(:query => query), :xhr => true
390 assert_response :success
391 assert_template :search
392 assert_template :layout => "xhr"
393 assert_equal sources, assigns(:sources).pluck(:name)
396 def results_check(*results)
397 assert_response :success
398 assert_template :results
399 assert_template :layout => nil
401 assert_select "ul.results-list", 0
403 assert_select "ul.results-list", 1 do
404 assert_select "li.search_results_entry", results.count
406 results.each do |result|
407 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join
408 assert_select "li.search_results_entry a.set_position#{attrs}", result[:name]
414 def results_check_error(error)
415 assert_response :success
416 assert_template :error
417 assert_template :layout => nil
418 assert_select ".alert.alert-danger", error