Back to Repositories

Testing Searchkick Callback System Implementation in ankane/searchkick

This test suite validates the callback functionality in Searchkick, focusing on different callback modes and their impact on search indexing behavior. It ensures proper handling of bulk operations, queued indexing, and callback enabling/disabling mechanisms.

Test Coverage Overview

The test suite provides comprehensive coverage of Searchkick’s callback system, examining multiple callback modes including false, bulk, and queue. It verifies core functionality for index updates, bulk operations, and queued processing.

  • Tests callback disabling with false mode
  • Validates bulk indexing operations
  • Verifies queued indexing with job processing
  • Confirms callback enable/disable state management

Implementation Analysis

The testing approach utilizes Minitest framework with focused test methods for each callback scenario. It employs a systematic pattern of setting up test data, executing operations under specific callback modes, and verifying search results.

Key implementation patterns include:
  • Callback context management using blocks
  • Queue processing verification
  • Index refresh coordination
  • State verification through search assertions

Technical Details

Testing infrastructure includes:

  • Minitest as the testing framework
  • Searchkick integration for search operations
  • Job processing simulation
  • Index management utilities
  • Custom assertions for search validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices for search functionality verification. Each test focuses on a specific callback behavior with clear setup and assertions.

  • Isolated test scenarios
  • Proper cleanup between tests
  • Comprehensive state verification
  • Edge case handling
  • Clear test method naming

ankane/searchkick

test/callbacks_test.rb

            
require_relative "test_helper"

class CallbacksTest < Minitest::Test
  def test_false
    Searchkick.callbacks(false) do
      store_names ["Product A", "Product B"]
    end
    assert_search "product", []
  end

  def test_bulk
    Searchkick.callbacks(:bulk) do
      store_names ["Product A", "Product B"]
    end
    Product.searchkick_index.refresh
    assert_search "product", ["Product A", "Product B"]
  end

  def test_queue
    # TODO figure out which earlier test leaves records in index
    Product.reindex

    reindex_queue = Product.searchkick_index.reindex_queue
    reindex_queue.clear

    Searchkick.callbacks(:queue) do
      store_names ["Product A", "Product B"]
    end
    Product.searchkick_index.refresh
    assert_search "product", [], load: false, conversions: false
    assert_equal 2, reindex_queue.length

    perform_enqueued_jobs do
      Searchkick::ProcessQueueJob.perform_now(class_name: "Product")
    end
    Product.searchkick_index.refresh
    assert_search "product", ["Product A", "Product B"], load: false
    assert_equal 0, reindex_queue.length

    Searchkick.callbacks(:queue) do
      Product.where(name: "Product B").destroy_all
      Product.create!(name: "Product C")
    end
    Product.searchkick_index.refresh
    assert_search "product", ["Product A", "Product B"], load: false
    assert_equal 2, reindex_queue.length

    perform_enqueued_jobs do
      Searchkick::ProcessQueueJob.perform_now(class_name: "Product")
    end
    Product.searchkick_index.refresh
    assert_search "product", ["Product A", "Product C"], load: false
    assert_equal 0, reindex_queue.length

    # ensure no error with empty queue
    Searchkick::ProcessQueueJob.perform_now(class_name: "Product")
  end

  def test_disable_callbacks
    # make sure callbacks default to on
    assert Searchkick.callbacks?

    store_names ["product a"]

    Searchkick.disable_callbacks
    assert !Searchkick.callbacks?

    store_names ["product b"]
    assert_search "product", ["product a"]

    Searchkick.enable_callbacks
    Product.reindex

    assert_search "product", ["product a", "product b"]
  end
end