class TestDestroyAsPartOfAutosaveAssociation

Public Instance Methods

test_a_child_marked_for_destruction_should_not_be_destroyed_twice() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 776
def test_a_child_marked_for_destruction_should_not_be_destroyed_twice
  @pirate.ship.mark_for_destruction
  assert @pirate.save
  class << @pirate.ship
    def destroy; raise "Should not be called" end
  end
  assert @pirate.save
end
test_a_child_marked_for_destruction_should_not_be_destroyed_twice_while_saving_habtm() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1044
def test_a_child_marked_for_destruction_should_not_be_destroyed_twice_while_saving_habtm
  @pirate.parrots.create!(name: "parrots_1")

  @pirate.parrots.each(&:mark_for_destruction)
  assert @pirate.save

  Pirate.transaction do
    assert_queries(0) do
      assert @pirate.save
    end
  end
end
test_a_child_marked_for_destruction_should_not_be_destroyed_twice_while_saving_has_many() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 918
def test_a_child_marked_for_destruction_should_not_be_destroyed_twice_while_saving_has_many
  @pirate.birds.create!(name: "birds_1")

  @pirate.birds.each(&:mark_for_destruction)
  assert @pirate.save

  @pirate.birds.each { |bird| bird.expects(:destroy).never }
  assert @pirate.save
end
test_a_marked_for_destruction_record_should_not_be_be_marked_after_reload() click to toggle source

reload

# File activerecord/test/cases/autosave_association_test.rb, line 744
def test_a_marked_for_destruction_record_should_not_be_be_marked_after_reload
  @pirate.mark_for_destruction
  @pirate.ship.mark_for_destruction

  assert !@pirate.reload.marked_for_destruction?
  assert !@pirate.ship.reload.marked_for_destruction?
end
test_a_parent_marked_for_destruction_should_not_be_destroyed_twice() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 837
def test_a_parent_marked_for_destruction_should_not_be_destroyed_twice
  @ship.pirate.mark_for_destruction
  assert @ship.save
  class << @ship.pirate
    def destroy; raise "Should not be called" end
  end
  assert @ship.save
end
test_should_destroy_a_child_association_as_part_of_the_save_transaction_if_it_was_marked_for_destruction() click to toggle source

has_one

# File activerecord/test/cases/autosave_association_test.rb, line 753
def test_should_destroy_a_child_association_as_part_of_the_save_transaction_if_it_was_marked_for_destruction
  assert !@pirate.ship.marked_for_destruction?

  @pirate.ship.mark_for_destruction
  id = @pirate.ship.id

  assert @pirate.ship.marked_for_destruction?
  assert Ship.find_by_id(id)

  @pirate.save
  assert_nil @pirate.reload.ship
  assert_nil Ship.find_by_id(id)
end
test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_it_was_marked_for_destruction() click to toggle source

belongs_to

# File activerecord/test/cases/autosave_association_test.rb, line 814
def test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_it_was_marked_for_destruction
  assert !@ship.pirate.marked_for_destruction?

  @ship.pirate.mark_for_destruction
  id = @ship.pirate.id

  assert @ship.pirate.marked_for_destruction?
  assert Pirate.find_by_id(id)

  @ship.save
  assert_nil @ship.reload.pirate
  assert_nil Pirate.find_by_id(id)
end
test_should_destroy_habtm_as_part_of_the_save_transaction_if_they_were_marked_for_destruction() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1003
def test_should_destroy_habtm_as_part_of_the_save_transaction_if_they_were_marked_for_destruction
  2.times { |i| @pirate.parrots.create!(name: "parrots_#{i}") }

  assert !@pirate.parrots.any?(&:marked_for_destruction?)
  @pirate.parrots.each(&:mark_for_destruction)

  assert_no_difference "Parrot.count" do
    @pirate.save
  end

  assert @pirate.reload.parrots.empty?

  join_records = Pirate.connection.select_all("SELECT * FROM parrots_pirates WHERE pirate_id = #{@pirate.id}")
  assert join_records.empty?
