]> git.openstreetmap.org Git - rails.git/blob - test/functional/geocoder_controller_test.rb
Remove unnecessary div/class/css
[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_namefinder", :method => :get },
32       { :controller => "geocoder", :action => "search_osm_namefinder" }
33     )
34     assert_routing(
35       { :path => "/geocoder/search_osm_nominatim", :method => :get },
36       { :controller => "geocoder", :action => "search_osm_nominatim" }
37     )
38     assert_routing(
39       { :path => "/geocoder/search_geonames", :method => :get },
40       { :controller => "geocoder", :action => "search_geonames" }
41     )
42
43     assert_routing(
44       { :path => "/geocoder/description", :method => :post },
45       { :controller => "geocoder", :action => "description" }
46     )
47     assert_routing(
48       { :path => "/geocoder/description_osm_namefinder", :method => :get },
49       { :controller => "geocoder", :action => "description_osm_namefinder" }
50     )
51     assert_routing(
52       { :path => "/geocoder/description_osm_nominatim", :method => :get },
53       { :controller => "geocoder", :action => "description_osm_nominatim" }
54     )
55     assert_routing(
56       { :path => "/geocoder/description_geonames", :method => :get },
57       { :controller => "geocoder", :action => "description_geonames" }
58     )
59   end
60
61   ##
62   # Test identification of basic lat/lon pairs
63   def test_identify_latlon_basic
64     [
65      '50.06773 14.37742',
66      '50.06773, 14.37742',
67      '+50.06773 +14.37742',
68      '+50.06773, +14.37742'
69     ].each do |code|
70       post :search, :query => code
71       assert_response :success
72       assert_equal ['latlon'], assigns(:sources)
73       assert_equal code, assigns(:query)
74     end
75   end
76
77   ##
78   # Test identification of lat/lon pairs using N/E with degrees
79   def test_identify_latlon_ne_d
80     [
81      'N50.06773 E14.37742',
82      'N50.06773, E14.37742',
83      '50.06773N 14.37742E',
84      '50.06773N, 14.37742E'
85     ].each do |code|
86       post :search, :query => code
87       assert_response :success
88       assert_equal ['latlon'], assigns(:sources)
89       assert_equal "50.06773, 14.37742", assigns(:query)
90     end
91   end
92
93   ##
94   # Test identification of lat/lon pairs using N/W with degrees
95   def test_identify_latlon_nw_d
96     [
97      'N50.06773 W14.37742',
98      'N50.06773, W14.37742',
99      '50.06773N 14.37742W',
100      '50.06773N, 14.37742W'
101     ].each do |code|
102       post :search, :query => code
103       assert_response :success
104       assert_equal ['latlon'], assigns(:sources)
105       assert_equal "50.06773, -14.37742", assigns(:query)
106     end
107   end
108
109   ##
110   # Test identification of lat/lon pairs using S/E with degrees
111   def test_identify_latlon_se_d
112     [
113      'S50.06773 E14.37742',
114      'S50.06773, E14.37742',
115      '50.06773S 14.37742E',
116      '50.06773S, 14.37742E'
117     ].each do |code|
118       post :search, :query => code
119       assert_response :success
120       assert_equal ['latlon'], assigns(:sources)
121       assert_equal "-50.06773, 14.37742", assigns(:query)
122     end
123   end
124
125   ##
126   # Test identification of lat/lon pairs using S/W with degrees
127   def test_identify_latlon_sw_d
128     [
129      'S50.06773 W14.37742',
130      'S50.06773, W14.37742',
131      '50.06773S 14.37742W',
132      '50.06773S, 14.37742W'
133     ].each do |code|
134       post :search, :query => code
135       assert_response :success
136       assert_equal ['latlon'], assigns(:sources)
137       assert_equal "-50.06773, -14.37742", assigns(:query)
138     end
139   end
140
141   ##
142   # Test identification of lat/lon pairs using N/E with degrees/mins
143   def test_identify_latlon_ne_dm
144     [
145      'N 50° 04.064 E 014° 22.645',
146      "N 50° 04.064' E 014° 22.645",
147      "N 50° 04.064', E 014° 22.645'",
148      'N50° 04.064 E14° 22.645',
149      'N 50 04.064 E 014 22.645',
150      'N50 4.064 E14 22.645',
151      "50° 04.064' N, 014° 22.645' E"
152     ].each do |code|
153       post :search, :query => code
154       assert_response :success
155       assert_equal ['latlon'], assigns(:sources)
156       assert_latlon_equal_round "50.06773, 14.37742", assigns(:query), 5
157     end
158   end
159
160   ##
161   # Test identification of lat/lon pairs using N/W with degrees/mins
162   def test_identify_latlon_nw_dm
163     [
164      'N 50° 04.064 W 014° 22.645',
165      "N 50° 04.064' W 014° 22.645",
166      "N 50° 04.064', W 014° 22.645'",
167      'N50° 04.064 W14° 22.645',
168      'N 50 04.064 W 014 22.645',
169      'N50 4.064 W14 22.645',
170      "50° 04.064' N, 014° 22.645' W"
171     ].each do |code|
172       post :search, :query => code
173       assert_response :success
174       assert_equal ['latlon'], assigns(:sources)
175       assert_latlon_equal_round "50.06773, -14.37742", assigns(:query), 5
176     end
177   end
178
179   ##
180   # Test identification of lat/lon pairs using S/E with degrees/mins
181   def test_identify_latlon_se_dm
182     [
183      'S 50° 04.064 E 014° 22.645',
184      "S 50° 04.064' E 014° 22.645",
185      "S 50° 04.064', E 014° 22.645'",
186      'S50° 04.064 E14° 22.645',
187      'S 50 04.064 E 014 22.645',
188      'S50 4.064 E14 22.645',
189      "50° 04.064' S, 014° 22.645' E"
190     ].each do |code|
191       post :search, :query => code
192       assert_response :success
193       assert_equal ['latlon'], assigns(:sources)
194       assert_latlon_equal_round "-50.06773, 14.37742", assigns(:query), 5
195     end
196   end
197
198   ##
199   # Test identification of lat/lon pairs using S/W with degrees/mins
200   def test_identify_latlon_sw_dm
201     [
202      'S 50° 04.064 W 014° 22.645',
203      "S 50° 04.064' W 014° 22.645",
204      "S 50° 04.064', W 014° 22.645'",
205      'S50° 04.064 W14° 22.645',
206      'S 50 04.064 W 014 22.645',
207      'S50 4.064 W14 22.645',
208      "50° 04.064' S, 014° 22.645' W"
209     ].each do |code|
210       post :search, :query => code
211       assert_response :success
212       assert_equal ['latlon'], assigns(:sources)
213       assert_latlon_equal_round "-50.06773, -14.37742", assigns(:query), 5
214     end
215   end
216
217   ##
218   # Test identification of lat/lon pairs using N/E with degrees/mins/secs
219   def test_identify_latlon_ne_dms
220     [
221      "N 50° 4' 03.828\" E 14° 22' 38.712\"",
222      "N 50° 4' 03.828\", E 14° 22' 38.712\"",
223      "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
224      'N50 4 03.828 E14 22 38.712',
225      'N50 4 03.828, E14 22 38.712',
226      "50°4'3.828\"N 14°22'38.712\"E"
227     ].each do |code|
228       post :search, :query => code
229       assert_response :success
230       assert_equal ['latlon'], assigns(:sources)
231       assert_equal "50.06773, 14.37742", assigns(:query)
232     end
233   end
234
235   ##
236   # Test identification of lat/lon pairs using N/W with degrees/mins/secs
237   def test_identify_latlon_nw_dms
238     [
239      "N 50° 4' 03.828\" W 14° 22' 38.712\"",
240      "N 50° 4' 03.828\", W 14° 22' 38.712\"",
241      "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
242      'N50 4 03.828 W14 22 38.712',
243      'N50 4 03.828, W14 22 38.712',
244      "50°4'3.828\"N 14°22'38.712\"W"
245     ].each do |code|
246       post :search, :query => code
247       assert_response :success
248       assert_equal ['latlon'], assigns(:sources)
249       assert_equal "50.06773, -14.37742", assigns(:query)
250     end
251   end
252
253   ##
254   # Test identification of lat/lon pairs using S/E with degrees/mins/secs
255   def test_identify_latlon_se_dms
256     [
257      "S 50° 4' 03.828\" E 14° 22' 38.712\"",
258      "S 50° 4' 03.828\", E 14° 22' 38.712\"",
259      "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
260      'S50 4 03.828 E14 22 38.712',
261      'S50 4 03.828, E14 22 38.712',
262      "50°4'3.828\"S 14°22'38.712\"E"
263     ].each do |code|
264       post :search, :query => code
265       assert_response :success
266       assert_equal ['latlon'], assigns(:sources)
267       assert_equal "-50.06773, 14.37742", assigns(:query)
268     end
269   end
270
271   ##
272   # Test identification of lat/lon pairs using S/W with degrees/mins/secs
273   def test_identify_latlon_sw_dms
274     [
275      "S 50° 4' 03.828\" W 14° 22' 38.712\"",
276      "S 50° 4' 03.828\", W 14° 22' 38.712\"",
277      "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
278      'S50 4 03.828 W14 22 38.712',
279      'S50 4 03.828, W14 22 38.712',
280      "50°4'3.828\"S 14°22'38.712\"W"
281     ].each do |code|
282       post :search, :query => code
283       assert_response :success
284       assert_equal ['latlon'], assigns(:sources)
285       assert_equal "-50.06773, -14.37742", assigns(:query)
286     end
287   end
288
289   ##
290   # Test identification of US zipcodes
291   def test_identify_us_postcode
292     [
293      '12345',
294      '12345-6789'
295     ].each do |code|
296       post :search, query: code
297       assert_response :success
298       assert_equal ['us_postcode', 'osm_nominatim'], assigns(:sources)
299     end
300   end
301
302   ##
303   # Test identification of UK postcodes using examples from 
304   # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
305   def test_identify_uk_postcode
306     [
307      'EC1A 1BB',
308      'W1A 1HQ',
309      'M1 1AA',
310      'B33 8TH',
311      'CR2 6XH',
312      'DN55 1PT'
313     ].each do |code|
314       post :search, query: code
315       assert_response :success
316       assert_equal ['uk_postcode', 'osm_nominatim'], assigns(:sources)
317     end
318   end
319
320   ##
321   # Test identification of Canadian postcodes
322   def test_identify_ca_postcode
323     post :search, query: 'A1B 2C3'
324     assert_response :success
325     assert_equal ['ca_postcode', 'osm_nominatim'], assigns(:sources)
326   end
327
328   ##
329   # Test identification fall through to the default case
330   def test_identify_default
331     post :search, query: 'foo bar baz'
332     assert_response :success
333     assert_equal ['osm_nominatim'], assigns(:sources)
334   end
335
336 private
337
338   ##
339   # this is a test helper for rounding latlon strings to a specified precision, e.g., at a precision 
340   # of 5, "50.06773333333334, -14.377416666666667" will become "50.06773, -14.37742"
341   def assert_latlon_equal_round(expected, actual, precision)
342     assert_equal expected.split(',').map {|i| i.to_f.round(precision)}.join(', '), actual.split(',').map {|i| i.to_f.round(precision)}.join(', ')
343   end
344 end