class TestDefaultAutosaveAssociationOnABelongsToAssociation

Public Instance Methods

test_assignment_before_either_saved() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 268
def test_assignment_before_either_saved
  final_cut = Client.new("name" => "Final Cut")
  apple = Firm.new("name" => "Apple")
  final_cut.firm = apple
  assert !final_cut.persisted?
  assert !apple.persisted?
  assert final_cut.save
  assert final_cut.persisted?
  assert apple.persisted?
  assert_equal apple, final_cut.firm
  final_cut.association(:firm).reload
  assert_equal apple, final_cut.firm
end
test_assignment_before_parent_saved() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 254
def test_assignment_before_parent_saved
  client = Client.first
  apple = Firm.new("name" => "Apple")
  client.firm = apple
  assert_equal apple, client.firm
  assert !apple.persisted?
  assert client.save
  assert apple.save
  assert apple.persisted?
  assert_equal apple, client.firm
  client.association(:firm).reload
  assert_equal apple, client.firm
end
test_build_and_then_save_parent_should_not_reload_target() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 370
def test_build_and_then_save_parent_should_not_reload_target
  client = Client.first
  apple = client.build_firm(name: "Apple")
  client.save!
  assert_no_queries { assert_equal apple, client.firm }
end
test_save_fails_for_invalid_belongs_to() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 233
def test_save_fails_for_invalid_belongs_to
  # Oracle saves empty string as NULL therefore :message changed to one space
  assert log = AuditLog.create(developer_id: 0, message: " ")

  log.developer = Developer.new
  assert !log.developer.valid?
  assert !log.valid?
  assert !log.save
  assert_equal ["is invalid"], log.errors["developer"]
end
test_save_succeeds_for_invalid_belongs_to_with_validate_false() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 244
def test_save_succeeds_for_invalid_belongs_to_with_validate_false
  # Oracle saves empty string as NULL therefore :message changed to one space
  assert log = AuditLog.create(developer_id: 0, message: " ")

  log.unvalidated_developer = Developer.new
  assert !log.unvalidated_developer.valid?
  assert log.valid?
  assert log.save
end
test_should_save_parent_but_not_invalid_child() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 222
def test_should_save_parent_but_not_invalid_child
  client = Client.new(name: "Joe (the Plumber)")
  assert client.valid?

  client.build_firm
  assert !client.firm.valid?

  assert client.save
  assert !client.firm.persisted?
end
test_store_association_in_two_relations_with_one_save() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 302
def test_store_association_in_two_relations_with_one_save
  num_orders = Order.count
  num_customers = Customer.count
  order = Order.new

  customer = order.billing = order.shipping = Customer.new
  assert order.save
  assert_equal customer, order.billing
  assert_equal customer, order.shipping

  order.reload

  assert_equal customer, order.billing
  assert_equal customer, order.shipping

  assert_equal num_orders + 1, Order.count
  assert_equal num_customers + 1, Customer.count
end
test_store_association_in_two_relations_with_one_save_in_existing_object() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 321
def test_store_association_in_two_relations_with_one_save_in_existing_object
  num_orders = Order.count
  num_customers = Customer.count
  order = Order.create

  customer = order.billing = order.shipping = Customer.new
  assert order.save
  assert_equal customer, order.billing
  assert_equal customer, order.shipping

  order.reload

  assert_equal customer, order.billing
  assert_equal customer, order.shipping

  assert_equal num_orders + 1, Order.count
  assert_equal num_customers + 1, Customer.count
end
test_store_association_in_two_relations_with_one_save_in_existing_object_with_values() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 340
def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
  num_orders = Order.count
  num_customers = Customer.count
  order = Order.create

  customer = order.billing = order.shipping = Customer.new
  assert order.save
  assert_equal customer, order.billing
  assert_equal customer, order.shipping

  order.reload

  customer = order.billing = order.shipping = Customer.new

  assert order.save
  order.reload

  assert_equal customer, order.billing
  assert_equal customer, order.shipping

  assert_equal num_orders + 1, Order.count
  assert_equal num_customers + 2, Customer.count
end
test_store_association_with_a_polymorphic_relationship() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 364
def test_store_association_with_a_polymorphic_relationship
  num_tagging = Tagging.count
  tags(:misc).create_tagging(taggable: posts(:thinking))
  assert_equal num_tagging + 1, Tagging.count
end
test_store_two_association_with_one_save() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 282
def test_store_two_association_with_one_save
  num_orders = Order.count
  num_customers = Customer.count
  order = Order.new

  customer1 = order.billing = Customer.new
  customer2 = order.shipping = Customer.new
  assert order.save
  assert_equal customer1, order.billing
  assert_equal customer2, order.shipping

  order.reload

  assert_equal customer1, order.billing
  assert_equal customer2, order.shipping

  assert_equal num_orders + 1, Order.count
  assert_equal num_customers + 2, Customer.count
end
test_validation_does_not_validate_stale_association_target() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 377
def test_validation_does_not_validate_stale_association_target
  valid_developer   = Developer.create!(name: "Dude", salary: 50_000)
  invalid_developer = Developer.new()

  auditlog = AuditLog.new(message: "foo")
  auditlog.developer    = invalid_developer
  auditlog.developer_id = valid_developer.id

  assert auditlog.valid?
end