Back to Repositories

Testing OSMNames Geocoding Implementation in alexreisner/geocoder

This test suite validates the OSMNames geocoding implementation in the Geocoder gem, covering API integration, coordinate lookup, and address parsing functionality. It ensures reliable geocoding operations for both forward and reverse lookups with proper error handling.

Test Coverage Overview

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

  • API key validation and URL construction
  • Forward geocoding with address components
  • Reverse geocoding capabilities
  • Viewport handling and coordinate accuracy
  • Error cases and exception handling

Implementation Analysis

The testing approach utilizes Ruby’s test framework with the GeocoderTestCase base class. It implements systematic validation of the OSMNames lookup service through isolated test methods that verify discrete functionality. The suite employs setup hooks for configuration and uses assertion methods to validate expected outcomes.

Technical Details

  • Testing Framework: Ruby Test::Unit
  • Configuration: Geocoder.configure for API settings
  • Mock Responses: GeocoderTestCase infrastructure
  • Assertion Methods: assert_equal, assert_includes, assert_raises
  • Test Environment: Isolated test cases with setup/teardown

Best Practices Demonstrated

The test suite exemplifies solid testing practices with clear separation of concerns and thorough validation of core functionality. Each test method focuses on a specific feature aspect, from basic API integration to complex geocoding operations. The code maintains high readability with descriptive test names and logical organization of test cases.

alexreisner/geocoder

test/unit/lookups/osmnames_test.rb

            
# encoding: utf-8
require 'test_helper'

class OsmnamesTest < GeocoderTestCase

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

  def test_url_contains_api_key
    Geocoder.configure(osmnames: {api_key: 'abc123'})
    query = Geocoder::Query.new('test')
    assert_includes query.url, 'key=abc123'
  end

  def test_url_contains_query_base
    query = Geocoder::Query.new("Madison Square Garden, New York, NY")
    assert_includes query.url, 'geocoder.tilehosting.com/q/Madison'
  end

  def test_url_contains_country_code
    query = Geocoder::Query.new("test", country_code: 'US')
    assert_includes query.url, 'https://geocoder.tilehosting.com/us/q/'
  end

  def test_result_components
    result = Geocoder.search('Madison Square Garden, New York, NY').first
    assert_equal [40.693073, -73.878418], result.coordinates
    assert_equal 'New York City, New York, United States of America', result.address
    assert_equal 'New York', result.state
    assert_equal 'New York City', result.city
    assert_equal 'us', result.country_code
  end

  def test_result_for_reverse_geocode
    result = Geocoder.search('-73.878418, 40.693073').first
    assert_equal 'New York City, New York, United States of America', result.address
    assert_equal 'New York', result.state
    assert_equal 'New York City', result.city
    assert_equal 'us', result.country_code
  end

  def test_url_for_reverse_geocode
    query = Geocoder::Query.new("-73.878418, 40.693073")
    assert_includes query.url, 'https://geocoder.tilehosting.com/r/-73.878418/40.693073.js'
  end

  def test_result_viewport
    result = Geocoder.search("Madison Square Garden, New York, NY").first
    assert_equal [40.477398, -74.259087, 40.91618, -73.70018], result.viewport
  end

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

  def test_raises_exception_when_return_message_error
    Geocoder.configure(always_raise: [Geocoder::InvalidRequest])
    assert_raises Geocoder::InvalidRequest.new("Invalid attribute value.") do
      Geocoder.search("invalid request")
    end
  end
end