2011-06-06 14:17:44 -04:00
|
|
|
require 'cases/helper'
|
2009-02-27 07:50:24 -05:00
|
|
|
require 'models/bird'
|
2014-10-13 10:50:32 -04:00
|
|
|
require 'models/comment'
|
2009-02-27 07:50:24 -05:00
|
|
|
require 'models/company'
|
|
|
|
require 'models/customer'
|
|
|
|
require 'models/developer'
|
2009-10-01 02:17:50 -04:00
|
|
|
require 'models/invoice'
|
|
|
|
require 'models/line_item'
|
2009-02-27 07:50:24 -05:00
|
|
|
require 'models/order'
|
|
|
|
require 'models/parrot'
|
|
|
|
require 'models/person'
|
|
|
|
require 'models/pirate'
|
|
|
|
require 'models/post'
|
|
|
|
require 'models/reader'
|
|
|
|
require 'models/ship'
|
|
|
|
require 'models/ship_part'
|
2010-07-26 15:25:21 -04:00
|
|
|
require 'models/tag'
|
|
|
|
require 'models/tagging'
|
2009-02-27 07:50:24 -05:00
|
|
|
require 'models/treasure'
|
2010-10-22 04:28:53 -04:00
|
|
|
require 'models/eye'
|
2014-02-08 15:46:32 -05:00
|
|
|
require 'models/electron'
|
|
|
|
require 'models/molecule'
|
2014-09-15 04:18:24 -04:00
|
|
|
require 'models/member'
|
|
|
|
require 'models/member_detail'
|
|
|
|
require 'models/organization'
|
2009-02-27 07:50:24 -05:00
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
|
2014-02-20 14:25:48 -05:00
|
|
|
def test_autosave_validation
|
|
|
|
person = Class.new(ActiveRecord::Base) {
|
|
|
|
self.table_name = 'people'
|
|
|
|
validate :should_be_cool, :on => :create
|
|
|
|
def self.name; 'Person'; end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def should_be_cool
|
|
|
|
unless self.first_name == 'cool'
|
|
|
|
errors.add :first_name, "not cool"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
reference = Class.new(ActiveRecord::Base) {
|
|
|
|
self.table_name = "references"
|
|
|
|
def self.name; 'Reference'; end
|
|
|
|
belongs_to :person, autosave: true, class: person
|
|
|
|
}
|
|
|
|
|
|
|
|
u = person.create!(first_name: 'cool')
|
|
|
|
u.update_attributes!(first_name: 'nah') # still valid because validation only applies on 'create'
|
|
|
|
assert reference.create!(person: u).persisted?
|
|
|
|
end
|
|
|
|
|
2009-12-15 10:40:02 -05:00
|
|
|
def test_should_not_add_the_same_callbacks_multiple_times_for_has_one
|
|
|
|
assert_no_difference_when_adding_callbacks_twice_for Pirate, :ship
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_add_the_same_callbacks_multiple_times_for_belongs_to
|
|
|
|
assert_no_difference_when_adding_callbacks_twice_for Ship, :pirate
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_add_the_same_callbacks_multiple_times_for_has_many
|
|
|
|
assert_no_difference_when_adding_callbacks_twice_for Pirate, :birds
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_add_the_same_callbacks_multiple_times_for_has_and_belongs_to_many
|
|
|
|
assert_no_difference_when_adding_callbacks_twice_for Pirate, :parrots
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
private
|
|
|
|
|
2009-12-15 10:40:02 -05:00
|
|
|
def assert_no_difference_when_adding_callbacks_twice_for(model, association_name)
|
|
|
|
reflection = model.reflect_on_association(association_name)
|
|
|
|
assert_no_difference "callbacks_for_model(#{model.name}).length" do
|
|
|
|
model.send(:add_autosave_association_callbacks, reflection)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def callbacks_for_model(model)
|
2014-03-03 22:23:22 -05:00
|
|
|
model.instance_variables.grep(/_callbacks$/).flat_map do |ivar|
|
2009-12-15 10:40:02 -05:00
|
|
|
model.instance_variable_get(ivar)
|
2014-03-03 22:23:22 -05:00
|
|
|
end
|
2009-12-15 10:40:02 -05:00
|
|
|
end
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
2009-02-27 07:50:24 -05:00
|
|
|
class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
|
2009-05-18 19:17:35 -04:00
|
|
|
fixtures :companies, :accounts
|
|
|
|
|
2009-03-16 20:04:47 -04:00
|
|
|
def test_should_save_parent_but_not_invalid_child
|
|
|
|
firm = Firm.new(:name => 'GlobalMegaCorp')
|
|
|
|
assert firm.valid?
|
|
|
|
|
|
|
|
firm.build_account_using_primary_key
|
|
|
|
assert !firm.build_account_using_primary_key.valid?
|
|
|
|
|
|
|
|
assert firm.save
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !firm.account_using_primary_key.persisted?
|
2009-03-16 20:04:47 -04:00
|
|
|
end
|
|
|
|
|
2009-02-27 07:50:24 -05:00
|
|
|
def test_save_fails_for_invalid_has_one
|
2012-04-26 13:32:55 -04:00
|
|
|
firm = Firm.first
|
2009-02-27 07:50:24 -05:00
|
|
|
assert firm.valid?
|
|
|
|
|
2011-01-08 14:59:30 -05:00
|
|
|
firm.build_account
|
2009-02-27 07:50:24 -05:00
|
|
|
|
|
|
|
assert !firm.account.valid?
|
|
|
|
assert !firm.valid?
|
|
|
|
assert !firm.save
|
2009-03-20 13:36:22 -04:00
|
|
|
assert_equal ["is invalid"], firm.errors["account"]
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_save_succeeds_for_invalid_has_one_with_validate_false
|
2012-04-26 13:32:55 -04:00
|
|
|
firm = Firm.first
|
2009-02-27 07:50:24 -05:00
|
|
|
assert firm.valid?
|
|
|
|
|
2011-01-08 14:59:30 -05:00
|
|
|
firm.build_unvalidated_account
|
2009-02-27 07:50:24 -05:00
|
|
|
|
|
|
|
assert !firm.unvalidated_account.valid?
|
|
|
|
assert firm.valid?
|
|
|
|
assert firm.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_build_before_child_saved
|
|
|
|
firm = Firm.find(1)
|
|
|
|
|
2011-02-17 18:55:05 -05:00
|
|
|
account = firm.build_account("credit_limit" => 1000)
|
2009-02-27 07:50:24 -05:00
|
|
|
assert_equal account, firm.account
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !account.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert firm.save
|
|
|
|
assert_equal account, firm.account
|
2010-11-07 09:05:18 -05:00
|
|
|
assert account.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_build_before_either_saved
|
|
|
|
firm = Firm.new("name" => "GlobalMegaCorp")
|
|
|
|
|
|
|
|
firm.account = account = Account.new("credit_limit" => 1000)
|
|
|
|
assert_equal account, firm.account
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !account.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert firm.save
|
|
|
|
assert_equal account, firm.account
|
2010-11-07 09:05:18 -05:00
|
|
|
assert account.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assignment_before_parent_saved
|
|
|
|
firm = Firm.new("name" => "GlobalMegaCorp")
|
|
|
|
firm.account = a = Account.find(1)
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !firm.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert_equal a, firm.account
|
|
|
|
assert firm.save
|
|
|
|
assert_equal a, firm.account
|
|
|
|
assert_equal a, firm.account(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assignment_before_either_saved
|
|
|
|
firm = Firm.new("name" => "GlobalMegaCorp")
|
|
|
|
firm.account = a = Account.new("credit_limit" => 1000)
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !firm.persisted?
|
|
|
|
assert !a.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert_equal a, firm.account
|
|
|
|
assert firm.save
|
2010-11-07 09:05:18 -05:00
|
|
|
assert firm.persisted?
|
|
|
|
assert a.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert_equal a, firm.account
|
|
|
|
assert_equal a, firm.account(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_not_resaved_when_unchanged
|
2012-07-27 12:27:47 -04:00
|
|
|
firm = Firm.all.merge!(:includes => :account).first
|
2009-02-27 07:50:24 -05:00
|
|
|
firm.name += '-changed'
|
|
|
|
assert_queries(1) { firm.save! }
|
|
|
|
|
2012-04-26 13:32:55 -04:00
|
|
|
firm = Firm.first
|
|
|
|
firm.account = Account.first
|
2012-10-19 11:45:41 -04:00
|
|
|
assert_queries(Firm.partial_writes? ? 0 : 1) { firm.save! }
|
2009-02-27 07:50:24 -05:00
|
|
|
|
2012-04-26 13:32:55 -04:00
|
|
|
firm = Firm.first.dup
|
|
|
|
firm.account = Account.first
|
2009-02-27 07:50:24 -05:00
|
|
|
assert_queries(2) { firm.save! }
|
|
|
|
|
2012-04-26 13:32:55 -04:00
|
|
|
firm = Firm.first.dup
|
|
|
|
firm.account = Account.first.dup
|
2009-02-27 07:50:24 -05:00
|
|
|
assert_queries(2) { firm.save! }
|
|
|
|
end
|
2010-11-23 15:00:17 -05:00
|
|
|
|
2010-10-22 04:28:53 -04:00
|
|
|
def test_callbacks_firing_order_on_create
|
|
|
|
eye = Eye.create(:iris_attributes => {:color => 'honey'})
|
|
|
|
assert_equal [true, false], eye.after_create_callbacks_stack
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_callbacks_firing_order_on_update
|
2013-01-02 11:46:58 -05:00
|
|
|
eye = Eye.create(iris_attributes: {color: 'honey'})
|
|
|
|
eye.update(iris_attributes: {color: 'green'})
|
2010-10-22 04:28:53 -04:00
|
|
|
assert_equal [true, false], eye.after_update_callbacks_stack
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_callbacks_firing_order_on_save
|
2013-01-02 11:46:58 -05:00
|
|
|
eye = Eye.create(iris_attributes: {color: 'honey'})
|
2010-10-22 04:28:53 -04:00
|
|
|
assert_equal [false, false], eye.after_save_callbacks_stack
|
|
|
|
|
2013-01-02 11:46:58 -05:00
|
|
|
eye.update(iris_attributes: {color: 'blue'})
|
2010-10-22 04:28:53 -04:00
|
|
|
assert_equal [false, false, false, false], eye.after_save_callbacks_stack
|
|
|
|
end
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
|
2010-07-25 20:38:15 -04:00
|
|
|
fixtures :companies, :posts, :tags, :taggings
|
2009-05-18 19:17:35 -04:00
|
|
|
|
2009-03-16 20:04:47 -04:00
|
|
|
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
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !client.firm.persisted?
|
2009-03-16 20:04:47 -04:00
|
|
|
end
|
|
|
|
|
2009-02-27 07:50:24 -05:00
|
|
|
def test_save_fails_for_invalid_belongs_to
|
2009-03-22 18:05:41 -04:00
|
|
|
# Oracle saves empty string as NULL therefore :message changed to one space
|
|
|
|
assert log = AuditLog.create(:developer_id => 0, :message => " ")
|
2009-02-27 07:50:24 -05:00
|
|
|
|
|
|
|
log.developer = Developer.new
|
|
|
|
assert !log.developer.valid?
|
|
|
|
assert !log.valid?
|
|
|
|
assert !log.save
|
2009-03-20 13:36:22 -04:00
|
|
|
assert_equal ["is invalid"], log.errors["developer"]
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_save_succeeds_for_invalid_belongs_to_with_validate_false
|
2009-03-22 18:05:41 -04:00
|
|
|
# Oracle saves empty string as NULL therefore :message changed to one space
|
|
|
|
assert log = AuditLog.create(:developer_id => 0, :message=> " ")
|
2009-02-27 07:50:24 -05:00
|
|
|
|
|
|
|
log.unvalidated_developer = Developer.new
|
|
|
|
assert !log.unvalidated_developer.valid?
|
|
|
|
assert log.valid?
|
|
|
|
assert log.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assignment_before_parent_saved
|
2012-04-26 13:32:55 -04:00
|
|
|
client = Client.first
|
2009-02-27 07:50:24 -05:00
|
|
|
apple = Firm.new("name" => "Apple")
|
|
|
|
client.firm = apple
|
|
|
|
assert_equal apple, client.firm
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !apple.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert client.save
|
|
|
|
assert apple.save
|
2010-11-07 09:05:18 -05:00
|
|
|
assert apple.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert_equal apple, client.firm
|
|
|
|
assert_equal apple, client.firm(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assignment_before_either_saved
|
|
|
|
final_cut = Client.new("name" => "Final Cut")
|
|
|
|
apple = Firm.new("name" => "Apple")
|
|
|
|
final_cut.firm = apple
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !final_cut.persisted?
|
|
|
|
assert !apple.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert final_cut.save
|
2010-11-07 09:05:18 -05:00
|
|
|
assert final_cut.persisted?
|
|
|
|
assert apple.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert_equal apple, final_cut.firm
|
|
|
|
assert_equal apple, final_cut.firm(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-08-18 20:07:46 -04:00
|
|
|
assert_equal num_orders + 1, Order.count
|
|
|
|
assert_equal num_customers + 2, Customer.count
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-08-18 20:07:46 -04:00
|
|
|
assert_equal num_orders + 1, Order.count
|
|
|
|
assert_equal num_customers + 1, Customer.count
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-08-18 20:07:46 -04:00
|
|
|
assert_equal num_orders + 1, Order.count
|
|
|
|
assert_equal num_customers + 1, Customer.count
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-08-18 20:07:46 -04:00
|
|
|
assert_equal num_orders + 1, Order.count
|
|
|
|
assert_equal num_customers + 2, Customer.count
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
2010-07-25 20:38:15 -04:00
|
|
|
|
|
|
|
def test_store_association_with_a_polymorphic_relationship
|
|
|
|
num_tagging = Tagging.count
|
|
|
|
tags(:misc).create_tagging(:taggable => posts(:thinking))
|
2010-08-18 20:07:46 -04:00
|
|
|
assert_equal num_tagging + 1, Tagging.count
|
2010-07-25 20:38:15 -04:00
|
|
|
end
|
2011-01-02 09:28:53 -05:00
|
|
|
|
|
|
|
def test_build_and_then_save_parent_should_not_reload_target
|
2012-04-26 13:32:55 -04:00
|
|
|
client = Client.first
|
2011-01-02 09:28:53 -05:00
|
|
|
apple = client.build_firm(:name => "Apple")
|
|
|
|
client.save!
|
|
|
|
assert_no_queries { assert_equal apple, client.firm }
|
|
|
|
end
|
2011-11-27 05:47:45 -05:00
|
|
|
|
|
|
|
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
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
2014-02-08 15:46:32 -05:00
|
|
|
class TestDefaultAutosaveAssociationOnAHasManyAssociationWithAcceptsNestedAttributes < ActiveRecord::TestCase
|
|
|
|
def test_invalid_adding_with_nested_attributes
|
|
|
|
molecule = Molecule.new
|
|
|
|
valid_electron = Electron.new(name: 'electron')
|
|
|
|
invalid_electron = Electron.new
|
|
|
|
|
|
|
|
molecule.electrons = [valid_electron, invalid_electron]
|
|
|
|
molecule.save
|
|
|
|
|
|
|
|
assert_not invalid_electron.valid?
|
|
|
|
assert valid_electron.valid?
|
|
|
|
assert_not molecule.persisted?, 'Molecule should not be persisted when its electrons are invalid'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_valid_adding_with_nested_attributes
|
|
|
|
molecule = Molecule.new
|
|
|
|
valid_electron = Electron.new(name: 'electron')
|
|
|
|
|
|
|
|
molecule.electrons = [valid_electron]
|
|
|
|
molecule.save
|
|
|
|
|
|
|
|
assert valid_electron.valid?
|
|
|
|
assert molecule.persisted?
|
|
|
|
assert_equal 1, molecule.electrons.count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-27 07:50:24 -05:00
|
|
|
class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase
|
|
|
|
fixtures :companies, :people
|
|
|
|
|
|
|
|
def test_invalid_adding
|
|
|
|
firm = Firm.find(1)
|
|
|
|
assert !(firm.clients_of_firm << c = Client.new)
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !c.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert !firm.valid?
|
|
|
|
assert !firm.save
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !c.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_adding_before_save
|
|
|
|
new_firm = Firm.new("name" => "A New Firm, Inc")
|
|
|
|
new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")])
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !c.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert !c.valid?
|
|
|
|
assert !new_firm.valid?
|
|
|
|
assert !new_firm.save
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !c.persisted?
|
|
|
|
assert !new_firm.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_adding_with_validate_false
|
2012-04-26 13:32:55 -04:00
|
|
|
firm = Firm.first
|
2009-02-27 07:50:24 -05:00
|
|
|
client = Client.new
|
|
|
|
firm.unvalidated_clients_of_firm << client
|
|
|
|
|
|
|
|
assert firm.valid?
|
|
|
|
assert !client.valid?
|
|
|
|
assert firm.save
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !client.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_valid_adding_with_validate_false
|
|
|
|
no_of_clients = Client.count
|
|
|
|
|
2012-04-26 13:32:55 -04:00
|
|
|
firm = Firm.first
|
2009-02-27 07:50:24 -05:00
|
|
|
client = Client.new("name" => "Apple")
|
|
|
|
|
|
|
|
assert firm.valid?
|
|
|
|
assert client.valid?
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !client.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
|
|
|
|
firm.unvalidated_clients_of_firm << client
|
|
|
|
|
|
|
|
assert firm.save
|
2010-11-07 09:05:18 -05:00
|
|
|
assert client.persisted?
|
2010-08-18 20:07:46 -04:00
|
|
|
assert_equal no_of_clients + 1, Client.count
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_build
|
|
|
|
new_client = companies(:first_firm).clients_of_firm.build
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !new_client.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert !new_client.valid?
|
|
|
|
assert_equal new_client, companies(:first_firm).clients_of_firm.last
|
|
|
|
assert !companies(:first_firm).save
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !new_client.persisted?
|
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where
newly created finder methods #second, #third, #forth, and #fifth
caused a NoMethodError error on reload associations and where we
were pulling the wrong element out of cached associations.
Examples:
some_book.authors.reload.second
# Before
# => NoMethodError: undefined method 'first' for nil:NilClass
# After
# => #<Author id: 2, name: "Sally Second", ...>
some_book.first.authors.first
some_book.first.authors.second
# Before
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 1, name: "Freddy First", ...>
# After
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 2, name: "Sally Second", ...>
Fixes #13783.
2014-01-21 17:34:39 -05:00
|
|
|
assert_equal 2, companies(:first_firm).clients_of_firm(true).size
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_adding_before_save
|
|
|
|
no_of_firms = Firm.count
|
|
|
|
no_of_clients = Client.count
|
|
|
|
|
|
|
|
new_firm = Firm.new("name" => "A New Firm, Inc")
|
|
|
|
c = Client.new("name" => "Apple")
|
|
|
|
|
|
|
|
new_firm.clients_of_firm.push Client.new("name" => "Natural Company")
|
|
|
|
assert_equal 1, new_firm.clients_of_firm.size
|
|
|
|
new_firm.clients_of_firm << c
|
|
|
|
assert_equal 2, new_firm.clients_of_firm.size
|
|
|
|
|
|
|
|
assert_equal no_of_firms, Firm.count # Firm was not saved to database.
|
|
|
|
assert_equal no_of_clients, Client.count # Clients were not saved to database.
|
|
|
|
assert new_firm.save
|
2010-11-07 09:05:18 -05:00
|
|
|
assert new_firm.persisted?
|
|
|
|
assert c.persisted?
|
2009-02-27 07:50:24 -05:00
|
|
|
assert_equal new_firm, c.firm
|
2010-08-18 20:07:46 -04:00
|
|
|
assert_equal no_of_firms + 1, Firm.count # Firm was saved to database.
|
|
|
|
assert_equal no_of_clients + 2, Client.count # Clients were saved to database.
|
2009-02-27 07:50:24 -05:00
|
|
|
|
|
|
|
assert_equal 2, new_firm.clients_of_firm.size
|
|
|
|
assert_equal 2, new_firm.clients_of_firm(true).size
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_ids
|
|
|
|
firm = Firm.new("name" => "Apple")
|
|
|
|
firm.client_ids = [companies(:first_client).id, companies(:second_client).id]
|
|
|
|
firm.save
|
|
|
|
firm.reload
|
|
|
|
assert_equal 2, firm.clients.length
|
|
|
|
assert firm.clients.include?(companies(:second_client))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_ids_for_through_a_belongs_to
|
2013-03-28 10:55:43 -04:00
|
|
|
post = Post.new(:title => "Assigning IDs works!", :body => "You heard it here first, folks!")
|
2009-02-27 07:50:24 -05:00
|
|
|
post.person_ids = [people(:david).id, people(:michael).id]
|
|
|
|
post.save
|
|
|
|
post.reload
|
|
|
|
assert_equal 2, post.people.length
|
|
|
|
assert post.people.include?(people(:david))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_build_before_save
|
|
|
|
company = companies(:first_firm)
|
2014-08-21 02:47:35 -04:00
|
|
|
new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.build("name" => "Another Client") }
|
2009-02-27 07:50:24 -05:00
|
|
|
assert !company.clients_of_firm.loaded?
|
|
|
|
|
|
|
|
company.name += '-changed'
|
|
|
|
assert_queries(2) { assert company.save }
|
2010-11-07 09:05:18 -05:00
|
|
|
assert new_client.persisted?
|
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where
newly created finder methods #second, #third, #forth, and #fifth
caused a NoMethodError error on reload associations and where we
were pulling the wrong element out of cached associations.
Examples:
some_book.authors.reload.second
# Before
# => NoMethodError: undefined method 'first' for nil:NilClass
# After
# => #<Author id: 2, name: "Sally Second", ...>
some_book.first.authors.first
some_book.first.authors.second
# Before
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 1, name: "Freddy First", ...>
# After
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 2, name: "Sally Second", ...>
Fixes #13783.
2014-01-21 17:34:39 -05:00
|
|
|
assert_equal 3, company.clients_of_firm(true).size
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_build_many_before_save
|
|
|
|
company = companies(:first_firm)
|
2014-08-21 02:47:35 -04:00
|
|
|
assert_no_queries(ignore_none: false) { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) }
|
2009-02-27 07:50:24 -05:00
|
|
|
|
|
|
|
company.name += '-changed'
|
|
|
|
assert_queries(3) { assert company.save }
|
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where
newly created finder methods #second, #third, #forth, and #fifth
caused a NoMethodError error on reload associations and where we
were pulling the wrong element out of cached associations.
Examples:
some_book.authors.reload.second
# Before
# => NoMethodError: undefined method 'first' for nil:NilClass
# After
# => #<Author id: 2, name: "Sally Second", ...>
some_book.first.authors.first
some_book.first.authors.second
# Before
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 1, name: "Freddy First", ...>
# After
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 2, name: "Sally Second", ...>
Fixes #13783.
2014-01-21 17:34:39 -05:00
|
|
|
assert_equal 4, company.clients_of_firm(true).size
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_build_via_block_before_save
|
|
|
|
company = companies(:first_firm)
|
2014-08-21 02:47:35 -04:00
|
|
|
new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.build {|client| client.name = "Another Client" } }
|
2009-02-27 07:50:24 -05:00
|
|
|
assert !company.clients_of_firm.loaded?
|
|
|
|
|
|
|
|
company.name += '-changed'
|
|
|
|
assert_queries(2) { assert company.save }
|
2010-11-07 09:05:18 -05:00
|
|
|
assert new_client.persisted?
|
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where
newly created finder methods #second, #third, #forth, and #fifth
caused a NoMethodError error on reload associations and where we
were pulling the wrong element out of cached associations.
Examples:
some_book.authors.reload.second
# Before
# => NoMethodError: undefined method 'first' for nil:NilClass
# After
# => #<Author id: 2, name: "Sally Second", ...>
some_book.first.authors.first
some_book.first.authors.second
# Before
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 1, name: "Freddy First", ...>
# After
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 2, name: "Sally Second", ...>
Fixes #13783.
2014-01-21 17:34:39 -05:00
|
|
|
assert_equal 3, company.clients_of_firm(true).size
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_build_many_via_block_before_save
|
|
|
|
company = companies(:first_firm)
|
2014-08-21 02:47:35 -04:00
|
|
|
assert_no_queries(ignore_none: false) do
|
2009-02-27 07:50:24 -05:00
|
|
|
company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) do |client|
|
|
|
|
client.name = "changed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
company.name += '-changed'
|
|
|
|
assert_queries(3) { assert company.save }
|
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where
newly created finder methods #second, #third, #forth, and #fifth
caused a NoMethodError error on reload associations and where we
were pulling the wrong element out of cached associations.
Examples:
some_book.authors.reload.second
# Before
# => NoMethodError: undefined method 'first' for nil:NilClass
# After
# => #<Author id: 2, name: "Sally Second", ...>
some_book.first.authors.first
some_book.first.authors.second
# Before
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 1, name: "Freddy First", ...>
# After
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 2, name: "Sally Second", ...>
Fixes #13783.
2014-01-21 17:34:39 -05:00
|
|
|
assert_equal 4, company.clients_of_firm(true).size
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_replace_on_new_object
|
|
|
|
firm = Firm.new("name" => "New Firm")
|
|
|
|
firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
|
|
|
|
assert firm.save
|
|
|
|
firm.reload
|
|
|
|
assert_equal 2, firm.clients.length
|
2013-01-18 09:15:19 -05:00
|
|
|
assert firm.clients.include?(Client.find_by_name("New Client"))
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-11 11:52:13 -04:00
|
|
|
class TestDefaultAutosaveAssociationOnNewRecord < ActiveRecord::TestCase
|
|
|
|
def test_autosave_new_record_on_belongs_to_can_be_disabled_per_relationship
|
|
|
|
new_account = Account.new("credit_limit" => 1000)
|
|
|
|
new_firm = Firm.new("name" => "some firm")
|
|
|
|
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !new_firm.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
new_account.firm = new_firm
|
|
|
|
new_account.save!
|
|
|
|
|
2010-11-07 09:05:18 -05:00
|
|
|
assert new_firm.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
|
|
|
|
new_account = Account.new("credit_limit" => 1000)
|
|
|
|
new_autosaved_firm = Firm.new("name" => "some firm")
|
|
|
|
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !new_autosaved_firm.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
new_account.unautosaved_firm = new_autosaved_firm
|
|
|
|
new_account.save!
|
|
|
|
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !new_autosaved_firm.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_autosave_new_record_on_has_one_can_be_disabled_per_relationship
|
|
|
|
firm = Firm.new("name" => "some firm")
|
|
|
|
account = Account.new("credit_limit" => 1000)
|
|
|
|
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !account.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
firm.account = account
|
|
|
|
firm.save!
|
|
|
|
|
2010-11-07 09:05:18 -05:00
|
|
|
assert account.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
|
|
|
|
firm = Firm.new("name" => "some firm")
|
|
|
|
account = Account.new("credit_limit" => 1000)
|
|
|
|
|
|
|
|
firm.unautosaved_account = account
|
|
|
|
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !account.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
firm.unautosaved_account = account
|
|
|
|
firm.save!
|
|
|
|
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !account.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_autosave_new_record_on_has_many_can_be_disabled_per_relationship
|
|
|
|
firm = Firm.new("name" => "some firm")
|
|
|
|
account = Account.new("credit_limit" => 1000)
|
|
|
|
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !account.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
firm.accounts << account
|
|
|
|
|
|
|
|
firm.save!
|
2010-11-07 09:05:18 -05:00
|
|
|
assert account.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
|
|
|
|
firm = Firm.new("name" => "some firm")
|
|
|
|
account = Account.new("credit_limit" => 1000)
|
|
|
|
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !account.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
firm.unautosaved_accounts << account
|
|
|
|
|
|
|
|
firm.save!
|
2010-11-07 09:05:18 -05:00
|
|
|
assert !account.persisted?
|
2009-07-11 11:52:13 -04:00
|
|
|
end
|
2014-10-13 10:50:32 -04:00
|
|
|
|
|
|
|
def test_autosave_new_record_with_after_create_callback
|
|
|
|
post = PostWithAfterCreateCallback.new(title: 'Captain Murphy', body: 'is back')
|
|
|
|
post.comments.build(body: 'foo')
|
|
|
|
post.save!
|
|
|
|
|
|
|
|
assert_not_nil post.author_id
|
|
|
|
end
|
2009-07-11 11:52:13 -04:00
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
|
2013-04-10 23:29:19 -04:00
|
|
|
self.use_transactional_fixtures = false
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2014-03-09 16:16:01 -04:00
|
|
|
setup do
|
2009-01-31 20:44:30 -05:00
|
|
|
@pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
|
|
|
|
@ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
|
|
|
|
end
|
|
|
|
|
2014-03-09 16:16:01 -04:00
|
|
|
teardown do
|
2014-01-15 05:42:26 -05:00
|
|
|
# We are running without transactional fixtures and need to cleanup.
|
|
|
|
Bird.delete_all
|
2014-03-09 16:16:01 -04:00
|
|
|
Parrot.delete_all
|
2014-01-15 05:42:26 -05:00
|
|
|
@ship.delete
|
|
|
|
@pirate.delete
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
# reload
|
|
|
|
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?
|
2011-02-15 10:01:04 -05:00
|
|
|
assert !@pirate.ship.reload.marked_for_destruction?
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# has_one
|
|
|
|
def test_should_destroy_a_child_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal
|
|
|
|
assert !@pirate.ship.marked_for_destruction?
|
|
|
|
|
|
|
|
@pirate.ship.mark_for_destruction
|
|
|
|
id = @pirate.ship.id
|
|
|
|
|
|
|
|
assert @pirate.ship.marked_for_destruction?
|
2013-01-18 09:15:19 -05:00
|
|
|
assert Ship.find_by_id(id)
|
2009-01-31 20:44:30 -05:00
|
|
|
|
|
|
|
@pirate.save
|
|
|
|
assert_nil @pirate.reload.ship
|
2013-01-18 09:15:19 -05:00
|
|
|
assert_nil Ship.find_by_id(id)
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
2009-02-26 05:56:27 -05:00
|
|
|
def test_should_skip_validation_on_a_child_association_if_marked_for_destruction
|
|
|
|
@pirate.ship.name = ''
|
|
|
|
assert !@pirate.valid?
|
|
|
|
|
|
|
|
@pirate.ship.mark_for_destruction
|
2009-07-11 12:46:11 -04:00
|
|
|
@pirate.ship.expects(:valid?).never
|
2009-02-26 05:56:27 -05:00
|
|
|
assert_difference('Ship.count', -1) { @pirate.save! }
|
|
|
|
end
|
|
|
|
|
2009-07-11 14:52:03 -04:00
|
|
|
def test_a_child_marked_for_destruction_should_not_be_destroyed_twice
|
|
|
|
@pirate.ship.mark_for_destruction
|
|
|
|
assert @pirate.save
|
2012-04-24 12:31:26 -04:00
|
|
|
class << @pirate.ship
|
|
|
|
def destroy; raise "Should not be called" end
|
|
|
|
end
|
2009-07-11 14:52:03 -04:00
|
|
|
assert @pirate.save
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
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
|
|
|
|
|
2014-05-20 19:42:46 -04:00
|
|
|
@ship.pirate.catchphrase = "Changed Catchphrase"
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
assert_raise(RuntimeError) { assert !@pirate.save }
|
|
|
|
assert_not_nil @pirate.reload.ship
|
|
|
|
end
|
|
|
|
|
2014-05-20 19:42:46 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
def test_should_not_save_changed_has_one_unchanged_object_if_child_is_saved
|
|
|
|
@pirate.ship.expects(:save).never
|
|
|
|
assert @pirate.save
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
# belongs_to
|
|
|
|
def test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal
|
|
|
|
assert !@ship.pirate.marked_for_destruction?
|
|
|
|
|
|
|
|
@ship.pirate.mark_for_destruction
|
|
|
|
id = @ship.pirate.id
|
|
|
|
|
|
|
|
assert @ship.pirate.marked_for_destruction?
|
2013-01-18 09:15:19 -05:00
|
|
|
assert Pirate.find_by_id(id)
|
2009-01-31 20:44:30 -05:00
|
|
|
|
|
|
|
@ship.save
|
|
|
|
assert_nil @ship.reload.pirate
|
2013-01-18 09:15:19 -05:00
|
|
|
assert_nil Pirate.find_by_id(id)
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
2009-02-26 05:56:27 -05:00
|
|
|
def test_should_skip_validation_on_a_parent_association_if_marked_for_destruction
|
|
|
|
@ship.pirate.catchphrase = ''
|
|
|
|
assert !@ship.valid?
|
|
|
|
|
|
|
|
@ship.pirate.mark_for_destruction
|
2009-07-11 12:46:11 -04:00
|
|
|
@ship.pirate.expects(:valid?).never
|
2009-02-26 05:56:27 -05:00
|
|
|
assert_difference('Pirate.count', -1) { @ship.save! }
|
|
|
|
end
|
|
|
|
|
2009-07-11 14:52:03 -04:00
|
|
|
def test_a_parent_marked_for_destruction_should_not_be_destroyed_twice
|
|
|
|
@ship.pirate.mark_for_destruction
|
|
|
|
assert @ship.save
|
2012-04-24 12:31:26 -04:00
|
|
|
class << @ship.pirate
|
|
|
|
def destroy; raise "Should not be called" end
|
|
|
|
end
|
2009-07-11 14:52:03 -04:00
|
|
|
assert @ship.save
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
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
|
|
|
|
|
2010-12-13 09:06:23 -05:00
|
|
|
@ship.pirate.catchphrase = "Changed Catchphrase"
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
assert_raise(RuntimeError) { assert !@ship.save }
|
|
|
|
assert_not_nil @ship.reload.pirate
|
|
|
|
end
|
|
|
|
|
2010-12-13 09:06:23 -05:00
|
|
|
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
|
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
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}") }
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
assert !@pirate.birds.any? { |child| child.marked_for_destruction? }
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.birds.each { |child| child.mark_for_destruction }
|
|
|
|
klass = @pirate.birds.first.class
|
|
|
|
ids = @pirate.birds.map(&:id)
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
assert @pirate.birds.all? { |child| child.marked_for_destruction? }
|
2013-01-18 09:15:19 -05:00
|
|
|
ids.each { |id| assert klass.find_by_id(id) }
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.save
|
|
|
|
assert @pirate.reload.birds.empty?
|
2013-01-18 09:15:19 -05:00
|
|
|
ids.each { |id| assert_nil klass.find_by_id(id) }
|
2011-01-30 14:07:08 -05:00
|
|
|
end
|
|
|
|
|
2013-07-15 07:07:33 -04:00
|
|
|
def test_should_not_resave_destroyed_association
|
|
|
|
@pirate.birds.create!(name: :parrot)
|
|
|
|
@pirate.birds.first.destroy
|
|
|
|
@pirate.save!
|
|
|
|
assert @pirate.reload.birds.empty?
|
|
|
|
end
|
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
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
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
2011-01-30 14:07:08 -05:00
|
|
|
assert_difference("Bird.count", -2) { @pirate.save! }
|
|
|
|
end
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
def test_should_skip_validation_on_has_many_if_destroyed
|
|
|
|
@pirate.birds.create!(:name => "birds_1")
|
2009-02-26 05:56:27 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.birds.each { |bird| bird.name = '' }
|
|
|
|
assert !@pirate.valid?
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.birds.each { |bird| bird.destroy }
|
|
|
|
assert @pirate.valid?
|
|
|
|
end
|
2009-02-26 05:56:27 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
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 { |bird| bird.mark_for_destruction }
|
|
|
|
assert @pirate.save
|
2009-02-26 05:56:27 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.birds.each { |bird| bird.expects(:destroy).never }
|
|
|
|
assert @pirate.save
|
|
|
|
end
|
2009-02-26 05:56:27 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
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 }
|
|
|
|
|
2011-02-19 23:47:12 -05:00
|
|
|
# Stub the destroy method of the second child to raise an exception
|
2011-01-30 14:07:08 -05:00
|
|
|
class << before.last
|
|
|
|
def destroy(*args)
|
|
|
|
super
|
|
|
|
raise 'Oh noes!'
|
2009-07-11 12:46:11 -04:00
|
|
|
end
|
2009-02-26 05:56:27 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
assert_raise(RuntimeError) { assert !@pirate.save }
|
|
|
|
assert_equal before, @pirate.reload.birds
|
|
|
|
end
|
|
|
|
|
2011-10-14 07:49:55 -04:00
|
|
|
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
|
|
|
|
|
2013-04-10 23:29:19 -04:00
|
|
|
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
|
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
# Add and remove callbacks tests for association collections.
|
|
|
|
%w{ method proc }.each do |callback_type|
|
|
|
|
define_method("test_should_run_add_callback_#{callback_type}s_for_has_many") do
|
|
|
|
association_name_with_callbacks = "birds_with_#{callback_type}_callbacks"
|
2009-07-11 09:09:09 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
pirate = Pirate.new(:catchphrase => "Arr")
|
|
|
|
pirate.send(association_name_with_callbacks).build(:name => "Crowe the One-Eyed")
|
2009-07-11 09:09:09 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
expected = [
|
|
|
|
"before_adding_#{callback_type}_bird_<new>",
|
|
|
|
"after_adding_#{callback_type}_bird_<new>"
|
|
|
|
]
|
2009-07-11 09:09:09 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
assert_equal expected, pirate.ship_log
|
2009-07-11 09:09:09 -04:00
|
|
|
end
|
2009-02-26 05:56:27 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
define_method("test_should_run_remove_callback_#{callback_type}s_for_has_many") do
|
|
|
|
association_name_with_callbacks = "birds_with_#{callback_type}_callbacks"
|
2009-07-11 14:52:03 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.send(association_name_with_callbacks).create!(:name => "Crowe the One-Eyed")
|
|
|
|
@pirate.send(association_name_with_callbacks).each { |c| c.mark_for_destruction }
|
|
|
|
child_id = @pirate.send(association_name_with_callbacks).first.id
|
|
|
|
|
|
|
|
@pirate.ship_log.clear
|
|
|
|
@pirate.save
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
"before_removing_#{callback_type}_bird_#{child_id}",
|
|
|
|
"after_removing_#{callback_type}_bird_#{child_id}"
|
|
|
|
]
|
|
|
|
|
|
|
|
assert_equal expected, @pirate.ship_log
|
2009-07-11 14:52:03 -04:00
|
|
|
end
|
2011-01-30 14:07:08 -05:00
|
|
|
end
|
2009-07-11 14:52:03 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
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}") }
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
assert !@pirate.parrots.any? { |parrot| parrot.marked_for_destruction? }
|
|
|
|
@pirate.parrots.each { |parrot| parrot.mark_for_destruction }
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
assert_no_difference "Parrot.count" do
|
|
|
|
@pirate.save
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
assert @pirate.reload.parrots.empty?
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
join_records = Pirate.connection.select_all("SELECT * FROM parrots_pirates WHERE pirate_id = #{@pirate.id}")
|
|
|
|
assert join_records.empty?
|
|
|
|
end
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
def test_should_skip_validation_on_habtm_if_marked_for_destruction
|
|
|
|
2.times { |i| @pirate.parrots.create!(:name => "parrots_#{i}") }
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.parrots.each { |parrot| parrot.name = '' }
|
|
|
|
assert !@pirate.valid?
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.parrots.each do |parrot|
|
|
|
|
parrot.mark_for_destruction
|
|
|
|
parrot.expects(:valid?).never
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.save!
|
|
|
|
assert @pirate.reload.parrots.empty?
|
|
|
|
end
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
def test_should_skip_validation_on_habtm_if_destroyed
|
|
|
|
@pirate.parrots.create!(:name => "parrots_1")
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.parrots.each { |parrot| parrot.name = '' }
|
|
|
|
assert !@pirate.valid?
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.parrots.each { |parrot| parrot.destroy }
|
|
|
|
assert @pirate.valid?
|
|
|
|
end
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
def test_a_child_marked_for_destruction_should_not_be_destroyed_twice_while_saving_habtm
|
|
|
|
@pirate.parrots.create!(:name => "parrots_1")
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
@pirate.parrots.each { |parrot| parrot.mark_for_destruction }
|
|
|
|
assert @pirate.save
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2013-04-10 23:29:19 -04:00
|
|
|
Pirate.transaction do
|
|
|
|
assert_queries(0) do
|
|
|
|
assert @pirate.save
|
|
|
|
end
|
2011-01-30 14:07:08 -05:00
|
|
|
end
|
|
|
|
end
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2011-01-30 14:07:08 -05:00
|
|
|
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 }
|
2009-03-12 11:24:37 -04:00
|
|
|
|
2012-05-11 12:19:30 -04:00
|
|
|
class << @pirate.association(:parrots)
|
2011-01-30 14:07:08 -05:00
|
|
|
def destroy(*args)
|
|
|
|
super
|
|
|
|
raise 'Oh noes!'
|
2009-03-12 11:24:37 -04:00
|
|
|
end
|
|
|
|
end
|
2011-01-30 14:07:08 -05:00
|
|
|
|
|
|
|
assert_raise(RuntimeError) { assert !@pirate.save }
|
|
|
|
assert_equal before, @pirate.reload.parrots
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add and remove callbacks tests for association collections.
|
|
|
|
%w{ method proc }.each do |callback_type|
|
|
|
|
define_method("test_should_run_add_callback_#{callback_type}s_for_habtm") do
|
|
|
|
association_name_with_callbacks = "parrots_with_#{callback_type}_callbacks"
|
|
|
|
|
|
|
|
pirate = Pirate.new(:catchphrase => "Arr")
|
|
|
|
pirate.send(association_name_with_callbacks).build(:name => "Crowe the One-Eyed")
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
"before_adding_#{callback_type}_parrot_<new>",
|
|
|
|
"after_adding_#{callback_type}_parrot_<new>"
|
|
|
|
]
|
|
|
|
|
|
|
|
assert_equal expected, pirate.ship_log
|
|
|
|
end
|
|
|
|
|
|
|
|
define_method("test_should_run_remove_callback_#{callback_type}s_for_habtm") do
|
|
|
|
association_name_with_callbacks = "parrots_with_#{callback_type}_callbacks"
|
|
|
|
|
|
|
|
@pirate.send(association_name_with_callbacks).create!(:name => "Crowe the One-Eyed")
|
|
|
|
@pirate.send(association_name_with_callbacks).each { |c| c.mark_for_destruction }
|
|
|
|
child_id = @pirate.send(association_name_with_callbacks).first.id
|
|
|
|
|
|
|
|
@pirate.ship_log.clear
|
|
|
|
@pirate.save
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
"before_removing_#{callback_type}_parrot_#{child_id}",
|
|
|
|
"after_removing_#{callback_type}_parrot_#{child_id}"
|
|
|
|
]
|
|
|
|
|
|
|
|
assert_equal expected, @pirate.ship_log
|
|
|
|
end
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
|
2011-01-09 10:52:41 -05:00
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
2009-01-31 20:44:30 -05:00
|
|
|
|
|
|
|
def setup
|
2012-01-04 19:21:16 -05:00
|
|
|
super
|
2009-01-31 20:44:30 -05:00
|
|
|
@pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
|
|
|
|
@ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_still_work_without_an_associated_model
|
|
|
|
@ship.destroy
|
|
|
|
@pirate.reload.catchphrase = "Arr"
|
|
|
|
@pirate.save
|
2010-05-16 09:37:57 -04:00
|
|
|
assert_equal 'Arr', @pirate.reload.catchphrase
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2009-02-04 15:40:53 -05:00
|
|
|
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
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
def test_should_automatically_validate_the_associated_model
|
|
|
|
@pirate.ship.name = ''
|
2009-03-20 13:36:22 -04:00
|
|
|
assert @pirate.invalid?
|
2009-12-31 06:29:44 -05:00
|
|
|
assert @pirate.errors[:"ship.name"].any?
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
2009-02-27 07:50:24 -05:00
|
|
|
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
|
2009-03-20 13:36:22 -04:00
|
|
|
assert @pirate.invalid?
|
2009-12-31 06:29:44 -05:00
|
|
|
assert @pirate.errors[:"ship.name"].any?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert @pirate.errors[:catchphrase].any?
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
2010-01-13 19:43:57 -05:00
|
|
|
def test_should_not_ignore_different_error_messages_on_the_same_attribute
|
|
|
|
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"]
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
def test_should_still_allow_to_bypass_validations_on_the_associated_model
|
|
|
|
@pirate.catchphrase = ''
|
|
|
|
@pirate.ship.name = ''
|
2010-01-17 08:22:27 -05:00
|
|
|
@pirate.save(:validate => false)
|
2009-03-22 18:05:41 -04:00
|
|
|
# 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
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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 = '' }
|
2010-01-17 08:22:27 -05:00
|
|
|
@pirate.save(:validate => false)
|
2009-01-31 20:44:30 -05:00
|
|
|
|
|
|
|
values = [@pirate.reload.catchphrase, @pirate.ship.name, *@pirate.ship.parts.map(&:name)]
|
2009-03-22 18:05:41 -04:00
|
|
|
# Oracle saves empty string as NULL
|
|
|
|
if current_adapter?(:OracleAdapter)
|
2012-03-01 22:10:06 -05:00
|
|
|
assert_equal [nil, nil, nil, nil], values
|
2009-03-22 18:05:41 -04:00
|
|
|
else
|
|
|
|
assert_equal ['', '', '', ''], values
|
|
|
|
end
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-01-08 14:47:49 -05:00
|
|
|
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
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
def test_should_not_load_the_associated_model
|
|
|
|
assert_queries(1) { @pirate.catchphrase = 'Arr'; @pirate.save! }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-15 04:18:24 -04:00
|
|
|
class TestAutosaveAssociationOnAHasOneThroughAssociation < ActiveRecord::TestCase
|
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
organization = Organization.create
|
|
|
|
@member = Member.create
|
|
|
|
MemberDetail.create(organization: organization, member: @member)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_has_one_through_model
|
|
|
|
class << @member.organization
|
|
|
|
def save(*args)
|
|
|
|
super
|
|
|
|
raise 'Oh noes!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_nothing_raised { @member.save }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
|
2011-01-09 10:52:41 -05:00
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
2009-01-31 20:44:30 -05:00
|
|
|
|
|
|
|
def setup
|
2012-01-04 19:21:16 -05:00
|
|
|
super
|
2009-01-31 20:44:30 -05:00
|
|
|
@ship = Ship.create(:name => 'Nights Dirty Lightning')
|
|
|
|
@pirate = @ship.create_pirate(:catchphrase => "Don' botharrr talkin' like one, savvy?")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_still_work_without_an_associated_model
|
|
|
|
@pirate.destroy
|
|
|
|
@ship.reload.name = "The Vile Insanity"
|
|
|
|
@ship.save
|
2010-05-16 09:37:57 -04:00
|
|
|
assert_equal 'The Vile Insanity', @ship.reload.name
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_automatically_save_the_associated_model
|
|
|
|
@ship.pirate.catchphrase = 'Arr'
|
|
|
|
@ship.save
|
|
|
|
assert_equal 'Arr', @ship.reload.pirate.catchphrase
|
|
|
|
end
|
|
|
|
|
2009-02-04 15:40:53 -05:00
|
|
|
def test_should_automatically_save_bang_the_associated_model
|
|
|
|
@ship.pirate.catchphrase = 'Arr'
|
|
|
|
@ship.save!
|
|
|
|
assert_equal 'Arr', @ship.reload.pirate.catchphrase
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
def test_should_automatically_validate_the_associated_model
|
|
|
|
@ship.pirate.catchphrase = ''
|
2009-03-19 19:28:59 -04:00
|
|
|
assert @ship.invalid?
|
2009-12-31 06:29:44 -05:00
|
|
|
assert @ship.errors[:"pirate.catchphrase"].any?
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
2009-02-27 07:50:24 -05:00
|
|
|
def test_should_merge_errors_on_the_associated_model_onto_the_parent_even_if_it_is_not_valid
|
|
|
|
@ship.name = nil
|
|
|
|
@ship.pirate.catchphrase = nil
|
2009-03-20 13:36:22 -04:00
|
|
|
assert @ship.invalid?
|
|
|
|
assert @ship.errors[:name].any?
|
2009-12-31 06:29:44 -05:00
|
|
|
assert @ship.errors[:"pirate.catchphrase"].any?
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
def test_should_still_allow_to_bypass_validations_on_the_associated_model
|
|
|
|
@ship.pirate.catchphrase = ''
|
|
|
|
@ship.name = ''
|
2010-01-17 08:22:27 -05:00
|
|
|
@ship.save(:validate => false)
|
2009-03-22 18:05:41 -04:00
|
|
|
# Oracle saves empty string as NULL
|
|
|
|
if current_adapter?(:OracleAdapter)
|
2012-03-01 22:10:06 -05:00
|
|
|
assert_equal [nil, nil], [@ship.reload.name, @ship.pirate.catchphrase]
|
2009-03-22 18:05:41 -04:00
|
|
|
else
|
|
|
|
assert_equal ['', ''], [@ship.reload.name, @ship.pirate.catchphrase]
|
|
|
|
end
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
|
|
|
|
@ship.pirate.catchphrase = ''
|
|
|
|
assert_raise(ActiveRecord::RecordInvalid) do
|
|
|
|
@ship.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-08 14:47:49 -05:00
|
|
|
def test_should_not_save_and_return_false_if_a_callback_cancelled_saving
|
|
|
|
ship = Ship.new(:name => 'The Vile Insanity')
|
|
|
|
pirate = ship.build_pirate(:catchphrase => 'Arr')
|
|
|
|
pirate.cancel_save_from_callback = true
|
|
|
|
|
|
|
|
assert_no_difference 'Ship.count' do
|
|
|
|
assert_no_difference 'Pirate.count' do
|
|
|
|
assert !ship.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
|
|
|
|
before = [@ship.pirate.catchphrase, @ship.name]
|
|
|
|
|
|
|
|
@ship.pirate.catchphrase = 'Arr'
|
|
|
|
@ship.name = 'The Vile Insanity'
|
|
|
|
|
|
|
|
# Stub the save method of the @ship.pirate instance to raise an exception
|
|
|
|
class << @ship.pirate
|
|
|
|
def save(*args)
|
|
|
|
super
|
|
|
|
raise 'Oh noes!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(RuntimeError) { assert !@ship.save }
|
|
|
|
assert_equal before, [@ship.pirate.reload.catchphrase, @ship.reload.name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_load_the_associated_model
|
|
|
|
assert_queries(1) { @ship.name = 'The Vile Insanity'; @ship.save! }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module AutosaveAssociationOnACollectionAssociationTests
|
|
|
|
def test_should_automatically_save_the_associated_models
|
|
|
|
new_names = ['Grace OMalley', 'Privateers Greed']
|
|
|
|
@pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
|
|
|
|
|
|
|
|
@pirate.save
|
|
|
|
assert_equal new_names, @pirate.reload.send(@association_name).map(&:name)
|
|
|
|
end
|
|
|
|
|
2009-02-04 15:40:53 -05:00
|
|
|
def test_should_automatically_save_bang_the_associated_models
|
|
|
|
new_names = ['Grace OMalley', 'Privateers Greed']
|
|
|
|
@pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
|
|
|
|
|
|
|
|
@pirate.save!
|
|
|
|
assert_equal new_names, @pirate.reload.send(@association_name).map(&:name)
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
def test_should_automatically_validate_the_associated_models
|
|
|
|
@pirate.send(@association_name).each { |child| child.name = '' }
|
|
|
|
|
|
|
|
assert !@pirate.valid?
|
2009-12-31 06:29:44 -05:00
|
|
|
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
|
2009-03-20 13:36:22 -04:00
|
|
|
assert @pirate.errors[@association_name].empty?
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
2009-02-27 07:50:24 -05:00
|
|
|
def test_should_not_use_default_invalid_error_on_associated_models
|
|
|
|
@pirate.send(@association_name).build(:name => '')
|
|
|
|
|
|
|
|
assert !@pirate.valid?
|
2009-12-31 06:29:44 -05:00
|
|
|
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
|
2009-03-20 13:36:22 -04:00
|
|
|
assert @pirate.errors[@association_name].empty?
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
2009-12-31 06:29:44 -05:00
|
|
|
def test_should_default_invalid_error_from_i18n
|
2014-02-03 08:48:59 -05:00
|
|
|
I18n.backend.store_translations(:en, activerecord: {errors: { models:
|
|
|
|
{ @associated_model_name.to_s.to_sym => { blank: "cannot be blank" } }
|
2010-01-30 07:07:48 -05:00
|
|
|
}})
|
2009-12-31 06:29:44 -05:00
|
|
|
|
2014-02-03 08:48:59 -05:00
|
|
|
@pirate.send(@association_name).build(name: '')
|
2009-12-31 06:29:44 -05:00
|
|
|
|
|
|
|
assert !@pirate.valid?
|
|
|
|
assert_equal ["cannot be blank"], @pirate.errors["#{@association_name}.name"]
|
2014-02-03 08:48:59 -05:00
|
|
|
assert_equal ["#{@association_name.to_s.humanize} name cannot be blank"], @pirate.errors.full_messages
|
2009-12-31 06:29:44 -05:00
|
|
|
assert @pirate.errors[@association_name].empty?
|
|
|
|
ensure
|
2010-01-06 19:04:25 -05:00
|
|
|
I18n.backend = I18n::Backend::Simple.new
|
2009-12-31 06:29:44 -05:00
|
|
|
end
|
|
|
|
|
2009-02-27 07:50:24 -05:00
|
|
|
def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid
|
|
|
|
@pirate.send(@association_name).each { |child| child.name = '' }
|
|
|
|
@pirate.catchphrase = nil
|
|
|
|
|
|
|
|
assert !@pirate.valid?
|
2009-12-31 06:29:44 -05:00
|
|
|
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
|
2009-03-20 13:36:22 -04:00
|
|
|
assert @pirate.errors[:catchphrase].any?
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_to_bypass_validations_on_the_associated_models_on_update
|
2009-01-31 20:44:30 -05:00
|
|
|
@pirate.catchphrase = ''
|
|
|
|
@pirate.send(@association_name).each { |child| child.name = '' }
|
|
|
|
|
2010-01-17 08:22:27 -05:00
|
|
|
assert @pirate.save(:validate => false)
|
2009-03-22 18:05:41 -04:00
|
|
|
# Oracle saves empty string as NULL
|
|
|
|
if current_adapter?(:OracleAdapter)
|
|
|
|
assert_equal [nil, nil, nil], [
|
|
|
|
@pirate.reload.catchphrase,
|
|
|
|
@pirate.send(@association_name).first.name,
|
|
|
|
@pirate.send(@association_name).last.name
|
|
|
|
]
|
|
|
|
else
|
|
|
|
assert_equal ['', '', ''], [
|
|
|
|
@pirate.reload.catchphrase,
|
|
|
|
@pirate.send(@association_name).first.name,
|
|
|
|
@pirate.send(@association_name).last.name
|
|
|
|
]
|
|
|
|
end
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
2009-02-27 07:50:24 -05:00
|
|
|
def test_should_validation_the_associated_models_on_create
|
|
|
|
assert_no_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count") do
|
|
|
|
2.times { @pirate.send(@association_name).build }
|
2010-01-17 08:22:27 -05:00
|
|
|
@pirate.save
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_to_bypass_validations_on_the_associated_models_on_create
|
2010-08-18 20:07:46 -04:00
|
|
|
assert_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count", 2) do
|
2009-02-27 07:50:24 -05:00
|
|
|
2.times { @pirate.send(@association_name).build }
|
2010-01-17 08:22:27 -05:00
|
|
|
@pirate.save(:validate => false)
|
2009-02-27 07:50:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-08 14:47:49 -05:00
|
|
|
def test_should_not_save_and_return_false_if_a_callback_cancelled_saving_in_either_create_or_update
|
|
|
|
@pirate.catchphrase = 'Changed'
|
|
|
|
@child_1.name = 'Changed'
|
|
|
|
@child_1.cancel_save_from_callback = true
|
|
|
|
|
|
|
|
assert !@pirate.save
|
|
|
|
assert_equal "Don' botharrr talkin' like one, savvy?", @pirate.reload.catchphrase
|
|
|
|
assert_equal "Posideons Killer", @child_1.reload.name
|
|
|
|
|
|
|
|
new_pirate = Pirate.new(:catchphrase => 'Arr')
|
|
|
|
new_child = new_pirate.send(@association_name).build(:name => 'Grace OMalley')
|
|
|
|
new_child.cancel_save_from_callback = true
|
|
|
|
|
|
|
|
assert_no_difference 'Pirate.count' do
|
|
|
|
assert_no_difference "#{new_child.class.name}.count" do
|
|
|
|
assert !new_pirate.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
|
|
|
|
before = [@pirate.catchphrase, *@pirate.send(@association_name).map(&:name)]
|
|
|
|
new_names = ['Grace OMalley', 'Privateers Greed']
|
|
|
|
|
|
|
|
@pirate.catchphrase = 'Arr'
|
|
|
|
@pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
|
|
|
|
|
|
|
|
# Stub the save method of the first child instance to raise an exception
|
|
|
|
class << @pirate.send(@association_name).first
|
|
|
|
def save(*args)
|
|
|
|
super
|
|
|
|
raise 'Oh noes!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(RuntimeError) { assert !@pirate.save }
|
|
|
|
assert_equal before, [@pirate.reload.catchphrase, *@pirate.send(@association_name).map(&:name)]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
|
|
|
|
@pirate.send(@association_name).each { |child| child.name = '' }
|
|
|
|
assert_raise(ActiveRecord::RecordInvalid) do
|
|
|
|
@pirate.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_load_the_associated_models_if_they_were_not_loaded_yet
|
|
|
|
assert_queries(1) { @pirate.catchphrase = 'Arr'; @pirate.save! }
|
|
|
|
|
2012-05-11 12:19:30 -04:00
|
|
|
@pirate.send(@association_name).load_target
|
2009-02-04 15:40:53 -05:00
|
|
|
|
|
|
|
assert_queries(3) do
|
2009-01-31 20:44:30 -05:00
|
|
|
@pirate.catchphrase = 'Yarr'
|
|
|
|
new_names = ['Grace OMalley', 'Privateers Greed']
|
|
|
|
@pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
|
|
|
|
@pirate.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase
|
2011-01-09 10:52:41 -05:00
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
2009-01-31 20:44:30 -05:00
|
|
|
|
|
|
|
def setup
|
2012-01-04 19:21:16 -05:00
|
|
|
super
|
2009-01-31 20:44:30 -05:00
|
|
|
@association_name = :birds
|
2014-02-03 08:48:59 -05:00
|
|
|
@associated_model_name = :bird
|
2009-01-31 20:44:30 -05:00
|
|
|
|
|
|
|
@pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
|
|
|
|
@child_1 = @pirate.birds.create(:name => 'Posideons Killer')
|
|
|
|
@child_2 = @pirate.birds.create(:name => 'Killer bandita Dionne')
|
|
|
|
end
|
|
|
|
|
|
|
|
include AutosaveAssociationOnACollectionAssociationTests
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestAutosaveAssociationOnAHasAndBelongsToManyAssociation < ActiveRecord::TestCase
|
2011-01-09 10:52:41 -05:00
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2014-02-03 08:48:59 -05:00
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@association_name = :autosaved_parrots
|
|
|
|
@associated_model_name = :parrot
|
|
|
|
@habtm = true
|
|
|
|
|
|
|
|
@pirate = Pirate.create(catchphrase: "Don' botharrr talkin' like one, savvy?")
|
|
|
|
@child_1 = @pirate.parrots.create(name: 'Posideons Killer')
|
|
|
|
@child_2 = @pirate.parrots.create(name: 'Killer bandita Dionne')
|
|
|
|
end
|
|
|
|
|
|
|
|
include AutosaveAssociationOnACollectionAssociationTests
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestAutosaveAssociationOnAHasAndBelongsToManyAssociationWithAcceptsNestedAttributes < ActiveRecord::TestCase
|
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
def setup
|
2012-01-04 19:21:16 -05:00
|
|
|
super
|
2009-01-31 20:44:30 -05:00
|
|
|
@association_name = :parrots
|
2014-02-03 08:48:59 -05:00
|
|
|
@associated_model_name = :parrot
|
2009-01-31 20:44:30 -05:00
|
|
|
@habtm = true
|
|
|
|
|
2014-02-03 08:48:59 -05:00
|
|
|
@pirate = Pirate.create(catchphrase: "Don' botharrr talkin' like one, savvy?")
|
|
|
|
@child_1 = @pirate.parrots.create(name: 'Posideons Killer')
|
|
|
|
@child_2 = @pirate.parrots.create(name: 'Killer bandita Dionne')
|
2009-01-31 20:44:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
include AutosaveAssociationOnACollectionAssociationTests
|
2009-05-18 19:17:35 -04:00
|
|
|
end
|
2009-09-12 08:55:34 -04:00
|
|
|
|
2010-05-18 21:49:19 -04:00
|
|
|
class TestAutosaveAssociationValidationsOnAHasManyAssociation < ActiveRecord::TestCase
|
2011-01-09 10:52:41 -05:00
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
2009-09-12 08:55:34 -04:00
|
|
|
|
|
|
|
def setup
|
2012-01-04 19:21:16 -05:00
|
|
|
super
|
2009-09-12 08:55:34 -04:00
|
|
|
@pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
|
|
|
|
@pirate.birds.create(:name => 'cookoo')
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should automatically validate associations" do
|
|
|
|
assert @pirate.valid?
|
|
|
|
@pirate.birds.each { |bird| bird.name = '' }
|
|
|
|
|
|
|
|
assert !@pirate.valid?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-18 21:49:19 -04:00
|
|
|
class TestAutosaveAssociationValidationsOnAHasOneAssociation < ActiveRecord::TestCase
|
2011-01-09 10:52:41 -05:00
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
2009-09-12 08:55:34 -04:00
|
|
|
|
|
|
|
def setup
|
2012-01-04 19:21:16 -05:00
|
|
|
super
|
2009-09-12 08:55:34 -04:00
|
|
|
@pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
|
|
|
|
@pirate.create_ship(:name => 'titanic')
|
2010-10-20 17:59:59 -04:00
|
|
|
super
|
2009-09-12 08:55:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should automatically validate associations with :validate => true" do
|
|
|
|
assert @pirate.valid?
|
|
|
|
@pirate.ship.name = ''
|
|
|
|
assert !@pirate.valid?
|
|
|
|
end
|
|
|
|
|
2013-03-28 10:55:43 -04:00
|
|
|
test "should not automatically add validate associations without :validate => true" do
|
2009-09-12 08:55:34 -04:00
|
|
|
assert @pirate.valid?
|
|
|
|
@pirate.non_validated_ship.name = ''
|
|
|
|
assert @pirate.valid?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-18 21:49:19 -04:00
|
|
|
class TestAutosaveAssociationValidationsOnABelongsToAssociation < ActiveRecord::TestCase
|
2011-01-09 10:52:41 -05:00
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
2009-09-12 08:55:34 -04:00
|
|
|
|
|
|
|
def setup
|
2012-01-04 19:21:16 -05:00
|
|
|
super
|
2009-09-12 08:55:34 -04:00
|
|
|
@pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should automatically validate associations with :validate => true" do
|
|
|
|
assert @pirate.valid?
|
|
|
|
@pirate.parrot = Parrot.new(:name => '')
|
|
|
|
assert !@pirate.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should not automatically validate associations without :validate => true" do
|
|
|
|
assert @pirate.valid?
|
|
|
|
@pirate.non_validated_parrot = Parrot.new(:name => '')
|
|
|
|
assert @pirate.valid?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-18 21:49:19 -04:00
|
|
|
class TestAutosaveAssociationValidationsOnAHABTMAssociation < ActiveRecord::TestCase
|
2011-01-09 10:52:41 -05:00
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
2009-09-12 08:55:34 -04:00
|
|
|
|
|
|
|
def setup
|
2012-01-04 19:21:16 -05:00
|
|
|
super
|
2009-09-12 08:55:34 -04:00
|
|
|
@pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should automatically validate associations with :validate => true" do
|
|
|
|
assert @pirate.valid?
|
|
|
|
@pirate.parrots = [ Parrot.new(:name => 'popuga') ]
|
|
|
|
@pirate.parrots.each { |parrot| parrot.name = '' }
|
|
|
|
assert !@pirate.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should not automatically validate associations without :validate => true" do
|
|
|
|
assert @pirate.valid?
|
|
|
|
@pirate.non_validated_parrots = [ Parrot.new(:name => 'popuga') ]
|
|
|
|
@pirate.non_validated_parrots.each { |parrot| parrot.name = '' }
|
|
|
|
assert @pirate.valid?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestAutosaveAssociationValidationMethodsGeneration < ActiveRecord::TestCase
|
2011-01-09 10:52:41 -05:00
|
|
|
self.use_transactional_fixtures = false unless supports_savepoints?
|
2009-09-12 08:55:34 -04:00
|
|
|
|
|
|
|
def setup
|
2012-01-04 19:21:16 -05:00
|
|
|
super
|
2009-09-12 08:55:34 -04:00
|
|
|
@pirate = Pirate.new
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should generate validation methods for has_many associations" do
|
2010-05-18 21:47:24 -04:00
|
|
|
assert_respond_to @pirate, :validate_associated_records_for_birds
|
2009-09-12 08:55:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should generate validation methods for has_one associations with :validate => true" do
|
2010-05-18 21:47:24 -04:00
|
|
|
assert_respond_to @pirate, :validate_associated_records_for_ship
|
2009-09-12 08:55:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should not generate validation methods for has_one associations without :validate => true" do
|
|
|
|
assert !@pirate.respond_to?(:validate_associated_records_for_non_validated_ship)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should generate validation methods for belongs_to associations with :validate => true" do
|
2010-05-18 21:47:24 -04:00
|
|
|
assert_respond_to @pirate, :validate_associated_records_for_parrot
|
2009-09-12 08:55:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should not generate validation methods for belongs_to associations without :validate => true" do
|
|
|
|
assert !@pirate.respond_to?(:validate_associated_records_for_non_validated_parrot)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should generate validation methods for HABTM associations with :validate => true" do
|
2010-05-18 21:47:24 -04:00
|
|
|
assert_respond_to @pirate, :validate_associated_records_for_parrots
|
2009-09-12 08:55:34 -04:00
|
|
|
end
|
|
|
|
end
|
2009-10-01 02:17:50 -04:00
|
|
|
|
|
|
|
class TestAutosaveAssociationWithTouch < ActiveRecord::TestCase
|
|
|
|
def test_autosave_with_touch_should_not_raise_system_stack_error
|
|
|
|
invoice = Invoice.create
|
|
|
|
assert_nothing_raised { invoice.line_items.create(:amount => 10) }
|
|
|
|
end
|
|
|
|
end
|