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