2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2010-01-12 14:00:53 -05:00
|
|
|
require "models/developer"
|
2011-05-02 03:00:54 -04:00
|
|
|
require "models/post"
|
2010-06-24 07:23:43 -04:00
|
|
|
require "active_support/log_subscriber/test_helper"
|
2010-01-12 14:00:53 -05:00
|
|
|
|
2010-07-14 19:35:52 -04:00
|
|
|
class LogSubscriberTest < ActiveRecord::TestCase
|
2010-06-24 07:23:43 -04:00
|
|
|
include ActiveSupport::LogSubscriber::TestHelper
|
2011-12-20 00:05:28 -05:00
|
|
|
include ActiveSupport::Logger::Severity
|
2010-01-12 14:00:53 -05:00
|
|
|
|
2011-05-02 03:00:54 -04:00
|
|
|
fixtures :posts
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
def setup
|
2010-03-04 05:20:01 -05:00
|
|
|
@old_logger = ActiveRecord::Base.logger
|
2011-04-05 16:01:54 -04:00
|
|
|
@using_identity_map = ActiveRecord::IdentityMap.enabled?
|
|
|
|
ActiveRecord::IdentityMap.enabled = false
|
2011-12-08 04:06:15 -05:00
|
|
|
Developer.primary_key
|
2010-01-12 14:00:53 -05:00
|
|
|
super
|
2010-06-24 07:23:43 -04:00
|
|
|
ActiveRecord::LogSubscriber.attach_to(:active_record)
|
2010-01-12 14:00:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
super
|
2010-06-24 07:23:43 -04:00
|
|
|
ActiveRecord::LogSubscriber.log_subscribers.pop
|
2010-01-12 14:00:53 -05:00
|
|
|
ActiveRecord::Base.logger = @old_logger
|
2011-04-05 16:01:54 -04:00
|
|
|
ActiveRecord::IdentityMap.enabled = @using_identity_map
|
2010-01-12 14:00:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_logger(logger)
|
2010-01-12 18:41:04 -05:00
|
|
|
ActiveRecord::Base.logger = logger
|
2010-01-12 14:00:53 -05:00
|
|
|
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
|
|
|
|
|
2010-01-12 14:00:53 -05:00
|
|
|
def test_basic_query_logging
|
|
|
|
Developer.all
|
|
|
|
wait
|
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
2010-03-15 21:09:33 -04:00
|
|
|
assert_match(/Developer Load/, @logger.logged(:debug).last)
|
2010-02-15 10:57:45 -05:00
|
|
|
assert_match(/SELECT .*?FROM .?developers.?/i, @logger.logged(:debug).last)
|
2011-06-22 16:41:34 -04:00
|
|
|
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)
|
2010-01-12 14:00:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_cached_queries
|
|
|
|
ActiveRecord::Base.cache do
|
|
|
|
Developer.all
|
|
|
|
Developer.all
|
|
|
|
end
|
|
|
|
wait
|
|
|
|
assert_equal 2, @logger.logged(:debug).size
|
2010-03-15 21:09:33 -04:00
|
|
|
assert_match(/CACHE/, @logger.logged(:debug).last)
|
2010-02-15 10:57:45 -05:00
|
|
|
assert_match(/SELECT .*?FROM .?developers.?/i, @logger.logged(:debug).last)
|
2010-01-12 14:00:53 -05:00
|
|
|
end
|
2010-07-19 16:28:51 -04:00
|
|
|
|
|
|
|
def test_basic_query_doesnt_log_when_level_is_not_debug
|
2010-07-20 16:00:12 -04:00
|
|
|
@logger.level = INFO
|
2010-07-19 16:28:51 -04:00
|
|
|
Developer.all
|
|
|
|
wait
|
|
|
|
assert_equal 0, @logger.logged(:debug).size
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cached_queries_doesnt_log_when_level_is_not_debug
|
2010-07-20 16:00:12 -04:00
|
|
|
@logger.level = INFO
|
2010-07-19 16:28:51 -04:00
|
|
|
ActiveRecord::Base.cache do
|
|
|
|
Developer.all
|
|
|
|
Developer.all
|
|
|
|
end
|
|
|
|
wait
|
|
|
|
assert_equal 0, @logger.logged(:debug).size
|
|
|
|
end
|
2010-07-26 13:14:24 -04:00
|
|
|
|
|
|
|
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
|
2010-03-15 21:09:33 -04:00
|
|
|
end
|