Commit 073e579a authored by Alishan Ladhani's avatar Alishan Ladhani

Fix false-positive N+1 tests

parent 400bcbc9
...@@ -11,10 +11,10 @@ RSpec.describe EE::EnvironmentSerializer do ...@@ -11,10 +11,10 @@ RSpec.describe EE::EnvironmentSerializer do
end end
before do before do
stub_licensed_features(environment_alerts: true) stub_licensed_features(environment_alerts: true, protected_environments: true)
end end
it_behaves_like 'avoid N+1 on environments serialization' it_behaves_like 'avoid N+1 on environments serialization', ee: true
def create_environment_with_associations(project) def create_environment_with_associations(project)
create(:environment, project: project).tap do |environment| create(:environment, project: project).tap do |environment|
......
# frozen_string_literal: true # frozen_string_literal: true
RSpec.shared_examples 'avoid N+1 on environments serialization' do RSpec.shared_examples 'avoid N+1 on environments serialization' do |ee: false|
# Investigating in https://gitlab.com/gitlab-org/gitlab/-/issues/353209
let(:query_threshold) { 50 + (ee ? 4 : 0) }
it 'avoids N+1 database queries with grouping', :request_store do it 'avoids N+1 database queries with grouping', :request_store do
create_environment_with_associations(project) create_environment_with_associations(project)
control = ActiveRecord::QueryRecorder.new { serialize(grouping: true) } control = ActiveRecord::QueryRecorder.new { serialize(grouping: true) }
create_environment_with_associations(project)
create_environment_with_associations(project) create_environment_with_associations(project)
expect { serialize(grouping: true) }.not_to exceed_query_limit(control.count) expect { serialize(grouping: true) }
.not_to exceed_query_limit(control.count)
.with_threshold(query_threshold)
end end
it 'avoids N+1 database queries without grouping', :request_store do it 'avoids N+1 database queries without grouping', :request_store do
...@@ -15,9 +21,12 @@ RSpec.shared_examples 'avoid N+1 on environments serialization' do ...@@ -15,9 +21,12 @@ RSpec.shared_examples 'avoid N+1 on environments serialization' do
control = ActiveRecord::QueryRecorder.new { serialize(grouping: false) } control = ActiveRecord::QueryRecorder.new { serialize(grouping: false) }
create_environment_with_associations(project)
create_environment_with_associations(project) create_environment_with_associations(project)
expect { serialize(grouping: false) }.not_to exceed_query_limit(control.count) expect { serialize(grouping: false) }
.not_to exceed_query_limit(control.count)
.with_threshold(query_threshold)
end end
it 'does not preload for environments that does not exist in the page', :request_store do it 'does not preload for environments that does not exist in the page', :request_store do
...@@ -35,7 +44,7 @@ RSpec.shared_examples 'avoid N+1 on environments serialization' do ...@@ -35,7 +44,7 @@ RSpec.shared_examples 'avoid N+1 on environments serialization' do
end end
def serialize(grouping:, query: nil) def serialize(grouping:, query: nil)
query ||= { page: 1, per_page: 1 } query ||= { page: 1, per_page: 20 }
request = double(url: "#{Gitlab.config.gitlab.url}:8080/api/v4/projects?#{query.to_query}", query_parameters: query) request = double(url: "#{Gitlab.config.gitlab.url}:8080/api/v4/projects?#{query.to_query}", query_parameters: query)
EnvironmentSerializer.new(current_user: user, project: project).yield_self do |serializer| EnvironmentSerializer.new(current_user: user, project: project).yield_self do |serializer|
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment