require File.dirname(__FILE__) + '/../../../test_helper' module Graticule module Geocoder class GoogleTest < Test::Unit::TestCase def setup URI::HTTP.responses = [] URI::HTTP.uris = [] @geocoder = Google.new(:key => 'APP_ID') end def test_success return unless prepare_response(:success) location = Location.new( :street => "1600 Amphitheatre Pkwy", :locality => "Mountain View", :region => "CA", :postal_code => "94043", :country => "US", :longitude => -122.083739, :latitude => 37.423021, :precision => Precision.address ) assert_equal location, @geocoder.locate('1600 Amphitheatre Parkway, Mountain View, CA') end # 15-17 200geocode-17.000000,15.000000,0 def test_only_coordinates return unless prepare_response(:only_coordinates) location = Location.new(:longitude => -17.000000, :latitude => 15.000000) assert_equal location, @geocoder.locate('15-17 & 16 Railroad Square, Nashua, NH, 03064') end def test_partial return unless prepare_response(:partial) location = Location.new( :locality => "San Francisco", :region => "CA", :country => "US", :longitude => -122.418333, :latitude => 37.775000, :precision => Precision.city ) assert_equal location, @geocoder.locate('sf ca') end def test_locate_with_first prepare_response(:multiple) location = Location.new( :locality => "Gardiner", :region => "ME", :country => "US", :longitude => -69.802949, :latitude => 44.198024 ) assert @geocoder.locate(:first, 'Gardiner') end def test_locate_with_all prepare_response(:multiple) assert_equal 3, @geocoder.locate(:all, 'Gardiner').size end def test_precision return unless prepare_response(:success) assert_equal Precision.address, @geocoder.locate("1600 Amphitheatre Parkway, Mountain View, CA").precision end def test_precision_comparison return unless prepare_response(:success) location1 = @geocoder.locate("1600 Amphitheatre Parkway, Mountain View, CA") return unless prepare_response(:partial) location2 = @geocoder.locate("sf ca") comparison = location1.precision <=> location2.precision assert_equal 1, comparison end def test_bad_key return unless prepare_response(:badkey) assert_raises(CredentialsError) { @geocoder.locate('x') } end def test_locate_missing_address return unless prepare_response(:missing_address) assert_raises(AddressError) { @geocoder.locate 'x' } end def test_locate_server_error return unless prepare_response(:server_error) assert_raises(Error) { @geocoder.locate 'x' } end def test_locate_too_many_queries return unless prepare_response(:limit) assert_raises(CredentialsError) { @geocoder.locate 'x' } end def test_locate_unavailable_address return unless prepare_response(:unavailable) assert_raises(AddressError) { @geocoder.locate 'x' } end def test_locate_unknown_address return unless prepare_response(:unknown_address) assert_raises(AddressError) { @geocoder.locate 'x' } end protected def prepare_response(id = :success) URI::HTTP.responses << response('google', id) end end end end