-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Minitest assertions and indexing controls. #396
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8161cba
Minitest assertions and indexing controls.
b5a2b60
Merge branch 'master' into minitest_helper
1225e6e
spec for minitesthelper
63ebd54
test SearchIndexReceiver
e759df8
Update README to match correct minitest require
ca85663
Merge branch 'master' into minitest_helper
7b0b177
minitest helper compatible with activesupport<4.2
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'chewy/minitest/helpers' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require_relative 'search_index_receiver' | ||
|
||
module Chewy | ||
module Minitest | ||
module Helpers | ||
extend ActiveSupport::Concern | ||
|
||
# Assert that an index *changes* during a block. | ||
# @param (Chewy::Type) index the index / type to watch, eg EntitiesIndex::Entity. | ||
# @param (Symbol) strategy the Chewy strategy to use around the block. See Chewy docs. | ||
# @param (boolean) assert the index changes | ||
# @param (boolean) bypass_actual_index | ||
# True to preempt the http call to Elastic, false otherwise. | ||
# Should be set to true unless actually testing search functionality. | ||
# | ||
# @return (SearchIndexReceiver) for optional further assertions on the nature of the index changes. | ||
def assert_indexes index, strategy: :atomic, bypass_actual_index: true, &test_actions | ||
type = Chewy.derive_type index | ||
receiver = SearchIndexReceiver.new filter: index | ||
|
||
bulk_method = type.method :bulk | ||
# Manually mocking #bulk because we need to properly capture `self` | ||
bulk_mock = -> (*bulk_args) do | ||
receiver.catch bulk_args, self | ||
|
||
unless bypass_actual_index | ||
bulk_method.call *bulk_args | ||
end | ||
|
||
{} | ||
end | ||
|
||
type.define_singleton_method :bulk, bulk_mock | ||
|
||
Chewy.strategy(strategy) do | ||
test_actions.call | ||
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 for the database changes during the block provided. | ||
# By default, indexing is run at the end of the block. | ||
# @param (Symbol) strategy the Chewy index update strategy see Chewy docs. | ||
def run_indexing strategy: :atomic | ||
Chewy.strategy strategy do | ||
yield | ||
end | ||
end | ||
|
||
class_methods do | ||
# Declare that all tests in this file require real indexing, always. | ||
# In my completely unscientific experiments, this roughly doubled test runtime. | ||
# Use with trepidation. | ||
def index_everything! | ||
setup do | ||
Chewy.strategy :urgent | ||
end | ||
|
||
teardown do | ||
Chewy.strategy.pop | ||
end | ||
end | ||
end | ||
|
||
included do | ||
teardown do | ||
# always destroy indexes between tests | ||
# Prevent croll pollution of test cases due to indexing | ||
Chewy.massacre | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# 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::Type class. (See SearchTestHelper) | ||
# | ||
# The class will capture the data from the *param on the Chewy::Type#bulk method and | ||
# aggregate the data for test analysis. Optionally, a (Chewy::Type) filter parameter | ||
# can be provided which will cause undesired indexes and deletes to be discarded. | ||
class SearchIndexReceiver | ||
# @param (Chewy::Type) filter | ||
# @see SearchIndexCatcher for an explanation of the filter. | ||
def initialize filter: nil | ||
@mutations = {} | ||
@filter = filter | ||
end | ||
|
||
# @param bulk_params the bulk_params that should be sent to the Chewy::Type#bulk method. | ||
# @param (Chewy::Type) type the Index::Type executing this query. | ||
def catch bulk_params, type | ||
return if filter? type | ||
Array.wrap(bulk_params).map {|y| y[:body] }.flatten.each do |update| | ||
if body = update[:delete] | ||
mutation_for(type).deletes << body[:_id] | ||
elsif body = update[:index] | ||
mutation_for(type).indexes << body | ||
end | ||
end | ||
end | ||
|
||
# @param index return only index requests to the specified Chewy::Type index. | ||
# @return the index changes captured by the mock. | ||
def indexes_for index = nil | ||
if index | ||
mutation_for(index).indexes | ||
elsif @filter | ||
mutation_for(@filter).indexes | ||
else | ||
@mutations.transform_values {|v| v.indexes} | ||
end | ||
end | ||
alias_method :indexes, :indexes_for | ||
|
||
# @param index return only delete requests to the specified Chewy::Type index. | ||
# @return the index deletes captured by the mock. | ||
def deletes_for index = nil | ||
if index | ||
mutation_for(index).deletes | ||
elsif @filter | ||
mutation_for(@filter).deletes | ||
else | ||
@mutations.transform_values {|v| v.deletes} | ||
end | ||
end | ||
alias_method :deletes, :deletes_for | ||
|
||
# Check to see if a given object has been indexed. | ||
# @param (#id) obj the object to look for. | ||
# @return bool if the object was indexed. | ||
def indexed? obj | ||
indexes.map {|i| i[:_id]}.compact.include? obj.id | ||
end | ||
|
||
# Check to see if a given object has been deleted. | ||
# @param (#id) obj the object to look for. | ||
# @return bool if the object was deleted. | ||
def deleted? obj | ||
deletes.map {|i| i[:_id]}.compact.include? obj.id | ||
end | ||
|
||
# @return a list of Chewy::Type indexes changed. | ||
def updated_indexes | ||
@mutations.keys | ||
end | ||
|
||
private | ||
# Get the mutation object for a given type. | ||
# @param (Chewy::Type) type the index type to fetch. | ||
# @return (#indexes, #deletes) an object with a list of indexes and a list of deletes. | ||
def mutation_for type | ||
@mutations[type] ||= OpenStruct.new(indexes: [], deletes: []) | ||
end | ||
|
||
def filter? type | ||
return false unless @filter | ||
return ! type == @filter | ||
end | ||
|
||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require 'chewy/minitest'
?