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

Version Path
shipit-engine-0.10.0 test/helpers/queries_helper.rb
shipit-engine-0.9.0 test/helpers/queries_helper.rb
shipit-engine-0.8.9 test/helpers/queries_helper.rb
shipit-engine-0.8.8 test/helpers/queries_helper.rb
shipit-engine-0.8.7 test/helpers/queries_helper.rb
shipit-engine-0.8.6 test/helpers/queries_helper.rb
shipit-engine-0.8.5 test/helpers/queries_helper.rb
shipit-engine-0.8.4 test/helpers/queries_helper.rb
shipit-engine-0.8.3 test/helpers/queries_helper.rb
shipit-engine-0.8.2 test/helpers/queries_helper.rb
shipit-engine-0.8.1 test/helpers/queries_helper.rb
shipit-engine-0.8.0 test/helpers/queries_helper.rb
shipit-engine-0.7.0 test/helpers/queries_helper.rb
shipit-engine-0.6.4 test/helpers/queries_helper.rb
shipit-engine-0.6.3 test/helpers/queries_helper.rb
shipit-engine-0.6.2 test/helpers/queries_helper.rb
shipit-engine-0.6.1 test/helpers/queries_helper.rb
shipit-engine-0.6.0 test/helpers/queries_helper.rb
shipit-engine-0.5.2 test/helpers/queries_helper.rb
shipit-engine-0.5.1 test/helpers/queries_helper.rb