Commit c4de6ec4 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'improve-extra-queries-output' into 'master'

Improve output for extra queries in specs

See merge request gitlab-org/gitlab-ce!15537
parents e72804ed db1925f9
......@@ -172,15 +172,15 @@ describe API::MergeRequests do
context "when authenticated" do
it 'avoids N+1 queries' do
control_count = ActiveRecord::QueryRecorder.new do
control = ActiveRecord::QueryRecorder.new do
get api("/projects/#{project.id}/merge_requests", user)
end.count
end
create(:merge_request, state: 'closed', milestone: milestone1, author: user, assignee: user, source_project: project, target_project: project, title: "Test", created_at: base_time)
expect do
get api("/projects/#{project.id}/merge_requests", user)
end.not_to exceed_query_limit(control_count)
end.not_to exceed_query_limit(control)
end
it "returns an array of all merge_requests" do
......
......@@ -76,10 +76,17 @@ RSpec::Matchers.define :exceed_query_limit do |expected|
@recorder.count
end
def count_queries(queries)
queries.each_with_object(Hash.new(0)) { |query, counts| counts[query] += 1 }
end
def log_message
if expected.is_a?(ActiveRecord::QueryRecorder)
extra_queries = (expected.log - @recorder.log).join("\n\n")
"Extra queries: \n\n #{extra_queries}"
counts = count_queries(expected.log)
extra_queries = @recorder.log.reject { |query| counts[query] -= 1 unless counts[query].zero? }
extra_queries_display = count_queries(extra_queries).map { |query, count| "[#{count}] #{query}" }
(['Extra queries:'] + extra_queries_display).join("\n\n")
else
@recorder.log_message
end
......
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