Back to Repositories

Testing Google Premier Geocoding Integration in alexreisner/geocoder

This test suite validates the Google Premier geocoding service integration within the Geocoder gem, focusing on API communication, result parsing, and caching mechanisms.

Test Coverage Overview

The test suite covers core functionality of the Google Premier geocoding service integration:

  • Address component extraction and validation
  • URL construction with proper API credentials
  • Cache key generation and management
  • Query parameter handling and encoding

Implementation Analysis

The testing approach utilizes Ruby’s Test::Unit framework with custom assertions for geocoding-specific validations. The tests employ a structured setup pattern to configure the Google Premier lookup and API credentials before each test execution.

Key patterns include isolated component testing, API URL validation, and cache key verification.

Technical Details

Testing infrastructure includes:

  • GeocoderTestCase as the base test class
  • Mock API key configuration
  • URL encoding verification
  • JSON response handling
  • Custom setup methods for test isolation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation through setup methods
  • Specific assertion checks for each component
  • Clear test naming conventions
  • Comprehensive API interaction validation
  • Secure handling of API credentials

alexreisner/geocoder

test/unit/lookups/google_premier_test.rb

            
# encoding: utf-8
require 'test_helper'

class GooglePremierTest < GeocoderTestCase

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

  def test_result_components
    result = Geocoder.search("Madison Square Garden, New York, NY").first
    assert_equal "Manhattan", result.address_components_of_type(:sublocality).first['long_name']
  end

  def test_query_url
    Geocoder.configure(google_premier: {api_key: ["deadbeef", "gme-test", "test-dev"]})
    query = Geocoder::Query.new("Madison Square Garden, New York, NY")
    assert_equal "https://maps.googleapis.com/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&channel=test-dev&client=gme-test&language=en&sensor=false&signature=doJvJqX7YJzgV9rJ0DnVkTGZqTg=", query.url
  end

  def test_cache_key
    Geocoder.configure(google_premier: {api_key: ["deadbeef", "gme-test", "test-dev"]})
    lookup = Geocoder::Lookup.get(:google_premier)
    query = Geocoder::Query.new("Madison Square Garden, New York, NY")
    cache_key = lookup.send(:cache_key, query)
    assert_equal "https://maps.googleapis.com/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&language=en&sensor=false", cache_key
  end
end