Back to Repositories

Testing Elasticsearch Routing Configuration in Searchkick

This test suite validates routing functionality in Searchkick, focusing on search query routing, mapping configurations, and node-specific operations. The tests ensure proper handling of routing parameters and async operations in Elasticsearch integration.

Test Coverage Overview

The test suite provides comprehensive coverage of Searchkick’s routing capabilities, including query routing, mapping configurations, and node-specific search operations.

  • Tests routing parameter handling in search queries
  • Verifies mapping configurations for required routing
  • Validates correct and incorrect node routing scenarios
  • Tests async and queue-based routing operations

Implementation Analysis

The testing approach utilizes Minitest framework to systematically verify routing functionality. Tests employ a combination of direct assertions and helper methods to validate routing behavior.

Key patterns include:
  • Store name fixtures for search testing
  • Assertion helpers for search results verification
  • Options configuration testing for async and queue operations

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • Searchkick integration for Elasticsearch operations
  • Custom assertion helpers (assert_search)
  • Store and Song model fixtures
  • ProcessQueueJob for async operations

Best Practices Demonstrated

The test suite exhibits strong testing practices through isolated test cases and comprehensive scenario coverage.

  • Clear test method naming conventions
  • Isolated test scenarios for different routing configurations
  • Proper setup and teardown of test data
  • Coverage of both synchronous and asynchronous operations

ankane/searchkick

test/routing_test.rb

            
require_relative "test_helper"

class RoutingTest < Minitest::Test
  def test_query
    query = Store.search("Dollar Tree", routing: "Dollar Tree")
    assert_equal query.params[:routing], "Dollar Tree"
  end

  def test_mappings
    mappings = Store.searchkick_index.index_options[:mappings]
    assert_equal mappings[:_routing], required: true
  end

  def test_correct_node
    store_names ["Dollar Tree"], Store
    assert_search "*", ["Dollar Tree"], {routing: "Dollar Tree"}, Store
  end

  def test_incorrect_node
    store_names ["Dollar Tree"], Store
    assert_search "*", ["Dollar Tree"], {routing: "Boom"}, Store
  end

  def test_async
    with_options({routing: true, callbacks: :async}, Song) do
      store_names ["Dollar Tree"], Song
      Song.destroy_all
    end
  end

  def test_queue
    with_options({routing: true, callbacks: :queue}, Song) do
      store_names ["Dollar Tree"], Song
      Song.destroy_all
      Searchkick::ProcessQueueJob.perform_later(class_name: "Song")
    end
  end
end