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 => "/search", :method => :get },
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" }
39 { :path => "/geocoder/search_osm_nominatim_reverse", :method => :get },
40 { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
43 { :path => "/geocoder/search_geonames_reverse", :method => :get },
44 { :controller => "geocoder", :action => "search_geonames_reverse" }
49 # Test identification of basic lat/lon pairs
50 def test_identify_latlon_basic
54 '+50.06773 +14.37742',
55 '+50.06773, +14.37742'
57 latlon_check code, 50.06773, 14.37742
62 # Test identification of lat/lon pairs using N/E with degrees
63 def test_identify_latlon_ne_d
65 'N50.06773 E14.37742',
66 'N50.06773, E14.37742',
67 '50.06773N 14.37742E',
68 '50.06773N, 14.37742E'
70 latlon_check code, 50.06773, 14.37742
75 # Test identification of lat/lon pairs using N/W with degrees
76 def test_identify_latlon_nw_d
78 'N50.06773 W14.37742',
79 'N50.06773, W14.37742',
80 '50.06773N 14.37742W',
81 '50.06773N, 14.37742W'
83 latlon_check code, 50.06773, -14.37742
88 # Test identification of lat/lon pairs using S/E with degrees
89 def test_identify_latlon_se_d
91 'S50.06773 E14.37742',
92 'S50.06773, E14.37742',
93 '50.06773S 14.37742E',
94 '50.06773S, 14.37742E'
96 latlon_check code, -50.06773, 14.37742
101 # Test identification of lat/lon pairs using S/W with degrees
102 def test_identify_latlon_sw_d
104 'S50.06773 W14.37742',
105 'S50.06773, W14.37742',
106 '50.06773S 14.37742W',
107 '50.06773S, 14.37742W'
109 latlon_check code, -50.06773, -14.37742
114 # Test identification of lat/lon pairs using N/E with degrees/mins
115 def test_identify_latlon_ne_dm
117 'N 50° 04.064 E 014° 22.645',
118 "N 50° 04.064' E 014° 22.645",
119 "N 50° 04.064', E 014° 22.645'",
120 'N50° 04.064 E14° 22.645',
121 'N 50 04.064 E 014 22.645',
122 'N50 4.064 E14 22.645',
123 "50° 04.064' N, 014° 22.645' E"
125 latlon_check code, 50.06773, 14.37742
130 # Test identification of lat/lon pairs using N/W with degrees/mins
131 def test_identify_latlon_nw_dm
133 'N 50° 04.064 W 014° 22.645',
134 "N 50° 04.064' W 014° 22.645",
135 "N 50° 04.064', W 014° 22.645'",
136 'N50° 04.064 W14° 22.645',
137 'N 50 04.064 W 014 22.645',
138 'N50 4.064 W14 22.645',
139 "50° 04.064' N, 014° 22.645' W"
141 latlon_check code, 50.06773, -14.37742
146 # Test identification of lat/lon pairs using S/E with degrees/mins
147 def test_identify_latlon_se_dm
149 'S 50° 04.064 E 014° 22.645',
150 "S 50° 04.064' E 014° 22.645",
151 "S 50° 04.064', E 014° 22.645'",
152 'S50° 04.064 E14° 22.645',
153 'S 50 04.064 E 014 22.645',
154 'S50 4.064 E14 22.645',
155 "50° 04.064' S, 014° 22.645' E"
157 latlon_check code, -50.06773, 14.37742
162 # Test identification of lat/lon pairs using S/W with degrees/mins
163 def test_identify_latlon_sw_dm
165 'S 50° 04.064 W 014° 22.645',
166 "S 50° 04.064' W 014° 22.645",
167 "S 50° 04.064', W 014° 22.645'",
168 'S50° 04.064 W14° 22.645',
169 'S 50 04.064 W 014 22.645',
170 'S50 4.064 W14 22.645',
171 "50° 04.064' S, 014° 22.645' W"
173 post :search, :query => code
174 assert_response :success
175 assert_equal ['latlon' ,'osm_nominatim_reverse', 'geonames_reverse'], assigns(:sources)
176 assert_nil @controller.params[:query]
177 assert_in_delta -50.06773, @controller.params[:lat]
178 assert_in_delta -14.37742, @controller.params[:lon]
183 # Test identification of lat/lon pairs using N/E with degrees/mins/secs
184 def test_identify_latlon_ne_dms
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"
193 latlon_check code, 50.06773, 14.37742
198 # Test identification of lat/lon pairs using N/W with degrees/mins/secs
199 def test_identify_latlon_nw_dms
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"
208 latlon_check code, 50.06773, -14.37742
213 # Test identification of lat/lon pairs using S/E with degrees/mins/secs
214 def test_identify_latlon_se_dms
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"
223 latlon_check code, -50.06773, 14.37742
228 # Test identification of lat/lon pairs using S/W with degrees/mins/secs
229 def test_identify_latlon_sw_dms
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"
238 latlon_check code, -50.06773, -14.37742
243 # Test identification of US zipcodes
244 def test_identify_us_postcode
249 post :search, query: code
250 assert_response :success
251 assert_equal ['us_postcode', 'osm_nominatim'], assigns(:sources)
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
267 search_check code, ['uk_postcode', 'osm_nominatim']
272 # Test identification of Canadian postcodes
273 def test_identify_ca_postcode
274 search_check 'A1B 2C3', ['ca_postcode', 'osm_nominatim']
278 # Test identification fall through to the default case
279 def test_identify_default
280 search_check 'foo bar baz', ['osm_nominatim']
284 def latlon_check(query, lat, lon)
285 post :search, :query => query
286 assert_response :success
287 assert_template "search"
288 assert_template :layout => "map"
289 assert_equal ['latlon' ,'osm_nominatim_reverse', 'geonames_reverse'], assigns(:sources)
290 assert_nil @controller.params[:query]
291 assert_in_delta lat, @controller.params[:lat]
292 assert_in_delta lon, @controller.params[:lon]
294 xhr :post, :search, :query => query
295 assert_response :success
296 assert_template "search"
297 assert_template :layout => "xhr"
298 assert_equal ['latlon' ,'osm_nominatim_reverse', 'geonames_reverse'], assigns(:sources)
299 assert_nil @controller.params[:query]
300 assert_in_delta lat, @controller.params[:lat]
301 assert_in_delta lon, @controller.params[:lon]
304 def search_check(query, sources)
305 post :search, :query => query
306 assert_response :success
307 assert_template "search"
308 assert_template :layout => "map"
309 assert_equal sources, assigns(:sources)
311 xhr :post, :search, :query => query
312 assert_response :success
313 assert_template "search"
314 assert_template :layout => "xhr"
315 assert_equal sources, assigns(:sources)