<commit_before>module OpenFoodNetwork
  module DelayedJobHelper
    def run_job(job)
      clear_jobs
      Delayed::Job.enqueue job
      flush_jobs
    end

    # Process all pending Delayed jobs, keeping in mind jobs could spawn new
    # delayed job (so things might be added to the queue while processing)
    def flush_jobs(options = {})
      options[:ignore_exceptions] ||= false

      Delayed::Worker.new.work_off(100)

      unless options[:ignore_exceptions]
        Delayed::Job.all.each do |job|
          if job.last_error.present?
            throw "There was an error in a delayed job: #{job.last_error}"
          end
        end
      end
    end

    def clear_jobs
      Delayed::Job.delete_all
    end


    # expect { foo }.to enqueue_job MyJob, field1: 'foo', field2: 'bar'
    RSpec::Matchers.define :enqueue_job do |klass, options = {}|
      match do |event_proc|
        last_job_id_before = Delayed::Job.last.andand.id || 0

        begin
          event_proc.call
        rescue StandardError => e
          @exception = e
          raise e
        end

        @jobs_created = Delayed::Job.where('id > ?', last_job_id_before)

        @jobs_created.any? do |job|
          job = job.payload_object

          match = true
          match &= (job.class == klass)

          options.each_pair do |k, v|
            begin
              match &= (job[k] == v)
            rescue NameError
              match = false
            end
          end

          match
        end
      end

      failure_message_for_should do |event_proc|
        @exception || "expected #{klass} to be enqueued matching #{options.inspect} (#{@jobs_created.count} others enqueued)"
      end

      failure_message_for_should_not do |event_proc|
        @exception || "expected #{klass} to not be enqueued matching #{options.inspect}"
      end
    end
  end
end<commit_msg>Rspec matcher enqueue_job for Delayed::Job supports block expectations<commit_after>module OpenFoodNetwork
  module DelayedJobHelper
    def run_job(job)
      clear_jobs
      Delayed::Job.enqueue job
      flush_jobs
    end

    # Process all pending Delayed jobs, keeping in mind jobs could spawn new
    # delayed job (so things might be added to the queue while processing)
    def flush_jobs(options = {})
      options[:ignore_exceptions] ||= false

      Delayed::Worker.new.work_off(100)

      unless options[:ignore_exceptions]
        Delayed::Job.all.each do |job|
          if job.last_error.present?
            throw "There was an error in a delayed job: #{job.last_error}"
          end
        end
      end
    end

    def clear_jobs
      Delayed::Job.delete_all
    end


    # expect { foo }.to enqueue_job MyJob, field1: 'foo', field2: 'bar'
    RSpec::Matchers.define :enqueue_job do |klass, options = {}|
      match do |event_proc|
        last_job_id_before = Delayed::Job.last.andand.id || 0

        begin
          event_proc.call
        rescue StandardError => e
          @exception = e
          raise e
        end

        @jobs_created = Delayed::Job.where('id > ?', last_job_id_before)

        @jobs_created.any? do |job|
          job = job.payload_object

          match = true
          match &= (job.class == klass)

          options.each_pair do |k, v|
            begin
              match &= (job[k] == v)
            rescue NameError
              match = false
            end
          end

          match
        end
      end

      failure_message_for_should do |event_proc|
        @exception || "expected #{klass} to be enqueued matching #{options.inspect} (#{@jobs_created.count} others enqueued)"
      end

      failure_message_for_should_not do |event_proc|
        @exception || "expected #{klass} to not be enqueued matching #{options.inspect}"
      end

      def supports_block_expectations?
        true
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08526532715095311, 'fraction_numerical': 0.0030911901081916537, 'mean_word_length': 2.035965598123534, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4021739', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1129, 'n_words': 322}