Sha256: 21f5dc6c1e70c1b2e767f05e120918dfc57aa6f81a45ed9792635c36dee55e66

Contents?: true

Size: 1.74 KB

Versions: 15

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.empty? ? '' : "\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 values[:name] == 'CACHE' || ignore.any? { |x| x =~ sql }
      log << sql
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
shipit-engine-0.31.0 test/helpers/queries_helper.rb
shipit-engine-0.30.0 test/helpers/queries_helper.rb
shipit-engine-0.29.0 test/helpers/queries_helper.rb
shipit-engine-0.28.1 test/helpers/queries_helper.rb
shipit-engine-0.28.0 test/helpers/queries_helper.rb
shipit-engine-0.27.1 test/helpers/queries_helper.rb
shipit-engine-0.27.0 test/helpers/queries_helper.rb
shipit-engine-0.26.0 test/helpers/queries_helper.rb
shipit-engine-0.25.1 test/helpers/queries_helper.rb
shipit-engine-0.25.0 test/helpers/queries_helper.rb
shipit-engine-0.24.0 test/helpers/queries_helper.rb
shipit-engine-0.23.1 test/helpers/queries_helper.rb
shipit-engine-0.23.0 test/helpers/queries_helper.rb
shipit-engine-0.22.0 test/helpers/queries_helper.rb
shipit-engine-0.21.0 test/helpers/queries_helper.rb