Back to Repositories

Testing Here Geocoding Service Integration in alexreisner/geocoder

This test suite validates the functionality of the Here geocoding service integration within the Geocoder gem. It covers API key handling, viewport retrieval, and URL generation for both forward and reverse geocoding operations.

Test Coverage Overview

The test suite provides comprehensive coverage of the Here geocoding service integration, focusing on key API interactions and data handling. It validates:

  • API key configuration and validation
  • Viewport data retrieval and parsing
  • URL generation for forward and reverse geocoding
  • Country code parameter handling

Implementation Analysis

The testing approach utilizes Ruby’s standard test framework with custom assertions specific to geocoding operations. The implementation follows a structured pattern of setup-execute-verify, with each test method focusing on a specific aspect of the Here geocoding service.

The test suite employs mock HTTP requests and response handling to validate URL formation and parameter encoding.

Technical Details

Key technical components include:

  • GeocoderTestCase as the base test class
  • Configuration management through Geocoder.configure
  • URL pattern matching using regular expressions
  • Custom viewport validation methods
  • API key validation checks

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear, single-responsibility assertions
  • Proper setup and configuration management
  • Comprehensive error case handling
  • Consistent naming conventions
  • Effective use of helper methods and shared setup

alexreisner/geocoder

test/unit/lookups/here_test.rb

            
# encoding: utf-8
require 'test_helper'

class HereTest < GeocoderTestCase

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

  def test_with_array_api_key_raises_when_configured
    Geocoder.configure(api_key: %w[foo bar])
    Geocoder.configure(always_raise: :all)
    assert_raises(Geocoder::ConfigurationError) { Geocoder.search('berlin').first }
  end

  def test_here_viewport
    result = Geocoder.search('berlin').first
    assert_equal [52.33812, 13.08835, 52.6755, 13.761], result.viewport
  end

  def test_here_no_viewport
    result = Geocoder.search("Madison Square Garden, New York, NY").first
    assert_equal [], result.viewport
  end

  def test_here_query_url_for_reverse_geocoding
    lookup = Geocoder::Lookup::Here.new
    url = lookup.query_url(
      Geocoder::Query.new(
        "42.42,21.21"
      )
    )

    expected = /revgeocode\.search\.hereapi\.com\/v1\/revgeocode.+at=42\.42%2C21\.21/

    assert_match(expected, url)
  end

  def test_here_query_url_for_geocode
    lookup = Geocoder::Lookup::Here.new
    url = lookup.query_url(
      Geocoder::Query.new(
        "Madison Square Garden, New York, NY"
      )
    )

    expected = /geocode\.search\.hereapi\.com\/v1\/geocode.+q=Madison\+Square\+Garden%2C\+New\+York%2C\+NY/

    assert_match(expected, url)
  end

  def test_here_query_url_contains_country
    lookup = Geocoder::Lookup::Here.new
    url = lookup.query_url(
      Geocoder::Query.new(
        'Some Intersection',
        country: 'GBR'
      )
    )
    assert_match(/in=countryCode%3AGBR/, url)
  end

  def test_here_query_url_contains_api_key
    lookup = Geocoder::Lookup::Here.new
    url = lookup.query_url(
      Geocoder::Query.new(
        'Some Intersection'
      )
    )
    assert_match(/apiKey=+/, url)
  end
end