]> git.openstreetmap.org Git - rails.git/blob - test/controllers/geocoder_controller_test.rb
Refactor factories to use FactoryBot
[rails.git] / test / controllers / geocoder_controller_test.rb
1
2 require "test_helper"
3 require "geocoder_controller"
4
5 class GeocoderControllerTest < ActionController::TestCase
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/search", :method => :get },
11       { :controller => "geocoder", :action => "search" }
12     )
13     assert_routing(
14       { :path => "/geocoder/search_latlon", :method => :get },
15       { :controller => "geocoder", :action => "search_latlon" }
16     )
17     assert_routing(
18       { :path => "/geocoder/search_uk_postcode", :method => :get },
19       { :controller => "geocoder", :action => "search_uk_postcode" }
20     )
21     assert_routing(
22       { :path => "/geocoder/search_ca_postcode", :method => :get },
23       { :controller => "geocoder", :action => "search_ca_postcode" }
24     )
25     assert_routing(
26       { :path => "/geocoder/search_osm_nominatim", :method => :get },
27       { :controller => "geocoder", :action => "search_osm_nominatim" }
28     )
29     assert_routing(
30       { :path => "/geocoder/search_geonames", :method => :get },
31       { :controller => "geocoder", :action => "search_geonames" }
32     )
33     assert_routing(
34       { :path => "/geocoder/search_osm_nominatim_reverse", :method => :get },
35       { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
36     )
37     assert_routing(
38       { :path => "/geocoder/search_geonames_reverse", :method => :get },
39       { :controller => "geocoder", :action => "search_geonames_reverse" }
40     )
41   end
42
43   ##
44   # Test identification with no arguments
45   def test_identify_error
46     get :search
47     assert_response :bad_request
48
49     get :search, :xhr => true
50     assert_response :bad_request
51   end
52
53   ##
54   # Test identification of basic lat/lon pairs
55   def test_identify_latlon_basic
56     [
57       "50.06773 14.37742",
58       "50.06773, 14.37742",
59       "+50.06773 +14.37742",
60       "+50.06773, +14.37742"
61     ].each do |code|
62       latlon_check code, 50.06773, 14.37742
63     end
64   end
65
66   ##
67   # Test identification of lat/lon pairs using N/E with degrees
68   def test_identify_latlon_ne_d
69     [
70       "N50.06773 E14.37742",
71       "N50.06773, E14.37742",
72       "50.06773N 14.37742E",
73       "50.06773N, 14.37742E"
74     ].each do |code|
75       latlon_check code, 50.06773, 14.37742
76     end
77   end
78
79   ##
80   # Test identification of lat/lon pairs using N/W with degrees
81   def test_identify_latlon_nw_d
82     [
83       "N50.06773 W14.37742",
84       "N50.06773, W14.37742",
85       "50.06773N 14.37742W",
86       "50.06773N, 14.37742W"
87     ].each do |code|
88       latlon_check code, 50.06773, -14.37742
89     end
90   end
91
92   ##
93   # Test identification of lat/lon pairs using S/E with degrees
94   def test_identify_latlon_se_d
95     [
96       "S50.06773 E14.37742",
97       "S50.06773, E14.37742",
98       "50.06773S 14.37742E",
99       "50.06773S, 14.37742E"
100     ].each do |code|
101       latlon_check code, -50.06773, 14.37742
102     end
103   end
104
105   ##
106   # Test identification of lat/lon pairs using S/W with degrees
107   def test_identify_latlon_sw_d
108     [
109       "S50.06773 W14.37742",
110       "S50.06773, W14.37742",
111       "50.06773S 14.37742W",
112       "50.06773S, 14.37742W"
113     ].each do |code|
114       latlon_check code, -50.06773, -14.37742
115     end
116   end
117
118   ##
119   # Test identification of lat/lon pairs using N/E with degrees/mins
120   def test_identify_latlon_ne_dm
121     [
122       "N 50° 04.064 E 014° 22.645",
123       "N 50° 04.064' E 014° 22.645",
124       "N 50° 04.064', E 014° 22.645'",
125       "N50° 04.064 E14° 22.645",
126       "N 50 04.064 E 014 22.645",
127       "N50 4.064 E14 22.645",
128       "50° 04.064' N, 014° 22.645' E"
129     ].each do |code|
130       latlon_check code, 50.06773, 14.37742
131     end
132   end
133
134   ##
135   # Test identification of lat/lon pairs using N/W with degrees/mins
136   def test_identify_latlon_nw_dm
137     [
138       "N 50° 04.064 W 014° 22.645",
139       "N 50° 04.064' W 014° 22.645",
140       "N 50° 04.064', W 014° 22.645'",
141       "N50° 04.064 W14° 22.645",
142       "N 50 04.064 W 014 22.645",
143       "N50 4.064 W14 22.645",
144       "50° 04.064' N, 014° 22.645' W"
145     ].each do |code|
146       latlon_check code, 50.06773, -14.37742
147     end
148   end
149
150   ##
151   # Test identification of lat/lon pairs using S/E with degrees/mins
152   def test_identify_latlon_se_dm
153     [
154       "S 50° 04.064 E 014° 22.645",
155       "S 50° 04.064' E 014° 22.645",
156       "S 50° 04.064', E 014° 22.645'",
157       "S50° 04.064 E14° 22.645",
158       "S 50 04.064 E 014 22.645",
159       "S50 4.064 E14 22.645",
160       "50° 04.064' S, 014° 22.645' E"
161     ].each do |code|
162       latlon_check code, -50.06773, 14.37742
163     end
164   end
165
166   ##
167   # Test identification of lat/lon pairs using S/W with degrees/mins
168   def test_identify_latlon_sw_dm
169     [
170       "S 50° 04.064 W 014° 22.645",
171       "S 50° 04.064' W 014° 22.645",
172       "S 50° 04.064', W 014° 22.645'",
173       "S50° 04.064 W14° 22.645",
174       "S 50 04.064 W 014 22.645",
175       "S50 4.064 W14 22.645",
176       "50° 04.064' S, 014° 22.645' W"
177     ].each do |code|
178       latlon_check code, -50.06773, -14.37742
179     end
180   end
181
182   ##
183   # Test identification of lat/lon pairs using N/E with degrees/mins/secs
184   def test_identify_latlon_ne_dms
185     [
186       "N 50° 4' 03.828\" E 14° 22' 38.712\"",
187       "N 50° 4' 03.828\", E 14° 22' 38.712\"",
188       "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
189       "N50 4 03.828 E14 22 38.712",
190       "N50 4 03.828, E14 22 38.712",
191       "50°4'3.828\"N 14°22'38.712\"E"
192     ].each do |code|
193       latlon_check code, 50.06773, 14.37742
194     end
195   end
196
197   ##
198   # Test identification of lat/lon pairs using N/W with degrees/mins/secs
199   def test_identify_latlon_nw_dms
200     [
201       "N 50° 4' 03.828\" W 14° 22' 38.712\"",
202       "N 50° 4' 03.828\", W 14° 22' 38.712\"",
203       "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
204       "N50 4 03.828 W14 22 38.712",
205       "N50 4 03.828, W14 22 38.712",
206       "50°4'3.828\"N 14°22'38.712\"W"
207     ].each do |code|
208       latlon_check code, 50.06773, -14.37742
209     end
210   end
211
212   ##
213   # Test identification of lat/lon pairs using S/E with degrees/mins/secs
214   def test_identify_latlon_se_dms
215     [
216       "S 50° 4' 03.828\" E 14° 22' 38.712\"",
217       "S 50° 4' 03.828\", E 14° 22' 38.712\"",
218       "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
219       "S50 4 03.828 E14 22 38.712",
220       "S50 4 03.828, E14 22 38.712",
221       "50°4'3.828\"S 14°22'38.712\"E"
222     ].each do |code|
223       latlon_check code, -50.06773, 14.37742
224     end
225   end
226
227   ##
228   # Test identification of lat/lon pairs using S/W with degrees/mins/secs
229   def test_identify_latlon_sw_dms
230     [
231       "S 50° 4' 03.828\" W 14° 22' 38.712\"",
232       "S 50° 4' 03.828\", W 14° 22' 38.712\"",
233       "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
234       "S50 4 03.828 W14 22 38.712",
235       "S50 4 03.828, W14 22 38.712",
236       "50°4'3.828\"S 14°22'38.712\"W"
237     ].each do |code|
238       latlon_check code, -50.06773, -14.37742
239     end
240   end
241
242   ##
243   # Test identification of US zipcodes
244   def test_identify_us_postcode
245     [
246       "12345",
247       "12345-6789"
248     ].each do |code|
249       post :search, :params => { :query => code }
250       assert_response :success
251       assert_equal %w[osm_nominatim], assigns(:sources)
252     end
253   end
254
255   ##
256   # Test identification of UK postcodes using examples from
257   # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
258   def test_identify_uk_postcode
259     [
260       "EC1A 1BB",
261       "W1A 1HQ",
262       "M1 1AA",
263       "B33 8TH",
264       "CR2 6XH",
265       "DN55 1PT"
266     ].each do |code|
267       search_check code, %w[uk_postcode osm_nominatim]
268     end
269   end
270
271   ##
272   # Test identification of Canadian postcodes
273   def test_identify_ca_postcode
274     search_check "A1B 2C3", %w[ca_postcode osm_nominatim]
275   end
276
277   ##
278   # Test identification fall through to the default case
279   def test_identify_default
280     search_check "foo bar baz", %w[osm_nominatim geonames]
281   end
282
283   ##
284   # Test the builtin latitude+longitude search
285   def test_search_latlon
286     get :search_latlon, :params => { :lat => 1.23, :lon => 4.56, :zoom => 16 }, :xhr => true
287     results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
288
289     get :search_latlon, :params => { :lat => -91.23, :lon => 4.56, :zoom => 16 }, :xhr => true
290     results_check_error "Latitude -91.23 out of range"
291
292     get :search_latlon, :params => { :lat => 91.23, :lon => 4.56, :zoom => 16 }, :xhr => true
293     results_check_error "Latitude 91.23 out of range"
294
295     get :search_latlon, :params => { :lat => 1.23, :lon => -180.23, :zoom => 16 }, :xhr => true
296     results_check_error "Longitude -180.23 out of range"
297
298     get :search_latlon, :params => { :lat => 1.23, :lon => 180.23, :zoom => 16 }, :xhr => true
299     results_check_error "Longitude 180.23 out of range"
300   end
301
302   ##
303   # Test the UK postcode search
304   def test_search_uk_postcode
305     with_http_stubs "npemap" do
306       get :search_uk_postcode, :xhr => true,
307                                :params => { :query => "CV4 7AL", :zoom => 10,
308                                             :minlon => -0.559, :minlat => 51.217,
309                                             :maxlon => 0.836, :maxlat => 51.766 }
310       results_check :name => "CV4 7AL", :lat => 52.381748701968, :lon => -1.56176420939232
311
312       get :search_uk_postcode, :xhr => true,
313                                :params => { :query => "XX9 9XX", :zoom => 10,
314                                             :minlon => -0.559, :minlat => 51.217,
315                                             :maxlon => 0.836, :maxlat => 51.766 }
316       results_check
317     end
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     end
429   end
430
431   ##
432   # Test the geonames reverse search
433   def test_search_geonames_reverse
434     with_http_stubs "geonames" do
435       get :search_geonames_reverse, :xhr => true,
436                                     :params => { :lat => 51.7632, :lon => -0.0076, :zoom => 15 }
437       results_check :name => "England", :suffix => ", United Kingdom",
438                     :lat => 51.7632, :lon => -0.0076
439     end
440   end
441
442   private
443
444   def latlon_check(query, lat, lon)
445     get :search, :params => { :query => query }
446     assert_response :success
447     assert_template :search
448     assert_template :layout => "map"
449     assert_equal %w[latlon osm_nominatim_reverse geonames_reverse], assigns(:sources)
450     assert_nil @controller.params[:query]
451     assert_in_delta lat, @controller.params[:lat]
452     assert_in_delta lon, @controller.params[:lon]
453
454     get :search, :params => { :query => query }, :xhr => true
455     assert_response :success
456     assert_template :search
457     assert_template :layout => "xhr"
458     assert_equal %w[latlon osm_nominatim_reverse geonames_reverse], assigns(:sources)
459     assert_nil @controller.params[:query]
460     assert_in_delta lat, @controller.params[:lat]
461     assert_in_delta lon, @controller.params[:lon]
462   end
463
464   def search_check(query, sources)
465     get :search, :params => { :query => query }
466     assert_response :success
467     assert_template :search
468     assert_template :layout => "map"
469     assert_equal sources, assigns(:sources)
470
471     get :search, :params => { :query => query }, :xhr => true
472     assert_response :success
473     assert_template :search
474     assert_template :layout => "xhr"
475     assert_equal sources, assigns(:sources)
476   end
477
478   def results_check(*results)
479     assert_response :success
480     assert_template :results
481     assert_template :layout => nil
482     if results.empty?
483       assert_select "ul.results-list", 0
484     else
485       assert_select "ul.results-list", 1 do
486         assert_select "p.search_results_entry", results.count
487
488         results.each do |result|
489           attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join("")
490           assert_select "p.search_results_entry a.set_position#{attrs}", result[:name]
491         end
492       end
493     end
494   end
495
496   def results_check_error(error)
497     assert_response :success
498     assert_template :error
499     assert_template :layout => nil
500     assert_select "p.search_results_error", error
501   end
502 end