diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-08-28 14:40:47 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-08-29 15:45:14 +0200 |
commit | 2003ffeb60cd31c89c452c4fe92ac010c224766a (patch) | |
tree | ed625ed42fe535e83bb28d2f168ff869a62d85af | |
parent | 073811074bf15742a86572b933b0e3ad0f06107b (diff) | |
download | gitlab-ce-2003ffeb60cd31c89c452c4fe92ac010c224766a.tar.gz |
Count specific queries in RSpec test suite
-rw-r--r-- | spec/spec_helper.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ff1754fbe7e..b569043a903 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -34,6 +34,56 @@ require 'rainbow/ext/string' # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } +class RSpecQuery + attr_reader :count + + def initialize(file) + @file = file + @count = 0 + end + + def to_s + "#{@count} #{@file}" + end + + def increment! + @count += 1 + end +end + +module RSpec + def self.queries_profile + @queries_profile.to_h.values.sort_by(&:count).reverse.map(&:to_s) + end + + def self.current_query_profile + @queries_profile.to_h.values.last + end + + def self.append_query_profile(file) + (@queries_profile ||= {})[file] ||= RSpecQuery.new(file) + end +end + +class RSpecQueryListener + def example_started(notification) + example = notification.try(:example) || notification + RSpec.append_query_profile(example.metadata[:file_path].to_s) + end + + def dump_summary(*) + puts RSpec.queries_profile.inspect + end +end + +ActiveSupport::Notifications.subscribe('sql.active_record') do |*args| + event = ActiveSupport::Notifications::Event.new(*args) + + if event.payload[:sql] =~ /INSERT INTO "projects"/ + RSpec.current_query_profile&.increment! + end +end + RSpec.configure do |config| config.use_transactional_fixtures = false config.use_instantiated_fixtures = false @@ -76,6 +126,8 @@ RSpec.configure do |config| config.reporter.register_listener(RspecFlaky::Listener.new, :example_passed, :dump_summary) end + config.reporter.register_listener(RSpecQueryListener.new, :example_started, :dump_summary) + config.before(:suite) do Timecop.safe_mode = true TestEnv.init |