# encoding: UTF-8
require 'test_helper'

module Graticule
  module Geocoder
    class YahooTest < Test::Unit::TestCase

      def setup
        URI::HTTP.responses = []
        URI::HTTP.uris = []
        @geocoder = Yahoo.new 'APP_ID'
        @location = Location.new(
          :street => "701 1st Ave",
          :locality => "Sunnyvale",
          :region => "CA",
          :postal_code => "94089-1019",
          :country => "US",
          :longitude => -122.025055,
          :latitude => 37.416397,
          :precision => :address,
          :warning => "The exact location could not be found, here is the closest match: 701 First Ave, Sunnyvale, CA 94089"
        )
      end

    def test_locate
      prepare_response(:success)
      assert_equal @location, @geocoder.locate('701 First Street, Sunnyvale, CA')
    end

    def test_url
      prepare_response(:success)
      @geocoder.locate('701 First Street, Sunnyvale, CA')
      assert_equal 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=APP_ID&location=701%20First%20Street,%20Sunnyvale,%20CA&output=xml',
                   URI::HTTP.uris.first
    end


    def test_locate_bad_address
      prepare_response(:unknown_address)
      assert_raise(Error) { @geocoder.locate('yucksthoeusthaoeusnhtaosu') }
    end

    protected
      def prepare_response(id)
        URI::HTTP.responses << response('yahoo', id)
      end

    end
  end
end