test/test_openstreetmap_geocoder.rb in geokit-1.10.0 vs test/test_openstreetmap_geocoder.rb in geokit-1.11.0

- old
+ new

@@ -1,7 +1,7 @@ # encoding: utf-8 -require File.join(File.dirname(__FILE__), "helper") +require File.join(File.dirname(__FILE__), 'helper') class OSMGeocoderTest < BaseGeocoderTest #:nodoc: all OSM_FULL = <<-EOF.strip [{"place_id":"425554497","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","boundingbox":["37.792341","37.792441","-122.394074","-122.393974"],"lat":"37.792391","lon":"-122.394024","display_name":"100, Spear Street, Financial District, San Francisco, California, 94105, United States of America","class":"place","type":"house","address":{"house_number":"100","road":"Spear Street","place":"Financial District","city":"San Francisco","county":"San Francisco","state":"California","postcode":"94105","country":"United States of America","country_code":"us"}}] EOF @@ -18,160 +18,162 @@ {"place_id":"46960069","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"way","osm_id":"39757255","lat":"41.3508581896005","lon":"21.549896984439","display_name":"Gimnasium Mirche Acev, Marksova, Prilep, Macedonia","address":{"school":"Gimnasium Mirche Acev","road":"Marksova","city":"Prilep","country":"Macedonia","country_code":"mk"}} EOF def setup super - @osm_full_hash = {street_address: "100 Spear St", city: "San Francisco", state: "CA", zip: "94105", country_code: "US"} - @osm_city_hash = {city: "San Francisco", state: "CA"} + @osm_full_hash = {street_address: '100 Spear St', city: 'San Francisco', state: 'CA', zip: '94105', country_code: 'US'} + @osm_city_hash = {city: 'San Francisco', state: 'CA'} @osm_full_loc = Geokit::GeoLoc.new(@osm_full_hash) @osm_city_loc = Geokit::GeoLoc.new(@osm_city_hash) + @base_url = 'http://nominatim.openstreetmap.org/search' + @reverse_url = 'http://nominatim.openstreetmap.org/reverse' end # the testing methods themselves def test_osm_full_address response = MockSuccess.new response.expects(:body).returns(OSM_FULL) - url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - do_full_address_assertions(Geokit::Geocoders::OSMGeocoder.geocode(@full_address_short_zip)) + url = "#{@base_url}?format=json&polygon=0&addressdetails=1&q=#{escape(@full_address_short_zip)}" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + do_full_address_assertions(geocode(@full_address_short_zip)) end def test_osm_full_address_accuracy response = MockSuccess.new response.expects(:body).returns(OSM_FULL) - url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - res = Geokit::Geocoders::OSMGeocoder.geocode(@full_address_short_zip) - assert_equal "house", res.accuracy + url = "#{@base_url}?format=json&polygon=0&addressdetails=1&q=#{escape(@full_address_short_zip)}" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + res = geocode(@full_address_short_zip) + assert_equal 'house', res.accuracy end def test_osm_full_address_with_geo_loc response = MockSuccess.new response.expects(:body).returns(OSM_FULL) - url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@full_address_short_zip)}" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - do_full_address_assertions(Geokit::Geocoders::OSMGeocoder.geocode(@osm_full_loc)) + url = "#{@base_url}?format=json&polygon=0&addressdetails=1&q=#{escape(@full_address_short_zip)}" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + do_full_address_assertions(geocode(@osm_full_loc)) end def test_osm_city response = MockSuccess.new response.expects(:body).returns(OSM_CITY) - url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - do_city_assertions(Geokit::Geocoders::OSMGeocoder.geocode(@address)) + url = "#{@base_url}?format=json&polygon=0&addressdetails=1&q=#{escape(@address)}" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + do_city_assertions(geocode(@address)) end def test_osm_city_with_accept_language response = MockSuccess.new response.expects(:body).returns(OSM_CITY) - url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&accept-language=pt-br&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - do_city_assertions(Geokit::Geocoders::OSMGeocoder.geocode(@address, {:'accept-language' => "pt-br"})) + url = "#{@base_url}?format=json&polygon=0&accept-language=pt-br&addressdetails=1&q=#{escape(@address)}" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + do_city_assertions(geocode(@address, {:'accept-language' => 'pt-br'})) end def test_osm_city_accuracy response = MockSuccess.new response.expects(:body).returns(OSM_CITY) - url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - res = Geokit::Geocoders::OSMGeocoder.geocode(@address) - assert_equal "county", res.accuracy + url = "#{@base_url}?format=json&polygon=0&addressdetails=1&q=#{escape(@address)}" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + res = geocode(@address) + assert_equal 'county', res.accuracy end def test_osm_city_with_geo_loc response = MockSuccess.new response.expects(:body).returns(OSM_CITY) - url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - do_city_assertions(Geokit::Geocoders::OSMGeocoder.geocode(@osm_city_loc)) + url = "#{@base_url}?format=json&polygon=0&addressdetails=1&q=#{escape(@address)}" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + do_city_assertions(geocode(@osm_city_loc)) end def test_reverse_geo_coding response = MockSuccess.new response.expects(:body).returns(OSM_REVERSE_PRILEP) prilep = Geokit::GeoLoc.new - prilep.lat, prilep.lng = "41.3527177", "21.5497808" - url = "http://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1&lat=#{prilep.lat}&lon=#{prilep.lng}" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - res = Geokit::Geocoders::OSMGeocoder.do_reverse_geocode(prilep.ll) + prilep.lat, prilep.lng = '41.3527177', '21.5497808' + url = "#{@reverse_url}?format=json&addressdetails=1&lat=#{prilep.lat}&lon=#{prilep.lng}" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + res = reverse_geocode(prilep.ll) # OSM does not return the exast lat lng in response # assert_equal prilep.lat.to_s.slice(1..5), res.lat.to_s.slice(1..5) # assert_equal prilep.lng.to_s.slice(1..5), res.lng.to_s.slice(1..5) - assert_equal "MK", res.country_code - assert_equal "osm", res.provider + assert_equal 'MK', res.country_code + assert_equal 'osm', res.provider - assert_equal "Prilep", res.city + assert_equal 'Prilep', res.city assert_nil res.state - assert_equal "Macedonia", res.country + assert_equal 'Macedonia', res.country assert_nil res.precision assert_equal true, res.success - assert_equal "Marksova, Prilep, MK", res.full_address - assert_equal "Marksova", res.street_address + assert_equal 'Marksova, Prilep, MK', res.full_address + assert_equal 'Marksova', res.street_address end def test_reverse_geo_code response = MockSuccess.new response.expects(:body).returns(OSM_REVERSE_MADRID) location = Geokit::GeoLoc.new # Madrid - location.lat, location.lng = "40.4167413", "-3.7032498" - url = "http://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1&lat=#{location.lat}&lon=#{location.lng}" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - res = Geokit::Geocoders::OSMGeocoder.do_reverse_geocode(location.ll) + location.lat, location.lng = '40.4167413', '-3.7032498' + url = "#{@reverse_url}?format=json&addressdetails=1&lat=#{location.lat}&lon=#{location.lng}" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + res = reverse_geocode(location.ll) - assert_equal "ES", res.country_code - assert_equal "osm", res.provider + assert_equal 'ES', res.country_code + assert_equal 'osm', res.provider - assert_equal "Madrid", res.city - assert_equal "Madrid", res.state + assert_equal 'Madrid', res.city + assert_equal 'Madrid', res.state - assert_equal "Spain", res.country + assert_equal 'Spain', res.country assert_equal true, res.success - assert_equal "Calle Del Doctor Tolosa Latour, Usera, Madrid, Madrid, 28026, ES", res.full_address - assert_equal "28026", res.zip - assert_equal "Calle Del Doctor Tolosa Latour", res.street_address + assert_equal 'Calle Del Doctor Tolosa Latour, Usera, Madrid, Madrid, 28026, ES', res.full_address + assert_equal '28026', res.zip + assert_equal 'Calle Del Doctor Tolosa Latour', res.street_address end def test_reverse_geo_code_with_accept_language response = MockSuccess.new response.expects(:body).returns(OSM_REVERSE_MADRID) location = Geokit::GeoLoc.new # Madrid - location.lat, location.lng = "40.4167413", "-3.7032498" - url = "http://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1&lat=#{location.lat}&lon=#{location.lng}&accept-language=pt-br" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - Geokit::Geocoders::OSMGeocoder.do_reverse_geocode(location.ll, {:'accept-language' => "pt-br"}) + location.lat, location.lng = '40.4167413', '-3.7032498' + url = "#{@reverse_url}?format=json&addressdetails=1&lat=#{location.lat}&lon=#{location.lng}&accept-language=pt-br" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + reverse_geocode(location.ll, {:'accept-language' => 'pt-br'}) end def test_service_unavailable response = MockFailure.new - url = "http://nominatim.openstreetmap.org/search?format=json&polygon=0&addressdetails=1&q=#{Geokit::Inflector.url_escape(@address)}" - Geokit::Geocoders::OSMGeocoder.expects(:call_geocoder_service).with(url).returns(response) - assert !Geokit::Geocoders::OSMGeocoder.geocode(@osm_city_loc).success + url = "#{@base_url}?format=json&polygon=0&addressdetails=1&q=#{escape(@address)}" + geocoder_class.expects(:call_geocoder_service).with(url).returns(response) + assert !geocode(@osm_city_loc).success end private # next two methods do the assertions for both address-level and city-level lookups def do_full_address_assertions(res) - assert_equal "California", res.state - assert_equal "San Francisco", res.city - assert_equal "37.792391,-122.394024", res.ll + assert_equal 'California', res.state + assert_equal 'San Francisco', res.city + assert_equal '37.792391,-122.394024', res.ll assert res.is_us? - assert_equal "Spear Street 100, San Francisco, California, 94105, US", res.full_address - assert_equal "osm", res.provider + assert_equal 'Spear Street 100, San Francisco, California, 94105, US', res.full_address + assert_equal 'osm', res.provider end def do_city_assertions(res) - assert_equal "California", res.state - assert_equal "San Francisco", res.city - assert_equal "37.7340974,-122.3912596", res.ll + assert_equal 'California', res.state + assert_equal 'San Francisco', res.city + assert_equal '37.7340974,-122.3912596', res.ll assert res.is_us? - assert_equal "San Francisco, California, US", res.full_address + assert_equal 'San Francisco, California, US', res.full_address assert_nil res.street_address - assert_equal "osm", res.provider + assert_equal 'osm', res.provider end end