Back to Repositories

Testing Latlon Lookup Implementation in Geocoder

This test suite validates the Latlon lookup functionality in the Geocoder gem, focusing on address parsing, reverse geocoding, and API key handling. It ensures accurate conversion between addresses and coordinates while maintaining proper error handling.

Test Coverage Overview

The test suite provides comprehensive coverage of the Latlon lookup implementation.

Key areas tested include:
  • Address component parsing and extraction
  • Reverse geocoding functionality
  • Error handling for invalid API keys
  • Empty result handling
The suite verifies both forward and reverse geocoding operations, ensuring accurate coordinate and address transformations.

Implementation Analysis

The testing approach utilizes Ruby’s native test framework with the GeocoderTestCase base class. The implementation follows a structured pattern with setup configuration and individual test methods.

Notable patterns include:
  • Consistent setup configuration for API keys
  • Detailed assertion checking for address components
  • URL pattern validation for reverse geocoding
  • Exception handling verification

Technical Details

Testing components include:
  • Ruby test framework
  • GeocoderTestCase custom test class
  • Geocoder configuration management
  • API key validation
  • UTF-8 encoding support
The suite leverages Geocoder’s built-in test helpers and configuration management.

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby.

Notable practices include:
  • Proper test isolation through setup methods
  • Comprehensive component validation
  • Explicit error case handling
  • Clear test method naming
  • Focused test scenarios with single responsibilities
The code organization follows Ruby testing conventions with clear separation of concerns.

alexreisner/geocoder

test/unit/lookups/latlon_test.rb

            
# encoding: utf-8
require 'test_helper'

class LatlonTest < GeocoderTestCase

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

  def test_result_components
    result = Geocoder.search("6000 Universal Blvd, Orlando, FL 32819").first
    assert_equal "6000", result.number
    assert_equal "Universal", result.street_name
    assert_equal "Blvd", result.street_type
    assert_equal "Universal Blvd", result.street
    assert_equal "Orlando", result.city
    assert_equal "FL", result.state
    assert_equal "32819", result.zip
    assert_equal "6000 Universal Blvd, Orlando, FL 32819", result.formatted_address
    assert_equal(28.4750507575094, result.latitude)
    assert_equal(-81.4630386931719, result.longitude)
  end

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

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

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

end