summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/serializers/environment_serializer_shared_examples.rb
blob: 00146335ef7d7f1e84ac4211319bea3786c06abb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true
RSpec.shared_examples 'avoid N+1 on environments serialization' do
  it 'avoids N+1 database queries with grouping', :request_store do
    create_environment_with_associations(project)

    control = ActiveRecord::QueryRecorder.new { serialize(grouping: true) }

    create_environment_with_associations(project)

    expect { serialize(grouping: true) }.not_to exceed_query_limit(control.count)
  end

  it 'avoids N+1 database queries without grouping', :request_store do
    create_environment_with_associations(project)

    control = ActiveRecord::QueryRecorder.new { serialize(grouping: false) }

    create_environment_with_associations(project)

    expect { serialize(grouping: false) }.not_to exceed_query_limit(control.count)
  end

  def serialize(grouping:)
    EnvironmentSerializer.new(current_user: user, project: project).yield_self do |serializer|
      serializer.within_folders if grouping
      serializer.represent(Environment.where(project: project))
    end
  end
end