Back to Repositories

Testing Mongoid Geocoding Implementation in alexreisner/geocoder

This test suite validates Mongoid integration with the Geocoder gem, covering location-based functionality and geocoding operations in MongoDB documents. It ensures proper handling of coordinates, distance calculations, and both forward and reverse geocoding implementations.

Test Coverage Overview

The test suite provides comprehensive coverage of Mongoid-specific geocoding operations.

Key areas tested include:
  • Coordinate validation and geocoding status checks
  • Distance calculation accuracy and unit conversions
  • Model configuration options for geocoding
  • Index handling for geospatial queries
  • Custom result handling for both forward and reverse geocoding

Implementation Analysis

The testing approach focuses on validating Mongoid document behavior with geocoding features. It employs a systematic pattern of testing both standard and edge cases, including nil coordinate handling and custom result processing.

Technical implementation details:
  • Coordinate pair validation logic
  • Distance calculation in multiple units (km/mi)
  • Mongoid-specific index configuration
  • Custom geocoding result handlers

Technical Details

Testing infrastructure includes:
  • Ruby test framework with Mongoid integration
  • GeocoderTestCase as the base test class
  • Multiple model configurations for different geocoding scenarios
  • Mock objects for geocoded and reverse geocoded parameters
  • Assertion methods for coordinate and address validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices for geospatial functionality.

Notable practices include:
  • Isolation of different geocoding configurations
  • Comprehensive edge case coverage
  • Verification of type safety for numerical returns
  • Separation of concerns between different geocoding operations
  • Clear test method naming conventions

alexreisner/geocoder

test/unit/mongoid_test.rb

            
# encoding: utf-8
require 'mongoid_test_helper'

class MongoidTest < GeocoderTestCase
  def test_geocoded_check
    p = PlaceUsingMongoid.new(*geocoded_object_params(:msg))
    p.location = [40.750354, -73.993371]
    assert p.geocoded?
  end

  def test_geocoded_check_single_coord
    p = PlaceUsingMongoid.new(*geocoded_object_params(:msg))
    p.location = [40.750354, nil]
    assert !p.geocoded?
  end

  def test_distance_to_returns_float
    p = PlaceUsingMongoid.new(*geocoded_object_params(:msg))
    p.location = [40.750354, -73.993371]
    assert p.distance_to([30, -94]).is_a?(Float)
  end

  def test_model_configuration
    p = PlaceUsingMongoid.new(*geocoded_object_params(:msg))
    p.location = [0, 0]

    PlaceUsingMongoid.geocoded_by :address, :coordinates => :location, :units => :km
    assert_equal 111, p.distance_to([0,1]).round

    PlaceUsingMongoid.geocoded_by :address, :coordinates => :location, :units => :mi
    assert_equal 69, p.distance_to([0,1]).round
  end

  def test_index_is_skipped_if_skip_option_flag
    if PlaceUsingMongoidWithoutIndex.respond_to?(:index_options)
      result = PlaceUsingMongoidWithoutIndex.index_options.keys.flatten[0] == :coordinates
    else
      result = PlaceUsingMongoidWithoutIndex.index_specifications[0] == :coordinates
    end
    assert !result
  end

  def test_geocoded_with_custom_handling
    p = PlaceUsingMongoidWithCustomResultsHandling.new(*geocoded_object_params(:msg))
    p.location = [40.750354, -73.993371]
    p.geocode
    assert_match(/[0-9\.,\-]+/, p.coords_string)
  end

  def test_reverse_geocoded
    p = PlaceUsingMongoidReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
    p.reverse_geocode
    assert_match(/New York/, p.address)
  end

  def test_reverse_geocoded_with_custom_handling
    p = PlaceUsingMongoidReverseGeocodedWithCustomResultsHandling.new(*reverse_geocoded_object_params(:msg))
    p.reverse_geocode
    assert_equal "US", p.country.upcase
  end
end