Back to Repositories

Testing Geocodio Integration Implementation in alexreisner/geocoder

This test suite validates the Geocodio geocoding service integration with the Geocoder gem, covering address lookup, reverse geocoding, and error handling scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of Geocodio’s geocoding functionality, including forward and reverse geocoding operations.

  • Address component validation for US and Canadian addresses
  • Reverse geocoding accuracy verification
  • Error handling for invalid requests and API limits
  • Response format and data structure validation

Implementation Analysis

The testing approach utilizes Ruby’s Test::Unit framework with the GeocoderTestCase base class for consistent setup and teardown.

Key patterns include:
  • Structured test methods for isolated functionality testing
  • API configuration management through Geocoder.configure
  • Assertion-based validation of geocoding results
  • Exception handling verification

Technical Details

Testing infrastructure includes:
  • Ruby Test::Unit framework
  • Geocoder gem configuration
  • Mock API responses for consistent testing
  • UTF-8 encoding enforcement
  • API key management through environment variables

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear naming conventions
  • Comprehensive error scenario coverage
  • Proper setup and configuration management
  • Detailed assertion messages for clear failure reporting
  • Consistent test structure and organization

alexreisner/geocoder

test/unit/lookups/geocodio_test.rb

            
# encoding: utf-8
require 'test_helper'

class GeocodioTest < GeocoderTestCase

  def setup
    super
    Geocoder.configure(lookup: :geocodio)
    set_api_key!(:geocodio)
  end

  def test_result_components
    result = Geocoder.search("1101 Pennsylvania Ave NW, Washington DC").first
    assert_equal 1.0, result.accuracy
    assert_equal "1101", result.number
    assert_equal "1101 Pennsylvania Ave NW", result.street_address
    assert_equal "Ave", result.suffix
    assert_equal "DC", result.state
    assert_equal "20004", result.zip
    assert_equal "NW", result.postdirectional
    assert_equal "Washington", result.city
    assert_equal "US", result.country_code
    assert_equal "United States", result.country
    assert_equal "1101 Pennsylvania Ave NW, Washington, DC 20004", result.formatted_address
    assert_equal({ "lat" => 38.895343, "lng" => -77.027385 }, result.location)
  end

  def test_reverse_canada_result
    result = Geocoder.search([43.652961, -79.382624]).first
    assert_equal 1.0, result.accuracy
    assert_equal "483", result.number
    assert_equal "Bay", result.street
    assert_equal "St", result.suffix
    assert_equal "ON", result.state
    assert_equal "Toronto", result.city
    assert_equal "CA", result.country_code
    assert_equal "Canada", result.country
  end

  def test_no_results
    results = Geocoder.search("no results")
    assert_equal 0, results.length
  end

  def test_geocodio_reverse_url
    query = Geocoder::Query.new([45.423733, -75.676333])
    assert_match(/reverse/, query.url)
  end

  def test_raises_invalid_request_exception
    Geocoder.configure(:always_raise => [Geocoder::InvalidRequest])
    assert_raises Geocoder::InvalidRequest do
      Geocoder.search("invalid")
    end
  end

  def test_raises_api_key_exception
    Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
    assert_raises Geocoder::InvalidApiKey do
      Geocoder.search("bad api key")
    end
  end

  def test_raises_over_limit_exception
    Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError])
    assert_raises Geocoder::OverQueryLimitError do
      Geocoder.search("over query limit")
    end
  end
end