1 # frozen_string_literal: true
5 class SearchesControllerTest < ActionDispatch::IntegrationTest
7 # test all routes which lead to this controller
10 { :path => "/search", :method => :get },
11 { :controller => "searches", :action => "show" }
16 # Test identification with no arguments
17 def test_identify_error
19 assert_response :bad_request
21 get search_path, :xhr => true
22 assert_response :bad_request
26 # Test identification of basic lat/lon pairs
27 def test_identify_latlon_basic
32 "+50.06773 +14.37742",
33 "+50.06773, +14.37742",
36 latlon_check code, 50.06773, 14.37742
41 # Test identification of lat/lon pairs using N/E with degrees
42 def test_identify_latlon_ne_d
44 "N50.06773 E14.37742",
45 "N50.06773, E14.37742",
46 "50.06773N 14.37742E",
47 "50.06773N, 14.37742E"
49 latlon_check code, 50.06773, 14.37742
54 # Test identification of integer lat/lon pairs using N/E with degrees
55 def test_identify_latlon_ne_d_int_deg
62 latlon_check code, 50, 14
67 # Test identification of lat/lon pairs using N/W with degrees
68 def test_identify_latlon_nw_d
70 "N50.06773 W14.37742",
71 "N50.06773, W14.37742",
72 "50.06773N 14.37742W",
73 "50.06773N, 14.37742W"
75 latlon_check code, 50.06773, -14.37742
80 # Test identification of lat/lon pairs using S/E with degrees
81 def test_identify_latlon_se_d
83 "S50.06773 E14.37742",
84 "S50.06773, E14.37742",
85 "50.06773S 14.37742E",
86 "50.06773S, 14.37742E"
88 latlon_check code, -50.06773, 14.37742
93 # Test identification of lat/lon pairs using S/W with degrees
94 def test_identify_latlon_sw_d
96 "S50.06773 W14.37742",
97 "S50.06773, W14.37742",
98 "50.06773S 14.37742W",
99 "50.06773S, 14.37742W"
101 latlon_check code, -50.06773, -14.37742
106 # Test identification of lat/lon pairs using N/E with degrees/mins
107 def test_identify_latlon_ne_dm
109 "N 50° 04.064 E 014° 22.645",
110 "N 50° 04.064' E 014° 22.645",
111 "N 50° 04.064', E 014° 22.645'",
112 "N50° 04.064 E14° 22.645",
113 "N 50 04.064 E 014 22.645",
114 "N50 4.064 E14 22.645",
115 "50° 04.064' N, 014° 22.645' E"
117 latlon_check code, 50.06773, 14.37742
122 # Test identification of lat/lon pairs using N/W with degrees/mins
123 def test_identify_latlon_nw_dm
125 "N 50° 04.064 W 014° 22.645",
126 "N 50° 04.064' W 014° 22.645",
127 "N 50° 04.064', W 014° 22.645'",
128 "N50° 04.064 W14° 22.645",
129 "N 50 04.064 W 014 22.645",
130 "N50 4.064 W14 22.645",
131 "50° 04.064' N, 014° 22.645' W"
133 latlon_check code, 50.06773, -14.37742
138 # Test identification of lat/lon pairs using S/E with degrees/mins
139 def test_identify_latlon_se_dm
141 "S 50° 04.064 E 014° 22.645",
142 "S 50° 04.064' E 014° 22.645",
143 "S 50° 04.064', E 014° 22.645'",
144 "S50° 04.064 E14° 22.645",
145 "S 50 04.064 E 014 22.645",
146 "S50 4.064 E14 22.645",
147 "50° 04.064' S, 014° 22.645' E"
149 latlon_check code, -50.06773, 14.37742
154 # Test identification of lat/lon pairs using S/W with degrees/mins
155 def test_identify_latlon_sw_dm
157 "S 50° 04.064 W 014° 22.645",
158 "S 50° 04.064' W 014° 22.645",
159 "S 50° 04.064', W 014° 22.645'",
160 "S50° 04.064 W14° 22.645",
161 "S 50 04.064 W 014 22.645",
162 "S50 4.064 W14 22.645",
163 "50° 04.064' S, 014° 22.645' W"
165 latlon_check code, -50.06773, -14.37742
170 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
171 def test_identify_latlon_ne_dms
173 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
174 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
175 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
176 "N50 4 03.828 E14 22 38.712",
177 "N50 4 03.828, E14 22 38.712",
178 "50°4'3.828\"N 14°22'38.712\"E"
180 latlon_check code, 50.06773, 14.37742
185 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
186 def test_identify_latlon_nw_dms
188 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
189 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
190 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
191 "N50 4 03.828 W14 22 38.712",
192 "N50 4 03.828, W14 22 38.712",
193 "50°4'3.828\"N 14°22'38.712\"W"
195 latlon_check code, 50.06773, -14.37742
200 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
201 def test_identify_latlon_se_dms
203 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
204 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
205 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
206 "S50 4 03.828 E14 22 38.712",
207 "S50 4 03.828, E14 22 38.712",
208 "50°4'3.828\"S 14°22'38.712\"E"
210 latlon_check code, -50.06773, 14.37742
215 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
216 def test_identify_latlon_sw_dms
218 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
219 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
220 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
221 "S50 4 03.828 W14 22 38.712",
222 "S50 4 03.828, W14 22 38.712",
223 "50°4'3.828\"S 14°22'38.712\"W"
225 latlon_check code, -50.06773, -14.37742
230 # Test identification of lat/lon pairs with missing fractions
231 def test_no_identify_latlon_ne_missing_fraction_part
241 "N50 1' 3,\" E14 2' 4.\"",
242 "N50° 1' 3.\" E14° 2' 4.\"",
243 "50N 1' 3.\" 14 2' 4.\"E",
244 "50° 1' 3.\"N 14° 2' 4.\"E"
246 get search_path(:query => code)
247 assert_response :success
248 assert_template :show
249 assert_template :layout => "map"
250 assert_equal %w[nominatim], assigns(:sources).pluck(:name)
255 # Test identification of lat/lon pairs with mixed precision
256 def test_identify_latlon_ne_mixed_precision
257 latlon_check "N1 5 E15", 1.083333, 15
258 latlon_check "N1 5 9 E15", 1.085833, 15
259 latlon_check "N1 5 9 E1 5", 1.085833, 1.083333
260 latlon_check "N15 E1 5", 15, 1.083333
261 latlon_check "N15 E1 5 9", 15, 1.085833
262 latlon_check "N1 5 E1 5 9", 1.083333, 1.085833
266 # Test identification of lat/lon pairs with values close to zero
267 def test_identify_latlon_close_to_zero
269 "0.0000123 -0.0000456",
270 "+0.0000123 -0.0000456",
271 "N 0° 0' 0.4428\", W 0° 0' 1.6416\""
273 latlon_check code, 0.0000123, -0.0000456
278 # Test identification of US zipcodes
279 def test_identify_us_postcode
284 search_check code, %w[nominatim]
289 # Test identification of UK postcodes using examples from
290 # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
291 def test_identify_uk_postcode
300 search_check code, %w[nominatim]
305 # Test identification of Canadian postcodes
306 def test_identify_ca_postcode
307 search_check "A1B 2C3", %w[nominatim]
311 # Test identification fall through to the default case
312 def test_identify_default
313 search_check "foo bar baz", %w[nominatim]
317 # Test the nominatim reverse JSON search
318 def test_search_osm_nominatim_reverse_json
319 with_http_stubs "nominatim" do
320 post search_nominatim_reverse_query_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15, :format => "json"), :xhr => true
321 result_name_check_json("Broxbourne, Hertfordshire, East of England, England, United Kingdom")
323 post search_nominatim_reverse_query_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17, :format => "json"), :xhr => true
324 result_name_check_json("Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom")
326 post search_nominatim_reverse_query_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19, :format => "json"), :xhr => true
327 result_name_check_json("MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย")
333 def latlon_check(query, lat, lon)
334 get search_path(:query => query)
335 assert_response :success
336 assert_template :show
337 assert_template :layout => "map"
338 assert_equal %w[latlon nominatim_reverse], assigns(:sources).pluck(:name)
339 assert_nil @controller.params[:query]
340 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lat])
341 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lon])
342 assert_in_delta lat, @controller.params[:lat].to_f
343 assert_in_delta lon, @controller.params[:lon].to_f
345 get search_path(:query => query), :xhr => true
346 assert_response :success
347 assert_template :show
348 assert_template :layout => "xhr"
349 assert_equal %w[latlon nominatim_reverse], assigns(:sources).pluck(:name)
350 assert_nil @controller.params[:query]
351 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lat])
352 assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lon])
353 assert_in_delta lat, @controller.params[:lat].to_f
354 assert_in_delta lon, @controller.params[:lon].to_f
357 def search_check(query, sources)
358 get search_path(:query => query)
359 assert_response :success
360 assert_template :show
361 assert_template :layout => "map"
362 assert_equal sources, assigns(:sources).pluck(:name)
364 get search_path(:query => query), :xhr => true
365 assert_response :success
366 assert_template :show
367 assert_template :layout => "xhr"
368 assert_equal sources, assigns(:sources).pluck(:name)
371 def result_name_check_json(name)
372 assert_response :success
373 js = ActiveSupport::JSON.decode(@response.body)
375 assert_equal name, js[0]["name"]