Back to Repositories

Testing Search Term Exclusion Patterns in Searchkick

This test suite validates the exclude functionality in Searchkick, focusing on search term exclusion patterns and matching behaviors. The tests verify different search scenarios with excluded terms across exact matches, word starts, and field-specific exclusions.

Test Coverage Overview

The test suite provides comprehensive coverage of Searchkick’s exclude functionality across various search patterns. Key test cases include:

  • Basic term exclusion with ‘butter’ searches
  • Word-start matching with exclusions
  • Exact field matching scenarios
  • Match-all queries with exclusions
  • Field-specific exclusion behavior

Implementation Analysis

The testing approach uses Minitest framework with a systematic pattern of storing test data and asserting search results. Each test case follows a consistent structure of setting up test data with store_names, then performing searches with various exclude parameters and matching options.

The implementation demonstrates different exclude configurations including string-based exclusions, array-based exclusions, and field-specific exclusions.

Technical Details

Testing tools and setup:

  • Minitest as the testing framework
  • Custom assert_search helper method
  • store_names for test data setup
  • Field-specific configuration using :exact and :word_start matching
  • Integration with Searchkick’s search API

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolated test cases, clear naming conventions, and comprehensive edge case coverage. Each test focuses on a specific aspect of the exclude functionality, with clear setup and assertions that validate both positive and negative cases.

The code organization follows a logical progression from simple to complex scenarios, ensuring thorough coverage of the exclusion feature.

ankane/searchkick

test/exclude_test.rb

            
require_relative "test_helper"

class ExcludeTest < Minitest::Test
  def test_butter
    store_names ["Butter Tub", "Peanut Butter Tub"]
    assert_search "butter", ["Butter Tub"], exclude: ["peanut butter"]
  end

  def test_butter_word_start
    store_names ["Butter Tub", "Peanut Butter Tub"]
    assert_search "butter", ["Butter Tub"], exclude: ["peanut butter"], match: :word_start
  end

  def test_butter_exact
    store_names ["Butter Tub", "Peanut Butter Tub"]
    assert_search "butter", [], exclude: ["peanut butter"], fields: [{name: :exact}]
  end

  def test_same_exact
    store_names ["Butter Tub", "Peanut Butter Tub"]
    assert_search "Butter Tub", ["Butter Tub"], exclude: ["Peanut Butter Tub"], fields: [{name: :exact}]
  end

  def test_egg_word_start
    store_names ["eggs", "eggplant"]
    assert_search "egg", ["eggs"], exclude: ["eggplant"], match: :word_start
  end

  def test_string
    store_names ["Butter Tub", "Peanut Butter Tub"]
    assert_search "butter", ["Butter Tub"], exclude: "peanut butter"
  end

  def test_match_all
    store_names ["Butter"]
    assert_search "*", [], exclude: "butter"
  end

  def test_match_all_fields
    store_names ["Butter"]
    assert_search "*", [], fields: [:name], exclude: "butter"
    assert_search "*", ["Butter"], fields: [:color], exclude: "butter"
  end
end