aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/explain_subscriber_test.rb
blob: fb53a92c8956441b277cbf8bf465b7fd29f6b1d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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 teardown
      ActiveRecord::ExplainRegistry.reset
    end

    def queries
      ActiveRecord::ExplainRegistry.queries
    end
  end
end