require File.join(File.dirname(__FILE__), 'test_base_geocoder') Geokit::Geocoders::yahoo = 'Yahoo' class YahooGeocoderTest < BaseGeocoderTest #:nodoc: all YAHOO_FULL=<<-EOF.strip 0No errorus_US8718737.792418-122.39391337.792332-122.394027500100 Spear StSan Francisco, CA 94105-1578United States100Spear St94105-1578San FranciscoSan Francisco CountyCaliforniaUnited StatesUSCA941050FA06819B5F53E751279715611 EOF YAHOO_CITY=<<-EOF.strip 0No errorus_US4014037.777125-122.41964437.777125-122.41964410700San Francisco, CAUnited StatesSan FranciscoSan Francisco CountyCaliforniaUnited StatesUSCA9410224879567 EOF def setup super @yahoo_full_hash = {:street_address=>"100 Spear St", :city=>"San Francisco", :state=>"CA", :zip=>"94105-1522", :country_code=>"US"} @yahoo_city_hash = {:city=>"San Francisco", :state=>"CA"} @yahoo_full_loc = Geokit::GeoLoc.new(@yahoo_full_hash) @yahoo_city_loc = Geokit::GeoLoc.new(@yahoo_city_hash) end # the testing methods themselves def test_yahoo_full_address response = MockSuccess.new response.expects(:body).returns(YAHOO_FULL) url = "http://where.yahooapis.com/geocode?appid=Yahoo&count=100&location=#{Geokit::Inflector.url_escape(@address)}" Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response) do_full_address_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@address)) end def test_yahoo_full_address_accuracy response = MockSuccess.new response.expects(:body).returns(YAHOO_FULL) url = "http://where.yahooapis.com/geocode?appid=Yahoo&count=100&location=#{Geokit::Inflector.url_escape(@address)}" Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response) res = Geokit::Geocoders::YahooGeocoder.geocode(@address) assert_equal 8, res.accuracy end def test_yahoo_full_address_precision response = MockSuccess.new response.expects(:body).returns(YAHOO_FULL) url = "http://where.yahooapis.com/geocode?appid=Yahoo&count=100&location=#{Geokit::Inflector.url_escape(@address)}" Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response) res = Geokit::Geocoders::YahooGeocoder.geocode(@address) assert_equal 'address', res.precision end def test_yahoo_full_address_with_geo_loc response = MockSuccess.new response.expects(:body).returns(YAHOO_FULL) url = "http://where.yahooapis.com/geocode?appid=Yahoo&count=100&location=#{Geokit::Inflector.url_escape(@full_address)}" Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response) do_full_address_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@yahoo_full_loc)) end def test_yahoo_city response = MockSuccess.new response.expects(:body).returns(YAHOO_CITY) url = "http://where.yahooapis.com/geocode?appid=Yahoo&count=100&location=#{Geokit::Inflector.url_escape(@address)}" Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response) do_city_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@address)) end def test_yahoo_city_accuracy response = MockSuccess.new response.expects(:body).returns(YAHOO_CITY) url = "http://where.yahooapis.com/geocode?appid=Yahoo&count=100&location=#{Geokit::Inflector.url_escape(@address)}" Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response) res = Geokit::Geocoders::YahooGeocoder.geocode(@address) assert_equal 4, res.accuracy end def test_yahoo_city_precision response = MockSuccess.new response.expects(:body).returns(YAHOO_CITY) url = "http://where.yahooapis.com/geocode?appid=Yahoo&count=100&location=#{Geokit::Inflector.url_escape(@address)}" Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response) res = Geokit::Geocoders::YahooGeocoder.geocode(@address) assert_equal 'city', res.precision end def test_yahoo_city_with_geo_loc response = MockSuccess.new response.expects(:body).returns(YAHOO_CITY) url = "http://where.yahooapis.com/geocode?appid=Yahoo&count=100&location=#{Geokit::Inflector.url_escape(@address)}" Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response) do_city_assertions(Geokit::Geocoders::YahooGeocoder.geocode(@yahoo_city_loc)) end def test_service_unavailable response = MockFailure.new url = "http://where.yahooapis.com/geocode?appid=Yahoo&count=100&location=#{Geokit::Inflector.url_escape(@address)}" Geokit::Geocoders::YahooGeocoder.expects(:call_geocoder_service).with(url).returns(response) assert !Geokit::Geocoders::YahooGeocoder.geocode(@yahoo_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 "CA", res.state assert_equal "San Francisco", res.city assert_equal "37.792418,-122.393913", res.ll assert res.is_us? assert_equal "100 Spear St, San Francisco, CA 94105-1578", res.full_address assert_equal "yahoo", res.provider end def do_city_assertions(res) assert_equal "CA", res.state assert_equal "San Francisco", res.city assert_equal "37.777125,-122.419644", res.ll assert res.is_us? assert_equal "San Francisco, CA, US", res.full_address assert_nil res.street_address assert_equal "yahoo", res.provider end end