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 => :get },
13 { :controller => "geocoder", :action => "search_latlon" }
16 { :path => "/geocoder/search_ca_postcode", :method => :get },
17 { :controller => "geocoder", :action => "search_ca_postcode" }
20 { :path => "/geocoder/search_osm_nominatim", :method => :get },
21 { :controller => "geocoder", :action => "search_osm_nominatim" }
24 { :path => "/geocoder/search_geonames", :method => :get },
25 { :controller => "geocoder", :action => "search_geonames" }
28 { :path => "/geocoder/search_osm_nominatim_reverse", :method => :get },
29 { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
32 { :path => "/geocoder/search_geonames_reverse", :method => :get },
33 { :controller => "geocoder", :action => "search_geonames_reverse" }
38 # Test identification with no arguments
39 def test_identify_error
41 assert_response :bad_request
43 get search_path, :xhr => true
44 assert_response :bad_request
48 # Test identification of basic lat/lon pairs
49 def test_identify_latlon_basic
53 "+50.06773 +14.37742",
54 "+50.06773, +14.37742"
56 latlon_check code, 50.06773, 14.37742
61 # Test identification of lat/lon pairs using N/E with degrees
62 def test_identify_latlon_ne_d
64 "N50.06773 E14.37742",
65 "N50.06773, E14.37742",
66 "50.06773N 14.37742E",
67 "50.06773N, 14.37742E"
69 latlon_check code, 50.06773, 14.37742
74 # Test identification of lat/lon pairs using N/W with degrees
75 def test_identify_latlon_nw_d
77 "N50.06773 W14.37742",
78 "N50.06773, W14.37742",
79 "50.06773N 14.37742W",
80 "50.06773N, 14.37742W"
82 latlon_check code, 50.06773, -14.37742
87 # Test identification of lat/lon pairs using S/E with degrees
88 def test_identify_latlon_se_d
90 "S50.06773 E14.37742",
91 "S50.06773, E14.37742",
92 "50.06773S 14.37742E",
93 "50.06773S, 14.37742E"
95 latlon_check code, -50.06773, 14.37742
100 # Test identification of lat/lon pairs using S/W with degrees
101 def test_identify_latlon_sw_d
103 "S50.06773 W14.37742",
104 "S50.06773, W14.37742",
105 "50.06773S 14.37742W",
106 "50.06773S, 14.37742W"
108 latlon_check code, -50.06773, -14.37742
113 # Test identification of lat/lon pairs using N/E with degrees/mins
114 def test_identify_latlon_ne_dm
116 "N 50° 04.064 E 014° 22.645",
117 "N 50° 04.064' E 014° 22.645",
118 "N 50° 04.064', E 014° 22.645'",
119 "N50° 04.064 E14° 22.645",
120 "N 50 04.064 E 014 22.645",
121 "N50 4.064 E14 22.645",
122 "50° 04.064' N, 014° 22.645' E"
124 latlon_check code, 50.06773, 14.37742
129 # Test identification of lat/lon pairs using N/W with degrees/mins
130 def test_identify_latlon_nw_dm
132 "N 50° 04.064 W 014° 22.645",
133 "N 50° 04.064' W 014° 22.645",
134 "N 50° 04.064', W 014° 22.645'",
135 "N50° 04.064 W14° 22.645",
136 "N 50 04.064 W 014 22.645",
137 "N50 4.064 W14 22.645",
138 "50° 04.064' N, 014° 22.645' W"
140 latlon_check code, 50.06773, -14.37742
145 # Test identification of lat/lon pairs using S/E with degrees/mins
146 def test_identify_latlon_se_dm
148 "S 50° 04.064 E 014° 22.645",
149 "S 50° 04.064' E 014° 22.645",
150 "S 50° 04.064', E 014° 22.645'",
151 "S50° 04.064 E14° 22.645",
152 "S 50 04.064 E 014 22.645",
153 "S50 4.064 E14 22.645",
154 "50° 04.064' S, 014° 22.645' E"
156 latlon_check code, -50.06773, 14.37742
161 # Test identification of lat/lon pairs using S/W with degrees/mins
162 def test_identify_latlon_sw_dm
164 "S 50° 04.064 W 014° 22.645",
165 "S 50° 04.064' W 014° 22.645",
166 "S 50° 04.064', W 014° 22.645'",
167 "S50° 04.064 W14° 22.645",
168 "S 50 04.064 W 014 22.645",
169 "S50 4.064 W14 22.645",
170 "50° 04.064' S, 014° 22.645' W"
172 latlon_check code, -50.06773, -14.37742
177 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
178 def test_identify_latlon_ne_dms
180 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
181 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
182 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
183 "N50 4 03.828 E14 22 38.712",
184 "N50 4 03.828, E14 22 38.712",
185 "50°4'3.828\"N 14°22'38.712\"E"
187 latlon_check code, 50.06773, 14.37742
192 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
193 def test_identify_latlon_nw_dms
195 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
196 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
197 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
198 "N50 4 03.828 W14 22 38.712",
199 "N50 4 03.828, W14 22 38.712",
200 "50°4'3.828\"N 14°22'38.712\"W"
202 latlon_check code, 50.06773, -14.37742
207 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
208 def test_identify_latlon_se_dms
210 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
211 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
212 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
213 "S50 4 03.828 E14 22 38.712",
214 "S50 4 03.828, E14 22 38.712",
215 "50°4'3.828\"S 14°22'38.712\"E"
217 latlon_check code, -50.06773, 14.37742
222 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
223 def test_identify_latlon_sw_dms
225 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
226 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
227 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
228 "S50 4 03.828 W14 22 38.712",
229 "S50 4 03.828, W14 22 38.712",
230 "50°4'3.828\"S 14°22'38.712\"W"
232 latlon_check code, -50.06773, -14.37742
237 # Test identification of US zipcodes
238 def test_identify_us_postcode
243 search_check code, %w[osm_nominatim]
248 # Test identification of UK postcodes using examples from
249 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
250 def test_identify_uk_postcode
259 search_check code, %w[osm_nominatim]
264 # Test identification of Canadian postcodes
265 def test_identify_ca_postcode
266 search_check "A1B 2C3", %w[ca_postcode osm_nominatim]
270 # Test identification fall through to the default case
271 def test_identify_default
272 search_check "foo bar baz", %w[osm_nominatim geonames]
276 # Test the builtin latitude+longitude search
277 def test_search_latlon
278 get geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16), :xhr => true
279 results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
281 get geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16), :xhr => true
282 results_check_error "Latitude -91.23 out of range"
284 get geocoder_search_latlon_path(:lat => 91.23, :lon => 4.56, :zoom => 16), :xhr => true
285 results_check_error "Latitude 91.23 out of range"
287 get geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16), :xhr => true
288 results_check_error "Longitude -180.23 out of range"
290 get geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16), :xhr => true
291 results_check_error "Longitude 180.23 out of range"
294 def test_search_latlon_digits
295 get geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
296 results_check({ :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 },
297 { :name => "4.56, 1.23", :lat => 4.56, :lon => 1.23, :zoom => 16 })
299 get geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
300 results_check :name => "4.56, -91.23", :lat => 4.56, :lon => -91.23, :zoom => 16
302 get geocoder_search_latlon_path(:lat => -1.23, :lon => 170.23, :zoom => 16, :latlon_digits => true), :xhr => true
303 results_check :name => "-1.23, 170.23", :lat => -1.23, :lon => 170.23, :zoom => 16
305 get geocoder_search_latlon_path(:lat => 91.23, :lon => 94.56, :zoom => 16, :latlon_digits => true), :xhr => true
306 results_check_error "Latitude or longitude are out of range"
308 get geocoder_search_latlon_path(:lat => -91.23, :lon => -94.56, :zoom => 16, :latlon_digits => true), :xhr => true
309 results_check_error "Latitude or longitude are out of range"
311 get geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16, :latlon_digits => true), :xhr => true
312 results_check_error "Latitude or longitude are out of range"
314 get geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16, :latlon_digits => true), :xhr => true
315 results_check_error "Latitude or longitude are out of range"
319 # Test the Canadian postcode search
320 def test_search_ca_postcode
321 with_http_stubs "geocoder_ca" do
322 get geocoder_search_ca_postcode_path(:query => "A1B 2C3", :zoom => 10,
323 :minlon => -0.559, :minlat => 51.217,
324 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
326 results_check :name => "A1B 2C3", :lat => "47.172520", :lon => "-55.440515"
328 get geocoder_search_ca_postcode_path(:query => "k1a 0b1", :zoom => 10,
329 :minlon => -0.559, :minlat => 51.217,
330 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
331 results_check :name => "K1A 0B1", :lat => "45.375437", :lon => "-75.691041"
333 get geocoder_search_ca_postcode_path(:query => "Q0Q 0Q0", :zoom => 10,
334 :minlon => -0.559, :minlat => 51.217,
335 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
341 # Test the nominatim forward search
342 def test_search_osm_nominatim
343 with_http_stubs "nominatim" do
344 get geocoder_search_osm_nominatim_path(:query => "Hoddesdon", :zoom => 10,
345 :minlon => -0.559, :minlat => 51.217,
346 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
347 results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
348 "min-lat" => 51.7216709, "max-lat" => 51.8016709,
349 "min-lon" => -0.0512898, "max-lon" => 0.0287102,
350 "type" => "node", "id" => 18007599
352 get geocoder_search_osm_nominatim_path(:query => "Broxbourne", :zoom => 10,
353 :minlon => -0.559, :minlat => 51.217,
354 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
355 results_check({ "prefix" => "Suburb",
356 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
357 "min-lat" => 51.7265723, "max-lat" => 51.7665723,
358 "min-lon" => -0.0390782, "max-lon" => 0.0009218,
359 "type" => "node", "id" => 28825933 },
360 { "prefix" => "Village",
361 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
362 "min-lat" => 51.6808751, "max-lat" => 51.7806237,
363 "min-lon" => -0.114204, "max-lon" => 0.0145267,
364 "type" => "relation", "id" => 2677978 },
365 { "prefix" => "Railway Station",
366 "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
367 "min-lat" => 51.7418469, "max-lat" => 51.7518469,
368 "min-lon" => -0.0156773, "max-lon" => -0.0056773,
369 "type" => "node", "id" => 17044599 })
374 # Test the geonames forward search
375 def test_search_geonames
376 with_http_stubs "geonames" do
377 get geocoder_search_geonames_path(:query => "Hoddesdon", :zoom => 10, :minlon => -0.559, :minlat => 51.217,
378 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
379 results_check :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144
381 get geocoder_search_geonames_path(:query => "Broxbourne", :zoom => 10,
382 :minlon => -0.559, :minlat => 51.217,
383 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
384 results_check({ :name => "Broxbourne", :lat => 51.74712, :lon => -0.01923 },
385 { :name => "Broxbourne District", :lat => 51.73026, :lon => -0.04821 },
386 { :name => "Cheshunt", :lat => 51.70791, :lon => -0.03739 },
387 { :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144 },
388 { :name => "Waltham Cross", :lat => 51.68905, :lon => -0.0333 },
389 { :name => "Goffs Oak", :lat => 51.71015, :lon => -0.0872 },
390 { :name => "Wormley", :lat => 51.7324, :lon => -0.0242 },
391 { :name => "Broxbourne", :lat => -27.50314, :lon => 151.378 },
392 { :name => "Lee Valley White Water Centre", :lat => 51.68814, :lon => -0.01682 },
393 { :name => "Cheshunt Railway Station", :lat => 51.703, :lon => -0.024 },
394 { :name => "Theobalds Grove Railway Station", :lat => 51.692, :lon => -0.035 },
395 { :name => "Waltham Cross Railway Station", :lat => 51.685, :lon => -0.027 },
396 { :name => "Rye House Station", :lat => 51.76938, :lon => 0.00562 },
397 { :name => "Broxbourne Station", :lat => 51.74697, :lon => -0.01105 },
398 { :name => "Broxbornebury Park", :lat => 51.75252, :lon => -0.03839 },
399 { :name => "Marriott Cheshunt", :lat => 51.7208, :lon => -0.0324 },
400 { :name => "Cheshunt Community Hospital", :lat => 51.68396, :lon => -0.03951 })
405 # Test the nominatim reverse search
406 def test_search_osm_nominatim_reverse
407 with_http_stubs "nominatim" do
408 get geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15), :xhr => true
409 results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
410 :lat => 51.7465723, :lon => -0.0190782,
411 :type => "node", :id => 28825933, :zoom => 15
413 get geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17), :xhr => true
414 results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
415 :lat => 51.7634883, :lon => -0.0088373,
416 :type => "way", :id => 3489841, :zoom => 17
418 get geocoder_search_osm_nominatim_reverse_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19), :xhr => true
419 results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
420 :lat => 13.7708691, :lon => 100.505073233221,
421 :type => "way", :id => 542901374, :zoom => 19
426 # Test the geonames reverse search
427 def test_search_geonames_reverse
428 with_http_stubs "geonames" do
429 get geocoder_search_geonames_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15), :xhr => true
430 results_check :name => "England", :suffix => ", United Kingdom",
431 :lat => 51.7632, :lon => -0.0076
437 def latlon_check(query, lat, lon)
438 get search_path(:query => query)
439 assert_response :success
440 assert_template :search
441 assert_template :layout => "map"
442 assert_equal %w[latlon osm_nominatim_reverse geonames_reverse], assigns(:sources)
443 assert_nil @controller.params[:query]
444 assert_in_delta lat, @controller.params[:lat]
445 assert_in_delta lon, @controller.params[:lon]
447 get search_path(:query => query), :xhr => true
448 assert_response :success
449 assert_template :search
450 assert_template :layout => "xhr"
451 assert_equal %w[latlon osm_nominatim_reverse geonames_reverse], assigns(:sources)
452 assert_nil @controller.params[:query]
453 assert_in_delta lat, @controller.params[:lat]
454 assert_in_delta lon, @controller.params[:lon]
457 def search_check(query, sources)
458 get search_path(:query => query)
459 assert_response :success
460 assert_template :search
461 assert_template :layout => "map"
462 assert_equal sources, assigns(:sources)
464 get search_path(:query => query), :xhr => true
465 assert_response :success
466 assert_template :search
467 assert_template :layout => "xhr"
468 assert_equal sources, assigns(:sources)
471 def results_check(*results)
472 assert_response :success
473 assert_template :results
474 assert_template :layout => nil
476 assert_select "ul.results-list", 0
478 assert_select "ul.results-list", 1 do
479 assert_select "li.search_results_entry", results.count
481 results.each do |result|
482 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join
483 assert_select "li.search_results_entry a.set_position#{attrs}", result[:name]
489 def results_check_error(error)
490 assert_response :success
491 assert_template :error
492 assert_template :layout => nil
493 assert_select "p.search_results_error", error