Back to Repositories

Testing Maxmind Geocoding Service Integration in geocoder

This test suite validates the Maxmind geocoding service integration within the Geocoder gem, focusing on IP address lookup functionality and service configuration handling.

Test Coverage Overview

The test suite comprehensively covers Maxmind’s IP geocoding capabilities across different service tiers including country, city, city_isp_org, and omni services. Key test scenarios include:

  • IP address search result validation
  • Service name verification for different Maxmind tiers
  • Special result components for extended data
  • Edge cases for loopback and private IP addresses

Implementation Analysis

The testing approach utilizes Ruby’s standard test framework with custom assertions specific to geocoding operations. The implementation follows a systematic pattern of configuring different Maxmind service levels and validating corresponding responses.

Each test focuses on isolated functionality using specific IP addresses and service configurations.

Technical Details

Testing infrastructure includes:

  • GeocoderTestCase as the base test class
  • Geocoder configuration management
  • Mock IP addresses for different scenarios
  • Service-specific result validation
  • Exception handling verification

Best Practices Demonstrated

The test suite exemplifies strong testing practices through comprehensive service coverage, proper setup/teardown management, and explicit error condition handling. Notable practices include:

  • Isolated test cases for each service tier
  • Edge case coverage for special IP addresses
  • Configuration validation
  • Consistent assertion patterns

alexreisner/geocoder

test/unit/lookups/maxmind_test.rb

            
# encoding: utf-8
require 'test_helper'

class MaxmindTest < GeocoderTestCase

  def setup
    super
    Geocoder.configure(ip_lookup: :maxmind)
  end

  def test_maxmind_result_on_ip_address_search
    Geocoder.configure(maxmind: {service: :city_isp_org})
    result = Geocoder.search("74.200.247.59").first
    assert result.is_a?(Geocoder::Result::Maxmind)
  end

  def test_maxmind_result_knows_country_service_name
    Geocoder.configure(maxmind: {service: :country})
    assert_equal :country, Geocoder.search("24.24.24.21").first.service_name
  end

  def test_maxmind_result_knows_city_service_name
    Geocoder.configure(maxmind: {service: :city})
    assert_equal :city, Geocoder.search("24.24.24.22").first.service_name
  end

  def test_maxmind_result_knows_city_isp_org_service_name
    Geocoder.configure(maxmind: {service: :city_isp_org})
    assert_equal :city_isp_org, Geocoder.search("24.24.24.23").first.service_name
  end

  def test_maxmind_result_knows_omni_service_name
    Geocoder.configure(maxmind: {service: :omni})
    assert_equal :omni, Geocoder.search("24.24.24.24").first.service_name
  end

  def test_maxmind_special_result_components
    Geocoder.configure(maxmind: {service: :omni})
    result = Geocoder.search("24.24.24.24").first
    assert_equal "Road Runner", result.isp_name
    assert_equal "Cable/DSL", result.netspeed
    assert_equal "rr.com", result.domain
  end

  def test_maxmind_raises_exception_when_service_not_configured
    Geocoder.configure(maxmind: {service: nil})
    assert_raises Geocoder::ConfigurationError do
      Geocoder::Query.new("24.24.24.24").url
    end
  end

  def test_maxmind_works_when_loopback_address_on_omni
    Geocoder.configure(maxmind: {service: :omni})
    result = Geocoder.search("127.0.0.1").first
    assert_equal "", result.country_code
  end

  def test_maxmind_works_when_loopback_address_on_country
    Geocoder.configure(maxmind: {service: :country})
    result = Geocoder.search("127.0.0.1").first
    assert_equal "", result.country_code
  end

  def test_maxmind_works_when_private_address_on_omni
    Geocoder.configure(maxmind: {service: :omni})
    result = Geocoder.search("172.19.0.1").first
    assert_equal "", result.country_code
  end

  def test_maxmind_works_when_private_address_on_country
    Geocoder.configure(maxmind: {service: :country})
    result = Geocoder.search("172.19.0.1").first
    assert_equal "", result.country_code
  end
end