Back to Repositories

Testing Pickpoint Geocoding Integration in alexreisner/geocoder

This test suite validates the Pickpoint geocoding service integration within the Geocoder gem. It verifies essential geocoding functionality including address lookups, viewport calculations, and API key handling.

Test Coverage Overview

The test suite provides comprehensive coverage of Pickpoint geocoding functionality, focusing on address component extraction, viewport calculations, and API authentication.

  • Tests address parsing and component extraction
  • Validates viewport coordinate calculations
  • Verifies API key integration
  • Handles error cases for invalid API keys

Implementation Analysis

The testing approach utilizes Ruby’s test framework with the GeocoderTestCase base class. It implements a setup method to configure the Pickpoint lookup and API key settings.

The tests follow a clear pattern of arranging test conditions, executing geocoding operations, and asserting expected results using Ruby’s assertion methods.

Technical Details

  • Uses Ruby’s built-in test framework
  • Extends GeocoderTestCase for shared functionality
  • Implements setup method for test configuration
  • Utilizes Geocoder.configure for runtime configuration
  • Employs assertion methods for result validation

Best Practices Demonstrated

The test suite demonstrates several testing best practices including proper test isolation, clear test naming, and comprehensive error handling.

  • Individual test methods for specific functionality
  • Descriptive test method names
  • Proper setup and configuration management
  • Error case handling with exception testing

alexreisner/geocoder

test/unit/lookups/pickpoint_test.rb

            
require 'test_helper'

class PickpointTest < GeocoderTestCase

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

  def test_result_components
    result = Geocoder.search("Madison Square Garden, New York, NY").first
    assert_equal "10001", result.postal_code
    assert_equal "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America", result.address
  end

  def test_result_viewport
    result = Geocoder.search("Madison Square Garden, New York, NY").first
    assert_equal [40.749828338623, -73.9943389892578, 40.7511596679688, -73.9926528930664], result.viewport
  end

  def test_url_contains_api_key
    Geocoder.configure(pickpoint: {api_key: "pickpoint-api-key"})
    query = Geocoder::Query.new("Leadville, CO")
    assert_match(/key=pickpoint-api-key/, query.url)
  end

  def test_raises_exception_with_invalid_api_key
    Geocoder.configure(always_raise: [Geocoder::InvalidApiKey])
    assert_raises Geocoder::InvalidApiKey do
      Geocoder.search("invalid api key")
    end
  end
end