<commit_before>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

        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<commit_msg>minitest helper compatible with activesupport<4.2<commit_after>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

        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

      module ClassMethods
        # 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
{'fraction_non_alphanumeric': 0.0497335701598579, 'fraction_numerical': 0.00039471087428458656, 'mean_word_length': 2.1675, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2335892', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1284, 'n_words': 499}