Sha256: 4893a4cad1608916aecf2feefe8beba856729f35eb4a23a05b9e0ef2d0f2edd4

Contents?: true

Size: 1.92 KB

Versions: 16

Compression:

Stored size: 1.92 KB

Contents

require 'cases/helper'
require 'active_record/explain_subscriber'
require 'active_record/explain_registry'

if ActiveRecord::Base.connection.supports_explain?
  class ExplainSubscriberTest < ActiveRecord::TestCase
    SUBSCRIBER = ActiveRecord::ExplainSubscriber.new

    def setup
      ActiveRecord::ExplainRegistry.reset
      ActiveRecord::ExplainRegistry.collect = true
    end

    def test_collects_nothing_if_the_payload_has_an_exception
      SUBSCRIBER.finish(nil, nil, exception: Exception.new)
      assert queries.empty?
    end

    def test_collects_nothing_for_ignored_payloads
      ActiveRecord::ExplainSubscriber::IGNORED_PAYLOADS.each do |ip|
        SUBSCRIBER.finish(nil, nil, name: ip)
      end
      assert queries.empty?
    end

    def test_collects_nothing_if_collect_is_false
      ActiveRecord::ExplainRegistry.collect = false
      SUBSCRIBER.finish(nil, nil, name: 'SQL', sql: 'select 1 from users', binds: [1, 2])
      assert queries.empty?
    end

    def test_collects_pairs_of_queries_and_binds
      sql   = 'select 1 from users'
      binds = [1, 2]
      SUBSCRIBER.finish(nil, nil, name: 'SQL', sql: sql, binds: binds)
      assert_equal 1, queries.size
      assert_equal sql, queries[0][0]
      assert_equal binds, queries[0][1]
    end

    def test_collects_nothing_if_the_statement_is_not_whitelisted
      SUBSCRIBER.finish(nil, nil, name: 'SQL', sql: 'SHOW max_identifier_length')
      assert queries.empty?
    end

    def test_collects_nothing_if_the_statement_is_only_partially_matched
      SUBSCRIBER.finish(nil, nil, name: 'SQL', sql: 'select_db yo_mama')
      assert queries.empty?
    end

    def test_collects_cte_queries
      SUBSCRIBER.finish(nil, nil, name: 'SQL', sql: 'with s as (values(3)) select 1 from s')
      assert_equal 1, queries.size
    end

    teardown do
      ActiveRecord::ExplainRegistry.reset
    end

    def queries
      ActiveRecord::ExplainRegistry.queries
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ibm_db-5.2.0 test/cases/explain_subscriber_test.rb
ibm_db-5.1.0 test/cases/explain_subscriber_test.rb
ibm_db-5.0.5 test/cases/explain_subscriber_test.rb
ibm_db-5.0.4 test/cases/explain_subscriber_test.rb
ibm_db-5.0.3 test/cases/explain_subscriber_test.rb
ibm_db-5.0.2 test/cases/explain_subscriber_test.rb
ibm_db-4.0.0-x86-mingw32 test/cases/explain_subscriber_test.rb
ibm_db-4.0.0 test/cases/explain_subscriber_test.rb
ibm_db-3.0.4-x86-mingw32 test/cases/explain_subscriber_test.rb
ibm_db-3.0.4 test/cases/explain_subscriber_test.rb
ibm_db-3.0.3-x86-mingw32 test/cases/explain_subscriber_test.rb
ibm_db-3.0.3 test/cases/explain_subscriber_test.rb
ibm_db-3.0.2-x86-mingw32 test/cases/explain_subscriber_test.rb
ibm_db-3.0.2 test/cases/explain_subscriber_test.rb
ibm_db-3.0.1 test/cases/explain_subscriber_test.rb
ibm_db-3.0.1-x86-mingw32 test/cases/explain_subscriber_test.rb