Back to Repositories

Testing ActiveRecord Custom Primary Key Implementation in geocoder

This test suite validates Geocoder’s integration with ActiveRecord models, specifically focusing on custom primary key handling. It ensures proper geocoding functionality when working with non-standard database configurations.

Test Coverage Overview

The test suite covers ActiveRecord model integration with Geocoder, focusing on edge cases involving custom primary keys.

Key areas tested include:
  • Custom primary key handling in exclusion conditions
  • Model-specific geocoding behavior
  • Database query construction with non-standard keys

Implementation Analysis

The testing approach uses Ruby’s unit test framework to verify Geocoder’s ActiveRecord integration. It employs direct method calls to test private functionality, specifically focusing on the add_exclude_condition method behavior with custom primary keys.

The implementation uses:
  • Direct private method testing
  • Custom model fixtures
  • Condition string validation

Technical Details

Testing tools and configuration include:
  • Ruby unit test framework
  • Custom GeocoderTestCase class
  • PlaceWithCustomPrimaryKey test model
  • Geocoded object parameter fixtures

Best Practices Demonstrated

The test demonstrates several testing best practices:

  • Isolated testing of specific functionality
  • Clear test method naming
  • Explicit assertion messages
  • Proper setup of test dependencies
  • Focused test scope for maintainability

alexreisner/geocoder

test/unit/active_record_test.rb

            
# encoding: utf-8
require 'test_helper'

class ActiveRecordTest < GeocoderTestCase

  def test_exclude_condition_when_model_has_a_custom_primary_key
    venue = PlaceWithCustomPrimaryKey.new(*geocoded_object_params(:msg))

    # just call private method directly so we don't have to stub .near scope
    conditions = venue.class.send(:add_exclude_condition, ["fake_condition"], venue)

    assert_match( /#{PlaceWithCustomPrimaryKey.primary_key}/, conditions.join)
  end

end