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