Skip to content
Snippets Groups Projects
smarty_streets_test.rb 2.08 KiB
Newer Older
  • Learn to ignore specific revisions
  • Dan Drinkard's avatar
    Dan Drinkard committed
    # encoding: utf-8
    require 'test_helper'
    
    class SmartyStreetsTest < GeocoderTestCase
    
      def setup
        Geocoder.configure(lookup: :smarty_streets)
        set_api_key!(:smarty_streets)
      end
    
      def test_url_contains_api_key
        Geocoder.configure(:smarty_streets => {:api_key => 'blah'})
        query = Geocoder::Query.new("Bluffton, SC")
    
        assert_match /auth-token=blah/, query.url
    
    Dan Drinkard's avatar
    Dan Drinkard committed
      end
    
      def test_query_for_address_geocode
        query = Geocoder::Query.new("42 Wallaby Way Sydney, AU")
    
        assert_match /api\.smartystreets\.com\/street-address\?/, query.url
    
    Dan Drinkard's avatar
    Dan Drinkard committed
      end
    
      def test_query_for_zipcode_geocode
        query = Geocoder::Query.new("22204")
    
        assert_match /api\.smartystreets\.com\/zipcode\?/, query.url
    
    Dan Drinkard's avatar
    Dan Drinkard committed
      end
    
      def test_query_for_zipfour_geocode
    
        query = Geocoder::Query.new("22204-1603")
        assert_match /api\.smartystreets\.com\/zipcode\?/, query.url
    
    Dan Drinkard's avatar
    Dan Drinkard committed
      end
    
      def test_smarty_streets_result_components
        result = Geocoder.search("Madison Square Garden, New York, NY").first
        assert_equal "Penn", result.street
        assert_equal "10121", result.zipcode
        assert_equal "1703", result.zip4
        assert_equal "New York", result.city
        assert_equal "36061", result.fips
    
    Alex Reisner's avatar
    Alex Reisner committed
        assert !result.zipcode_endpoint?
    
      def test_smarty_streets_result_components_with_zipcode_only_query
        result = Geocoder.search("11211").first
        assert_equal "Brooklyn", result.city
        assert_equal "New York", result.state
        assert_equal "NY", result.state_code
    
    Alex Reisner's avatar
    Alex Reisner committed
        assert result.zipcode_endpoint?
    
    Dan Drinkard's avatar
    Dan Drinkard committed
      def test_no_results
        results = Geocoder.search("no results")
        assert_equal 0, results.length
      end
    
    
      def test_raises_exception_on_error_http_status
        error_statuses = {
          '400' => Geocoder::InvalidRequest,
          '401' => Geocoder::RequestDenied,
          '402' => Geocoder::OverQueryLimitError
        }
        Geocoder.configure(always_raise: error_statuses.values)
        lookup = Geocoder::Lookup.get(:smarty_streets)
        error_statuses.each do |code, err|
          assert_raises err do
            response = MockHttpResponse.new(code: code.to_i)
            lookup.send(:check_response_for_errors!, response)
          end
        end
      end
    
    Dan Drinkard's avatar
    Dan Drinkard committed
    end