class LogSubscriberTest

Constants

Event
REGEXP_BOLD
REGEXP_CLEAR
REGEXP_CYAN
REGEXP_MAGENTA
SQL_COLORINGS

Public Instance Methods

set_logger(logger) click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 56
def set_logger(logger)
  ActiveRecord::Base.logger = logger
end
setup() click to toggle source
Calls superclass method
# File activerecord/test/cases/log_subscriber_test.rb, line 43
def setup
  @old_logger = ActiveRecord::Base.logger
  Developer.primary_key
  super
  ActiveRecord::LogSubscriber.attach_to(:active_record)
end
teardown() click to toggle source
Calls superclass method
# File activerecord/test/cases/log_subscriber_test.rb, line 50
def teardown
  super
  ActiveRecord::LogSubscriber.log_subscribers.pop
  ActiveRecord::Base.logger = @old_logger
end
test_basic_payload_name_logging_coloration_generic_sql() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 97
def test_basic_payload_name_logging_coloration_generic_sql
  logger = TestDebugLogSubscriber.new
  logger.colorize_logging = true
  SQL_COLORINGS.each do |verb, _|
    logger.sql(Event.new(0.9, sql: verb.to_s))
    assert_match(/#{REGEXP_BOLD}#{REGEXP_MAGENTA} \(0\.9ms\)#{REGEXP_CLEAR}/i, logger.debugs.last)

    logger.sql(Event.new(0.9, sql: verb.to_s, name: "SQL"))
    assert_match(/#{REGEXP_BOLD}#{REGEXP_MAGENTA}SQL \(0\.9ms\)#{REGEXP_CLEAR}/i, logger.debugs.last)
  end
end
test_basic_payload_name_logging_coloration_named_sql() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 109
def test_basic_payload_name_logging_coloration_named_sql
  logger = TestDebugLogSubscriber.new
  logger.colorize_logging = true
  SQL_COLORINGS.each do |verb, _|
    logger.sql(Event.new(0.9, sql: verb.to_s, name: "Model Load"))
    assert_match(/#{REGEXP_BOLD}#{REGEXP_CYAN}Model Load \(0\.9ms\)#{REGEXP_CLEAR}/i, logger.debugs.last)

    logger.sql(Event.new(0.9, sql: verb.to_s, name: "Model Exists"))
    assert_match(/#{REGEXP_BOLD}#{REGEXP_CYAN}Model Exists \(0\.9ms\)#{REGEXP_CLEAR}/i, logger.debugs.last)

    logger.sql(Event.new(0.9, sql: verb.to_s, name: "ANY SPECIFIC NAME"))
    assert_match(/#{REGEXP_BOLD}#{REGEXP_CYAN}ANY SPECIFIC NAME \(0\.9ms\)#{REGEXP_CLEAR}/i, logger.debugs.last)
  end
end
test_basic_query_doesnt_log_when_level_is_not_debug() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 185
def test_basic_query_doesnt_log_when_level_is_not_debug
  @logger.level = INFO
  Developer.all.load
  wait
  assert_equal 0, @logger.logged(:debug).size
end
test_basic_query_logging() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 80
def test_basic_query_logging
  Developer.all.load
  wait
  assert_equal 1, @logger.logged(:debug).size
  assert_match(/Developer Load/, @logger.logged(:debug).last)
  assert_match(/SELECT .*?FROM .?developers.?/i, @logger.logged(:debug).last)
end
test_basic_query_logging_coloration() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 88
def test_basic_query_logging_coloration
  logger = TestDebugLogSubscriber.new
  logger.colorize_logging = true
  SQL_COLORINGS.each do |verb, color_regex|
    logger.sql(Event.new(0.9, sql: verb.to_s))
    assert_match(/#{REGEXP_BOLD}#{color_regex}#{verb}#{REGEXP_CLEAR}/i, logger.debugs.last)
  end
end
test_binary_data_hash() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 213
def test_binary_data_hash
  Binary.create(data: { a: 1 })
  wait
  assert_match(/<7 bytes of binary data>/, @logger.logged(:debug).join)
end
test_binary_data_is_not_logged() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 207
def test_binary_data_is_not_logged
  Binary.create(data: "some binary data")
  wait
  assert_match(/<16 bytes of binary data>/, @logger.logged(:debug).join)
end
test_cached_queries() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 174
def test_cached_queries
  ActiveRecord::Base.cache do
    Developer.all.load
    Developer.all.load
  end
  wait
  assert_equal 2, @logger.logged(:debug).size
  assert_match(/CACHE/, @logger.logged(:debug).last)
  assert_match(/SELECT .*?FROM .?developers.?/i, @logger.logged(:debug).last)
end
test_cached_queries_doesnt_log_when_level_is_not_debug() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 192
def test_cached_queries_doesnt_log_when_level_is_not_debug
  @logger.level = INFO
  ActiveRecord::Base.cache do
    Developer.all.load
    Developer.all.load
  end
  wait
  assert_equal 0, @logger.logged(:debug).size
end
test_exists_query_logging() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 166
def test_exists_query_logging
  Developer.exists? 1
  wait
  assert_equal 1, @logger.logged(:debug).size
  assert_match(/Developer Exists/, @logger.logged(:debug).last)
  assert_match(/SELECT .*?FROM .?developers.?/i, @logger.logged(:debug).last)
end
test_initializes_runtime() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 202
def test_initializes_runtime
  Thread.new { assert_equal 0, ActiveRecord::LogSubscriber.runtime }.join
end
test_query_logging_coloration_with_lock() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 148
  def test_query_logging_coloration_with_lock
    logger = TestDebugLogSubscriber.new
    logger.colorize_logging = true
    sql = <<-EOS
      SELECT * FROM
        (SELECT * FROM mytable FOR UPDATE) ss
      WHERE col1 = 5;
    EOS
    logger.sql(Event.new(0.9, sql: sql))
    assert_match(/#{REGEXP_BOLD}#{REGEXP_MAGENTA} \(0\.9ms\)#{REGEXP_CLEAR}  #{REGEXP_BOLD}#{SQL_COLORINGS[:LOCK]}.*FOR UPDATE.*#{REGEXP_CLEAR}/mi, logger.debugs.last)

    sql = <<-EOS
      LOCK TABLE films IN SHARE MODE;
    EOS
    logger.sql(Event.new(0.9, sql: sql))
    assert_match(/#{REGEXP_BOLD}#{REGEXP_MAGENTA} \(0\.9ms\)#{REGEXP_CLEAR}  #{REGEXP_BOLD}#{SQL_COLORINGS[:LOCK]}.*LOCK TABLE.*#{REGEXP_CLEAR}/mi, logger.debugs.last)
  end
test_query_logging_coloration_with_multi_line_nested_select() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 133
  def test_query_logging_coloration_with_multi_line_nested_select
    logger = TestDebugLogSubscriber.new
    logger.colorize_logging = true
    SQL_COLORINGS.slice(:SELECT, :INSERT, :UPDATE, :DELETE).each do |verb, color_regex|
      sql = <<-EOS
        #{verb}
        WHERE ID IN (
          SELECT ID FROM THINGS
        )
      EOS
      logger.sql(Event.new(0.9, sql: sql))
      assert_match(/#{REGEXP_BOLD}#{REGEXP_MAGENTA} \(0\.9ms\)#{REGEXP_CLEAR}  #{REGEXP_BOLD}#{color_regex}.*#{verb}.*#{REGEXP_CLEAR}/mi, logger.debugs.last)
    end
  end
test_query_logging_coloration_with_nested_select() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 124
def test_query_logging_coloration_with_nested_select
  logger = TestDebugLogSubscriber.new
  logger.colorize_logging = true
  SQL_COLORINGS.slice(:SELECT, :INSERT, :UPDATE, :DELETE).each do |verb, color_regex|
    logger.sql(Event.new(0.9, sql: "#{verb} WHERE ID IN SELECT"))
    assert_match(/#{REGEXP_BOLD}#{REGEXP_MAGENTA} \(0\.9ms\)#{REGEXP_CLEAR}  #{REGEXP_BOLD}#{color_regex}#{verb} WHERE ID IN SELECT#{REGEXP_CLEAR}/i, logger.debugs.last)
  end
end
test_schema_statements_are_ignored() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 60
def test_schema_statements_are_ignored
  logger = TestDebugLogSubscriber.new
  assert_equal 0, logger.debugs.length

  logger.sql(Event.new(0.9, sql: "hi mom!"))
  assert_equal 1, logger.debugs.length

  logger.sql(Event.new(0.9, sql: "hi mom!", name: "foo"))
  assert_equal 2, logger.debugs.length

  logger.sql(Event.new(0.9, sql: "hi mom!", name: "SCHEMA"))
  assert_equal 2, logger.debugs.length
end
test_sql_statements_are_not_squeezed() click to toggle source
# File activerecord/test/cases/log_subscriber_test.rb, line 74
def test_sql_statements_are_not_squeezed
  logger = TestDebugLogSubscriber.new
  logger.sql(Event.new(0.9, sql: "ruby   quails"))
  assert_match(/ruby   quails/, logger.debugs.first)
end