3 require File.dirname(__FILE__) + '/../test_helper'
4 require 'geocoder_controller'
6 class GeocoderControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/geocoder/search", :method => :post },
12 { :controller => "geocoder", :action => "search" }
15 { :path => "/geocoder/search_latlon", :method => :get },
16 { :controller => "geocoder", :action => "search_latlon" }
19 { :path => "/geocoder/search_us_postcode", :method => :get },
20 { :controller => "geocoder", :action => "search_us_postcode" }
23 { :path => "/geocoder/search_uk_postcode", :method => :get },
24 { :controller => "geocoder", :action => "search_uk_postcode" }
27 { :path => "/geocoder/search_ca_postcode", :method => :get },
28 { :controller => "geocoder", :action => "search_ca_postcode" }
31 { :path => "/geocoder/search_osm_namefinder", :method => :get },
32 { :controller => "geocoder", :action => "search_osm_namefinder" }
35 { :path => "/geocoder/search_osm_nominatim", :method => :get },
36 { :controller => "geocoder", :action => "search_osm_nominatim" }
39 { :path => "/geocoder/search_geonames", :method => :get },
40 { :controller => "geocoder", :action => "search_geonames" }
44 { :path => "/geocoder/description", :method => :post },
45 { :controller => "geocoder", :action => "description" }
48 { :path => "/geocoder/description_osm_namefinder", :method => :get },
49 { :controller => "geocoder", :action => "description_osm_namefinder" }
52 { :path => "/geocoder/description_osm_nominatim", :method => :get },
53 { :controller => "geocoder", :action => "description_osm_nominatim" }
56 { :path => "/geocoder/description_geonames", :method => :get },
57 { :controller => "geocoder", :action => "description_geonames" }
62 # test the regular expressions that split search queries into 'latlon', 'us_postcode', and the like
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)
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(', ')
81 def test_identify_latlon_degdec_nsew
82 target = '50.06773, 14.37742'
84 'N50.06773 E14.37742',
85 'N50.06773, E14.37742',
86 '50.06773N 14.37742E',
87 '50.06773N, 14.37742E'
89 post :search, :query => code
90 assert_response :success
91 assert_equal ['latlon'], assigns(:sources)
92 assert_equal target, assigns(:query)
96 def test_identify_latlon_ddm
97 target = '50.06773, 14.37742'
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"
107 post :search, :query => code
108 assert_response :success
109 assert_equal ['latlon'], assigns(:sources)
110 assert_latlon_equal_round(target, assigns(:query), 5)
114 def test_identify_latlon_dms
115 target = '50.06773, 14.37742'
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"
123 post :search, :query => code
124 assert_response :success
125 assert_equal ['latlon'], assigns(:sources)
126 assert_equal target, assigns(:query)
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)
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)
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)
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)
160 target = '50.06773, 14.37742'
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"
167 post :search, :query => code
168 assert_response :success
169 assert_equal ['latlon'], assigns(:sources)
170 assert_latlon_equal_round(target, assigns(:query), 5)
175 target = '50.06773, -14.37742'
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"
182 post :search, :query => code
183 assert_response :success
184 assert_equal ['latlon'], assigns(:sources)
185 assert_latlon_equal_round(target, assigns(:query), 5)
190 target = '-50.06773, 14.37742'
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"
197 post :search, :query => code
198 assert_response :success
199 assert_equal ['latlon'], assigns(:sources)
200 assert_latlon_equal_round(target, assigns(:query), 5)
205 target = '-50.06773, -14.37742'
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"
212 post :search, :query => code
213 assert_response :success
214 assert_equal ['latlon'], assigns(:sources)
215 assert_latlon_equal_round(target, assigns(:query), 5)
219 def test_primes_and_double_primes
220 target = '50.06773, -14.37742'
222 "N 50° 4′ 03.828″, W 14° 22′ 38.712″"
224 post :search, :query => code
225 assert_response :success
226 assert_equal ['latlon'], assigns(:sources)
227 assert_equal target, assigns(:query)