3 class SearchesControllerTest < ActionDispatch::IntegrationTest
 
   5   # test all routes which lead to this controller
 
   8       { :path => "/search", :method => :get },
 
   9       { :controller => "searches", :action => "show" }
 
  14   # Test identification with no arguments
 
  15   def test_identify_error
 
  17     assert_response :bad_request
 
  19     get search_path, :xhr => true
 
  20     assert_response :bad_request
 
  24   # Test identification of basic lat/lon pairs
 
  25   def test_identify_latlon_basic
 
  30       "+50.06773 +14.37742",
 
  31       "+50.06773, +14.37742",
 
  34       latlon_check code, 50.06773, 14.37742
 
  39   # Test identification of lat/lon pairs using N/E with degrees
 
  40   def test_identify_latlon_ne_d
 
  42       "N50.06773 E14.37742",
 
  43       "N50.06773, E14.37742",
 
  44       "50.06773N 14.37742E",
 
  45       "50.06773N, 14.37742E"
 
  47       latlon_check code, 50.06773, 14.37742
 
  52   # Test identification of integer lat/lon pairs using N/E with degrees
 
  53   def test_identify_latlon_ne_d_int_deg
 
  60       latlon_check code, 50, 14
 
  65   # Test identification of lat/lon pairs using N/W with degrees
 
  66   def test_identify_latlon_nw_d
 
  68       "N50.06773 W14.37742",
 
  69       "N50.06773, W14.37742",
 
  70       "50.06773N 14.37742W",
 
  71       "50.06773N, 14.37742W"
 
  73       latlon_check code, 50.06773, -14.37742
 
  78   # Test identification of lat/lon pairs using S/E with degrees
 
  79   def test_identify_latlon_se_d
 
  81       "S50.06773 E14.37742",
 
  82       "S50.06773, E14.37742",
 
  83       "50.06773S 14.37742E",
 
  84       "50.06773S, 14.37742E"
 
  86       latlon_check code, -50.06773, 14.37742
 
  91   # Test identification of lat/lon pairs using S/W with degrees
 
  92   def test_identify_latlon_sw_d
 
  94       "S50.06773 W14.37742",
 
  95       "S50.06773, W14.37742",
 
  96       "50.06773S 14.37742W",
 
  97       "50.06773S, 14.37742W"
 
  99       latlon_check code, -50.06773, -14.37742
 
 104   # Test identification of lat/lon pairs using N/E with degrees/mins
 
 105   def test_identify_latlon_ne_dm
 
 107       "N 50° 04.064 E 014° 22.645",
 
 108       "N 50° 04.064' E 014° 22.645",
 
 109       "N 50° 04.064', E 014° 22.645'",
 
 110       "N50° 04.064 E14° 22.645",
 
 111       "N 50 04.064 E 014 22.645",
 
 112       "N50 4.064 E14 22.645",
 
 113       "50° 04.064' N, 014° 22.645' E"
 
 115       latlon_check code, 50.06773, 14.37742
 
 120   # Test identification of lat/lon pairs using N/W with degrees/mins
 
 121   def test_identify_latlon_nw_dm
 
 123       "N 50° 04.064 W 014° 22.645",
 
 124       "N 50° 04.064' W 014° 22.645",
 
 125       "N 50° 04.064', W 014° 22.645'",
 
 126       "N50° 04.064 W14° 22.645",
 
 127       "N 50 04.064 W 014 22.645",
 
 128       "N50 4.064 W14 22.645",
 
 129       "50° 04.064' N, 014° 22.645' W"
 
 131       latlon_check code, 50.06773, -14.37742
 
 136   # Test identification of lat/lon pairs using S/E with degrees/mins
 
 137   def test_identify_latlon_se_dm
 
 139       "S 50° 04.064 E 014° 22.645",
 
 140       "S 50° 04.064' E 014° 22.645",
 
 141       "S 50° 04.064', E 014° 22.645'",
 
 142       "S50° 04.064 E14° 22.645",
 
 143       "S 50 04.064 E 014 22.645",
 
 144       "S50 4.064 E14 22.645",
 
 145       "50° 04.064' S, 014° 22.645' E"
 
 147       latlon_check code, -50.06773, 14.37742
 
 152   # Test identification of lat/lon pairs using S/W with degrees/mins
 
 153   def test_identify_latlon_sw_dm
 
 155       "S 50° 04.064 W 014° 22.645",
 
 156       "S 50° 04.064' W 014° 22.645",
 
 157       "S 50° 04.064', W 014° 22.645'",
 
 158       "S50° 04.064 W14° 22.645",
 
 159       "S 50 04.064 W 014 22.645",
 
 160       "S50 4.064 W14 22.645",
 
 161       "50° 04.064' S, 014° 22.645' W"
 
 163       latlon_check code, -50.06773, -14.37742
 
 168   # Test identification of lat/lon pairs using N/E with degrees/mins/secs
 
 169   def test_identify_latlon_ne_dms
 
 171       "N 50° 4' 03.828\" E 14° 22' 38.712\"",
 
 172       "N 50° 4' 03.828\", E 14° 22' 38.712\"",
 
 173       "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
 
 174       "N50 4 03.828 E14 22 38.712",
 
 175       "N50 4 03.828, E14 22 38.712",
 
 176       "50°4'3.828\"N 14°22'38.712\"E"
 
 178       latlon_check code, 50.06773, 14.37742
 
 183   # Test identification of lat/lon pairs using N/W with degrees/mins/secs
 
 184   def test_identify_latlon_nw_dms
 
 186       "N 50° 4' 03.828\" W 14° 22' 38.712\"",
 
 187       "N 50° 4' 03.828\", W 14° 22' 38.712\"",
 
 188       "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
 
 189       "N50 4 03.828 W14 22 38.712",
 
 190       "N50 4 03.828, W14 22 38.712",
 
 191       "50°4'3.828\"N 14°22'38.712\"W"
 
 193       latlon_check code, 50.06773, -14.37742
 
 198   # Test identification of lat/lon pairs using S/E with degrees/mins/secs
 
 199   def test_identify_latlon_se_dms
 
 201       "S 50° 4' 03.828\" E 14° 22' 38.712\"",
 
 202       "S 50° 4' 03.828\", E 14° 22' 38.712\"",
 
 203       "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
 
 204       "S50 4 03.828 E14 22 38.712",
 
 205       "S50 4 03.828, E14 22 38.712",
 
 206       "50°4'3.828\"S 14°22'38.712\"E"
 
 208       latlon_check code, -50.06773, 14.37742
 
 213   # Test identification of lat/lon pairs using S/W with degrees/mins/secs
 
 214   def test_identify_latlon_sw_dms
 
 216       "S 50° 4' 03.828\" W 14° 22' 38.712\"",
 
 217       "S 50° 4' 03.828\", W 14° 22' 38.712\"",
 
 218       "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
 
 219       "S50 4 03.828 W14 22 38.712",
 
 220       "S50 4 03.828, W14 22 38.712",
 
 221       "50°4'3.828\"S 14°22'38.712\"W"
 
 223       latlon_check code, -50.06773, -14.37742
 
 228   # Test identification of lat/lon pairs with missing fractions
 
 229   def test_no_identify_latlon_ne_missing_fraction_part
 
 239       "N50 1' 3,\" E14 2' 4.\"",
 
 240       "N50° 1' 3.\" E14° 2' 4.\"",
 
 241       "50N 1' 3.\" 14 2' 4.\"E",
 
 242       "50° 1' 3.\"N 14° 2' 4.\"E"
 
 244       get search_path(:query => code)
 
 245       assert_response :success
 
 246       assert_template :show
 
 247       assert_template :layout => "map"
 
 248       assert_equal %w[nominatim], assigns(:sources).pluck(:name)
 
 253   # Test identification of lat/lon pairs with mixed precision
 
 254   def test_identify_latlon_ne_mixed_precision
 
 255     latlon_check "N1 5 E15",    1.083333, 15
 
 256     latlon_check "N1 5 9 E15",  1.085833, 15
 
 257     latlon_check "N1 5 9 E1 5", 1.085833, 1.083333
 
 258     latlon_check "N15 E1 5",    15, 1.083333
 
 259     latlon_check "N15 E1 5 9",  15, 1.085833
 
 260     latlon_check "N1 5 E1 5 9", 1.083333, 1.085833
 
 264   # Test identification of lat/lon pairs with values close to zero
 
 265   def test_identify_latlon_close_to_zero
 
 267       "0.0000123 -0.0000456",
 
 268       "+0.0000123 -0.0000456",
 
 269       "N 0° 0' 0.4428\", W 0° 0' 1.6416\""
 
 271       latlon_check code, 0.0000123, -0.0000456
 
 276   # Test identification of US zipcodes
 
 277   def test_identify_us_postcode
 
 282       search_check code, %w[nominatim]
 
 287   # Test identification of UK postcodes using examples from
 
 288   # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
 
 289   def test_identify_uk_postcode
 
 298       search_check code, %w[nominatim]
 
 303   # Test identification of Canadian postcodes
 
 304   def test_identify_ca_postcode
 
 305     search_check "A1B 2C3", %w[nominatim]
 
 309   # Test identification fall through to the default case
 
 310   def test_identify_default
 
 311     search_check "foo bar baz", %w[nominatim]
 
 315   # Test the nominatim reverse JSON search
 
 316   def test_search_osm_nominatim_reverse_json
 
 317     with_http_stubs "nominatim" do
 
 318       post search_nominatim_reverse_query_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15, :format => "json"), :xhr => true
 
 319       result_name_check_json("Broxbourne, Hertfordshire, East of England, England, United Kingdom")
 
 321       post search_nominatim_reverse_query_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17, :format => "json"), :xhr => true
 
 322       result_name_check_json("Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom")
 
 324       post search_nominatim_reverse_query_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19, :format => "json"), :xhr => true
 
 325       result_name_check_json("MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย")
 
 331   def latlon_check(query, lat, lon)
 
 332     get search_path(:query => query)
 
 333     assert_response :success
 
 334     assert_template :show
 
 335     assert_template :layout => "map"
 
 336     assert_equal %w[latlon nominatim_reverse], assigns(:sources).pluck(:name)
 
 337     assert_nil @controller.params[:query]
 
 338     assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lat])
 
 339     assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lon])
 
 340     assert_in_delta lat, @controller.params[:lat].to_f
 
 341     assert_in_delta lon, @controller.params[:lon].to_f
 
 343     get search_path(:query => query), :xhr => true
 
 344     assert_response :success
 
 345     assert_template :show
 
 346     assert_template :layout => "xhr"
 
 347     assert_equal %w[latlon nominatim_reverse], assigns(:sources).pluck(:name)
 
 348     assert_nil @controller.params[:query]
 
 349     assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lat])
 
 350     assert_match(/^[+-]?\d+(?:\.\d+)?$/, @controller.params[:lon])
 
 351     assert_in_delta lat, @controller.params[:lat].to_f
 
 352     assert_in_delta lon, @controller.params[:lon].to_f
 
 355   def search_check(query, sources)
 
 356     get search_path(:query => query)
 
 357     assert_response :success
 
 358     assert_template :show
 
 359     assert_template :layout => "map"
 
 360     assert_equal sources, assigns(:sources).pluck(:name)
 
 362     get search_path(:query => query), :xhr => true
 
 363     assert_response :success
 
 364     assert_template :show
 
 365     assert_template :layout => "xhr"
 
 366     assert_equal sources, assigns(:sources).pluck(:name)
 
 369   def result_name_check_json(name)
 
 370     assert_response :success
 
 371     js = ActiveSupport::JSON.decode(@response.body)
 
 373     assert_equal name, js[0]["name"]