diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-04-17 11:06:43 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2019-04-17 11:06:43 +0900 |
commit | ef21edbc45275f3be434732d14a284980a1a291b (patch) | |
tree | d208cb64f7ff06562c5966171d13f41bb9c7d1f3 /activerecord/test | |
parent | 315dcb6751c8e10f40189a9838f418e06df45560 (diff) | |
parent | 63ff495bdf90e0ab20114a49db5cffe3cb9ef2fd (diff) | |
download | rails-ef21edbc45275f3be434732d14a284980a1a291b.tar.gz rails-ef21edbc45275f3be434732d14a284980a1a291b.tar.bz2 rails-ef21edbc45275f3be434732d14a284980a1a291b.zip |
Merge pull request #35987 from kamipo/fix_dirty_tracking_after_rollback
Fix dirty tracking after rollback.
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/transactions_test.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index 7bad3de343..6795996cca 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -18,6 +18,65 @@ class TransactionTest < ActiveRecord::TestCase @first, @second = Topic.find(1, 2).sort_by(&:id) end + def test_rollback_dirty_changes + topic = topics(:fifth) + + ActiveRecord::Base.transaction do + topic.update(title: "Ruby on Rails") + raise ActiveRecord::Rollback + end + + title_change = ["The Fifth Topic of the day", "Ruby on Rails"] + assert_equal title_change, topic.changes["title"] + end + + def test_rollback_dirty_changes_multiple_saves + topic = topics(:fifth) + + ActiveRecord::Base.transaction do + topic.update(title: "Ruby on Rails") + topic.update(title: "Another Title") + raise ActiveRecord::Rollback + end + + title_change = ["The Fifth Topic of the day", "Another Title"] + assert_equal title_change, topic.changes["title"] + end + + def test_rollback_dirty_changes_then_retry_save + topic = topics(:fifth) + + ActiveRecord::Base.transaction do + topic.update(title: "Ruby on Rails") + raise ActiveRecord::Rollback + end + + title_change = ["The Fifth Topic of the day", "Ruby on Rails"] + assert_equal title_change, topic.changes["title"] + + assert topic.save + + assert_equal title_change, topic.saved_changes["title"] + assert_equal topic.title, topic.reload.title + end + + def test_rollback_dirty_changes_then_retry_save_on_new_record + topic = Topic.new(title: "Ruby on Rails") + + ActiveRecord::Base.transaction do + topic.save + raise ActiveRecord::Rollback + end + + title_change = [nil, "Ruby on Rails"] + assert_equal title_change, topic.changes["title"] + + assert topic.save + + assert_equal title_change, topic.saved_changes["title"] + assert_equal topic.title, topic.reload.title + end + def test_persisted_in_a_model_with_custom_primary_key_after_failed_save movie = Movie.create assert_not_predicate movie, :persisted? |