]> git.openstreetmap.org Git - rails.git/blob - test/controllers/geocoder_controller_test.rb
Create latlon search query resource
[rails.git] / test / controllers / geocoder_controller_test.rb
1 require "test_helper"
2
3 class GeocoderControllerTest < ActionDispatch::IntegrationTest
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_osm_nominatim", :method => :post },
13       { :controller => "geocoder", :action => "search_osm_nominatim" }
14     )
15     assert_routing(
16       { :path => "/geocoder/search_osm_nominatim_reverse", :method => :post },
17       { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
18     )
19   end
20
21   ##
22   # Test identification with no arguments
23   def test_identify_error
24     get search_path
25     assert_response :bad_request
26
27     get search_path, :xhr => true
28     assert_response :bad_request
29   end
30
31   ##
32   # Test identification of basic lat/lon pairs
33   def test_identify_latlon_basic
34     [
35       "50.06773 14.37742",
36       "50.06773/14.37742",
37       "50.06773, 14.37742",
38       "+50.06773 +14.37742",
39       "+50.06773, +14.37742",
40       "+50.06773/+14.37742"
41     ].each do |code|
42       latlon_check code, 50.06773, 14.37742
43     end
44   end
45
46   ##
47   # Test identification of lat/lon pairs using N/E with degrees
48   def test_identify_latlon_ne_d
49     [
50       "N50.06773 E14.37742",
51       "N50.06773, E14.37742",
52       "50.06773N 14.37742E",
53       "50.06773N, 14.37742E"
54     ].each do |code|
55       latlon_check code, 50.06773, 14.37742
56     end
57   end
58
59   ##
60   # Test identification of integer lat/lon pairs using N/E with degrees
61   def test_identify_latlon_ne_d_int_deg
62     [
63       "N50 E14",
64       "N50° E14°",
65       "50N 14E",
66       "50°N 14°E"
67     ].each do |code|
68       latlon_check code, 50, 14
69     end
70   end
71
72   ##
73   # Test identification of lat/lon pairs using N/W with degrees
74   def test_identify_latlon_nw_d
75     [
76       "N50.06773 W14.37742",
77       "N50.06773, W14.37742",
78       "50.06773N 14.37742W",
79       "50.06773N, 14.37742W"
80     ].each do |code|
81       latlon_check code, 50.06773, -14.37742
82     end
83   end
84
85   ##
86   # Test identification of lat/lon pairs using S/E with degrees
87   def test_identify_latlon_se_d
88     [
89       "S50.06773 E14.37742",
90       "S50.06773, E14.37742",
91       "50.06773S 14.37742E",
92       "50.06773S, 14.37742E"
93     ].each do |code|
94       latlon_check code, -50.06773, 14.37742
95     end
96   end
97
98   ##
99   # Test identification of lat/lon pairs using S/W with degrees
100   def test_identify_latlon_sw_d
101     [
102       "S50.06773 W14.37742",
103       "S50.06773, W14.37742",
104       "50.06773S 14.37742W",
105       "50.06773S, 14.37742W"
106     ].each do |code|
107       latlon_check code, -50.06773, -14.37742
108     end
109   end
110
111   ##
112   # Test identification of lat/lon pairs using N/E with degrees/mins
113   def test_identify_latlon_ne_dm
114     [
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"
122     ].each do |code|
123       latlon_check code, 50.06773, 14.37742
124     end
125   end
126
127   ##
128   # Test identification of lat/lon pairs using N/W with degrees/mins
129   def test_identify_latlon_nw_dm
130     [
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"
138     ].each do |code|
139       latlon_check code, 50.06773, -14.37742
140     end
141   end
142
143   ##
144   # Test identification of lat/lon pairs using S/E with degrees/mins
145   def test_identify_latlon_se_dm
146     [
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"
154     ].each do |code|
155       latlon_check code, -50.06773, 14.37742
156     end
157   end
158
159   ##
160   # Test identification of lat/lon pairs using S/W with degrees/mins
161   def test_identify_latlon_sw_dm
162     [
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"
170     ].each do |code|
171       latlon_check code, -50.06773, -14.37742
172     end
173   end
174
175   ##
176   # Test identification of lat/lon pairs using N/E with degrees/mins/secs
177   def test_identify_latlon_ne_dms
178     [
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"
185     ].each do |code|
186       latlon_check code, 50.06773, 14.37742
187     end
188   end
189
190   ##
191   # Test identification of lat/lon pairs using N/W with degrees/mins/secs
192   def test_identify_latlon_nw_dms
193     [
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"
200     ].each do |code|
201       latlon_check code, 50.06773, -14.37742
202     end
203   end
204
205   ##
206   # Test identification of lat/lon pairs using S/E with degrees/mins/secs
207   def test_identify_latlon_se_dms
208     [
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"
215     ].each do |code|
216       latlon_check code, -50.06773, 14.37742
217     end
218   end
219
220   ##
221   # Test identification of lat/lon pairs using S/W with degrees/mins/secs
222   def test_identify_latlon_sw_dms
223     [
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"
230     ].each do |code|
231       latlon_check code, -50.06773, -14.37742
232     end
233   end
234
235   ##
236   # Test identification of lat/lon pairs with missing fractions
237   def test_no_identify_latlon_ne_missing_fraction_part
238     [
239       "N50. E14.",
240       "N50.° E14.°",
241       "50.N 14.E",
242       "50.°N 14.°E",
243       "N50 1.' E14 2.'",
244       "N50° 1.' E14° 2.'",
245       "50N 1.' 14 2.'E",
246       "50° 1.'N 14° 2.'E",
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"
251     ].each do |code|
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)
257     end
258   end
259
260   #
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
269   end
270
271   #
272   # Test identification of lat/lon pairs with values close to zero
273   def test_identify_latlon_close_to_zero
274     [
275       "0.0000123 -0.0000456",
276       "+0.0000123 -0.0000456",
277       "N 0° 0' 0.4428\", W 0° 0' 1.6416\""
278     ].each do |code|
279       latlon_check code, 0.0000123, -0.0000456
280     end
281   end
282
283   ##
284   # Test identification of US zipcodes
285   def test_identify_us_postcode
286     %w[
287       12345
288       12345-6789
289     ].each do |code|
290       search_check code, %w[osm_nominatim]
291     end
292   end
293
294   ##
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
298     [
299       "EC1A 1BB",
300       "W1A 1HQ",
301       "M1 1AA",
302       "B33 8TH",
303       "CR2 6XH",
304       "DN55 1PT"
305     ].each do |code|
306       search_check code, %w[osm_nominatim]
307     end
308   end
309
310   ##
311   # Test identification of Canadian postcodes
312   def test_identify_ca_postcode
313     search_check "A1B 2C3", %w[osm_nominatim]
314   end
315
316   ##
317   # Test identification fall through to the default case
318   def test_identify_default
319     search_check "foo bar baz", %w[osm_nominatim]
320   end
321
322   ##
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
333
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 })
352     end
353   end
354
355   ##
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
363
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
368
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
373     end
374   end
375
376   private
377
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
389
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
400   end
401
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)
408
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)
414   end
415
416   def results_check(*results)
417     assert_response :success
418     assert_template :create
419     assert_template :layout => nil
420     if results.empty?
421       assert_select "ul.results-list", 0
422     else
423       assert_select "ul.results-list", 1 do
424         assert_select "li.search_results_entry", results.count
425
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]
429         end
430       end
431     end
432   end
433
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
439   end
440 end