1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases/log_subscriber_test.rb

116 lines
2.9 KiB
Ruby
Raw Normal View History

require "cases/helper"
require "models/developer"
2011-05-02 03:00:54 -04:00
require "models/post"
require "active_support/log_subscriber/test_helper"
class LogSubscriberTest < ActiveRecord::TestCase
include ActiveSupport::LogSubscriber::TestHelper
include ActiveSupport::Logger::Severity
2011-05-02 03:00:54 -04:00
fixtures :posts
def setup
@old_logger = ActiveRecord::Base.logger
@using_identity_map = ActiveRecord::IdentityMap.enabled?
ActiveRecord::IdentityMap.enabled = false
Developer.primary_key
super
ActiveRecord::LogSubscriber.attach_to(:active_record)
end
def teardown
super
ActiveRecord::LogSubscriber.log_subscribers.pop
ActiveRecord::Base.logger = @old_logger
ActiveRecord::IdentityMap.enabled = @using_identity_map
end
def set_logger(logger)
ActiveRecord::Base.logger = logger
end
2011-03-28 18:27:37 -04:00
def test_schema_statements_are_ignored
event = Struct.new(:duration, :payload)
logger = Class.new(ActiveRecord::LogSubscriber) {
attr_accessor :debugs
def initialize
@debugs = []
super
end
def debug message
@debugs << message
end
}.new
assert_equal 0, logger.debugs.length
logger.sql(event.new(0, { :sql => 'hi mom!' }))
assert_equal 1, logger.debugs.length
logger.sql(event.new(0, { :sql => 'hi mom!', :name => 'foo' }))
assert_equal 2, logger.debugs.length
logger.sql(event.new(0, { :sql => 'hi mom!', :name => 'SCHEMA' }))
assert_equal 2, logger.debugs.length
end
def test_basic_query_logging
Developer.all
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
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
def test_cached_queries
ActiveRecord::Base.cache do
Developer.all
Developer.all
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
def test_basic_query_doesnt_log_when_level_is_not_debug
@logger.level = INFO
Developer.all
wait
assert_equal 0, @logger.logged(:debug).size
end
def test_cached_queries_doesnt_log_when_level_is_not_debug
@logger.level = INFO
ActiveRecord::Base.cache do
Developer.all
Developer.all
end
wait
assert_equal 0, @logger.logged(:debug).size
end
def test_initializes_runtime
Thread.new { assert_equal 0, ActiveRecord::LogSubscriber.runtime }.join
end
2011-05-02 03:00:54 -04:00
def test_log
ActiveRecord::IdentityMap.use do
Post.find 1
Post.find 1
end
wait
assert_match(/From Identity Map/, @logger.logged(:debug).last)
end
end