class SearchIndexReceiver
Test helper class to provide minitest hooks for Chewy::Index
testing.
@note Intended to be used in conjunction with a test helper which mocks over the bulk
method on a {Chewy::Index} class. (See {Chewy::Minitest::Helpers})
The class will capture the data from the *param on the Chewy::Index.bulk method and aggregate the data for test analysis.
Constants
- MUTATION_FOR_CLASS
Public Class Methods
# File lib/chewy/minitest/search_index_receiver.rb, line 11 def initialize @mutations = {} end
Public Instance Methods
@param bulk_params [Hash] the bulk_params that should be sent to the Chewy::Index.bulk method. @param index [Chewy::Index] the index executing this query.
# File lib/chewy/minitest/search_index_receiver.rb, line 17 def catch(bulk_params, index) Array.wrap(bulk_params).map { |y| y[:body] }.flatten.each do |update| if update[:delete] mutation_for(index).deletes << update[:delete][:_id] elsif update[:index] mutation_for(index).indexes << update[:index] end end end
Check to see if a given object has been deleted. @param obj [#id] obj the object to look for. @param index [Chewy::Index] what index the object should have been deleted from. @return [true, false] if the object was deleted.
# File lib/chewy/minitest/search_index_receiver.rb, line 61 def deleted?(obj, index) deletes_for(index).include? obj.id end
@param index [Chewy::Index] return only delete requests to the specified {Chewy::Index} index. @return [Hash] the index deletes captured by the mock.
# File lib/chewy/minitest/search_index_receiver.rb, line 40 def deletes_for(index = nil) if index mutation_for(index).deletes else @mutations.transform_values(&:deletes) end end
Check to see if a given object has been indexed. @param obj [#id] obj the object to look for. @param index [Chewy::Index] what index the object should be indexed in. @return [true, false] if the object was indexed.
# File lib/chewy/minitest/search_index_receiver.rb, line 53 def indexed?(obj, index) indexes_for(index).map { |i| i[:_id] }.include? obj.id end
@param index [Chewy::Index] return only index requests to the specified {Chewy::Index} index. @return [Hash] the index changes captured by the mock.
# File lib/chewy/minitest/search_index_receiver.rb, line 29 def indexes_for(index = nil) if index mutation_for(index).indexes else @mutations.transform_values(&:indexes) end end
@return [Array<Chewy::Index>] a list of indexes changed.
# File lib/chewy/minitest/search_index_receiver.rb, line 66 def updated_indexes @mutations.keys end
Private Instance Methods
Get the mutation object for a given index. @param index [Chewy::Index] the index to fetch. @return [#indexes, deletes
] an object with a list of indexes and a list of deletes.
# File lib/chewy/minitest/search_index_receiver.rb, line 75 def mutation_for(index) @mutations[index] ||= MUTATION_FOR_CLASS.new(indexes: [], deletes: []) end