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_elasticsearch_query(query, expected_query) ⇒ Boolean

Check the assertion that actual Elasticsearch query is rendered to the expected query

Parameters:

  • query (::Query)

    the actual Elasticsearch query.

  • expected_query (Hash)

    expected query.

Returns:

  • (Boolean)

    True - in the case when actual Elasticsearch query is rendered to the expected query. False - in the opposite case.



121
122
123
124
# File 'lib/chewy/minitest/helpers.rb', line 121

def assert_elasticsearch_query(query, expected_query)
  actual_query = query.render
  assert_equal expected_query, actual_query, "got #{actual_query.inspect} instead of expected query."
end

#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

#mock_elasticsearch_response(index, raw_response) ⇒ Object

Mock Elasticsearch response Simple usage - just pass index, expected raw response and block with the query.

Parameters:

  • index (Chewy::Index)

    the index to watch, eg EntitiesIndex.

  • raw_response (Hash)

    hash with response.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chewy/minitest/helpers.rb', line 57

def mock_elasticsearch_response(index, raw_response)
  mocked_request = Chewy::Search::Request.new(index)

  original_new = Chewy::Search::Request.method(:new)

  Chewy::Search::Request.define_singleton_method(:new) { |*_args| mocked_request }

  original_perform = mocked_request.method(:perform)
  mocked_request.define_singleton_method(:perform) { raw_response }

  yield
ensure
  mocked_request.define_singleton_method(:perform, original_perform)
  Chewy::Search::Request.define_singleton_method(:new, original_new)
end

#mock_elasticsearch_response_sources(index, hits, &block) ⇒ Object

Mock Elasticsearch response with defined sources Simple usage - just pass index, expected sources and block with the query.

Parameters:

  • index (Chewy::Index)

    the index to watch, eg EntitiesIndex.

  • hits (Hash)

    hash with sources.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chewy/minitest/helpers.rb', line 80

def mock_elasticsearch_response_sources(index, hits, &block)
  raw_response = {
    'took' => 4,
    'timed_out' => false,
    '_shards' => {
      'total' => 1,
      'successful' => 1,
      'skipped' => 0,
      'failed' => 0
    },
    'hits' => {
      'total' => {
        'value' => hits.count,
        'relation' => 'eq'
      },
      'max_score' => 1.0,
      'hits' => hits.each_with_index.map do |hit, i|
        {
          '_index' => index.index_name,
          '_type' => '_doc',
          '_id' => (i + 1).to_s,
          '_score' => 3.14,
          '_source' => hit
        }
      end
    }
  }

  mock_elasticsearch_response(index, raw_response, &block)
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.



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

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