summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2017-11-22 16:33:10 +0000
committerRémy Coutable <remy@rymai.me>2017-11-22 16:33:10 +0000
commitc4de6ec496fd9556ac6cdacbeeb008c833ce9bf1 (patch)
tree68f7ffb3f7187baaaec40d87c0cf7df65c7a5ea4
parente72804ed0a73f8807ca58881645f0c95c70de9c9 (diff)
parentdb1925f917b30571e0e9d4de63417db90dc693d7 (diff)
downloadgitlab-ce-c4de6ec496fd9556ac6cdacbeeb008c833ce9bf1.tar.gz
Merge branch 'improve-extra-queries-output' into 'master'
Improve output for extra queries in specs See merge request gitlab-org/gitlab-ce!15537
-rw-r--r--spec/requests/api/merge_requests_spec.rb6
-rw-r--r--spec/support/query_recorder.rb11
2 files changed, 12 insertions, 5 deletions
diff --git a/spec/requests/api/merge_requests_spec.rb b/spec/requests/api/merge_requests_spec.rb
index 35c6b3bb2fb..91616da6d9a 100644
--- a/spec/requests/api/merge_requests_spec.rb
+++ b/spec/requests/api/merge_requests_spec.rb
@@ -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
diff --git a/spec/support/query_recorder.rb b/spec/support/query_recorder.rb
index cd67b517ea0..369775db462 100644
--- a/spec/support/query_recorder.rb
+++ b/spec/support/query_recorder.rb
@@ -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