Back to Repositories

Testing Multi-Search Operations and Error Handling in Searchkick

A comprehensive test suite for Searchkick’s multi-search functionality, validating concurrent search operations across different models. The tests verify basic search operations, error handling, and misspelling tolerance features when performing multiple searches simultaneously.

Test Coverage Overview

The test suite provides thorough coverage of Searchkick’s multi-search capabilities.

Key areas tested include:
  • Basic multi-model search operations
  • Method consistency between search results
  • Error handling scenarios
  • Misspelling tolerance configuration
  • Query validation and error reporting

Implementation Analysis

The testing approach utilizes Minitest framework with a focus on isolated test cases for different multi-search scenarios. The implementation demonstrates systematic validation of search functionality across Product and Store models, with particular attention to error states and edge cases.

Technical patterns include:
  • Model-specific search operations
  • Error state verification
  • Misspelling threshold testing
  • Query result validation

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • Searchkick gem integration
  • Multiple model configurations (Product and Store)
  • Custom test helper methods for data storage
  • Error handling mechanisms for invalid queries

Best Practices Demonstrated

The test suite exemplifies strong testing practices through comprehensive coverage of both success and failure scenarios. Notable practices include:
  • Isolated test cases for specific functionality
  • Explicit error condition testing
  • Validation of error messages and states
  • Consistent test naming conventions
  • Clear separation of concerns between test cases

ankane/searchkick

test/multi_search_test.rb

            
require_relative "test_helper"

class MultiSearchTest < Minitest::Test
  def test_basic
    store_names ["Product A"]
    store_names ["Store A"], Store
    products = Product.search("*")
    stores = Store.search("*")
    Searchkick.multi_search([products, stores])
    assert_equal ["Product A"], products.map(&:name)
    assert_equal ["Store A"], stores.map(&:name)
  end

  def test_methods
    result = Product.search("*")
    query = Product.search("*")
    assert_empty(result.methods - query.methods)
  end

  def test_error
    store_names ["Product A"]
    products = Product.search("*")
    stores = Store.search("*", order: [:bad_field])
    Searchkick.multi_search([products, stores])
    assert !products.error
    assert stores.error
  end

  def test_misspellings_below_unmet
    store_names ["abc", "abd", "aee"]
    products = Product.search("abc", misspellings: {below: 5})
    Searchkick.multi_search([products])
    assert_equal ["abc", "abd"], products.map(&:name)
  end

  def test_misspellings_below_error
    products = Product.search("abc", order: [:bad_field], misspellings: {below: 1})
    Searchkick.multi_search([products])
    assert products.error
  end

  def test_query_error
    products = Product.search("*", order: {bad_field: :asc})
    Searchkick.multi_search([products])
    assert products.error
    error = assert_raises(Searchkick::Error) { products.results }
    assert_equal error.message, "Query error - use the error method to view it"
  end
end