class TestAutosaveAssociationOnAHasOneAssociation

Public Instance Methods

setup() click to toggle source
Calls superclass method
# File activerecord/test/cases/autosave_association_test.rb, line 1111
def setup
  super
  @pirate = Pirate.create(catchphrase: "Don' botharrr talkin' like one, savvy?")
  @ship = @pirate.create_ship(name: "Nights Dirty Lightning")
end
test_changed_for_autosave_should_handle_cycles() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1130
def test_changed_for_autosave_should_handle_cycles
  @ship.pirate = @pirate
  assert_queries(0) { @ship.save! }

  @parrot = @pirate.parrots.create(name: "some_name")
  @parrot.name = "changed_name"
  assert_queries(1) { @ship.save! }
  assert_queries(0) { @ship.save! }
end
test_mark_for_destruction_is_ignored_without_autosave_true() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1243
def test_mark_for_destruction_is_ignored_without_autosave_true
  ship = ShipWithoutNestedAttributes.new(name: "The Black Flag")
  ship.parts.build.mark_for_destruction

  assert_not ship.valid?
end
test_should_allow_to_bypass_validations_on_associated_models_at_any_depth() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1185
def test_should_allow_to_bypass_validations_on_associated_models_at_any_depth
  2.times { |i| @pirate.ship.parts.create!(name: "part #{i}") }

  @pirate.catchphrase = ""
  @pirate.ship.name = ""
  @pirate.ship.parts.each { |part| part.name = "" }
  @pirate.save(validate: false)

  values = [@pirate.reload.catchphrase, @pirate.ship.name, *@pirate.ship.parts.map(&:name)]
  # Oracle saves empty string as NULL
  if current_adapter?(:OracleAdapter)
    assert_equal [nil, nil, nil, nil], values
  else
    assert_equal ["", "", "", ""], values
  end
end
test_should_automatically_save_bang_the_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1140
def test_should_automatically_save_bang_the_associated_model
  @pirate.ship.name = "The Vile Insanity"
  @pirate.save!
  assert_equal "The Vile Insanity", @pirate.reload.ship.name
end
test_should_automatically_save_the_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1124
def test_should_automatically_save_the_associated_model
  @pirate.ship.name = "The Vile Insanity"
  @pirate.save
  assert_equal "The Vile Insanity", @pirate.reload.ship.name
end
test_should_automatically_validate_the_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1146
def test_should_automatically_validate_the_associated_model
  @pirate.ship.name = ""
  assert @pirate.invalid?
  assert @pirate.errors[:"ship.name"].any?
end
test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1152
def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid
  @pirate.ship.name   = nil
  @pirate.catchphrase = nil
  assert @pirate.invalid?
  assert @pirate.errors[:"ship.name"].any?
  assert @pirate.errors[:catchphrase].any?
end
test_should_not_ignore_different_error_messages_on_the_same_attribute() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1160
def test_should_not_ignore_different_error_messages_on_the_same_attribute
  old_validators = Ship._validators.deep_dup
  old_callbacks = Ship._validate_callbacks.deep_dup
  Ship.validates_format_of :name, with: /\w/
  @pirate.ship.name   = ""
  @pirate.catchphrase = nil
  assert @pirate.invalid?
  assert_equal ["can't be blank", "is invalid"], @pirate.errors[:"ship.name"]
ensure
  Ship._validators = old_validators if old_validators
  Ship._validate_callbacks = old_callbacks if old_callbacks
end
test_should_not_load_the_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1239
def test_should_not_load_the_associated_model
  assert_queries(1) { @pirate.catchphrase = "Arr"; @pirate.save! }
end
test_should_not_save_and_return_false_if_a_callback_cancelled_saving() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1209
def test_should_not_save_and_return_false_if_a_callback_cancelled_saving
  pirate = Pirate.new(catchphrase: "Arr")
  ship = pirate.build_ship(name: "The Vile Insanity")
  ship.cancel_save_from_callback = true

  assert_no_difference "Pirate.count" do
    assert_no_difference "Ship.count" do
      assert !pirate.save
    end
  end
end
test_should_rollback_any_changes_if_an_exception_occurred_while_saving() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1221
def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
  before = [@pirate.catchphrase, @pirate.ship.name]

  @pirate.catchphrase = "Arr"
  @pirate.ship.name = "The Vile Insanity"

  # Stub the save method of the @pirate.ship instance to raise an exception
  class << @pirate.ship
    def save(*args)
      super
      raise "Oh noes!"
    end
  end

  assert_raise(RuntimeError) { assert !@pirate.save }
  assert_equal before, [@pirate.reload.catchphrase, @pirate.ship.name]
end
test_should_still_allow_to_bypass_validations_on_the_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1173
def test_should_still_allow_to_bypass_validations_on_the_associated_model
  @pirate.catchphrase = ""
  @pirate.ship.name = ""
  @pirate.save(validate: false)
  # Oracle saves empty string as NULL
  if current_adapter?(:OracleAdapter)
    assert_equal [nil, nil], [@pirate.reload.catchphrase, @pirate.ship.name]
  else
    assert_equal ["", ""], [@pirate.reload.catchphrase, @pirate.ship.name]
  end
end
test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1202
def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
  @pirate.ship.name = ""
  assert_raise(ActiveRecord::RecordInvalid) do
    @pirate.save!
  end
end
test_should_still_work_without_an_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1117
def test_should_still_work_without_an_associated_model
  @ship.destroy
  @pirate.reload.catchphrase = "Arr"
  @pirate.save
  assert_equal "Arr", @pirate.reload.catchphrase
end