3 require File.dirname(__FILE__) + '/../test_helper'
4 require 'geocoder_controller'
6 class GeocoderControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/geocoder/search", :method => :post },
12 { :controller => "geocoder", :action => "search" }
15 { :path => "/geocoder/search_latlon", :method => :get },
16 { :controller => "geocoder", :action => "search_latlon" }
19 { :path => "/geocoder/search_us_postcode", :method => :get },
20 { :controller => "geocoder", :action => "search_us_postcode" }
23 { :path => "/geocoder/search_uk_postcode", :method => :get },
24 { :controller => "geocoder", :action => "search_uk_postcode" }
27 { :path => "/geocoder/search_ca_postcode", :method => :get },
28 { :controller => "geocoder", :action => "search_ca_postcode" }
31 { :path => "/geocoder/search_osm_nominatim", :method => :get },
32 { :controller => "geocoder", :action => "search_osm_nominatim" }
35 { :path => "/geocoder/search_geonames", :method => :get },
36 { :controller => "geocoder", :action => "search_geonames" }
40 { :path => "/geocoder/description", :method => :post },
41 { :controller => "geocoder", :action => "description" }
44 { :path => "/geocoder/description_osm_nominatim", :method => :get },
45 { :controller => "geocoder", :action => "description_osm_nominatim" }
48 { :path => "/geocoder/description_geonames", :method => :get },
49 { :controller => "geocoder", :action => "description_geonames" }
54 # Test identification of basic lat/lon pairs
55 def test_identify_latlon_basic
59 '+50.06773 +14.37742',
60 '+50.06773, +14.37742'
62 post :search, :query => code
63 assert_response :success
64 assert_equal ['latlon'], assigns(:sources)
65 assert_equal code, assigns(:query)
70 # Test identification of lat/lon pairs using N/E with degrees
71 def test_identify_latlon_ne_d
73 'N50.06773 E14.37742',
74 'N50.06773, E14.37742',
75 '50.06773N 14.37742E',
76 '50.06773N, 14.37742E'
78 post :search, :query => code
79 assert_response :success
80 assert_equal ['latlon'], assigns(:sources)
81 assert_equal "50.06773, 14.37742", assigns(:query)
86 # Test identification of lat/lon pairs using N/W with degrees
87 def test_identify_latlon_nw_d
89 'N50.06773 W14.37742',
90 'N50.06773, W14.37742',
91 '50.06773N 14.37742W',
92 '50.06773N, 14.37742W'
94 post :search, :query => code
95 assert_response :success
96 assert_equal ['latlon'], assigns(:sources)
97 assert_equal "50.06773, -14.37742", assigns(:query)
102 # Test identification of lat/lon pairs using S/E with degrees
103 def test_identify_latlon_se_d
105 'S50.06773 E14.37742',
106 'S50.06773, E14.37742',
107 '50.06773S 14.37742E',
108 '50.06773S, 14.37742E'
110 post :search, :query => code
111 assert_response :success
112 assert_equal ['latlon'], assigns(:sources)
113 assert_equal "-50.06773, 14.37742", assigns(:query)
118 # Test identification of lat/lon pairs using S/W with degrees
119 def test_identify_latlon_sw_d
121 'S50.06773 W14.37742',
122 'S50.06773, W14.37742',
123 '50.06773S 14.37742W',
124 '50.06773S, 14.37742W'
126 post :search, :query => code
127 assert_response :success
128 assert_equal ['latlon'], assigns(:sources)
129 assert_equal "-50.06773, -14.37742", assigns(:query)
134 # Test identification of lat/lon pairs using N/E with degrees/mins
135 def test_identify_latlon_ne_dm
137 'N 50° 04.064 E 014° 22.645',
138 "N 50° 04.064' E 014° 22.645",
139 "N 50° 04.064', E 014° 22.645'",
140 'N50° 04.064 E14° 22.645',
141 'N 50 04.064 E 014 22.645',
142 'N50 4.064 E14 22.645',
143 "50° 04.064' N, 014° 22.645' E"
145 post :search, :query => code
146 assert_response :success
147 assert_equal ['latlon'], assigns(:sources)
148 assert_latlon_equal_round "50.06773, 14.37742", assigns(:query), 5
153 # Test identification of lat/lon pairs using N/W with degrees/mins
154 def test_identify_latlon_nw_dm
156 'N 50° 04.064 W 014° 22.645',
157 "N 50° 04.064' W 014° 22.645",
158 "N 50° 04.064', W 014° 22.645'",
159 'N50° 04.064 W14° 22.645',
160 'N 50 04.064 W 014 22.645',
161 'N50 4.064 W14 22.645',
162 "50° 04.064' N, 014° 22.645' W"
164 post :search, :query => code
165 assert_response :success
166 assert_equal ['latlon'], assigns(:sources)
167 assert_latlon_equal_round "50.06773, -14.37742", assigns(:query), 5
172 # Test identification of lat/lon pairs using S/E with degrees/mins
173 def test_identify_latlon_se_dm
175 'S 50° 04.064 E 014° 22.645',
176 "S 50° 04.064' E 014° 22.645",
177 "S 50° 04.064', E 014° 22.645'",
178 'S50° 04.064 E14° 22.645',
179 'S 50 04.064 E 014 22.645',
180 'S50 4.064 E14 22.645',
181 "50° 04.064' S, 014° 22.645' E"
183 post :search, :query => code
184 assert_response :success
185 assert_equal ['latlon'], assigns(:sources)
186 assert_latlon_equal_round "-50.06773, 14.37742", assigns(:query), 5
191 # Test identification of lat/lon pairs using S/W with degrees/mins
192 def test_identify_latlon_sw_dm
194 'S 50° 04.064 W 014° 22.645',
195 "S 50° 04.064' W 014° 22.645",
196 "S 50° 04.064', W 014° 22.645'",
197 'S50° 04.064 W14° 22.645',
198 'S 50 04.064 W 014 22.645',
199 'S50 4.064 W14 22.645',
200 "50° 04.064' S, 014° 22.645' W"
202 post :search, :query => code
203 assert_response :success
204 assert_equal ['latlon'], assigns(:sources)
205 assert_latlon_equal_round "-50.06773, -14.37742", assigns(:query), 5
210 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
211 def test_identify_latlon_ne_dms
213 "N 50° 4' 03.828\" E 14° 22' 38.712\"",
214 "N 50° 4' 03.828\", E 14° 22' 38.712\"",
215 "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
216 'N50 4 03.828 E14 22 38.712',
217 'N50 4 03.828, E14 22 38.712',
218 "50°4'3.828\"N 14°22'38.712\"E"
220 post :search, :query => code
221 assert_response :success
222 assert_equal ['latlon'], assigns(:sources)
223 assert_equal "50.06773, 14.37742", assigns(:query)
228 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
229 def test_identify_latlon_nw_dms
231 "N 50° 4' 03.828\" W 14° 22' 38.712\"",
232 "N 50° 4' 03.828\", W 14° 22' 38.712\"",
233 "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
234 'N50 4 03.828 W14 22 38.712',
235 'N50 4 03.828, W14 22 38.712',
236 "50°4'3.828\"N 14°22'38.712\"W"
238 post :search, :query => code
239 assert_response :success
240 assert_equal ['latlon'], assigns(:sources)
241 assert_equal "50.06773, -14.37742", assigns(:query)
246 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
247 def test_identify_latlon_se_dms
249 "S 50° 4' 03.828\" E 14° 22' 38.712\"",
250 "S 50° 4' 03.828\", E 14° 22' 38.712\"",
251 "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
252 'S50 4 03.828 E14 22 38.712',
253 'S50 4 03.828, E14 22 38.712',
254 "50°4'3.828\"S 14°22'38.712\"E"
256 post :search, :query => code
257 assert_response :success
258 assert_equal ['latlon'], assigns(:sources)
259 assert_equal "-50.06773, 14.37742", assigns(:query)
264 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
265 def test_identify_latlon_sw_dms
267 "S 50° 4' 03.828\" W 14° 22' 38.712\"",
268 "S 50° 4' 03.828\", W 14° 22' 38.712\"",
269 "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
270 'S50 4 03.828 W14 22 38.712',
271 'S50 4 03.828, W14 22 38.712',
272 "50°4'3.828\"S 14°22'38.712\"W"
274 post :search, :query => code
275 assert_response :success
276 assert_equal ['latlon'], assigns(:sources)
277 assert_equal "-50.06773, -14.37742", assigns(:query)
282 # Test identification of US zipcodes
283 def test_identify_us_postcode
288 post :search, query: code
289 assert_response :success
290 assert_equal ['us_postcode', 'osm_nominatim'], assigns(:sources)
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
306 post :search, query: code
307 assert_response :success
308 assert_equal ['uk_postcode', 'osm_nominatim'], assigns(:sources)
313 # Test identification of Canadian postcodes
314 def test_identify_ca_postcode
315 post :search, query: 'A1B 2C3'
316 assert_response :success
317 assert_equal ['ca_postcode', 'osm_nominatim'], assigns(:sources)
321 # Test identification fall through to the default case
322 def test_identify_default
323 post :search, query: 'foo bar baz'
324 assert_response :success
325 assert_equal ['osm_nominatim'], assigns(:sources)
331 # this is a test helper for rounding latlon strings to a specified precision, e.g., at a precision
332 # of 5, "50.06773333333334, -14.377416666666667" will become "50.06773, -14.37742"
333 def assert_latlon_equal_round(expected, actual, precision)
334 assert_equal expected.split(',').map {|i| i.to_f.round(precision)}.join(', '), actual.split(',').map {|i| i.to_f.round(precision)}.join(', ')