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_osm_nominatim", :method => :post },
13 { :controller => "geocoder", :action => "search_osm_nominatim" }
16 { :path => "/geocoder/search_osm_nominatim_reverse", :method => :post },
17 { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
22 # Test identification with no arguments
23 def test_identify_error
25 assert_response :bad_request
27 get search_path, :xhr => true
28 assert_response :bad_request
32 # Test identification of basic lat/lon pairs
33 def test_identify_latlon_basic
38 "+50.06773 +14.37742",
39 "+50.06773, +14.37742",
42 latlon_check code, 50.06773, 14.37742
47 # Test identification of lat/lon pairs using N/E with degrees
48 def test_identify_latlon_ne_d
50 "N50.06773 E14.37742",
51 "N50.06773, E14.37742",
52 "50.06773N 14.37742E",
53 "50.06773N, 14.37742E"
55 latlon_check code, 50.06773, 14.37742
60 # Test identification of integer lat/lon pairs using N/E with degrees
61 def test_identify_latlon_ne_d_int_deg
68 latlon_check code, 50, 14
73 # Test identification of lat/lon pairs using N/W with degrees
74 def test_identify_latlon_nw_d
76 "N50.06773 W14.37742",
77 "N50.06773, W14.37742",
78 "50.06773N 14.37742W",
79 "50.06773N, 14.37742W"
81 latlon_check code, 50.06773, -14.37742
86 # Test identification of lat/lon pairs using S/E with degrees
87 def test_identify_latlon_se_d
89 "S50.06773 E14.37742",
90 "S50.06773, E14.37742",
91 "50.06773S 14.37742E",
92 "50.06773S, 14.37742E"
94 latlon_check code, -50.06773, 14.37742
99 # Test identification of lat/lon pairs using S/W with degrees
100 def test_identify_latlon_sw_d
102 "S50.06773 W14.37742",
103 "S50.06773, W14.37742",
104 "50.06773S 14.37742W",
105 "50.06773S, 14.37742W"
107 latlon_check code, -50.06773, -14.37742
112 # Test identification of lat/lon pairs using N/E with degrees/mins
113 def test_identify_latlon_ne_dm
115 "N 50° 04.064 E 014° 22.645",
116 "N 50° 04.064' E 014° 22.645",
117 "N 50° 04.064', E 014° 22.645'",
118 "N50° 04.064 E14° 22.645",
119 "N 50 04.064 E 014 22.645",
120 "N50 4.064 E14 22.645",
121 "50° 04.064' N, 014° 22.645' E"
123 latlon_check code, 50.06773, 14.37742
128 # Test identification of lat/lon pairs using N/W with degrees/mins
129 def test_identify_latlon_nw_dm
131 "N 50° 04.064 W 014° 22.645",
132 "N 50° 04.064' W 014° 22.645",
133 "N 50° 04.064', W 014° 22.645'",
134 "N50° 04.064 W14° 22.645",
135 "N 50 04.064 W 014 22.645",
136 "N50 4.064 W14 22.645",
137 "50° 04.064' N, 014° 22.645' W"
139 latlon_check code, 50.06773, -14.37742
144 # Test identification of lat/lon pairs using S/E with degrees/mins
145 def test_identify_latlon_se_dm
147 "S 50° 04.064 E 014° 22.645",
148 "S 50° 04.064' E 014° 22.645",
149 "S 50° 04.064', E 014° 22.645'",
150 "S50° 04.064 E14° 22.645",
151 "S 50 04.064 E 014 22.645",
152 "S50 4.064 E14 22.645",
153 "50° 04.064' S, 014° 22.645' E"
155 latlon_check code, -50.06773, 14.37742
160 # Test identification of lat/lon pairs using S/W with degrees/mins
161 def test_identify_latlon_sw_dm
163 "S 50° 04.064 W 014° 22.645",
164 "S 50° 04.064' W 014° 22.645",
165 "S 50° 04.064', W 014° 22.645'",
166 "S50° 04.064 W14° 22.645",
167 "S 50 04.064 W 014 22.645",
168 "S50 4.064 W14 22.645",
169 "50° 04.064' S, 014° 22.645' W"
171 latlon_check code, -50.06773, -14.37742
176 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
177 def test_identify_latlon_ne_dms
179 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
180 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
181 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
182 "N50 4 03.828 E14 22 38.712",
183 "N50 4 03.828, E14 22 38.712",
184 "50°4'3.828\"N 14°22'38.712\"E"
186 latlon_check code, 50.06773, 14.37742
191 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
192 def test_identify_latlon_nw_dms
194 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
195 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
196 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
197 "N50 4 03.828 W14 22 38.712",
198 "N50 4 03.828, W14 22 38.712",
199 "50°4'3.828\"N 14°22'38.712\"W"
201 latlon_check code, 50.06773, -14.37742
206 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
207 def test_identify_latlon_se_dms
209 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
210 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
211 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
212 "S50 4 03.828 E14 22 38.712",
213 "S50 4 03.828, E14 22 38.712",
214 "50°4'3.828\"S 14°22'38.712\"E"
216 latlon_check code, -50.06773, 14.37742
221 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
222 def test_identify_latlon_sw_dms
224 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
225 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
226 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
227 "S50 4 03.828 W14 22 38.712",
228 "S50 4 03.828, W14 22 38.712",
229 "50°4'3.828\"S 14°22'38.712\"W"
231 latlon_check code, -50.06773, -14.37742
236 # Test identification of lat/lon pairs with missing fractions
237 def test_no_identify_latlon_ne_missing_fraction_part
247 "N50 1' 3,\" E14 2' 4.\"",
248 "N50° 1' 3.\" E14° 2' 4.\"",
249 "50N 1' 3.\" 14 2' 4.\"E",
250 "50° 1' 3.\"N 14° 2' 4.\"E"
252 get search_path(:query => code)
253 assert_response :success
254 assert_template :search
255 assert_template :layout => "map"
256 assert_equal %w[osm_nominatim], assigns(:sources).pluck(:name)
261 # Test identification of lat/lon pairs with mixed precision
262 def test_identify_latlon_ne_mixed_precision
263 latlon_check "N1 5 E15", 1.083333, 15
264 latlon_check "N1 5 9 E15", 1.085833, 15
265 latlon_check "N1 5 9 E1 5", 1.085833, 1.083333
266 latlon_check "N15 E1 5", 15, 1.083333
267 latlon_check "N15 E1 5 9", 15, 1.085833
268 latlon_check "N1 5 E1 5 9", 1.083333, 1.085833
272 # Test identification of lat/lon pairs with values close to zero
273 def test_identify_latlon_close_to_zero
275 "0.0000123 -0.0000456",
276 "+0.0000123 -0.0000456",
277 "N 0° 0' 0.4428\", W 0° 0' 1.6416\""
279 latlon_check code, 0.0000123, -0.0000456
284 # Test identification of US zipcodes
285 def test_identify_us_postcode
290 search_check code, %w[osm_nominatim]
295 # Test identification of UK postcodes using examples from
296 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
297 def test_identify_uk_postcode
306 search_check code, %w[osm_nominatim]
311 # Test identification of Canadian postcodes
312 def test_identify_ca_postcode
313 search_check "A1B 2C3", %w[osm_nominatim]
317 # Test identification fall through to the default case
318 def test_identify_default
319 search_check "foo bar baz", %w[osm_nominatim]
323 # Test the nominatim forward search
324 def test_search_osm_nominatim
325 with_http_stubs "nominatim" do
326 post geocoder_search_osm_nominatim_path(:query => "Hoddesdon", :zoom => 10,
327 :minlon => -0.559, :minlat => 51.217,
328 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
329 results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
330 "min-lat" => 51.7216709, "max-lat" => 51.8016709,
331 "min-lon" => -0.0512898, "max-lon" => 0.0287102,
332 "type" => "node", "id" => 18007599
334 post geocoder_search_osm_nominatim_path(:query => "Broxbourne", :zoom => 10,
335 :minlon => -0.559, :minlat => 51.217,
336 :maxlon => 0.836, :maxlat => 51.766), :xhr => true
337 results_check({ "prefix" => "Suburb",
338 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
339 "min-lat" => 51.7265723, "max-lat" => 51.7665723,
340 "min-lon" => -0.0390782, "max-lon" => 0.0009218,
341 "type" => "node", "id" => 28825933 },
342 { "prefix" => "Village",
343 "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
344 "min-lat" => 51.6808751, "max-lat" => 51.7806237,
345 "min-lon" => -0.114204, "max-lon" => 0.0145267,
346 "type" => "relation", "id" => 2677978 },
347 { "prefix" => "Railway Station",
348 "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
349 "min-lat" => 51.7418469, "max-lat" => 51.7518469,
350 "min-lon" => -0.0156773, "max-lon" => -0.0056773,
351 "type" => "node", "id" => 17044599 })
356 # Test the nominatim reverse search
357 def test_search_osm_nominatim_reverse
358 with_http_stubs "nominatim" do
359 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15), :xhr => true
360 results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
361 :lat => 51.7465723, :lon => -0.0190782,
362 :type => "node", :id => 28825933, :zoom => 15
364 post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17), :xhr => true
365 results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
366 :lat => 51.7634883, :lon => -0.0088373,
367 :type => "way", :id => 3489841, :zoom => 17
369 post geocoder_search_osm_nominatim_reverse_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19), :xhr => true
370 results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
371 :lat => 13.7708691, :lon => 100.505073233221,
372 :type => "way", :id => 542901374, :zoom => 19
378 def latlon_check(query, lat, lon)
379 get search_path(:query => query)
380 assert_response :success
381 assert_template :search
382 assert_template :layout => "map"
383 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
384 assert_nil @controller.params[:query]
385 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lat])
386 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lon])
387 assert_in_delta lat, @controller.params[:lat].to_f
388 assert_in_delta lon, @controller.params[:lon].to_f
390 get search_path(:query => query), :xhr => true
391 assert_response :success
392 assert_template :search
393 assert_template :layout => "xhr"
394 assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
395 assert_nil @controller.params[:query]
396 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lat])
397 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lon])
398 assert_in_delta lat, @controller.params[:lat].to_f
399 assert_in_delta lon, @controller.params[:lon].to_f
402 def search_check(query, sources)
403 get search_path(:query => query)
404 assert_response :success
405 assert_template :search
406 assert_template :layout => "map"
407 assert_equal sources, assigns(:sources).pluck(:name)
409 get search_path(:query => query), :xhr => true
410 assert_response :success
411 assert_template :search
412 assert_template :layout => "xhr"
413 assert_equal sources, assigns(:sources).pluck(:name)
416 def results_check(*results)
417 assert_response :success
418 assert_template :create
419 assert_template :layout => nil
421 assert_select "ul.results-list", 0
423 assert_select "ul.results-list", 1 do
424 assert_select "li.search_results_entry", results.count
426 results.each do |result|
427 attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join
428 assert_select "li.search_results_entry a.set_position#{attrs}", result[:name]
434 def results_check_error(error)
435 assert_response :success
436 assert_template :error
437 assert_template :layout => nil
438 assert_select ".alert.alert-danger", error