Commit 01fdb668 authored by Alex Kalderimis's avatar Alex Kalderimis

Improve performance specs

This improves the alert_management/alert performance specs by separating
out the query validation from the N+1 test.
parent c7fe7d80
...@@ -60,19 +60,35 @@ RSpec.describe 'getting Alert Management Alert Assignees' do ...@@ -60,19 +60,35 @@ RSpec.describe 'getting Alert Management Alert Assignees' do
expect(second_assignees).to be_empty expect(second_assignees).to be_empty
end end
it 'avoids N+1 queries' do describe 'performance' do
base_count = ActiveRecord::QueryRecorder.new do let(:first_n) { var('Int') }
post_graphql(query, current_user: current_user) let(:params) { { first: first_n } }
let(:limited_query) { with_signature([first_n], query) }
before do
create(:alert_management_alert, project: project, assignees: [current_user])
end
it 'can limit results' do
post_graphql(limited_query, current_user: current_user, variables: first_n.with(1))
expect(alerts.size).to eq 1
expect(alerts).not_to include(a_hash_including('iid' => first_alert.iid.to_s))
end end
# An N+1 would mean a new alert would increase the query count it 'can include all results' do
third_alert = create(:alert_management_alert, project: project, assignees: [current_user]) post_graphql(limited_query, current_user: current_user)
expect { post_graphql(query, current_user: current_user) }.not_to exceed_query_limit(base_count) expect(alerts.size).to be > 1
expect(alerts).to include(a_hash_including('iid' => first_alert.iid.to_s))
end
third_assignees = assignees[third_alert.iid.to_s] it 'avoids N+1 queries' do
base_count = ActiveRecord::QueryRecorder.new do
post_graphql(limited_query, current_user: current_user, variables: first_n.with(1))
end
expect(third_assignees.length).to eq(1) expect { post_graphql(limited_query, current_user: current_user) }.not_to exceed_query_limit(base_count)
expect(third_assignees.first).to include('username' => current_user.username) end
end end
end end
...@@ -65,16 +65,28 @@ RSpec.describe 'getting Alert Management Alert Notes' do ...@@ -65,16 +65,28 @@ RSpec.describe 'getting Alert Management Alert Notes' do
expect(second_notes_result).to be_empty expect(second_notes_result).to be_empty
end end
it 'avoids N+1 queries' do describe 'performance' do
base_count = ActiveRecord::QueryRecorder.new do let(:first_n) { var('Int') }
post_graphql(query, current_user: current_user) let(:params) { { first: first_n } }
before do
# An N+1 would mean a new alert would increase the query count
create(:alert_management_alert, project: project)
end end
# An N+1 would mean a new alert would increase the query count it 'avoids N+1 queries' do
create(:alert_management_alert, project: project) q = with_signature([first_n], query)
expect { post_graphql(query, current_user: current_user) }.not_to exceed_query_limit(base_count) base_count = ActiveRecord::QueryRecorder.new do
expect(alerts_result.length).to eq(3) post_graphql(q, current_user: current_user, variables: first_n.with(1))
expect(alerts_result.length).to eq(1)
end
expect do
post_graphql(q, current_user: current_user, variables: first_n.with(3))
expect(alerts_result.length).to eq(3)
end.not_to exceed_query_limit(base_count)
end
end end
context 'for non-system notes' do context 'for non-system notes' do
......
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