aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/subscriber_test.rb
blob: ce91d9385d32bd1ce137d0358feecfad8d35d7cd (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
require "cases/helper"
require "models/developer"
require "rails/subscriber/test_helper"
require "active_record/railties/subscriber"

module SubscriberTest
  Rails::Subscriber.add(:active_record, ActiveRecord::Railties::Subscriber.new)

  def setup
    @old_logger = ActiveRecord::Base.logger
    super
  end

  def teardown
    super
    ActiveRecord::Base.logger = @old_logger
  end

  def set_logger(logger)
    ActiveRecord::Base.logger = logger
  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.?/, @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.?/, @logger.logged(:debug).last
  end

  class SyncSubscriberTest < ActiveSupport::TestCase
    include Rails::Subscriber::SyncTestHelper
    include SubscriberTest
  end

  class AsyncSubscriberTest < ActiveSupport::TestCase
    include Rails::Subscriber::AsyncTestHelper
    include SubscriberTest
  end
end