Sha256: ec6898eb38298e90092dc47720dbb4190509250dd8ff0dc7fd6bfcbedf3c1b44

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

# rubocop:disable  Metrics/BlockLength
::RSpec::Matchers.define :perform_constant_number_of_queries do
  supports_block_expectations

  chain :with_scale_factors do |*factors|
    @factors = factors
  end

  chain :matching do |pattern|
    @pattern = pattern
  end

  chain :with_warming_up do
    @warmup = true
  end

  match do |actual, *_args|
    raise ArgumentError, "Block is required" unless actual.is_a? Proc

    raise "Missing tag :n_plus_one" unless
      @matcher_execution_context.respond_to?(:n_plus_one_populate)

    populate = @matcher_execution_context.n_plus_one_populate
    warmup = @warmup ? actual : @matcher_execution_context.n_plus_one_warmup

    warmup.call if warmup.present?

    # by default we're looking for select queries
    pattern = @pattern || /^SELECT/i

    @matcher_execution_context.executor = NPlusOneControl::Executor.new(
      population: populate,
      matching: pattern,
      scale_factors: @factors
    )

    @queries = @matcher_execution_context.executor.call(&actual)

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

    counts.max == counts.min
  end

  match_when_negated do |_actual|
    raise "This matcher doesn't support negation"
  end

  failure_message { |_actual| NPlusOneControl.failure_message(@queries) }
end
# rubocop:enable  Metrics/BlockLength

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
n_plus_one_control-0.4.1 lib/n_plus_one_control/rspec/matcher.rb
n_plus_one_control-0.4.0 lib/n_plus_one_control/rspec/matcher.rb