2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "active_record/explain_subscriber"
|
|
|
|
require "active_record/explain_registry"
|
2012-02-03 19:06:50 -05:00
|
|
|
|
|
|
|
if ActiveRecord::Base.connection.supports_explain?
|
|
|
|
class ExplainSubscriberTest < ActiveRecord::TestCase
|
|
|
|
SUBSCRIBER = ActiveRecord::ExplainSubscriber.new
|
|
|
|
|
2013-04-16 16:46:55 -04:00
|
|
|
def setup
|
|
|
|
ActiveRecord::ExplainRegistry.reset
|
|
|
|
ActiveRecord::ExplainRegistry.collect = true
|
2012-02-03 19:06:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collects_nothing_if_the_payload_has_an_exception
|
2013-04-16 16:46:55 -04:00
|
|
|
SUBSCRIBER.finish(nil, nil, exception: Exception.new)
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty queries
|
2012-02-03 19:06:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collects_nothing_for_ignored_payloads
|
2013-04-16 16:46:55 -04:00
|
|
|
ActiveRecord::ExplainSubscriber::IGNORED_PAYLOADS.each do |ip|
|
|
|
|
SUBSCRIBER.finish(nil, nil, name: ip)
|
2012-02-03 19:06:50 -05:00
|
|
|
end
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty queries
|
2013-04-16 16:46:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collects_nothing_if_collect_is_false
|
|
|
|
ActiveRecord::ExplainRegistry.collect = false
|
2016-08-06 12:26:20 -04:00
|
|
|
SUBSCRIBER.finish(nil, nil, name: "SQL", sql: "select 1 from users", binds: [1, 2])
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty queries
|
2012-02-03 19:06:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collects_pairs_of_queries_and_binds
|
2016-08-06 12:26:20 -04:00
|
|
|
sql = "select 1 from users"
|
2012-02-03 19:06:50 -05:00
|
|
|
binds = [1, 2]
|
2016-08-06 12:26:20 -04:00
|
|
|
SUBSCRIBER.finish(nil, nil, name: "SQL", sql: sql, binds: binds)
|
2013-04-16 16:46:55 -04:00
|
|
|
assert_equal 1, queries.size
|
|
|
|
assert_equal sql, queries[0][0]
|
|
|
|
assert_equal binds, queries[0][1]
|
2012-02-03 19:06:50 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 09:30:05 -04:00
|
|
|
def test_collects_nothing_if_the_statement_is_not_explainable
|
2016-08-06 12:26:20 -04:00
|
|
|
SUBSCRIBER.finish(nil, nil, name: "SQL", sql: "SHOW max_identifier_length")
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty queries
|
2013-04-16 16:46:55 -04:00
|
|
|
end
|
|
|
|
|
2013-05-10 10:21:59 -04:00
|
|
|
def test_collects_nothing_if_the_statement_is_only_partially_matched
|
2016-08-06 12:26:20 -04:00
|
|
|
SUBSCRIBER.finish(nil, nil, name: "SQL", sql: "select_db yo_mama")
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty queries
|
2013-05-10 10:21:59 -04:00
|
|
|
end
|
|
|
|
|
2015-04-21 09:45:21 -04:00
|
|
|
def test_collects_cte_queries
|
2016-08-06 12:26:20 -04:00
|
|
|
SUBSCRIBER.finish(nil, nil, name: "SQL", sql: "with s as (values(3)) select 1 from s")
|
2015-04-21 09:45:21 -04:00
|
|
|
assert_equal 1, queries.size
|
|
|
|
end
|
|
|
|
|
2014-03-14 00:35:58 -04:00
|
|
|
teardown do
|
2013-04-16 16:46:55 -04:00
|
|
|
ActiveRecord::ExplainRegistry.reset
|
2012-09-15 21:43:30 -04:00
|
|
|
end
|
|
|
|
|
2013-04-16 16:46:55 -04:00
|
|
|
def queries
|
|
|
|
ActiveRecord::ExplainRegistry.queries
|
2012-02-03 19:06:50 -05:00
|
|
|
end
|
|
|
|
end
|
2012-06-20 17:20:55 -04:00
|
|
|
end
|