end
test_should_destroy_has_many_as_part_of_the_save_transaction_if_they_were_marked_for_destruction() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 871
def test_should_destroy_has_many_as_part_of_the_save_transaction_if_they_were_marked_for_destruction
  2.times { |i| @pirate.birds.create!(name: "birds_#{i}") }

  assert !@pirate.birds.any?(&:marked_for_destruction?)

  @pirate.birds.each(&:mark_for_destruction)
  klass = @pirate.birds.first.class
  ids = @pirate.birds.map(&:id)

  assert @pirate.birds.all?(&:marked_for_destruction?)
  ids.each { |id| assert klass.find_by_id(id) }

  @pirate.save
  assert @pirate.reload.birds.empty?
  ids.each { |id| assert_nil klass.find_by_id(id) }
end
test_should_not_resave_destroyed_association() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 888
def test_should_not_resave_destroyed_association
  @pirate.birds.create!(name: :parrot)
  @pirate.birds.first.destroy
  @pirate.save!
  assert @pirate.reload.birds.empty?
end
test_should_not_save_changed_has_one_unchanged_object_if_child_is_saved() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 808
def test_should_not_save_changed_has_one_unchanged_object_if_child_is_saved
  @pirate.ship.expects(:save).never
  assert @pirate.save
end
test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_child() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 785
def test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_child
  # Stub the save method of the @pirate.ship instance to destroy and then raise an exception
  class << @pirate.ship
    def save(*args)
      super
      destroy
      raise "Oh noes!"
    end
  end

  @ship.pirate.catchphrase = "Changed Catchphrase"
  @ship.name_will_change!

  assert_raise(RuntimeError) { assert !@pirate.save }
  assert_not_nil @pirate.reload.ship
end
test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_parent() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 846
def test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_parent
  # Stub the save method of the @ship.pirate instance to destroy and then raise an exception
  class << @ship.pirate
    def save(*args)
      super
      destroy
      raise "Oh noes!"
    end
  end

  @ship.pirate.catchphrase = "Changed Catchphrase"

  assert_raise(RuntimeError) { assert !@ship.save }
  assert_not_nil @ship.reload.pirate
end
test_should_rollback_destructions_if_an_exception_occurred_while_saving_habtm() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1057
def test_should_rollback_destructions_if_an_exception_occurred_while_saving_habtm
  2.times { |i| @pirate.parrots.create!(name: "parrots_#{i}") }
  before = @pirate.parrots.map { |c| c.mark_for_destruction ; c }

  class << @pirate.association(:parrots)
    def destroy(*args)
      super
      raise "Oh noes!"
    end
  end

  assert_raise(RuntimeError) { assert !@pirate.save }
  assert_equal before, @pirate.reload.parrots
end
test_should_rollback_destructions_if_an_exception_occurred_while_saving_has_many() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 928
def test_should_rollback_destructions_if_an_exception_occurred_while_saving_has_many
  2.times { |i| @pirate.birds.create!(name: "birds_#{i}") }
  before = @pirate.birds.map { |c| c.mark_for_destruction ; c }

  # Stub the destroy method of the second child to raise an exception
  class << before.last
    def destroy(*args)
      super
      raise "Oh noes!"
    end
  end

  assert_raise(RuntimeError) { assert !@pirate.save }
  assert_equal before, @pirate.reload.birds
end
test_should_save_changed_child_objects_if_parent_is_saved() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 862
def test_should_save_changed_child_objects_if_parent_is_saved
  @pirate = @ship.create_pirate(catchphrase: "Don' botharrr talkin' like one, savvy?")
  @parrot = @pirate.parrots.create!(name: "Posideons Killer")
  @parrot.name = "NewName"
  @ship.save

  assert_equal "NewName", @parrot.reload.name
