Back to Repositories

Testing Partial Text Matching Patterns in Searchkick

This test suite validates partial text matching functionality in Searchkick, focusing on various text search patterns including autocomplete, word positioning, and exact matches. The tests ensure robust text search capabilities across different matching scenarios and field configurations.

Test Coverage Overview

The test suite comprehensively covers multiple text matching patterns including:

  • Autocomplete functionality for single and multi-word queries
  • Text positioning tests (start, middle, end)
  • Word-level matching (start, middle, end)
  • Exact matching with case sensitivity
  • Field-specific search configurations

Implementation Analysis

The testing approach utilizes Minitest framework with a systematic pattern of storing sample data and asserting search results. Each test case follows a consistent structure using store_names for test data setup and assert_search for validation, with various matching configurations specified through fields parameter.

Technical Details

Testing tools and configuration:

  • Minitest as the testing framework
  • Custom assert_search helper method
  • store_names utility for test data setup
  • Field-specific matching configurations
  • Case sensitivity handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for specific functionality
  • Clear test naming conventions
  • Comprehensive edge case coverage
  • Consistent test structure and organization
  • Granular testing of individual matching patterns

ankane/searchkick

test/partial_match_test.rb

            
require_relative "test_helper"

class PartialMatchTest < Minitest::Test
  def test_autocomplete
    store_names ["Hummus"]
    assert_search "hum", ["Hummus"], match: :text_start
  end

  def test_autocomplete_two_words
    store_names ["Organic Hummus"]
    assert_search "hum", [], match: :text_start
  end

  def test_autocomplete_fields
    store_names ["Hummus"]
    assert_search "hum", ["Hummus"], match: :text_start, fields: [:name]
  end

  def test_text_start
    store_names ["Where in the World is Carmen San Diego"]
    assert_search "where in the world is", ["Where in the World is Carmen San Diego"], fields: [{name: :text_start}]
    assert_search "in the world", [], fields: [{name: :text_start}]
  end

  def test_text_middle
    store_names ["Where in the World is Carmen San Diego"]
    assert_search "where in the world is", ["Where in the World is Carmen San Diego"], fields: [{name: :text_middle}]
    assert_search "n the wor", ["Where in the World is Carmen San Diego"], fields: [{name: :text_middle}]
    assert_search "men san diego", ["Where in the World is Carmen San Diego"], fields: [{name: :text_middle}]
    assert_search "world carmen", [], fields: [{name: :text_middle}]
  end

  def test_text_end
    store_names ["Where in the World is Carmen San Diego"]
    assert_search "men san diego", ["Where in the World is Carmen San Diego"], fields: [{name: :text_end}]
    assert_search "carmen san", [], fields: [{name: :text_end}]
  end

  def test_word_start
    store_names ["Where in the World is Carmen San Diego"]
    assert_search "car san wor", ["Where in the World is Carmen San Diego"], fields: [{name: :word_start}]
  end

  def test_word_middle
    store_names ["Where in the World is Carmen San Diego"]
    assert_search "orl", ["Where in the World is Carmen San Diego"], fields: [{name: :word_middle}]
  end

  def test_word_end
    store_names ["Where in the World is Carmen San Diego"]
    assert_search "rld men ego", ["Where in the World is Carmen San Diego"], fields: [{name: :word_end}]
  end

  def test_word_start_multiple_words
    store_names ["Dark Grey", "Dark Blue"]
    assert_search "dark grey", ["Dark Grey"], fields: [{name: :word_start}]
  end

  def test_word_start_exact
    store_names ["Back Scratcher", "Backpack"]
    assert_order "back", ["Back Scratcher", "Backpack"], fields: [{name: :word_start}]
  end

  def test_word_start_exact_martin
    store_names ["Martina", "Martin"]
    assert_order "martin", ["Martin", "Martina"], fields: [{name: :word_start}]
  end

  # TODO find a better place

  def test_exact
    store_names ["[email protected]"]
    assert_search "[email protected]", ["[email protected]"], fields: [{name: :exact}]
  end

  def test_exact_case
    store_names ["Hello"]
    assert_search "hello", [], fields: [{name: :exact}]
    assert_search "Hello", ["Hello"], fields: [{name: :exact}]
  end
end