Sha256: 730a7b1da24750df1b793e8e5708e41ce747983433a5bd9380ad882eeed202f4
Contents?: true
Size: 1.74 KB
Versions: 34
Compression:
Stored size: 1.74 KB
Contents
module QueriesHelper def assert_no_queries(ignored_sql = nil, &block) assert_queries(0, ignored_sql, &block) end def assert_queries(num = 1, ignored_sql = nil) counter = SQLCounter.new(ignored_sql) subscriber = ActiveSupport::Notifications.subscribe('sql.active_record', counter) yield counter queries = counter.log.size == 0 ? '' : "\nQueries:\n#{counter.log.join("\n")}" assert_equal num, counter.log.size, "#{counter.log.size} instead of #{num} queries were executed.#{queries}" ensure ActiveSupport::Notifications.unsubscribe(subscriber) end class SQLCounter cattr_accessor :ignored_sql self.ignored_sql = [ /^PRAGMA (?!(table_info))/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SELECT @@FOREIGN_KEY_CHECKS/, /^SET FOREIGN_KEY_CHECKS/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^SHOW max_identifier_length/, /^BEGIN/, /^COMMIT/, /^SHOW FULL FIELDS/, /^SHOW TABLES LIKE/, ] # FIXME: this needs to be refactored so specific database can add their own # ignored SQL. This ignored SQL is for Oracle. ignored_sql.concat [/^select .*nextval/i, /^SAVEPOINT/, /^ROLLBACK TO/, /^\s*select .* from all_triggers/im] attr_reader :ignore attr_reader :log def initialize(ignore = nil) @ignore = ignore || self.class.ignored_sql @log = [] end def call(_name, _start, _finish, _message_id, values) sql = values[:sql] # FIXME: this seems bad. we should probably have a better way to indicate # the query was cached return if 'CACHE' == values[:name] || ignore.any? { |x| x =~ sql } log << sql end end end
Version data entries
34 entries across 34 versions & 1 rubygems