File: n_plus_one_query_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (23 lines) | stat: -rw-r--r-- 831 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# frozen_string_literal: true

RSpec.shared_examples 'N+1 query check' do |threshold: 0, skip_cached: true|
  it 'prevents N+1 queries' do
    execute_query # "warm up" to prevent undeterministic counts
    expect(graphql_errors).to be_blank # Sanity check - ex falso quodlibet!

    control = ActiveRecord::QueryRecorder.new(skip_cached: skip_cached) { execute_query }
    expect(control.count).to be > 0

    search_params[:iids] << extra_iid_for_second_query

    expect { execute_query }.not_to exceed_query_count_limit(control, skip_cached: skip_cached, threshold: threshold)
  end

  def exceed_query_count_limit(control, skip_cached: true, threshold: 0)
    if skip_cached
      exceed_query_limit(control).with_threshold(threshold)
    else
      exceed_all_query_limit(control).with_threshold(threshold)
    end
  end
end