Back to Repositories

Testing Pelias Geocoding Integration in geocoder

This test suite validates the Pelias geocoding lookup functionality in the Geocoder gem. It ensures proper configuration, endpoint handling, and reverse geocoding capabilities for the Pelias integration.

Test Coverage Overview

The test suite covers essential Pelias geocoding functionality including endpoint configuration and reverse geocoding operations. Key test areas include:

  • Default endpoint validation
  • Custom endpoint configuration verification
  • Reverse geocoding query formation
  • API key integration

Implementation Analysis

The testing approach uses Ruby’s unit testing framework with the GeocoderTestCase base class. The implementation follows a clear setup-execution-assertion pattern, with each test focusing on a specific aspect of the Pelias integration.

Tests utilize configuration management and URL validation to ensure proper endpoint construction and query parameter handling.

Technical Details

Testing components include:

  • Ruby unit test framework
  • GeocoderTestCase custom test class
  • Geocoder::Query for request formation
  • Geocoder::Lookup::Pelias for core functionality
  • UTF-8 encoding specification

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Proper test isolation through setup method
  • Configuration reset between tests
  • Clear test method naming
  • Specific assertion messages
  • Focused test scenarios with single responsibility

alexreisner/geocoder

test/unit/lookups/pelias_test.rb

            
# encoding: utf-8
require 'test_helper'

class PeliasTest < GeocoderTestCase

  def setup
    super
    Geocoder.configure(lookup: :pelias, api_key: 'abc123', pelias: {}) # Empty pelias hash only for test (pollution control)
  end

  def test_configure_default_endpoint
    query = Geocoder::Query.new('Madison Square Garden, New York, NY')
    assert_true query.url.start_with?('http://localhost/v1/search'), query.url
  end

  def test_configure_custom_endpoint
    Geocoder.configure(lookup: :pelias, api_key: 'abc123', pelias: {endpoint: 'self.hosted.pelias/proxy'})
    query = Geocoder::Query.new('Madison Square Garden, New York, NY')
    assert_true query.url.start_with?('http://self.hosted.pelias/proxy/v1/search'), query.url
  end

  def test_query_for_reverse_geocode
    lookup = Geocoder::Lookup::Pelias.new
    url = lookup.query_url(Geocoder::Query.new([45.423733, -75.676333]))
    assert_match(/point.lat=45.423733&point.lon=-75.676333/, url)
  end
end