Module: Chewy::Minitest::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/chewy/minitest/helpers.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#assert_indexes(index, strategy: :atomic, bypass_actual_index: true, &block) ⇒ SearchIndexReceiver

Assert that an index changes during a block.

Parameters:

  • index (Chewy::Index)

    the index to watch, eg EntitiesIndex.

  • strategy (Symbol) (defaults to: :atomic)

    the Chewy strategy to use around the block. See Chewy docs.

  • bypass_actual_index (true, false) (defaults to: true)

    True to preempt the http call to Elastic, false otherwise. Should be set to true unless actually testing search functionality.

Returns:

  • (SearchIndexReceiver)

    for optional further assertions on the nature of the index changes.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chewy/minitest/helpers.rb', line 17

def assert_indexes(index, strategy: :atomic, bypass_actual_index: true, &block)
  index_class = Chewy.derive_name index
  receiver = SearchIndexReceiver.new

  bulk_method = index_class.method :bulk
  # Manually mocking #bulk because we need to properly capture `self`
  bulk_mock = lambda do |*bulk_args|
    receiver.catch bulk_args, self

    bulk_method.call(*bulk_args) unless bypass_actual_index

    {}
  end

  index_class.define_singleton_method :bulk, bulk_mock

  Chewy.strategy(strategy, &block)

  index_class.define_singleton_method :bulk, bulk_method

  assert_includes receiver.updated_indexes, index, "Expected #{index} to be updated but it wasn't"

  receiver
end

#run_indexing(strategy: :atomic, &block) ⇒ Object

Run indexing for the database changes during the block provided. By default, indexing is run at the end of the block.

Parameters:

  • strategy (Symbol) (defaults to: :atomic)

    the Chewy index update strategy see Chewy docs.



45
46
47
# File 'lib/chewy/minitest/helpers.rb', line 45

def run_indexing(strategy: :atomic, &block)
  Chewy.strategy strategy, &block
end