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 identification of basic lat/lon pairs
 
  63   def test_identify_latlon_basic
 
  67      '+50.06773 +14.37742',
 
  68      '+50.06773, +14.37742'
 
  70       post :search, :query => code
 
  71       assert_response :success
 
  72       assert_equal ['latlon'], assigns(:sources)
 
  73       assert_equal code, assigns(:query)
 
  78   # Test identification of lat/lon pairs using N/E with degrees
 
  79   def test_identify_latlon_ne_d
 
  81      'N50.06773 E14.37742',
 
  82      'N50.06773, E14.37742',
 
  83      '50.06773N 14.37742E',
 
  84      '50.06773N, 14.37742E'
 
  86       post :search, :query => code
 
  87       assert_response :success
 
  88       assert_equal ['latlon'], assigns(:sources)
 
  89       assert_equal "50.06773, 14.37742", assigns(:query)
 
  94   # Test identification of lat/lon pairs using N/W with degrees
 
  95   def test_identify_latlon_nw_d
 
  97      'N50.06773 W14.37742',
 
  98      'N50.06773, W14.37742',
 
  99      '50.06773N 14.37742W',
 
 100      '50.06773N, 14.37742W'
 
 102       post :search, :query => code
 
 103       assert_response :success
 
 104       assert_equal ['latlon'], assigns(:sources)
 
 105       assert_equal "50.06773, -14.37742", assigns(:query)
 
 110   # Test identification of lat/lon pairs using S/E with degrees
 
 111   def test_identify_latlon_se_d
 
 113      'S50.06773 E14.37742',
 
 114      'S50.06773, E14.37742',
 
 115      '50.06773S 14.37742E',
 
 116      '50.06773S, 14.37742E'
 
 118       post :search, :query => code
 
 119       assert_response :success
 
 120       assert_equal ['latlon'], assigns(:sources)
 
 121       assert_equal "-50.06773, 14.37742", assigns(:query)
 
 126   # Test identification of lat/lon pairs using S/W with degrees
 
 127   def test_identify_latlon_sw_d
 
 129      'S50.06773 W14.37742',
 
 130      'S50.06773, W14.37742',
 
 131      '50.06773S 14.37742W',
 
 132      '50.06773S, 14.37742W'
 
 134       post :search, :query => code
 
 135       assert_response :success
 
 136       assert_equal ['latlon'], assigns(:sources)
 
 137       assert_equal "-50.06773, -14.37742", assigns(:query)
 
 142   # Test identification of lat/lon pairs using N/E with degrees/mins
 
 143   def test_identify_latlon_ne_dm
 
 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"
 
 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
 
 161   # Test identification of lat/lon pairs using N/W with degrees/mins
 
 162   def test_identify_latlon_nw_dm
 
 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"
 
 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
 
 180   # Test identification of lat/lon pairs using S/E with degrees/mins
 
 181   def test_identify_latlon_se_dm
 
 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"
 
 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
 
 199   # Test identification of lat/lon pairs using S/W with degrees/mins
 
 200   def test_identify_latlon_sw_dm
 
 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"
 
 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
 
 218   # Test identification of lat/lon pairs using N/E with degrees/mins/secs
 
 219   def test_identify_latlon_ne_dms
 
 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"
 
 228       post :search, :query => code
 
 229       assert_response :success
 
 230       assert_equal ['latlon'], assigns(:sources)
 
 231       assert_equal "50.06773, 14.37742", assigns(:query)
 
 236   # Test identification of lat/lon pairs using N/W with degrees/mins/secs
 
 237   def test_identify_latlon_nw_dms
 
 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"
 
 246       post :search, :query => code
 
 247       assert_response :success
 
 248       assert_equal ['latlon'], assigns(:sources)
 
 249       assert_equal "50.06773, -14.37742", assigns(:query)
 
 254   # Test identification of lat/lon pairs using S/E with degrees/mins/secs
 
 255   def test_identify_latlon_se_dms
 
 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"
 
 264       post :search, :query => code
 
 265       assert_response :success
 
 266       assert_equal ['latlon'], assigns(:sources)
 
 267       assert_equal "-50.06773, 14.37742", assigns(:query)
 
 272   # Test identification of lat/lon pairs using S/W with degrees/mins/secs
 
 273   def test_identify_latlon_sw_dms
 
 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"
 
 282       post :search, :query => code
 
 283       assert_response :success
 
 284       assert_equal ['latlon'], assigns(:sources)
 
 285       assert_equal "-50.06773, -14.37742", assigns(:query)
 
 290   # Test identification of US zipcodes
 
 291   def test_identify_us_postcode
 
 296       post :search, query: code
 
 297       assert_response :success
 
 298       assert_equal ['us_postcode', 'osm_nominatim'], assigns(:sources)
 
 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
 
 314       post :search, query: code
 
 315       assert_response :success
 
 316       assert_equal ['uk_postcode', 'osm_nominatim'], assigns(:sources)
 
 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)
 
 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)
 
 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(', ')