Sha256: 76c871dd0c42250a5ce6caf6e06d90f3438550f8e3c9638132c6f4a51c6ea9de

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require "n_plus_one_control"

module NPlusOneControl
  # Minitest assertions
  module MinitestHelper
    def assert_perform_constant_number_of_queries(
      populate: nil,
      matching: nil,
      scale_factors: nil,
      warmup: nil
    )

      raise ArgumentError, "Block is required" unless block_given?

      warming_up warmup

      @executor = NPlusOneControl::Executor.new(
        population: populate || population_method,
        matching: matching || /^SELECT/i,
        scale_factors: scale_factors || NPlusOneControl.default_scale_factors
      )

      queries = @executor.call { yield }

      counts = queries.map(&:last).map(&:size)

      assert counts.max == counts.min, NPlusOneControl.failure_message(queries)
    end

    def current_scale
      @executor&.current_scale
    end

    private

    def warming_up(warmup)
      (warmup || methods.include?(:warmup) ? method(:warmup) : nil)&.call
    end

    def population_method
      methods.include?(:populate) ? method(:populate) : nil
    end
  end
end

Minitest::Test.include NPlusOneControl::MinitestHelper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
n_plus_one_control-0.4.1 lib/n_plus_one_control/minitest.rb