diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2016-12-20 22:54:36 +0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-12-21 17:08:39 +0300 |
commit | b987cfd4f852c1e687d922a4d79136bcf6a27c34 (patch) | |
tree | 4a44b6efe19b699409e77f9f8c9e9f76d75c1487 /spec/support | |
parent | 15badbc304b545419c00c8588dea4a6bb70b00d4 (diff) |
Merge branch 'jej-fix-n+1-queries-milestones-show' into 'master'
Fix N+1 queries on milestone show pages
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/25832
See merge request !8185
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/query_recorder.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/spec/support/query_recorder.rb b/spec/support/query_recorder.rb new file mode 100644 index 00000000000..e40d5ebd9a8 --- /dev/null +++ b/spec/support/query_recorder.rb @@ -0,0 +1,40 @@ +module ActiveRecord + class QueryRecorder + attr_reader :log + + def initialize(&block) + @log = [] + ActiveSupport::Notifications.subscribed(method(:callback), 'sql.active_record', &block) + end + + def callback(name, start, finish, message_id, values) + return if %w(CACHE SCHEMA).include?(values[:name]) + @log << values[:sql] + end + + def count + @log.count + end + + def log_message + @log.join("\n\n") + end + end +end + +RSpec::Matchers.define :exceed_query_limit do |expected| + supports_block_expectations + + match do |block| + query_count(&block) > expected + end + + failure_message_when_negated do |actual| + "Expected a maximum of #{expected} queries, got #{@recorder.count}:\n\n#{@recorder.log_message}" + end + + def query_count(&block) + @recorder = ActiveRecord::QueryRecorder.new(&block) + @recorder.count + end +end |