aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/suppressor_test.rb
blob: b68f0033d9f9302e445407fb61afe8cac96353c8 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true

require "cases/helper"
require "models/notification"
require "models/user"

class SuppressorTest < ActiveRecord::TestCase
  def test_suppresses_create
    assert_no_difference -> { Notification.count } do
      Notification.suppress do
        Notification.create
        Notification.create!
        Notification.new.save
        Notification.new.save!
      end
    end
  end

  def test_suppresses_update
    user = User.create! token: "asdf"

    User.suppress do
      user.update token: "ghjkl"
      assert_equal "asdf", user.reload.token

      user.update! token: "zxcvbnm"
      assert_equal "asdf", user.reload.token

      user.token = "qwerty"
      user.save
      assert_equal "asdf", user.reload.token

      user.token = "uiop"
      user.save!
      assert_equal "asdf", user.reload.token
    end
  end

  def test_suppresses_create_in_callback
    assert_difference -> { User.count } do
      assert_no_difference -> { Notification.count } do
        Notification.suppress { UserWithNotification.create! }
      end
    end
  end

  def test_resumes_saving_after_suppression_complete
    Notification.suppress { UserWithNotification.create! }

    assert_difference -> { Notification.count } do
      Notification.create!(message: "New Comment")
    end
  end

  def test_suppresses_validations_on_create
    assert_no_difference -> { Notification.count } do
      Notification.suppress do
        User.create
        User.create!
        User.new.save
        User.new.save!
      end
    end
  end

  def test_suppresses_when_nested_multiple_times
    assert_no_difference -> { Notification.count } do
      Notification.suppress do
        Notification.suppress {}
        Notification.create
        Notification.create!
        Notification.new.save
        Notification.new.save!
      end
    end
  end
end