Back to Repositories

Testing Multi-Tenant Search Isolation in Searchkick

This test suite validates multi-tenancy functionality in Searchkick using the Apartment gem for database isolation. It ensures proper data separation and search functionality across different tenants in a multi-tenant application environment.

Test Coverage Overview

The test suite covers essential multi-tenancy scenarios in Searchkick integration.

Key areas tested include:
  • Tenant isolation and data separation
  • Search functionality within specific tenant contexts
  • Product name indexing across different tenants
  • Proper tenant switching and state management

Implementation Analysis

The testing approach utilizes Minitest framework with Apartment gem integration for tenant management.

Testing patterns include:
  • Setup and teardown hooks for tenant management
  • Explicit tenant switching using Apartment::Tenant.switch!
  • Assertion-based validation of search results
  • Conditional test execution based on Apartment availability

Technical Details

Testing components and configuration:
  • Minitest as the testing framework
  • Apartment gem for multi-tenancy support
  • Searchkick integration for search functionality
  • Custom store_names helper method
  • assert_search for validation
  • Load: false option for performance optimization

Best Practices Demonstrated

The test suite demonstrates several testing best practices for multi-tenant applications.

Notable practices include:
  • Proper test isolation through setup/teardown
  • Graceful handling of optional dependencies
  • Clear test case organization
  • Explicit state management
  • Clean tenant reset after tests

ankane/searchkick

test/multi_tenancy_test.rb

            
require_relative "test_helper"

class MultiTenancyTest < Minitest::Test
  def setup
    skip unless defined?(Apartment)
  end

  def test_basic
    Apartment::Tenant.switch!("tenant1")
    store_names ["Product A"]
    Apartment::Tenant.switch!("tenant2")
    store_names ["Product B"]
    Apartment::Tenant.switch!("tenant1")
    assert_search "product", ["Product A"], {load: false}
    Apartment::Tenant.switch!("tenant2")
    assert_search "product", ["Product B"], {load: false}
  end

  def teardown
    Apartment::Tenant.reset if defined?(Apartment)
  end

  def default_model
    Tenant
  end
end