Back to Repositories

Testing Mapquest Geocoding Integration in geocoder

This test suite validates the Mapquest geocoding integration within the Geocoder gem, focusing on API interactions, result parsing, and error handling. It ensures proper functionality of both standard and Open Street Maps endpoints while verifying response formatting and error scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of the Mapquest geocoding functionality.

Key areas tested include:
  • API key validation and URL construction
  • Support for both standard and Open Street Maps endpoints
  • Result component parsing and validation
  • Error handling for invalid requests, API keys, and general errors

Implementation Analysis

The testing approach utilizes Ruby’s test framework with the GeocoderTestCase base class. The implementation follows a systematic pattern of validating configuration, request formation, and response handling.

Key patterns include:
  • Setup method for test initialization and configuration
  • URL construction verification
  • Response parsing validation
  • Exception handling tests

Technical Details

Testing infrastructure includes:
  • Ruby test framework
  • GeocoderTestCase custom test class
  • Geocoder configuration management
  • API key configuration
  • Mock request/response handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable practices include:
  • Isolated test cases with clear objectives
  • Proper setup and configuration management
  • Comprehensive error scenario coverage
  • Consistent assertion patterns
  • Clear test case naming conventions

alexreisner/geocoder

test/unit/lookups/mapquest_test.rb

            
# encoding: utf-8
require 'test_helper'

class MapquestTest < GeocoderTestCase

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

  def test_url_contains_api_key
    Geocoder.configure(mapquest: {api_key: "abc123"})
    query = Geocoder::Query.new("Bluffton, SC")
    assert_equal "http://www.mapquestapi.com/geocoding/v1/address?key=abc123&location=Bluffton%2C+SC", query.url
  end

  def test_url_for_open_street_maps
    Geocoder.configure(mapquest: {api_key: "abc123", open: true})
    query = Geocoder::Query.new("Bluffton, SC")
    assert_equal "http://open.mapquestapi.com/geocoding/v1/address?key=abc123&location=Bluffton%2C+SC", query.url
  end

  def test_result_components
    result = Geocoder.search("Madison Square Garden, New York, NY").first
    assert_equal "10001", result.postal_code
    assert_equal "46 West 31st Street, New York, NY, 10001, US", result.address
  end

  def test_no_results
    assert_equal [], Geocoder.search("no results")
  end

  def test_raises_exception_when_invalid_request
    Geocoder.configure(always_raise: [Geocoder::InvalidRequest])
    assert_raises Geocoder::InvalidRequest do
      Geocoder.search("invalid request")
    end
  end

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

  def test_raises_exception_when_error
    Geocoder.configure(always_raise: [Geocoder::Error])
    assert_raises Geocoder::Error do
      Geocoder.search("error")
    end
  end
end