]> git.openstreetmap.org Git - rails.git/blob - test/functional/geocoder_controller_test.rb
Improve functional tests for notes#feed
[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 the regular expressions that split search queries into 'latlon', 'us_postcode', and the like
63
64   # latlon examples/motivation from https://trac.openstreetmap.org/ticket/4730 & https://trac.openstreetmap.org/ticket/4748
65   def test_identify_latlon_degdec
66     ['50.06773 14.37742', '50.06773, 14.37742', '+50.06773 +14.37742', '+50.06773, +14.37742'].each do |code|
67       post :search, :query => code
68       assert_response :success
69       assert_equal ['latlon'], assigns(:sources)
70       assert_equal code, assigns(:query)
71     end
72   end
73
74   ##
75   # this is a test helper for rounding latlon strings to a specified precision, e.g., at a precision 
76   # of 5, "50.06773333333334, -14.377416666666667" will become "50.06773, -14.37742"
77   def assert_latlon_equal_round(expected, actual, precision)
78     assert_equal expected.split(',').map {|i| i.to_f.round(precision)}.join(', '), actual.split(',').map {|i| i.to_f.round(precision)}.join(', ')
79   end
80
81   def test_identify_latlon_degdec_nsew
82     target = '50.06773, 14.37742'
83     [
84         'N50.06773 E14.37742',
85         'N50.06773, E14.37742',
86         '50.06773N 14.37742E',
87         '50.06773N, 14.37742E'
88     ].each do |code|
89       post :search, :query => code
90       assert_response :success
91       assert_equal ['latlon'], assigns(:sources)
92       assert_equal target, assigns(:query)
93     end
94   end
95
96   def test_identify_latlon_ddm
97     target = '50.06773, 14.37742'
98     [
99         'N 50° 04.064 E 014° 22.645',
100         "N 50° 04.064' E 014° 22.645",
101         "N 50° 04.064', E 014° 22.645'",
102         'N50° 04.064 E14° 22.645',
103         'N 50 04.064 E 014 22.645',
104         'N50 4.064 E14 22.645',
105         "50° 04.064' N, 014° 22.645' E"
106     ].each do |code|
107       post :search, :query => code
108       assert_response :success
109       assert_equal ['latlon'], assigns(:sources)
110       assert_latlon_equal_round(target, assigns(:query), 5)
111     end
112   end
113
114   def test_identify_latlon_dms
115     target = '50.06773, 14.37742'
116     [
117         "N 50° 4' 03.828\" E 14° 22' 38.712\"",
118         "N 50° 4' 03.828\", E 14° 22' 38.712\"",
119         'N50 4 03.828 E14 22 38.712',
120         'N50 4 03.828, E14 22 38.712',
121         "50°4'3.828\"N 14°22'38.712\"E"
122     ].each do |code|
123       post :search, :query => code
124       assert_response :success
125       assert_equal ['latlon'], assigns(:sources)
126       assert_equal target, assigns(:query)
127     end
128   end
129
130   def test_identify_us_postcode
131     ['12345', '12345-6789'].each do |code|
132       post :search, query: code
133       assert_response :success
134       assert_equal ['us_postcode', 'osm_nominatim'], assigns(:sources)
135     end
136   end
137
138   def test_identify_uk_postcode
139     # examples from http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
140     ['EC1A 1BB', 'W1A 1HQ', 'M1 1AA', 'B33 8TH', 'CR2 6XH', 'DN55 1PT'].each do |code|
141       post :search, query: code
142       assert_response :success
143       assert_equal ['uk_postcode', 'osm_nominatim'], assigns(:sources)
144     end
145   end
146
147   def test_identify_ca_postcode
148     post :search, query: 'A1B 2C3'
149     assert_response :success
150     assert_equal ['ca_postcode', 'osm_nominatim'], assigns(:sources)
151   end
152
153   def test_identify_fall_through_no_geonames
154     post :search, query: 'foo bar baz'
155     assert_response :success
156     assert_equal ['osm_nominatim'], assigns(:sources)
157   end
158
159   def test_ne
160     target = '50.06773, 14.37742'
161     [
162         "N 50° 04.064', E 014° 22.645'",
163         "N 50° 4' 03.828\", E 14° 22' 38.712\"",
164         "50°4'3.828\"N 14°22'38.712\"E",
165         "50° 04.064' N, 014° 22.645' E"
166     ].each do |code|
167       post :search, :query => code
168       assert_response :success
169       assert_equal ['latlon'], assigns(:sources)
170       assert_latlon_equal_round(target, assigns(:query), 5)
171     end
172   end
173
174   def test_nw
175     target = '50.06773, -14.37742'
176     [
177         "N 50° 04.064', W 014° 22.645'",
178         "N 50° 4' 03.828\", W 14° 22' 38.712\"",
179         "50°4'3.828\"N 14°22'38.712\"W",
180         "50° 04.064' N, 014° 22.645' W"
181     ].each do |code|
182       post :search, :query => code
183       assert_response :success
184       assert_equal ['latlon'], assigns(:sources)
185       assert_latlon_equal_round(target, assigns(:query), 5)
186     end
187   end
188
189   def test_se
190     target = '-50.06773, 14.37742'
191     [
192         "S 50° 04.064', E 014° 22.645'",
193         "S 50° 4' 03.828\", E 14° 22' 38.712\"",
194         "50°4'3.828\"S 14°22'38.712\"E",
195         "50° 04.064' S, 014° 22.645' E"
196     ].each do |code|
197       post :search, :query => code
198       assert_response :success
199       assert_equal ['latlon'], assigns(:sources)
200       assert_latlon_equal_round(target, assigns(:query), 5)
201     end
202   end
203
204   def test_sw
205     target = '-50.06773, -14.37742'
206     [
207         "S 50° 04.064', W 014° 22.645'",
208         "S 50° 4' 03.828\", W 14° 22' 38.712\"",
209         "50°4'3.828\"S 14°22'38.712\"W",
210         "50° 04.064' S, 014° 22.645' W"
211     ].each do |code|
212       post :search, :query => code
213       assert_response :success
214       assert_equal ['latlon'], assigns(:sources)
215       assert_latlon_equal_round(target, assigns(:query), 5)
216     end
217   end
218
219   def test_primes_and_double_primes
220     target = '50.06773, -14.37742'
221     [
222         "N 50° 4′ 03.828″, W 14° 22′ 38.712″"
223     ].each do |code|
224       post :search, :query => code
225       assert_response :success
226       assert_equal ['latlon'], assigns(:sources)
227       assert_equal target, assigns(:query)
228     end
229   end
230 end