Back to Repositories

Testing Searchkick Index Configuration Options in ankane/searchkick

This test suite validates index options functionality in the Searchkick Ruby gem, focusing on text search configurations like case sensitivity, stemming, and special character handling. The tests ensure proper behavior of various indexing options and naming conventions.

Test Coverage Overview

The test suite provides comprehensive coverage of Searchkick’s index configuration options.

Key areas tested include:
  • Case sensitivity in search operations
  • Stemming behavior and exclusions
  • Special character handling
  • Index naming and prefix configurations
Edge cases cover stemming overrides and dynamic index name generation using callable objects.

Implementation Analysis

The testing approach uses Minitest framework with a setup method clearing test data before each test. The implementation follows a pattern of option-based testing using with_options helper to modify index configurations.

Tests utilize assert_search and assert_equal methods to verify search results and token generation, demonstrating both functional and unit testing aspects.

Technical Details

Testing tools and components:
  • Minitest as the testing framework
  • Custom assertions for search validation
  • Song model as the test subject
  • Searchkick index analyzers
  • Dynamic index naming capabilities

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test cases and clear test naming conventions.

Notable practices include:
  • Consistent test data cleanup
  • Focused test cases for specific features
  • Clear separation of concerns
  • Comprehensive edge case coverage

ankane/searchkick

test/index_options_test.rb

            
require_relative "test_helper"

class IndexOptionsTest < Minitest::Test
  def setup
    Song.destroy_all
  end

  def test_case_sensitive
    with_options({case_sensitive: true}) do
      store_names ["Test", "test"]
      assert_search "test", ["test"], {misspellings: false}
    end
  end

  def test_no_stemming
    with_options({stem: false}) do
      store_names ["milk", "milks"]
      assert_search "milks", ["milks"], {misspellings: false}
    end
  end

  def test_no_stem_exclusion
    with_options({}) do
      store_names ["animals", "anime"]
      assert_search "animals", ["animals", "anime"], {misspellings: false}
      assert_search "anime", ["animals", "anime"], {misspellings: false}
      assert_equal ["anim"], Song.searchkick_index.tokens("anime", analyzer: "searchkick_index")
      assert_equal ["anim"], Song.searchkick_index.tokens("anime", analyzer: "searchkick_search2")
    end
  end

  def test_stem_exclusion
    with_options({stem_exclusion: ["anime"]}) do
      store_names ["animals", "anime"]
      assert_search "animals", ["animals"], {misspellings: false}
      assert_search "anime", ["anime"], {misspellings: false}
      assert_equal ["anime"], Song.searchkick_index.tokens("anime", analyzer: "searchkick_index")
      assert_equal ["anime"], Song.searchkick_index.tokens("anime", analyzer: "searchkick_search2")
    end
  end

  def test_no_stemmer_override
    with_options({}) do
      store_names ["animals", "animations"]
      assert_search "animals", ["animals", "animations"], {misspellings: false}
      assert_search "animations", ["animals", "animations"], {misspellings: false}
      assert_equal ["anim"], Song.searchkick_index.tokens("animations", analyzer: "searchkick_index")
      assert_equal ["anim"], Song.searchkick_index.tokens("animations", analyzer: "searchkick_search2")
    end
  end

  def test_stemmer_override
    with_options({stemmer_override: ["animations => animat"]}) do
      store_names ["animals", "animations"]
      assert_search "animals", ["animals"], {misspellings: false}
      assert_search "animations", ["animations"], {misspellings: false}
      assert_equal ["animat"], Song.searchkick_index.tokens("animations", analyzer: "searchkick_index")
      assert_equal ["animat"], Song.searchkick_index.tokens("animations", analyzer: "searchkick_search2")
    end
  end

  def test_special_characters
    with_options({special_characters: false}) do
      store_names ["jalapeño"]
      assert_search "jalapeno", [], {misspellings: false}
    end
  end

  def test_index_name
    with_options({index_name: "songs_v2"}) do
      assert_equal "songs_v2", Song.searchkick_index.name
    end
  end

  def test_index_name_callable
    with_options({index_name: -> { "songs_v2" }}) do
      assert_equal "songs_v2", Song.searchkick_index.name
    end
  end

  def test_index_prefix
    with_options({index_prefix: "hello"}) do
      assert_equal "hello_songs_test", Song.searchkick_index.name
    end
  end

  def test_index_prefix_callable
    with_options({index_prefix: -> { "hello" }}) do
      assert_equal "hello_songs_test", Song.searchkick_index.name
    end
  end

  def default_model
    Song
  end
end