reset_callbacks(target, type) { || ... }
click to toggle source
def reset_callbacks(target, type)
old_callbacks = target.send(:get_callbacks, type).deep_dup
yield
ensure
target.send(:set_callbacks, type, old_callbacks) if old_callbacks
end
test_child_instance_should_point_to_parent_without_saving()
click to toggle source
def test_child_instance_should_point_to_parent_without_saving
man = Man.new
i = Interest.create(topic: "Industrial Revolution Re-enactment")
man.interests << i
assert_not_nil i.man
i.man.name = "Charles"
assert_equal i.man.name, man.name
assert !man.persisted?
end
test_find_on_child_instance_with_id_should_not_load_all_child_records()
click to toggle source
def test_find_on_child_instance_with_id_should_not_load_all_child_records
man = Man.create!
interest = Interest.create!(man: man)
man.interests.find(interest.id)
assert_not man.interests.loaded?
end
test_inverse_instance_should_be_set_before_find_callbacks_are_run()
click to toggle source
def test_inverse_instance_should_be_set_before_find_callbacks_are_run
reset_callbacks(Interest, :find) do
Interest.after_find { raise unless association(:man).loaded? && man.present? }
assert Man.first.interests.reload.any?
assert Man.includes(:interests).first.interests.any?
assert Man.joins(:interests).includes(:interests).first.interests.any?
end
end
test_inverse_instance_should_be_set_before_initialize_callbacks_are_run()
click to toggle source
def test_inverse_instance_should_be_set_before_initialize_callbacks_are_run
reset_callbacks(Interest, :initialize) do
Interest.after_initialize { raise unless association(:man).loaded? && man.present? }
assert Man.first.interests.reload.any?
assert Man.includes(:interests).first.interests.any?
assert Man.joins(:interests).includes(:interests).first.interests.any?
end
end
test_parent_instance_should_be_shared_with_eager_loaded_children()
click to toggle source
def test_parent_instance_should_be_shared_with_eager_loaded_children
m = Man.all.merge!(where: { name: "Gordon" }, includes: :interests).first
is = m.interests
is.each do |i|
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = "Bongo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
i.man.name = "Mungo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance"
end
m = Man.all.merge!(where: { name: "Gordon" }, includes: :interests, order: "interests.id").first
is = m.interests
is.each do |i|
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = "Bongo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
i.man.name = "Mungo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance"
end
end
test_parent_instance_should_be_shared_with_every_child_on_find()
click to toggle source
def test_parent_instance_should_be_shared_with_every_child_on_find
m = men(:gordon)
is = m.interests
is.each do |i|
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = "Bongo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
i.man.name = "Mungo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance"
end
end
test_parent_instance_should_be_shared_with_every_child_on_find_for_sti()
click to toggle source
def test_parent_instance_should_be_shared_with_every_child_on_find_for_sti
a = authors(:david)
ps = a.posts
ps.each do |p|
assert_equal a.name, p.author.name, "Name of man should be the same before changes to parent instance"
a.name = "Bongo"
assert_equal a.name, p.author.name, "Name of man should be the same after changes to parent instance"
p.author.name = "Mungo"
assert_equal a.name, p.author.name, "Name of man should be the same after changes to child-owned instance"
end
sps = a.special_posts
sps.each do |sp|
assert_equal a.name, sp.author.name, "Name of man should be the same before changes to parent instance"
a.name = "Bongo"
assert_equal a.name, sp.author.name, "Name of man should be the same after changes to parent instance"
sp.author.name = "Mungo"
assert_equal a.name, sp.author.name, "Name of man should be the same after changes to child-owned instance"
end
end
test_parent_instance_should_be_shared_with_first_and_last_child()
click to toggle source
def test_parent_instance_should_be_shared_with_first_and_last_child
man = Man.first
assert man.interests.first.man.equal? man
assert man.interests.last.man.equal? man
end
test_parent_instance_should_be_shared_with_first_n_and_last_n_children()
click to toggle source
def test_parent_instance_should_be_shared_with_first_n_and_last_n_children
man = Man.first
interests = man.interests.first(2)
assert interests[0].man.equal? man
assert interests[1].man.equal? man
interests = man.interests.last(2)
assert interests[0].man.equal? man
assert interests[1].man.equal? man
end
test_parent_instance_should_be_shared_with_newly_block_style_built_child()
click to toggle source
def test_parent_instance_should_be_shared_with_newly_block_style_built_child
m = Man.first
i = m.interests.build { |ii| ii.topic = "Industrial Revolution Re-enactment" }
assert_not_nil i.topic, "Child attributes supplied to build via blocks should be populated"
assert_not_nil i.man
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = "Bongo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
i.man.name = "Mungo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
end
test_parent_instance_should_be_shared_with_newly_block_style_created_child()
click to toggle source
def test_parent_instance_should_be_shared_with_newly_block_style_created_child
m = Man.first
i = m.interests.create { |ii| ii.topic = "Industrial Revolution Re-enactment" }
assert_not_nil i.topic, "Child attributes supplied to create via blocks should be populated"
assert_not_nil i.man
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = "Bongo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
i.man.name = "Mungo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
end
test_parent_instance_should_be_shared_with_newly_created_via_bang_method_child()
click to toggle source
def test_parent_instance_should_be_shared_with_newly_created_via_bang_method_child
m = Man.first
i = m.interests.create!(topic: "Industrial Revolution Re-enactment")
assert_not_nil i.man
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = "Bongo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
i.man.name = "Mungo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
end
test_parent_instance_should_be_shared_with_poked_in_child()
click to toggle source
def test_parent_instance_should_be_shared_with_poked_in_child
m = men(:gordon)
i = Interest.create(topic: "Industrial Revolution Re-enactment")
m.interests << i
assert_not_nil i.man
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = "Bongo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
i.man.name = "Mungo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
end
test_parent_instance_should_be_shared_with_replaced_via_accessor_children()
click to toggle source
def test_parent_instance_should_be_shared_with_replaced_via_accessor_children
m = Man.first
i = Interest.new(topic: "Industrial Revolution Re-enactment")
m.interests = [i]
assert_not_nil i.man
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = "Bongo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
i.man.name = "Mungo"
assert_equal m.name, i.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
end
test_parent_instance_should_be_shared_within_build_block_of_new_child()
click to toggle source
def test_parent_instance_should_be_shared_within_build_block_of_new_child
man = Man.first
interest = man.interests.build do |i|
assert i.man.equal?(man), "Man of child should be the same instance as a parent"
end
assert interest.man.equal?(man), "Man of the child should still be the same instance as a parent"
end
test_parent_instance_should_be_shared_within_create_block_of_new_child()
click to toggle source
def test_parent_instance_should_be_shared_within_create_block_of_new_child
man = Man.first
interest = man.interests.create do |i|
assert i.man.equal?(man), "Man of child should be the same instance as a parent"
end
assert interest.man.equal?(man), "Man of the child should still be the same instance as a parent"
end
test_parent_instance_should_find_child_instance_using_child_instance_id()
click to toggle source
def test_parent_instance_should_find_child_instance_using_child_instance_id
man = Man.create!
interest = Interest.create!
man.interests = [interest]
assert interest.equal?(man.interests.first), "The inverse association should use the interest already created and held in memory"
assert interest.equal?(man.interests.find(interest.id)), "The inverse association should use the interest already created and held in memory"
assert man.equal?(man.interests.first.man), "Two inversion should lead back to the same object that was originally held"
assert man.equal?(man.interests.find(interest.id).man), "Two inversions should lead back to the same object that was originally held"
end
test_parent_instance_should_find_child_instance_using_child_instance_id_when_created()
click to toggle source
def test_parent_instance_should_find_child_instance_using_child_instance_id_when_created
man = Man.create!
interest = Interest.create!(man: man)
assert man.equal?(man.interests.first.man), "Two inverses should lead back to the same object that was originally held"
assert man.equal?(man.interests.find(interest.id).man), "Two inversions should lead back to the same object that was originally held"
assert_nil man.interests.find(interest.id).man.name, "The name of the man should match before the name is changed"
man.name = "Ben Bitdiddle"
assert_equal man.name, man.interests.find(interest.id).man.name, "The name of the man should match after the parent name is changed"
man.interests.find(interest.id).man.name = "Alyssa P. Hacker"
assert_equal man.name, man.interests.find(interest.id).man.name, "The name of the man should match after the child name is changed"
end
test_raise_record_not_found_error_when_invalid_ids_are_passed()
click to toggle source
def test_raise_record_not_found_error_when_invalid_ids_are_passed
Interest.delete_all
man = Man.create!
invalid_id = 245324523
assert_raise(ActiveRecord::RecordNotFound) { man.interests.find(invalid_id) }
invalid_ids = [8432342, 2390102913, 2453245234523452]
assert_raise(ActiveRecord::RecordNotFound) { man.interests.find(invalid_ids) }
end
test_raise_record_not_found_error_when_no_ids_are_passed()
click to toggle source
def test_raise_record_not_found_error_when_no_ids_are_passed
man = Man.create!
assert_raise(ActiveRecord::RecordNotFound) { man.interests.find() }
end
test_trying_to_use_inverses_that_dont_exist_should_raise_an_error()
click to toggle source
def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.first.secret_interests }
end