2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/notification"
|
|
|
|
require "models/user"
|
2015-02-18 17:55:48 -05:00
|
|
|
|
|
|
|
class SuppressorTest < ActiveRecord::TestCase
|
2015-06-04 00:00:20 -04:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
user = User.create! token: "asdf"
|
2015-06-04 00:00:20 -04:00
|
|
|
|
|
|
|
User.suppress do
|
2016-08-06 12:26:20 -04:00
|
|
|
user.update token: "ghjkl"
|
|
|
|
assert_equal "asdf", user.reload.token
|
2015-06-04 00:00:20 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
user.update! token: "zxcvbnm"
|
|
|
|
assert_equal "asdf", user.reload.token
|
2015-06-04 00:00:20 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
user.token = "qwerty"
|
2015-06-04 00:00:20 -04:00
|
|
|
user.save
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "asdf", user.reload.token
|
2015-06-04 00:00:20 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
user.token = "uiop"
|
2015-06-04 00:00:20 -04:00
|
|
|
user.save!
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "asdf", user.reload.token
|
2015-06-04 00:00:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_suppresses_create_in_callback
|
2015-02-18 17:55:48 -05:00
|
|
|
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
|
2016-02-24 13:45:36 -05:00
|
|
|
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
|
2015-02-18 17:55:48 -05:00
|
|
|
end
|
|
|
|
end
|
2016-05-13 10:14:51 -04:00
|
|
|
|
|
|
|
def test_suppresses_when_nested_multiple_times
|
|
|
|
assert_no_difference -> { Notification.count } do
|
|
|
|
Notification.suppress do
|
2016-08-16 03:30:11 -04:00
|
|
|
Notification.suppress {}
|
2016-05-13 10:14:51 -04:00
|
|
|
Notification.create
|
|
|
|
Notification.create!
|
|
|
|
Notification.new.save
|
|
|
|
Notification.new.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-02-18 17:55:48 -05:00
|
|
|
end
|