Back to Repositories

Testing PostcodeAnywhere UK Geocoding Integration in alexreisner/geocoder

This test suite validates the PostcodeAnywhere UK geocoding service integration within the Geocoder gem. It verifies address lookup functionality for UK postcodes, place names, and counties while handling various API response scenarios and error conditions.

Test Coverage Overview

The test suite provides comprehensive coverage of PostcodeAnywhere UK geocoding functionality, including:

  • Place name search validation with coordinate verification
  • Postcode lookup with address component parsing
  • County-level search capabilities
  • Error handling for API limits and invalid keys
  • Empty result handling

Implementation Analysis

The testing approach uses Ruby’s Test::Unit framework with the GeocoderTestCase base class. It implements systematic test cases for different search scenarios, validating response parsing, coordinate accuracy, and address component extraction. The suite employs configuration management for API keys and error handling settings.

Technical Details

  • Testing Framework: Test::Unit
  • Setup Requirements: API key configuration
  • Test Dependencies: GeocoderTestCase custom test class
  • Configuration Features: Configurable error handling, API key management

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear purpose
  • Comprehensive error scenario coverage
  • Proper test setup and teardown management
  • Consistent assertion patterns
  • Clear test naming conventions

alexreisner/geocoder

test/unit/lookups/postcode_anywhere_uk_test.rb

            
# encoding: utf-8
require 'test_helper'

class PostcodeAnywhereUkTest < GeocoderTestCase

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

  def test_result_components_with_placename_search
    results = Geocoder.search('Romsey')

    assert_equal 1, results.size
    assert_equal 'Romsey, Hampshire', results.first.address
    assert_equal 'SU 35270 21182', results.first.os_grid
    assert_equal [50.9889, -1.4989], results.first.coordinates
    assert_equal 'Romsey', results.first.city
  end

  def test_result_components_with_postcode
    results = Geocoder.search('WR26NJ')

    assert_equal 1, results.size
    assert_equal 'Moseley Road, Hallow, Worcester', results.first.address
    assert_equal 'SO 81676 59425', results.first.os_grid
    assert_equal [52.2327, -2.2697], results.first.coordinates
    assert_equal 'Hallow', results.first.city
  end

  def test_result_components_with_county
    results = Geocoder.search('hampshire')

    assert_equal 1, results.size
    assert_equal 'Hampshire', results.first.address
    assert_equal 'SU 48701 26642', results.first.os_grid
    assert_equal [51.037, -1.3068], results.first.coordinates
    assert_equal '', results.first.city
  end

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

  def test_key_limit_exceeded_error
    Geocoder.configure(always_raise: [Geocoder::OverQueryLimitError])

    assert_raises Geocoder::OverQueryLimitError do
      Geocoder.search('key limit exceeded')
    end
  end

  def test_unknown_key_error
    Geocoder.configure(always_raise: [Geocoder::InvalidApiKey])

    assert_raises Geocoder::InvalidApiKey do
      Geocoder.search('unknown key')
    end
  end

  def test_generic_error
    Geocoder.configure(always_raise: [Geocoder::Error])

    exception = assert_raises(Geocoder::Error) do
      Geocoder.search('generic error')
    end
    assert_equal 'A generic error occured.', exception.message
  end
end