]> git.openstreetmap.org Git - rails.git/blob - test/controllers/searches_controller_test.rb
Localisation updates from https://translatewiki.net.
[rails.git] / test / controllers / searches_controller_test.rb
1 require "test_helper"
2
3 class SearchesControllerTest < 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 => "searches", :action => "show" }
10     )
11   end
12
13   ##
14   # Test identification with no arguments
15   def test_identify_error
16     get search_path
17     assert_response :bad_request
18
19     get search_path, :xhr => true
20     assert_response :bad_request
21   end
22
23   ##
24   # Test identification of basic lat/lon pairs
25   def test_identify_latlon_basic
26     [
27       "50.06773 14.37742",
28       "50.06773/14.37742",
29       "50.06773, 14.37742",
30       "+50.06773 +14.37742",
31       "+50.06773, +14.37742",
32       "+50.06773/+14.37742"
33     ].each do |code|
34       latlon_check code, 50.06773, 14.37742
35     end
36   end
37
38   ##
39   # Test identification of lat/lon pairs using N/E with degrees
40   def test_identify_latlon_ne_d
41     [
42       "N50.06773 E14.37742",
43       "N50.06773, E14.37742",
44       "50.06773N 14.37742E",
45       "50.06773N, 14.37742E"
46     ].each do |code|
47       latlon_check code, 50.06773, 14.37742
48     end
49   end
50
51   ##
52   # Test identification of integer lat/lon pairs using N/E with degrees
53   def test_identify_latlon_ne_d_int_deg
54     [
55       "N50 E14",
56       "N50° E14°",
57       "50N 14E",
58       "50°N 14°E"
59     ].each do |code|
60       latlon_check code, 50, 14
61     end
62   end
63
64   ##
65   # Test identification of lat/lon pairs using N/W with degrees
66   def test_identify_latlon_nw_d
67     [
68       "N50.06773 W14.37742",
69       "N50.06773, W14.37742",
70       "50.06773N 14.37742W",
71       "50.06773N, 14.37742W"
72     ].each do |code|
73       latlon_check code, 50.06773, -14.37742
74     end
75   end
76
77   ##
78   # Test identification of lat/lon pairs using S/E with degrees
79   def test_identify_latlon_se_d
80     [
81       "S50.06773 E14.37742",
82       "S50.06773, E14.37742",
83       "50.06773S 14.37742E",
84       "50.06773S, 14.37742E"
85     ].each do |code|
86       latlon_check code, -50.06773, 14.37742
87     end
88   end
89
90   ##
91   # Test identification of lat/lon pairs using S/W with degrees
92   def test_identify_latlon_sw_d
93     [
94       "S50.06773 W14.37742",
95       "S50.06773, W14.37742",
96       "50.06773S 14.37742W",
97       "50.06773S, 14.37742W"
98     ].each do |code|
99       latlon_check code, -50.06773, -14.37742
100     end
101   end
102
103   ##
104   # Test identification of lat/lon pairs using N/E with degrees/mins
105   def test_identify_latlon_ne_dm
106     [
107       "N 50° 04.064 E 014° 22.645",
108       "N 50° 04.064' E 014° 22.645",
109       "N 50° 04.064', E 014° 22.645'",
110       "N50° 04.064 E14° 22.645",
111       "N 50 04.064 E 014 22.645",
112       "N50 4.064 E14 22.645",
113       "50° 04.064' N, 014° 22.645' E"
114     ].each do |code|
115       latlon_check code, 50.06773, 14.37742
116     end
117   end
118
119   ##
120   # Test identification of lat/lon pairs using N/W with degrees/mins
121   def test_identify_latlon_nw_dm
122     [
123       "N 50° 04.064 W 014° 22.645",
124       "N 50° 04.064' W 014° 22.645",
125       "N 50° 04.064', W 014° 22.645'",
126       "N50° 04.064 W14° 22.645",
127       "N 50 04.064 W 014 22.645",
128       "N50 4.064 W14 22.645",
129       "50° 04.064' N, 014° 22.645' W"
130     ].each do |code|
131       latlon_check code, 50.06773, -14.37742
132     end
133   end
134
135   ##
136   # Test identification of lat/lon pairs using S/E with degrees/mins
137   def test_identify_latlon_se_dm
138     [
139       "S 50° 04.064 E 014° 22.645",
140       "S 50° 04.064' E 014° 22.645",
141       "S 50° 04.064', E 014° 22.645'",
142       "S50° 04.064 E14° 22.645",
143       "S 50 04.064 E 014 22.645",
144       "S50 4.064 E14 22.645",
145       "50° 04.064' S, 014° 22.645' E"
146     ].each do |code|
147       latlon_check code, -50.06773, 14.37742
148     end
149   end
150
151   ##
152   # Test identification of lat/lon pairs using S/W with degrees/mins
153   def test_identify_latlon_sw_dm
154     [
155       "S 50° 04.064 W 014° 22.645",
156       "S 50° 04.064' W 014° 22.645",
157       "S 50° 04.064', W 014° 22.645'",
158       "S50° 04.064 W14° 22.645",
159       "S 50 04.064 W 014 22.645",
160       "S50 4.064 W14 22.645",
161       "50° 04.064' S, 014° 22.645' W"
162     ].each do |code|
163       latlon_check code, -50.06773, -14.37742
164     end
165   end
166
167   ##
168   # Test identification of lat/lon pairs using N/E with degrees/mins/secs
169   def test_identify_latlon_ne_dms
170     [
171       "N 50° 4' 03.828\" E 14° 22' 38.712\"",
172       "N 50° 4' 03.828\", E 14° 22' 38.712\"",
173       "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
174       "N50 4 03.828 E14 22 38.712",
175       "N50 4 03.828, E14 22 38.712",
176       "50°4'3.828\"N 14°22'38.712\"E"
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/W with degrees/mins/secs
184   def test_identify_latlon_nw_dms
185     [
186       "N 50° 4' 03.828\" W 14° 22' 38.712\"",
187       "N 50° 4' 03.828\", W 14° 22' 38.712\"",
188       "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
189       "N50 4 03.828 W14 22 38.712",
190       "N50 4 03.828, W14 22 38.712",
191       "50°4'3.828\"N 14°22'38.712\"W"
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 S/E with degrees/mins/secs
199   def test_identify_latlon_se_dms
200     [
201       "S 50° 4' 03.828\" E 14° 22' 38.712\"",
202       "S 50° 4' 03.828\", E 14° 22' 38.712\"",
203       "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
204       "S50 4 03.828 E14 22 38.712",
205       "S50 4 03.828, E14 22 38.712",
206       "50°4'3.828\"S 14°22'38.712\"E"
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/W with degrees/mins/secs
214   def test_identify_latlon_sw_dms
215     [
216       "S 50° 4' 03.828\" W 14° 22' 38.712\"",
217       "S 50° 4' 03.828\", W 14° 22' 38.712\"",
218       "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
219       "S50 4 03.828 W14 22 38.712",
220       "S50 4 03.828, W14 22 38.712",
221       "50°4'3.828\"S 14°22'38.712\"W"
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 with missing fractions
229   def test_no_identify_latlon_ne_missing_fraction_part
230     [
231       "N50. E14.",
232       "N50.° E14.°",
233       "50.N 14.E",
234       "50.°N 14.°E",
235       "N50 1.' E14 2.'",
236       "N50° 1.' E14° 2.'",
237       "50N 1.' 14 2.'E",
238       "50° 1.'N 14° 2.'E",
239       "N50 1' 3,\" E14 2' 4.\"",
240       "N50° 1' 3.\" E14° 2' 4.\"",
241       "50N 1' 3.\" 14 2' 4.\"E",
242       "50° 1' 3.\"N 14° 2' 4.\"E"
243     ].each do |code|
244       get search_path(:query => code)
245       assert_response :success
246       assert_template :show
247       assert_template :layout => "map"
248       assert_equal %w[nominatim], assigns(:sources).pluck(:name)
249     end
250   end
251
252   #
253   # Test identification of lat/lon pairs with mixed precision
254   def test_identify_latlon_ne_mixed_precision
255     latlon_check "N1 5 E15",    1.083333, 15
256     latlon_check "N1 5 9 E15",  1.085833, 15
257     latlon_check "N1 5 9 E1 5", 1.085833, 1.083333
258     latlon_check "N15 E1 5",    15, 1.083333
259     latlon_check "N15 E1 5 9",  15, 1.085833
260     latlon_check "N1 5 E1 5 9", 1.083333, 1.085833
261   end
262
263   #
264   # Test identification of lat/lon pairs with values close to zero
265   def test_identify_latlon_close_to_zero
266     [
267       "0.0000123 -0.0000456",
268       "+0.0000123 -0.0000456",
269       "N 0° 0' 0.4428\", W 0° 0' 1.6416\""
270     ].each do |code|
271       latlon_check code, 0.0000123, -0.0000456
272     end
273   end
274
275   ##
276   # Test identification of US zipcodes
277   def test_identify_us_postcode
278     %w[
279       12345
280       12345-6789
281     ].each do |code|
282       search_check code, %w[nominatim]
283     end
284   end
285
286   ##
287   # Test identification of UK postcodes using examples from
288   # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
289   def test_identify_uk_postcode
290     [
291       "EC1A 1BB",
292       "W1A 1HQ",
293       "M1 1AA",
294       "B33 8TH",
295       "CR2 6XH",
296       "DN55 1PT"
297     ].each do |code|
298       search_check code, %w[nominatim]
299     end
300   end
301
302   ##
303   # Test identification of Canadian postcodes
304   def test_identify_ca_postcode
305     search_check "A1B 2C3", %w[nominatim]
306   end
307
308   ##
309   # Test identification fall through to the default case
310   def test_identify_default
311     search_check "foo bar baz", %w[nominatim]
312   end
313
314   ##
315   # Test the nominatim reverse JSON search
316   def test_search_osm_nominatim_reverse_json
317     with_http_stubs "nominatim" do
318       post search_nominatim_reverse_query_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15, :format => "json"), :xhr => true
319       result_name_check_json("Broxbourne, Hertfordshire, East of England, England, United Kingdom")
320
321       post search_nominatim_reverse_query_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17, :format => "json"), :xhr => true
322       result_name_check_json("Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom")
323
324       post search_nominatim_reverse_query_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19, :format => "json"), :xhr => true
325       result_name_check_json("MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย")
326     end
327   end
328
329   private
330
331   def latlon_check(query, lat, lon)
332     get search_path(:query => query)
333     assert_response :success
334     assert_template :show
335     assert_template :layout => "map"
336     assert_equal %w[latlon nominatim_reverse], assigns(:sources).pluck(:name)
337     assert_nil @controller.params[:query]
338     assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lat])
339     assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lon])
340     assert_in_delta lat, @controller.params[:lat].to_f
341     assert_in_delta lon, @controller.params[:lon].to_f
342
343     get search_path(:query => query), :xhr => true
344     assert_response :success
345     assert_template :show
346     assert_template :layout => "xhr"
347     assert_equal %w[latlon nominatim_reverse], assigns(:sources).pluck(:name)
348     assert_nil @controller.params[:query]
349     assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lat])
350     assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lon])
351     assert_in_delta lat, @controller.params[:lat].to_f
352     assert_in_delta lon, @controller.params[:lon].to_f
353   end
354
355   def search_check(query, sources)
356     get search_path(:query => query)
357     assert_response :success
358     assert_template :show
359     assert_template :layout => "map"
360     assert_equal sources, assigns(:sources).pluck(:name)
361
362     get search_path(:query => query), :xhr => true
363     assert_response :success
364     assert_template :show
365     assert_template :layout => "xhr"
366     assert_equal sources, assigns(:sources).pluck(:name)
367   end
368
369   def result_name_check_json(name)
370     assert_response :success
371     js = ActiveSupport::JSON.decode(@response.body)
372     assert_not_nil js
373     assert_equal name, js[0]["name"]
374   end
375 end