]> git.openstreetmap.org Git - rails.git/blob - test/functional/geocoder_controller_test.rb
bbb1b0f01a12535f764877e104fa6943bb69a271
[rails.git] / test / functional / geocoder_controller_test.rb
1 # coding: utf-8
2
3 require File.dirname(__FILE__) + '/../test_helper'
4 require 'geocoder_controller'
5
6 class GeocoderControllerTest < ActionController::TestCase
7   ##
8   # test all routes which lead to this controller
9   def test_routes
10     assert_routing(
11       { :path => "/geocoder/search", :method => :post },
12       { :controller => "geocoder", :action => "search" }
13     )
14     assert_routing(
15       { :path => "/geocoder/search_latlon", :method => :get },
16       { :controller => "geocoder", :action => "search_latlon" }
17     )
18     assert_routing(
19       { :path => "/geocoder/search_us_postcode", :method => :get },
20       { :controller => "geocoder", :action => "search_us_postcode" }
21     )
22     assert_routing(
23       { :path => "/geocoder/search_uk_postcode", :method => :get },
24       { :controller => "geocoder", :action => "search_uk_postcode" }
25     )
26     assert_routing(
27       { :path => "/geocoder/search_ca_postcode", :method => :get },
28       { :controller => "geocoder", :action => "search_ca_postcode" }
29     )
30     assert_routing(
31       { :path => "/geocoder/search_osm_nominatim", :method => :get },
32       { :controller => "geocoder", :action => "search_osm_nominatim" }
33     )
34     assert_routing(
35       { :path => "/geocoder/search_geonames", :method => :get },
36       { :controller => "geocoder", :action => "search_geonames" }
37     )
38
39     assert_routing(
40       { :path => "/geocoder/description", :method => :post },
41       { :controller => "geocoder", :action => "description" }
42     )
43     assert_routing(
44       { :path => "/geocoder/description_osm_nominatim", :method => :get },
45       { :controller => "geocoder", :action => "description_osm_nominatim" }
46     )
47     assert_routing(
48       { :path => "/geocoder/description_geonames", :method => :get },
49       { :controller => "geocoder", :action => "description_geonames" }
50     )
51   end
52
53   ##
54   # Test identification of basic lat/lon pairs
55   def test_identify_latlon_basic
56     [
57      '50.06773 14.37742',
58      '50.06773, 14.37742',
59      '+50.06773 +14.37742',
60      '+50.06773, +14.37742'
61     ].each do |code|
62       post :search, :query => code
63       assert_response :success
64       assert_equal ['latlon'], assigns(:sources)
65       assert_equal code, assigns(:query)
66     end
67   end
68
69   ##
70   # Test identification of lat/lon pairs using N/E with degrees
71   def test_identify_latlon_ne_d
72     [
73      'N50.06773 E14.37742',
74      'N50.06773, E14.37742',
75      '50.06773N 14.37742E',
76      '50.06773N, 14.37742E'
77     ].each do |code|
78       post :search, :query => code
79       assert_response :success
80       assert_equal ['latlon'], assigns(:sources)
81       assert_equal "50.06773, 14.37742", assigns(:query)
82     end
83   end
84
85   ##
86   # Test identification of lat/lon pairs using N/W with degrees
87   def test_identify_latlon_nw_d
88     [
89      'N50.06773 W14.37742',
90      'N50.06773, W14.37742',
91      '50.06773N 14.37742W',
92      '50.06773N, 14.37742W'
93     ].each do |code|
94       post :search, :query => code
95       assert_response :success
96       assert_equal ['latlon'], assigns(:sources)
97       assert_equal "50.06773, -14.37742", assigns(:query)
98     end
99   end
100
101   ##
102   # Test identification of lat/lon pairs using S/E with degrees
103   def test_identify_latlon_se_d
104     [
105      'S50.06773 E14.37742',
106      'S50.06773, E14.37742',
107      '50.06773S 14.37742E',
108      '50.06773S, 14.37742E'
109     ].each do |code|
110       post :search, :query => code
111       assert_response :success
112       assert_equal ['latlon'], assigns(:sources)
113       assert_equal "-50.06773, 14.37742", assigns(:query)
114     end
115   end
116
117   ##
118   # Test identification of lat/lon pairs using S/W with degrees
119   def test_identify_latlon_sw_d
120     [
121      'S50.06773 W14.37742',
122      'S50.06773, W14.37742',
123      '50.06773S 14.37742W',
124      '50.06773S, 14.37742W'
125     ].each do |code|
126       post :search, :query => code
127       assert_response :success
128       assert_equal ['latlon'], assigns(:sources)
129       assert_equal "-50.06773, -14.37742", assigns(:query)
130     end
131   end
132
133   ##
134   # Test identification of lat/lon pairs using N/E with degrees/mins
135   def test_identify_latlon_ne_dm
136     [
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"
144     ].each do |code|
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
149     end
150   end
151
152   ##
153   # Test identification of lat/lon pairs using N/W with degrees/mins
154   def test_identify_latlon_nw_dm
155     [
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"
163     ].each do |code|
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
168     end
169   end
170
171   ##
172   # Test identification of lat/lon pairs using S/E with degrees/mins
173   def test_identify_latlon_se_dm
174     [
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"
182     ].each do |code|
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
187     end
188   end
189
190   ##
191   # Test identification of lat/lon pairs using S/W with degrees/mins
192   def test_identify_latlon_sw_dm
193     [
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"
201     ].each do |code|
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
206     end
207   end
208
209   ##
210   # Test identification of lat/lon pairs using N/E with degrees/mins/secs
211   def test_identify_latlon_ne_dms
212     [
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"
219     ].each do |code|
220       post :search, :query => code
221       assert_response :success
222       assert_equal ['latlon'], assigns(:sources)
223       assert_equal "50.06773, 14.37742", assigns(:query)
224     end
225   end
226
227   ##
228   # Test identification of lat/lon pairs using N/W with degrees/mins/secs
229   def test_identify_latlon_nw_dms
230     [
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"
237     ].each do |code|
238       post :search, :query => code
239       assert_response :success
240       assert_equal ['latlon'], assigns(:sources)
241       assert_equal "50.06773, -14.37742", assigns(:query)
242     end
243   end
244
245   ##
246   # Test identification of lat/lon pairs using S/E with degrees/mins/secs
247   def test_identify_latlon_se_dms
248     [
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"
255     ].each do |code|
256       post :search, :query => code
257       assert_response :success
258       assert_equal ['latlon'], assigns(:sources)
259       assert_equal "-50.06773, 14.37742", assigns(:query)
260     end
261   end
262
263   ##
264   # Test identification of lat/lon pairs using S/W with degrees/mins/secs
265   def test_identify_latlon_sw_dms
266     [
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"
273     ].each do |code|
274       post :search, :query => code
275       assert_response :success
276       assert_equal ['latlon'], assigns(:sources)
277       assert_equal "-50.06773, -14.37742", assigns(:query)
278     end
279   end
280
281   ##
282   # Test identification of US zipcodes
283   def test_identify_us_postcode
284     [
285      '12345',
286      '12345-6789'
287     ].each do |code|
288       post :search, query: code
289       assert_response :success
290       assert_equal ['us_postcode', 'osm_nominatim'], assigns(:sources)
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       post :search, query: code
307       assert_response :success
308       assert_equal ['uk_postcode', 'osm_nominatim'], assigns(:sources)
309     end
310   end
311
312   ##
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)
318   end
319
320   ##
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)
326   end
327
328 private
329
330   ##
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(', ')
335   end
336 end