Back to Repositories

Testing Multi-Index Search Operations in searchkick

This test suite validates multi-index search functionality in Searchkick, focusing on searching across multiple models and handling various index configurations. The tests ensure proper behavior when querying multiple indices simultaneously and verify error handling for invalid model combinations.

Test Coverage Overview

The test suite provides comprehensive coverage of multi-index search capabilities in Searchkick.

Key areas tested include:
  • Basic multi-index search functionality
  • Index name specification and validation
  • Model combinations and restrictions
  • Error handling for invalid configurations
  • Search across multiple models with different parameters

Implementation Analysis

The testing approach uses Minitest framework with a structured setup for multiple model types (Product and Speaker).

Key implementation patterns include:
  • Custom assertion helpers for multi-index searches
  • Consistent test data setup using store_names helper
  • Explicit index refresh calls for test consistency
  • Systematic error case validation

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • Custom test helper methods for search validation
  • Setup methods for consistent test state
  • Index management through Searchkick API
  • Multiple model configurations (Product and Speaker classes)

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear purpose
  • Comprehensive error scenario coverage
  • Helper methods for repeated operations
  • Consistent test data management
  • Clear separation of test scenarios
  • Proper setup and teardown handling

ankane/searchkick

test/multi_indices_test.rb

            
require_relative "test_helper"

class MultiIndicesTest < Minitest::Test
  def setup
    super
    setup_speaker
  end

  def test_basic
    store_names ["Product A"]
    store_names ["Product B"], Speaker
    assert_search_multi "product", ["Product A", "Product B"]
  end

  def test_index_name
    store_names ["Product A"]
    assert_equal ["Product A"], Product.search("product", index_name: Product.searchkick_index.name).map(&:name)
    assert_equal ["Product A"], Product.search("product", index_name: Product).map(&:name)

    Speaker.searchkick_index.refresh
    assert_equal [], Product.search("product", index_name: Speaker.searchkick_index.name, conversions: false).map(&:name)
  end

  def test_models_and_index_name
    store_names ["Product A"]
    store_names ["Product B"], Speaker
    assert_equal ["Product A"], Searchkick.search("product", models: [Product, Store], index_name: Product.searchkick_index.name).map(&:name)
    error = assert_raises(Searchkick::Error) do
      Searchkick.search("product", models: [Product, Store], index_name: Speaker.searchkick_index.name).map(&:name)
    end
    assert_includes error.message, "Unknown model"
    # legacy
    assert_equal ["Product A"], Searchkick.search("product", index_name: [Product, Store]).map(&:name)
  end

  def test_model_with_another_model
    error = assert_raises(ArgumentError) do
      Product.search(models: [Store])
    end
    assert_includes error.message, "Use Searchkick.search"
  end

  def test_model_with_another_model_in_index_name
    error = assert_raises(ArgumentError) do
      # legacy protection
      Product.search(index_name: [Store, "another"])
    end
    assert_includes error.message, "Use Searchkick.search"
  end

  def test_no_models_or_index_name
    store_names ["Product A"]

    error = assert_raises(Searchkick::Error) do
      Searchkick.search("product").results
    end
    assert_includes error.message, "Unknown model"
  end

  def test_no_models_or_index_name_load_false
    store_names ["Product A"]
    Searchkick.search("product", load: false).results
  end

  private

  def assert_search_multi(term, expected, options = {})
    options[:models] = [Product, Speaker]
    options[:fields] = [:name]
    assert_search(term, expected, options, Searchkick)
  end
end