]> git.openstreetmap.org Git - rails.git/blob - test/controllers/geocoder_controller_test.rb
Add thunderforest API key to example configuration
[rails.git] / test / controllers / geocoder_controller_test.rb
1 # coding: utf-8
2
3 require "test_helper"
4 require "geocoder_controller"
5
6 class GeocoderControllerTest < ActionController::TestCase
7   ##
8   # test all routes which lead to this controller
9   def test_routes
10     assert_routing(
11       { :path => "/search", :method => :get },
12       { :controller => "geocoder", :action => "search" }
13     )
14     assert_routing(
15       { :path => "/geocoder/search_latlon", :method => :get },
16       { :controller => "geocoder", :action => "search_latlon" }
17     )
18     assert_routing(
19       { :path => "/geocoder/search_us_postcode", :method => :get },
20       { :controller => "geocoder", :action => "search_us_postcode" }
21     )
22     assert_routing(
23       { :path => "/geocoder/search_uk_postcode", :method => :get },
24       { :controller => "geocoder", :action => "search_uk_postcode" }
25     )
26     assert_routing(
27       { :path => "/geocoder/search_ca_postcode", :method => :get },
28       { :controller => "geocoder", :action => "search_ca_postcode" }
29     )
30     assert_routing(
31       { :path => "/geocoder/search_osm_nominatim", :method => :get },
32       { :controller => "geocoder", :action => "search_osm_nominatim" }
33     )
34     assert_routing(
35       { :path => "/geocoder/search_geonames", :method => :get },
36       { :controller => "geocoder", :action => "search_geonames" }
37     )
38     assert_routing(
39       { :path => "/geocoder/search_osm_nominatim_reverse", :method => :get },
40       { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
41     )
42     assert_routing(
43       { :path => "/geocoder/search_geonames_reverse", :method => :get },
44       { :controller => "geocoder", :action => "search_geonames_reverse" }
45     )
46   end
47
48   ##
49   # Test identification with no arguments
50   def test_identify_error
51     get :search
52     assert_response :bad_request
53
54     xhr :get, :search
55     assert_response :bad_request
56   end
57
58   ##
59   # Test identification of basic lat/lon pairs
60   def test_identify_latlon_basic
61     [
62       "50.06773 14.37742",
63       "50.06773, 14.37742",
64       "+50.06773 +14.37742",
65       "+50.06773, +14.37742"
66     ].each do |code|
67       latlon_check code, 50.06773, 14.37742
68     end
69   end
70
71   ##
72   # Test identification of lat/lon pairs using N/E with degrees
73   def test_identify_latlon_ne_d
74     [
75       "N50.06773 E14.37742",
76       "N50.06773, E14.37742",
77       "50.06773N 14.37742E",
78       "50.06773N, 14.37742E"
79     ].each do |code|
80       latlon_check code, 50.06773, 14.37742
81     end
82   end
83
84   ##
85   # Test identification of lat/lon pairs using N/W with degrees
86   def test_identify_latlon_nw_d
87     [
88       "N50.06773 W14.37742",
89       "N50.06773, W14.37742",
90       "50.06773N 14.37742W",
91       "50.06773N, 14.37742W"
92     ].each do |code|
93       latlon_check code, 50.06773, -14.37742
94     end
95   end
96
97   ##
98   # Test identification of lat/lon pairs using S/E with degrees
99   def test_identify_latlon_se_d
100     [
101       "S50.06773 E14.37742",
102       "S50.06773, E14.37742",
103       "50.06773S 14.37742E",
104       "50.06773S, 14.37742E"
105     ].each do |code|
106       latlon_check code, -50.06773, 14.37742
107     end
108   end
109
110   ##
111   # Test identification of lat/lon pairs using S/W with degrees
112   def test_identify_latlon_sw_d
113     [
114       "S50.06773 W14.37742",
115       "S50.06773, W14.37742",
116       "50.06773S 14.37742W",
117       "50.06773S, 14.37742W"
118     ].each do |code|
119       latlon_check code, -50.06773, -14.37742
120     end
121   end
122
123   ##
124   # Test identification of lat/lon pairs using N/E with degrees/mins
125   def test_identify_latlon_ne_dm
126     [
127       "N 50° 04.064 E 014° 22.645",
128       "N 50° 04.064' E 014° 22.645",
129       "N 50° 04.064', E 014° 22.645'",
130       "N50° 04.064 E14° 22.645",
131       "N 50 04.064 E 014 22.645",
132       "N50 4.064 E14 22.645",
133       "50° 04.064' N, 014° 22.645' E"
134     ].each do |code|
135       latlon_check code, 50.06773, 14.37742
136     end
137   end
138
139   ##
140   # Test identification of lat/lon pairs using N/W with degrees/mins
141   def test_identify_latlon_nw_dm
142     [
143       "N 50° 04.064 W 014° 22.645",
144       "N 50° 04.064' W 014° 22.645",
145       "N 50° 04.064', W 014° 22.645'",
146       "N50° 04.064 W14° 22.645",
147       "N 50 04.064 W 014 22.645",
148       "N50 4.064 W14 22.645",
149       "50° 04.064' N, 014° 22.645' W"
150     ].each do |code|
151       latlon_check code, 50.06773, -14.37742
152     end
153   end
154
155   ##
156   # Test identification of lat/lon pairs using S/E with degrees/mins
157   def test_identify_latlon_se_dm
158     [
159       "S 50° 04.064 E 014° 22.645",
160       "S 50° 04.064' E 014° 22.645",
161       "S 50° 04.064', E 014° 22.645'",
162       "S50° 04.064 E14° 22.645",
163       "S 50 04.064 E 014 22.645",
164       "S50 4.064 E14 22.645",
165       "50° 04.064' S, 014° 22.645' E"
166     ].each do |code|
167       latlon_check code, -50.06773, 14.37742
168     end
169   end
170
171   ##
172   # Test identification of lat/lon pairs using S/W with degrees/mins
173   def test_identify_latlon_sw_dm
174     [
175       "S 50° 04.064 W 014° 22.645",
176       "S 50° 04.064' W 014° 22.645",
177       "S 50° 04.064', W 014° 22.645'",
178       "S50° 04.064 W14° 22.645",
179       "S 50 04.064 W 014 22.645",
180       "S50 4.064 W14 22.645",
181       "50° 04.064' S, 014° 22.645' W"
182     ].each do |code|
183       latlon_check code, -50.06773, -14.37742
184     end
185   end
186
187   ##
188   # Test identification of lat/lon pairs using N/E with degrees/mins/secs
189   def test_identify_latlon_ne_dms
190     [
191       "N 50° 4' 03.828\" E 14° 22' 38.712\"",
192       "N 50° 4' 03.828\", E 14° 22' 38.712\"",
193       "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
194       "N50 4 03.828 E14 22 38.712",
195       "N50 4 03.828, E14 22 38.712",
196       "50°4'3.828\"N 14°22'38.712\"E"
197     ].each do |code|
198       latlon_check code, 50.06773, 14.37742
199     end
200   end
201
202   ##
203   # Test identification of lat/lon pairs using N/W with degrees/mins/secs
204   def test_identify_latlon_nw_dms
205     [
206       "N 50° 4' 03.828\" W 14° 22' 38.712\"",
207       "N 50° 4' 03.828\", W 14° 22' 38.712\"",
208       "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
209       "N50 4 03.828 W14 22 38.712",
210       "N50 4 03.828, W14 22 38.712",
211       "50°4'3.828\"N 14°22'38.712\"W"
212     ].each do |code|
213       latlon_check code, 50.06773, -14.37742
214     end
215   end
216
217   ##
218   # Test identification of lat/lon pairs using S/E with degrees/mins/secs
219   def test_identify_latlon_se_dms
220     [
221       "S 50° 4' 03.828\" E 14° 22' 38.712\"",
222       "S 50° 4' 03.828\", E 14° 22' 38.712\"",
223       "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
224       "S50 4 03.828 E14 22 38.712",
225       "S50 4 03.828, E14 22 38.712",
226       "50°4'3.828\"S 14°22'38.712\"E"
227     ].each do |code|
228       latlon_check code, -50.06773, 14.37742
229     end
230   end
231
232   ##
233   # Test identification of lat/lon pairs using S/W with degrees/mins/secs
234   def test_identify_latlon_sw_dms
235     [
236       "S 50° 4' 03.828\" W 14° 22' 38.712\"",
237       "S 50° 4' 03.828\", W 14° 22' 38.712\"",
238       "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
239       "S50 4 03.828 W14 22 38.712",
240       "S50 4 03.828, W14 22 38.712",
241       "50°4'3.828\"S 14°22'38.712\"W"
242     ].each do |code|
243       latlon_check code, -50.06773, -14.37742
244     end
245   end
246
247   ##
248   # Test identification of US zipcodes
249   def test_identify_us_postcode
250     [
251       "12345",
252       "12345-6789"
253     ].each do |code|
254       post :search, :query => code
255       assert_response :success
256       assert_equal %w(us_postcode osm_nominatim), assigns(:sources)
257     end
258   end
259
260   ##
261   # Test identification of UK postcodes using examples from
262   # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
263   def test_identify_uk_postcode
264     [
265       "EC1A 1BB",
266       "W1A 1HQ",
267       "M1 1AA",
268       "B33 8TH",
269       "CR2 6XH",
270       "DN55 1PT"
271     ].each do |code|
272       search_check code, %w(uk_postcode osm_nominatim)
273     end
274   end
275
276   ##
277   # Test identification of Canadian postcodes
278   def test_identify_ca_postcode
279     search_check "A1B 2C3", %w(ca_postcode osm_nominatim)
280   end
281
282   ##
283   # Test identification fall through to the default case
284   def test_identify_default
285     search_check "foo bar baz", %w(osm_nominatim geonames)
286   end
287
288   ##
289   # Test the builtin latitude+longitude search
290   def test_search_latlon
291     xhr :get, :search_latlon, :lat => 1.23, :lon => 4.56, :zoom => 16
292     results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
293
294     xhr :get, :search_latlon, :lat => -91.23, :lon => 4.56, :zoom => 16
295     results_check_error "Latitude -91.23 out of range"
296
297     xhr :get, :search_latlon, :lat => 91.23, :lon => 4.56, :zoom => 16
298     results_check_error "Latitude 91.23 out of range"
299
300     xhr :get, :search_latlon, :lat => 1.23, :lon => -180.23, :zoom => 16
301     results_check_error "Longitude -180.23 out of range"
302
303     xhr :get, :search_latlon, :lat => 1.23, :lon => 180.23, :zoom => 16
304     results_check_error "Longitude 180.23 out of range"
305   end
306
307   ##
308   # Test the US postcode search
309   def test_search_us_postcode
310     with_http_stubs "geocoder_us" do
311       xhr :get, :search_us_postcode,
312           :query => "90210", :zoom => 10,
313           :minlon => -0.559, :minlat => 51.217,
314           :maxlon => 0.836, :maxlat => 51.766
315       results_check :prefix => "Beverly Hills, CA,", :name => "90210",
316                     :lat => 34.088808, :lon => -118.40612
317
318       xhr :get, :search_us_postcode,
319           :query => "00000", :zoom => 10,
320           :minlon => -0.559, :minlat => 51.217,
321           :maxlon => 0.836, :maxlat => 51.766
322       results_check
323     end
324   end
325
326   ##
327   # Test the UK postcode search
328   def test_search_uk_postcode
329     with_http_stubs "npemap" do
330       xhr :get, :search_uk_postcode,
331           :query => "CV4 7AL", :zoom => 10,
332           :minlon => -0.559, :minlat => 51.217,
333           :maxlon => 0.836, :maxlat => 51.766
334       results_check :name => "CV4 7AL", :lat => 52.381748701968, :lon => -1.56176420939232
335
336       xhr :get, :search_uk_postcode,
337           :query => "XX9 9XX", :zoom => 10,
338           :minlon => -0.559, :minlat => 51.217,
339           :maxlon => 0.836, :maxlat => 51.766
340       results_check
341     end
342   end
343
344   ##
345   # Test the Canadian postcode search
346   def test_search_ca_postcode
347     with_http_stubs "geocoder_ca" do
348       xhr :get, :search_ca_postcode,
349           :query => "A1B 2C3", :zoom => 10,
350           :minlon => -0.559, :minlat => 51.217,
351           :maxlon => 0.836, :maxlat => 51.766
352       results_check :name => "A1B 2C3", :lat => "47.172520", :lon => "-55.440515"
353
354       xhr :get, :search_ca_postcode,
355           :query => "k1a 0b1", :zoom => 10,
356           :minlon => -0.559, :minlat => 51.217,
357           :maxlon => 0.836, :maxlat => 51.766
358       results_check :name => "K1A 0B1", :lat => "45.375437", :lon => "-75.691041"
359
360       xhr :get, :search_ca_postcode,
361           :query => "Q0Q 0Q0", :zoom => 10,
362           :minlon => -0.559, :minlat => 51.217,
363           :maxlon => 0.836, :maxlat => 51.766
364       results_check
365     end
366   end
367
368   ##
369   # Test the nominatim forward search
370   def test_search_osm_nominatim
371     with_http_stubs "nominatim" do
372       xhr :get, :search_osm_nominatim,
373           :query => "Hoddesdon", :zoom => 10,
374           :minlon => -0.559, :minlat => 51.217,
375           :maxlon => 0.836, :maxlat => 51.766
376       results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
377                     "min-lat" => 51.7216709, "max-lat" => 51.8016709,
378                     "min-lon" => -0.0512898, "max-lon" => 0.0287102,
379                     "type" => "node", "id" => 18007599
380
381       xhr :get, :search_osm_nominatim,
382           :query => "Broxbourne", :zoom => 10,
383           :minlon => -0.559, :minlat => 51.217,
384           :maxlon => 0.836, :maxlat => 51.766
385       results_check({ "prefix" => "Suburb",
386                       "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
387                       "min-lat" => 51.7265723, "max-lat" => 51.7665723,
388                       "min-lon" => -0.0390782, "max-lon" => 0.0009218,
389                       "type" => "node", "id" => 28825933 },
390                     { "prefix" => "City Boundary",
391                       "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
392                       "min-lat" => 51.6808751, "max-lat" => 51.7806237,
393                       "min-lon" => -0.114204, "max-lon" => 0.0145267,
394                       "type" => "relation", "id" => 2677978 },
395                     { "prefix" => "Railway Station",
396                       "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
397                       "min-lat" => 51.7418469, "max-lat" => 51.7518469,
398                       "min-lon" => -0.0156773, "max-lon" => -0.0056773,
399                       "type" => "node", "id" => 17044599 })
400     end
401   end
402
403   ##
404   # Test the geonames forward search
405   def test_search_geonames
406     with_http_stubs "geonames" do
407       xhr :get, :search_geonames,
408           :query => "Hoddesdon", :zoom => 10,
409           :minlon => -0.559, :minlat => 51.217,
410           :maxlon => 0.836, :maxlat => 51.766
411       results_check :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144
412
413       xhr :get, :search_geonames,
414           :query => "Broxbourne", :zoom => 10,
415           :minlon => -0.559, :minlat => 51.217,
416           :maxlon => 0.836, :maxlat => 51.766
417       results_check({ :name => "Broxbourne", :lat => 51.74712, :lon => -0.01923 },
418                     { :name => "Broxbourne District", :lat => 51.73026, :lon => -0.04821 },
419                     { :name => "Cheshunt", :lat => 51.70791, :lon => -0.03739 },
420                     { :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144 },
421                     { :name => "Waltham Cross", :lat => 51.68905, :lon => -0.0333 },
422                     { :name => "Goffs Oak", :lat => 51.71015, :lon => -0.0872 },
423                     { :name => "Wormley", :lat => 51.7324, :lon => -0.0242 },
424                     { :name => "Broxbourne", :lat => -27.50314, :lon => 151.378 },
425                     { :name => "Lee Valley White Water Centre", :lat => 51.68814, :lon => -0.01682 },
426                     { :name => "Cheshunt Railway Station", :lat => 51.703, :lon => -0.024 },
427                     { :name => "Theobalds Grove Railway Station", :lat => 51.692, :lon => -0.035 },
428                     { :name => "Waltham Cross Railway Station", :lat => 51.685, :lon => -0.027 },
429                     { :name => "Rye House Station", :lat => 51.76938, :lon => 0.00562 },
430                     { :name => "Broxbourne Station", :lat => 51.74697, :lon => -0.01105 },
431                     { :name => "Broxbornebury Park", :lat => 51.75252, :lon => -0.03839 },
432                     { :name => "Marriott Cheshunt", :lat => 51.7208, :lon => -0.0324 },
433                     { :name => "Cheshunt Community Hospital", :lat => 51.68396, :lon => -0.03951 })
434     end
435   end
436
437   ##
438   # Test the nominatim reverse search
439   def test_search_osm_nominatim_reverse
440     with_http_stubs "nominatim" do
441       xhr :get, :search_osm_nominatim_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 15
442       results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
443                     :lat => 51.7465723, :lon => -0.0190782,
444                     :type => "node", :id => 28825933, :zoom => 15
445
446       xhr :get, :search_osm_nominatim_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 17
447       results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
448                     :lat => 51.7634883, :lon => -0.0088373,
449                     :type => "way", :id => 3489841, :zoom => 17
450     end
451   end
452
453   ##
454   # Test the geonames reverse search
455   def test_search_geonames_reverse
456     with_http_stubs "geonames" do
457       xhr :get, :search_geonames_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 15
458       results_check :name => "England", :suffix => ", United Kingdom",
459                     :lat => 51.7632, :lon => -0.0076
460     end
461   end
462
463   private
464
465   def latlon_check(query, lat, lon)
466     get :search, :query => query
467     assert_response :success
468     assert_template :search
469     assert_template :layout => "map"
470     assert_equal %w(latlon osm_nominatim_reverse geonames_reverse), assigns(:sources)
471     assert_nil @controller.params[:query]
472     assert_in_delta lat, @controller.params[:lat]
473     assert_in_delta lon, @controller.params[:lon]
474
475     xhr :get, :search, :query => query
476     assert_response :success
477     assert_template :search
478     assert_template :layout => "xhr"
479     assert_equal %w(latlon osm_nominatim_reverse geonames_reverse), assigns(:sources)
480     assert_nil @controller.params[:query]
481     assert_in_delta lat, @controller.params[:lat]
482     assert_in_delta lon, @controller.params[:lon]
483   end
484
485   def search_check(query, sources)
486     get :search, :query => query
487     assert_response :success
488     assert_template :search
489     assert_template :layout => "map"
490     assert_equal sources, assigns(:sources)
491
492     xhr :get, :search, :query => query
493     assert_response :success
494     assert_template :search
495     assert_template :layout => "xhr"
496     assert_equal sources, assigns(:sources)
497   end
498
499   def results_check(*results)
500     assert_response :success
501     assert_template :results
502     assert_template :layout => nil
503     if results.empty?
504       assert_select "ul.results-list", 0
505     else
506       assert_select "ul.results-list", 1 do
507         assert_select "p.search_results_entry", results.count
508
509         results.each do |result|
510           attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join("")
511           assert_select "p.search_results_entry a.set_position#{attrs}", result[:name]
512         end
513       end
514     end
515   end
516
517   def results_check_error(error)
518     assert_response :success
519     assert_template :error
520     assert_template :layout => nil
521     assert_select "p.search_results_error", error
522   end
523 end