Skip to content

Commit

Permalink
Cleanup duplicated setup of callbacks in transactions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosantoniodasilva committed Jan 16, 2014
1 parent 7b11b06 commit da4b5e8
Showing 1 changed file with 19 additions and 48 deletions.
67 changes: 19 additions & 48 deletions activerecord/test/cases/transaction_callbacks_test.rb
Expand Up @@ -79,37 +79,22 @@ def test_call_after_commit_after_transaction_commits
end

def test_only_call_after_commit_on_update_after_transaction_commits_for_existing_record
@first.after_commit_block(:create){|r| r.history << :commit_on_create}
@first.after_commit_block(:update){|r| r.history << :commit_on_update}
@first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
@first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
@first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
@first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
add_transaction_execution_blocks @first

@first.save!
assert_equal [:commit_on_update], @first.history
end

def test_only_call_after_commit_on_destroy_after_transaction_commits_for_destroyed_record
@first.after_commit_block(:create){|r| r.history << :commit_on_create}
@first.after_commit_block(:update){|r| r.history << :commit_on_update}
@first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
@first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
@first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
@first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
add_transaction_execution_blocks @first

@first.destroy
assert_equal [:commit_on_destroy], @first.history
end

def test_only_call_after_commit_on_create_after_transaction_commits_for_new_record
@new_record = TopicWithCallbacks.new(:title => "New topic", :written_on => Date.today)
@new_record.after_commit_block(:create){|r| r.history << :commit_on_create}
@new_record.after_commit_block(:update){|r| r.history << :commit_on_update}
@new_record.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
@new_record.after_rollback_block(:create){|r| r.history << :rollback_on_create}
@new_record.after_rollback_block(:update){|r| r.history << :rollback_on_update}
@new_record.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
add_transaction_execution_blocks @new_record

@new_record.save!
assert_equal [:commit_on_create], @new_record.history
Expand All @@ -123,12 +108,7 @@ def test_only_call_after_commit_on_create_after_transaction_commits_for_new_reco
end

def test_only_call_after_commit_on_update_after_transaction_commits_for_existing_record_on_touch
@first.after_commit_block(:create){|r| r.history << :commit_on_create}
@first.after_commit_block(:update){|r| r.history << :commit_on_update}
@first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
@first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
@first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
@first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
add_transaction_execution_blocks @first

@first.touch
assert_equal [:commit_on_update], @first.history
Expand All @@ -147,12 +127,7 @@ def test_call_after_rollback_after_transaction_rollsback
end

def test_only_call_after_rollback_on_update_after_transaction_rollsback_for_existing_record
@first.after_commit_block(:create){|r| r.history << :commit_on_create}
@first.after_commit_block(:update){|r| r.history << :commit_on_update}
@first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
@first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
@first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
@first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
add_transaction_execution_blocks @first

Topic.transaction do
@first.save!
Expand All @@ -163,12 +138,7 @@ def test_only_call_after_rollback_on_update_after_transaction_rollsback_for_exis
end

def test_only_call_after_rollback_on_update_after_transaction_rollsback_for_existing_record_on_touch
@first.after_commit_block(:create){|r| r.history << :commit_on_create}
@first.after_commit_block(:update){|r| r.history << :commit_on_update}
@first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
@first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
@first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
@first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
add_transaction_execution_blocks @first

Topic.transaction do
@first.touch
Expand All @@ -179,12 +149,7 @@ def test_only_call_after_rollback_on_update_after_transaction_rollsback_for_exis
end

def test_only_call_after_rollback_on_destroy_after_transaction_rollsback_for_destroyed_record
@first.after_commit_block(:create){|r| r.history << :commit_on_create}
@first.after_commit_block(:update){|r| r.history << :commit_on_update}
@first.after_commit_block(:destroy){|r| r.history << :commit_on_update}
@first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
@first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
@first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
add_transaction_execution_blocks @first

Topic.transaction do
@first.destroy
Expand All @@ -196,12 +161,7 @@ def test_only_call_after_rollback_on_destroy_after_transaction_rollsback_for_des

def test_only_call_after_rollback_on_create_after_transaction_rollsback_for_new_record
@new_record = TopicWithCallbacks.new(:title => "New topic", :written_on => Date.today)
@new_record.after_commit_block(:create){|r| r.history << :commit_on_create}
@new_record.after_commit_block(:update){|r| r.history << :commit_on_update}
@new_record.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
@new_record.after_rollback_block(:create){|r| r.history << :rollback_on_create}
@new_record.after_rollback_block(:update){|r| r.history << :rollback_on_update}
@new_record.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
add_transaction_execution_blocks @new_record

Topic.transaction do
@new_record.save!
Expand Down Expand Up @@ -324,6 +284,17 @@ def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_sh

assert flag
end

private

def add_transaction_execution_blocks(record)
record.after_commit_block(:create) { |r| r.history << :commit_on_create }
record.after_commit_block(:update) { |r| r.history << :commit_on_update }
record.after_commit_block(:destroy) { |r| r.history << :commit_on_destroy }
record.after_rollback_block(:create) { |r| r.history << :rollback_on_create }
record.after_rollback_block(:update) { |r| r.history << :rollback_on_update }
record.after_rollback_block(:destroy) { |r| r.history << :rollback_on_destroy }
end
end

class CallbacksOnMultipleActionsTest < ActiveRecord::TestCase
Expand Down

0 comments on commit da4b5e8

Please sign in to comment.