end
test_should_save_changed_has_one_changed_object_if_child_is_saved() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 802
def test_should_save_changed_has_one_changed_object_if_child_is_saved
  @pirate.ship.name = "NewName"
  assert @pirate.save
  assert_equal "NewName", @pirate.ship.reload.name
end
test_should_save_new_record_that_has_same_value_as_existing_record_marked_for_destruction_on_field_that_has_unique_index() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 954
def test_should_save_new_record_that_has_same_value_as_existing_record_marked_for_destruction_on_field_that_has_unique_index
  Bird.connection.add_index :birds, :name, unique: true

  3.times { |i| @pirate.birds.create(name: "unique_birds_#{i}") }

  @pirate.birds[0].mark_for_destruction
  @pirate.birds.build(name: @pirate.birds[0].name)
  @pirate.save!

  assert_equal 3, @pirate.birds.reload.length
ensure
  Bird.connection.remove_index :birds, column: :name
end
test_should_skip_validation_on_a_child_association_if_marked_for_destruction() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 767
def test_should_skip_validation_on_a_child_association_if_marked_for_destruction
  @pirate.ship.name = ""
  assert !@pirate.valid?

  @pirate.ship.mark_for_destruction
  @pirate.ship.expects(:valid?).never
  assert_difference("Ship.count", -1) { @pirate.save! }
end
test_should_skip_validation_on_a_parent_association_if_marked_for_destruction() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 828
def test_should_skip_validation_on_a_parent_association_if_marked_for_destruction
  @ship.pirate.catchphrase = ""
  assert !@ship.valid?

  @ship.pirate.mark_for_destruction
  @ship.pirate.expects(:valid?).never
  assert_difference("Pirate.count", -1) { @ship.save! }
end
test_should_skip_validation_on_habtm_if_destroyed() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1034
def test_should_skip_validation_on_habtm_if_destroyed
  @pirate.parrots.create!(name: "parrots_1")

  @pirate.parrots.each { |parrot| parrot.name = "" }
  assert !@pirate.valid?

  @pirate.parrots.each(&:destroy)
  assert @pirate.valid?
end
test_should_skip_validation_on_habtm_if_marked_for_destruction() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1019
def test_should_skip_validation_on_habtm_if_marked_for_destruction
  2.times { |i| @pirate.parrots.create!(name: "parrots_#{i}") }

  @pirate.parrots.each { |parrot| parrot.name = "" }
  assert !@pirate.valid?

  @pirate.parrots.each do |parrot|
    parrot.mark_for_destruction
    parrot.expects(:valid?).never
  end

  @pirate.save!
  assert @pirate.reload.parrots.empty?
end
test_should_skip_validation_on_has_many_if_destroyed() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 908
def test_should_skip_validation_on_has_many_if_destroyed
  @pirate.birds.create!(name: "birds_1")

  @pirate.birds.each { |bird| bird.name = "" }
  assert !@pirate.valid?

  @pirate.birds.each(&:destroy)
  assert @pirate.valid?
end
test_should_skip_validation_on_has_many_if_marked_for_destruction() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 895
def test_should_skip_validation_on_has_many_if_marked_for_destruction
  2.times { |i| @pirate.birds.create!(name: "birds_#{i}") }

  @pirate.birds.each { |bird| bird.name = "" }
  assert !@pirate.valid?

  @pirate.birds.each do |bird|
    bird.mark_for_destruction
    bird.expects(:valid?).never
  end
  assert_difference("Bird.count", -2) { @pirate.save! }
end
test_when_new_record_a_child_marked_for_destruction_should_not_affect_other_records_from_saving() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 944
def test_when_new_record_a_child_marked_for_destruction_should_not_affect_other_records_from_saving
  @pirate = @ship.build_pirate(catchphrase: "Arr' now I shall keep me eye on you matey!") # new record

  3.times { |i| @pirate.birds.build(name: "birds_#{i}") }
  @pirate.birds[1].mark_for_destruction
  @pirate.save!

  assert_equal 2, @pirate.birds.reload.length
end