class AutomaticInverseFindingTests

Public Instance Methods

test_has_many_and_belongs_to_automatic_inverse_shares_objects_on_comment() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 105
def test_has_many_and_belongs_to_automatic_inverse_shares_objects_on_comment
  rating = Rating.create!
  comment = Comment.first
  rating.comment = comment

  assert_equal rating.comment, comment, "The Rating's comment should be the original Comment"

  rating.comment.body = "Fennec foxes are the smallest of the foxes."
  assert_equal rating.comment.body, comment.body, "Changing the Comment's body on the association should change the original Comment's body"

  comment.body = "Kittens are adorable."
  assert_equal comment.body, rating.comment.body, "Changing the original Comment's body should change the Comment's body on the association"
end
test_has_many_and_belongs_to_automatic_inverse_shares_objects_on_rating() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 92
def test_has_many_and_belongs_to_automatic_inverse_shares_objects_on_rating
  comment = Comment.first
  rating = Rating.create!(comment: comment)

  assert_equal rating.comment, comment, "The Rating's comment should be the original Comment"

  rating.comment.body = "Fennec foxes are the smallest of the foxes."
  assert_equal rating.comment.body, comment.body, "Changing the Comment's body on the association should change the original Comment's body"

  comment.body = "Kittens are adorable."
  assert_equal comment.body, rating.comment.body, "Changing the original Comment's body should change the Comment's body on the association"
end
test_has_many_and_belongs_to_should_find_inverse_automatically() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 57
def test_has_many_and_belongs_to_should_find_inverse_automatically
  comment_reflection = Comment.reflect_on_association(:ratings)
  rating_reflection = Rating.reflect_on_association(:comment)

  assert comment_reflection.has_inverse?, "The Comment reflection should have an inverse"
  assert_equal rating_reflection, comment_reflection.inverse_of, "The Comment reflection's inverse should be the Rating reflection"
end
test_has_many_and_belongs_to_should_find_inverse_automatically_for_model_in_module() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 38
def test_has_many_and_belongs_to_should_find_inverse_automatically_for_model_in_module
  account_reflection = Admin::Account.reflect_on_association(:users)
  user_reflection = Admin::User.reflect_on_association(:account)

  assert account_reflection.has_inverse?, "The Admin::Account reflection should have an inverse"
  assert_equal user_reflection, account_reflection.inverse_of, "The Admin::Account reflection's inverse should be the Admin::User reflection"
end
test_has_many_and_belongs_to_should_find_inverse_automatically_for_sti() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 65
def test_has_many_and_belongs_to_should_find_inverse_automatically_for_sti
  author_reflection = Author.reflect_on_association(:posts)
  author_child_reflection = Author.reflect_on_association(:special_posts)
  post_reflection = Post.reflect_on_association(:author)

  assert_respond_to author_reflection, :has_inverse?
  assert author_reflection.has_inverse?, "The Author reflection should have an inverse"
  assert_equal post_reflection, author_reflection.inverse_of, "The Author reflection's inverse should be the Post reflection"

  assert_respond_to author_child_reflection, :has_inverse?
  assert author_child_reflection.has_inverse?, "The Author reflection should have an inverse"
  assert_equal post_reflection, author_child_reflection.inverse_of, "The Author reflection's inverse should be the Post reflection"
end
test_has_one_and_belongs_to_automatic_inverse_shares_objects() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 79
def test_has_one_and_belongs_to_automatic_inverse_shares_objects
  car = Car.first
  bulb = Bulb.create!(car: car)

  assert_equal car.bulb, bulb, "The Car's bulb should be the original bulb"

  car.bulb.color = "Blue"
  assert_equal car.bulb.color, bulb.color, "Changing the bulb's color on the car association should change the bulb's color"

  bulb.color = "Red"
  assert_equal bulb.color, car.bulb.color, "Changing the bulb's color should change the bulb's color on the car association"
end
test_has_one_and_belongs_to_should_find_inverse_automatically() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 46
def test_has_one_and_belongs_to_should_find_inverse_automatically
  car_reflection = Car.reflect_on_association(:bulb)
  bulb_reflection = Bulb.reflect_on_association(:car)

  assert car_reflection.has_inverse?, "The Car reflection should have an inverse"
  assert_equal bulb_reflection, car_reflection.inverse_of, "The Car reflection's inverse should be the Bulb reflection"

  assert bulb_reflection.has_inverse?, "The Bulb reflection should have an inverse"
  assert_equal car_reflection, bulb_reflection.inverse_of, "The Bulb reflection's inverse should be the Car reflection"
end
test_has_one_and_belongs_to_should_find_inverse_automatically_on_multiple_word_name() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 27
def test_has_one_and_belongs_to_should_find_inverse_automatically_on_multiple_word_name
  monkey_reflection = MixedCaseMonkey.reflect_on_association(:man)
  man_reflection = Man.reflect_on_association(:mixed_case_monkey)

  assert monkey_reflection.has_inverse?, "The monkey reflection should have an inverse"
  assert_equal man_reflection, monkey_reflection.inverse_of, "The monkey reflection's inverse should be the man reflection"

  assert man_reflection.has_inverse?, "The man reflection should have an inverse"
  assert_equal monkey_reflection, man_reflection.inverse_of, "The man reflection's inverse should be the monkey reflection"
end
test_polymorphic_and_has_many_through_relationships_should_not_have_inverses() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 119
def test_polymorphic_and_has_many_through_relationships_should_not_have_inverses
  sponsor_reflection = Sponsor.reflect_on_association(:sponsorable)

  assert !sponsor_reflection.has_inverse?, "A polymorphic association should not find an inverse automatically"

  club_reflection = Club.reflect_on_association(:members)

  assert !club_reflection.has_inverse?, "A has_many_through association should not find an inverse automatically"
end
test_polymorphic_has_one_should_find_inverse_automatically() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 129
def test_polymorphic_has_one_should_find_inverse_automatically
  man_reflection = Man.reflect_on_association(:polymorphic_face_without_inverse)

  assert man_reflection.has_inverse?
end