Class: SearchIndexReceiver

Inherits:
Object show all
Defined in:
lib/chewy/minitest/search_index_receiver.rb

Overview

Note:

Intended to be used in conjunction with a test helper which mocks over the #bulk method on a Chewy::Type class. (See SearchTestHelper)

Test helper class to provide minitest hooks for Chewy::Index testing.

The class will capture the data from the *param on the Chewy::Type#bulk method and aggregate the data for test analysis.

Instance Method Summary collapse

Constructor Details

#initializeSearchIndexReceiver

Returns a new instance of SearchIndexReceiver.



9
10
11
# File 'lib/chewy/minitest/search_index_receiver.rb', line 9

def initialize
  @mutations = {}
end

Instance Method Details

#catch(bulk_params, type) ⇒ Object

Parameters:

  • bulk_params

    the bulk_params that should be sent to the Chewy::Type#bulk method.

  • type (Chewy::Type)

    the Index::Type executing this query.



15
16
17
18
19
20
21
22
23
# File 'lib/chewy/minitest/search_index_receiver.rb', line 15

def catch(bulk_params, type)
  Array.wrap(bulk_params).map { |y| y[:body] }.flatten.each do |update|
    if update[:delete]
      mutation_for(type).deletes << update[:delete][:_id]
    elsif update[:index]
      mutation_for(type).indexes << update[:index]
    end
  end
end

#deleted?(obj, type) ⇒ Boolean

Check to see if a given object has been deleted.

Parameters:

  • obj (#id)

    the object to look for.

  • Chewy::Type

    what type the object should have been deleted from.

Returns:

  • (Boolean)

    bool if the object was deleted.



63
64
65
# File 'lib/chewy/minitest/search_index_receiver.rb', line 63

def deleted?(obj, type)
  deletes_for(type).include? obj.id
end

#deletes_for(index = nil) ⇒ Object Also known as: deletes

Returns the index deletes captured by the mock.

Parameters:

  • index (defaults to: nil)

    return only delete requests to the specified Chewy::Type index.

Returns:

  • the index deletes captured by the mock.



40
41
42
43
44
45
46
47
48
# File 'lib/chewy/minitest/search_index_receiver.rb', line 40

def deletes_for(index = nil)
  if index
    mutation_for(index).deletes
  else
    Hash[
      @mutations.map { |a, b| [a, b.deletes] }
    ]
  end
end

#indexed?(obj, type) ⇒ Boolean

Check to see if a given object has been indexed.

Parameters:

  • obj (#id)

    the object to look for.

  • Chewy::Type

    what type the object should be indexed as.

Returns:

  • (Boolean)

    bool if the object was indexed.



55
56
57
# File 'lib/chewy/minitest/search_index_receiver.rb', line 55

def indexed?(obj, type)
  indexes_for(type).map { |i| i[:_id] }.include? obj.id
end

#indexes_for(index = nil) ⇒ Object Also known as: indexes

Returns the index changes captured by the mock.

Parameters:

  • index (defaults to: nil)

    return only index requests to the specified Chewy::Type index.

Returns:

  • the index changes captured by the mock.



27
28
29
30
31
32
33
34
35
# File 'lib/chewy/minitest/search_index_receiver.rb', line 27

def indexes_for(index = nil)
  if index
    mutation_for(index).indexes
  else
    Hash[
      @mutations.map { |a, b| [a, b.indexes] }
    ]
  end
end

#updated_indexesObject

Returns a list of Chewy::Type indexes changed.

Returns:

  • a list of Chewy::Type indexes changed.



68
69
70
# File 'lib/chewy/minitest/search_index_receiver.rb', line 68

def updated_indexes
  @mutations.keys
end