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