Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #4289: self_referential_has_many_through_tests.diff

File self_referential_has_many_through_tests.diff, 4.7 kB (added by lmarlow@yahoo.com, 2 years ago)

failing tests for self-referential has_many :through relationships

  • vendor/rails/activerecord/test/associations_join_model_test.rb

    old new  
    22require 'fixtures/tag' 
    33require 'fixtures/tagging' 
    44require 'fixtures/post' 
     5require 'fixtures/related_post' 
    56require 'fixtures/comment' 
    67require 'fixtures/author' 
    78require 'fixtures/category' 
     
    910 
    1011class AssociationsJoinModelTest < Test::Unit::TestCase 
    1112  self.use_transactional_fixtures = false 
    12   fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings 
     13  fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :related_posts 
    1314 
    1415  def test_has_many 
    1516    assert_equal categories(:general), authors(:david).categories.first 
     
    233234    assert_equal [authors(:mary)], posts(:authorless).authors 
    234235  end 
    235236 
     237  def test_self_referential_has_many_going_through_join_model 
     238    assert_equal [], posts(:thinking).related_posts 
     239    assert_equal [posts(:thinking)], posts(:authorless).related_posts 
     240    assert_equal [posts(:thinking), posts(:authorless), posts(:sti_comments)], posts(:welcome).related_posts 
     241  end 
     242 
    236243  def test_belongs_to_polymorphic_with_counter_cache 
    237244    assert_equal 0, posts(:welcome)[:taggings_count] 
    238245    tagging = posts(:welcome).taggings.create(:tag => tags(:general)) 
  • vendor/rails/activerecord/test/associations_go_eager_test.rb

    old new  
    11require 'abstract_unit' 
    22require 'fixtures/post' 
     3require 'fixtures/related_post' 
    34require 'fixtures/comment' 
    45require 'fixtures/author' 
    56require 'fixtures/category' 
     
    910 
    1011class EagerAssociationTest < Test::Unit::TestCase 
    1112  fixtures :posts, :comments, :authors, :categories, :categories_posts, 
    12             :companies, :accounts, :tags, :people, :readers 
     13            :companies, :accounts, :tags, :people, :readers, :related_posts 
    1314 
    1415  def test_loading_with_one_association 
    1516    posts = Post.find(:all, :include => :comments) 
     
    111112    assert_equal authors(:david), assert_no_queries { posts_with_author.first.author } 
    112113    assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author } 
    113114  end 
     115   
     116  def test_eager_with_self_referential_has_many_through 
     117    related_posts_with_comments = posts(:welcome).related_posts.find(:all, :include => :comments) 
     118    assert_equal 3, related_posts_with_comments.size 
     119    related_posts_with_comments.each do |p| 
     120      assert_no_queries { p.comments } 
     121    end 
     122  end 
    114123 
    115124  def test_eager_with_has_many_and_limit 
    116125    posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2) 
  • vendor/rails/activerecord/test/fixtures/db_definitions/mysql.sql

    old new  
    181181  PRIMARY KEY  (`id`) 
    182182) TYPE=InnoDB; 
    183183 
     184CREATE TABLE `related_posts` ( 
     185  `id` INTEGER NOT NULL auto_increment, 
     186  `post_id` INTEGER, 
     187  `related_post_id` INTEGER, 
     188  `position` INTEGER, 
     189  PRIMARY KEY  (`id`) 
     190) TYPE=InnoDB; 
     191 
    184192CREATE TABLE `categories` ( 
    185193  `id` int(11) NOT NULL auto_increment, 
    186194  `name` VARCHAR(255) NOT NULL, 
  • vendor/rails/activerecord/test/fixtures/db_definitions/mysql.drop.sql

    old new  
    2222DROP TABLE posts; 
    2323DROP TABLE comments; 
    2424DROP TABLE authors; 
     25DROP TABLE related_posts; 
    2526DROP TABLE categories; 
    2627DROP TABLE categories_posts; 
    2728DROP TABLE fk_test_has_fk; 
  • vendor/rails/activerecord/test/fixtures/post.rb

    old new  
    3535  has_many :readers 
    3636  has_many :people, :through => :readers 
    3737 
     38  has_many :related_post_infos, :class_name => 'RelatedPost' 
     39  has_many :related_to_post_infos, :class_name => 'RelatedPost', :foreign_key => 'related_post_id' 
     40  has_many :related_posts, :class_name => 'Post', :through => :related_post_infos 
     41 
    3842  def self.what_are_you 
    3943    'a post...' 
    4044  end