Back to Repositories

Testing Document Indexing Behavior in Searchkick

This test suite validates the indexing behavior in Searchkick, focusing on when documents should be indexed or excluded from search operations. It covers basic indexing rules, dynamic index updates, and bulk operation scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of Searchkick’s indexing functionality, including:

  • Basic indexing rules and default behaviors
  • Dynamic index updates when documents change state
  • Bulk indexing operations with callback management
  • Edge cases for index/no-index transitions

Implementation Analysis

The testing approach uses Minitest framework with a focused set of test cases that validate the should_index? functionality. It employs a systematic pattern of creating test data, performing operations, and verifying search results. The implementation leverages Searchkick’s refresh capabilities to ensure index updates are immediately visible.

Technical Details

Key technical components include:

  • Minitest as the testing framework
  • Searchkick index management methods
  • Store and Product model integration
  • Index refresh operations for immediate testing
  • Callback control for bulk operations

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear intentions
  • Proper setup and verification patterns
  • Comprehensive coverage of state transitions
  • Explicit handling of asynchronous operations
  • Clean and maintainable test structure

ankane/searchkick

test/should_index_test.rb

            
require_relative "test_helper"

class ShouldIndexTest < Minitest::Test
  def test_basic
    store_names ["INDEX", "DO NOT INDEX"]
    assert_search "index", ["INDEX"]
  end

  def test_default_true
    assert Store.new.should_index?
  end

  def test_change_to_true
    store_names ["DO NOT INDEX"]
    assert_search "index", []
    product = Product.first
    product.name = "INDEX"
    product.save!
    Product.searchkick_index.refresh
    assert_search "index", ["INDEX"]
  end

  def test_change_to_false
    store_names ["INDEX"]
    assert_search "index", ["INDEX"]
    product = Product.first
    product.name = "DO NOT INDEX"
    product.save!
    Product.searchkick_index.refresh
    assert_search "index", []
  end

  def test_bulk
    store_names ["INDEX"]
    product = Product.first
    product.name = "DO NOT INDEX"
    Searchkick.callbacks(false) do
      product.save!
    end
    Product.where(id: product.id).reindex
    Product.searchkick_index.refresh
    assert_search "index", []
  end
end