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) ⇒ SearchIndexReceiver

Assert that an index changes during a block.

Parameters:

  • index (Chewy::Type)

    the index / type to watch, eg EntitiesIndex::Entity.

  • 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
41
42
# File 'lib/chewy/minitest/helpers.rb', line 17

def assert_indexes(index, strategy: :atomic, bypass_actual_index: true)
  type = Chewy.derive_type index
  receiver = SearchIndexReceiver.new

  bulk_method = type.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

  type.define_singleton_method :bulk, bulk_mock

  Chewy.strategy(strategy) do
    yield
  end

  type.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) ⇒ 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.



47
48
49
50
51
# File 'lib/chewy/minitest/helpers.rb', line 47

def run_indexing(strategy: :atomic)
  Chewy.strategy strategy do
    yield
  end
end