cascaded_eager_loading_test.rb 8.2 KB
Newer Older
1
require "cases/helper"
J
Jeremy Kemper 已提交
2 3 4 5
require 'models/post'
require 'models/comment'
require 'models/author'
require 'models/categorization'
6
require 'models/category'
J
Jeremy Kemper 已提交
7 8 9
require 'models/company'
require 'models/topic'
require 'models/reply'
10
require 'models/person'
11 12
require 'models/vertex'
require 'models/edge'
13

14
class CascadedEagerLoadingTest < ActiveRecord::TestCase
15
  fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments,
16
           :categorizations, :people, :categories, :edges, :vertices
17 18

  def test_eager_association_loading_with_cascaded_two_levels
19
    authors = Author.scoped(:includes=>{:posts=>:comments}, :order=>"authors.id").to_a
20
    assert_equal 3, authors.size
21
    assert_equal 5, authors[0].posts.size
22
    assert_equal 3, authors[1].posts.size
23
    assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
24 25 26
  end

  def test_eager_association_loading_with_cascaded_two_levels_and_one_level
27
    authors = Author.scoped(:includes=>[{:posts=>:comments}, :categorizations], :order=>"authors.id").to_a
28
    assert_equal 3, authors.size
29
    assert_equal 5, authors[0].posts.size
30
    assert_equal 3, authors[1].posts.size
31
    assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
32
    assert_equal 1, authors[0].categorizations.size
33
    assert_equal 2, authors[1].categorizations.size
34 35
  end

36 37
  def test_eager_association_loading_with_hmt_does_not_table_name_collide_when_joining_associations
    assert_nothing_raised do
38
      Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).to_a
39
    end
40
    authors = Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).to_a
41
    assert_equal 1, assert_no_queries { authors.size }
42
    assert_equal 10, assert_no_queries { authors[0].comments.size }
43 44
  end

45 46
  def test_eager_association_loading_grafts_stashed_associations_to_correct_parent
    assert_nothing_raised do
47
      Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').order('people.id').to_a
48
    end
49
    assert_equal people(:michael), Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').order('people.id').first
50 51
  end

52 53 54 55
  def test_cascaded_eager_association_loading_with_join_for_count
    categories = Category.joins(:categorizations).includes([{:posts=>:comments}, :authors])

    assert_nothing_raised do
56
      assert_equal 4, categories.count
57
      assert_equal 4, categories.to_a.count
58
      assert_equal 3, categories.count(:distinct => true)
59
      assert_equal 3, categories.to_a.uniq.size # Must uniq since instantiating with inner joins will get dupes
60 61 62 63
    end
  end

  def test_cascaded_eager_association_loading_with_duplicated_includes
64
    categories = Category.includes(:categorizations).includes(:categorizations => :author).where("categorizations.id is not null").references(:categorizations)
65
    assert_nothing_raised do
66
      assert_equal 3, categories.count
67
      assert_equal 3, categories.to_a.size
68 69 70
    end
  end

71
  def test_cascaded_eager_association_loading_with_twice_includes_edge_cases
72
    categories = Category.includes(:categorizations => :author).includes(:categorizations => :post).where("posts.id is not null").references(:posts)
73
    assert_nothing_raised do
74
      assert_equal 3, categories.count
75
      assert_equal 3, categories.to_a.size
76 77 78
    end
  end

A
Aaron Patterson 已提交
79
  def test_eager_association_loading_with_join_for_count
80 81 82
    authors = Author.joins(:special_posts).includes([:posts, :categorizations])

    assert_nothing_raised { authors.count }
83
    assert_queries(3) { authors.to_a }
84 85
  end

86
  def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
87
    authors = Author.scoped(:includes=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id").to_a
88
    assert_equal 3, authors.size
89
    assert_equal 5, authors[0].posts.size
90
    assert_equal 3, authors[1].posts.size
91
    assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
92 93 94
  end

  def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
95
    authors = Author.scoped(:includes=>{:posts=>[:comments, :author]}, :order=>"authors.id").to_a
96
    assert_equal 3, authors.size
97 98 99 100 101 102
    assert_equal 5, authors[0].posts.size
    assert_equal authors(:david).name, authors[0].name
    assert_equal [authors(:david).name], authors[0].posts.collect{|post| post.author.name}.uniq
  end

  def test_eager_association_loading_with_cascaded_two_levels_with_condition
103
    authors = Author.scoped(:includes=>{:posts=>:comments}, :where=>"authors.id=1", :order=>"authors.id").to_a
104 105 106 107 108
    assert_equal 1, authors.size
    assert_equal 5, authors[0].posts.size
  end

  def test_eager_association_loading_with_cascaded_three_levels_by_ping_pong
109
    firms = Firm.scoped(:includes=>{:account=>{:firm=>:account}}, :order=>"companies.id").to_a
110 111
    assert_equal 2, firms.size
    assert_equal firms.first.account, firms.first.account.firm.account
112 113
    assert_equal companies(:first_firm).account, assert_no_queries { firms.first.account.firm.account }
    assert_equal companies(:first_firm).account.firm.account, assert_no_queries { firms.first.account.firm.account }
114 115
  end

116
  def test_eager_association_loading_with_has_many_sti
117
    topics = Topic.scoped(:includes => :replies, :order => 'topics.id').to_a
118
    first, second, = topics(:first).replies.size, topics(:second).replies.size
119
    assert_no_queries do
120 121
      assert_equal first, topics[0].replies.size
      assert_equal second, topics[1].replies.size
122 123 124
    end
  end

125 126 127 128 129
  def test_eager_association_loading_with_has_many_sti_and_subclasses
    silly = SillyReply.new(:title => "gaga", :content => "boo-boo", :parent_id => 1)
    silly.parent_id = 1
    assert silly.save

130
    topics = Topic.scoped(:includes => :replies, :order => ['topics.id', 'replies_topics.id']).to_a
131 132 133 134 135 136
    assert_no_queries do
      assert_equal 2, topics[0].replies.size
      assert_equal 0, topics[1].replies.size
    end
  end

137
  def test_eager_association_loading_with_belongs_to_sti
138
    replies = Reply.scoped(:includes => :topic, :order => 'topics.id').to_a
139 140
    assert replies.include?(topics(:second))
    assert !replies.include?(topics(:first))
141
    assert_equal topics(:first), assert_no_queries { replies.first.topic }
142
  end
143 144

  def test_eager_association_loading_with_multiple_stis_and_order
J
Jon Leighton 已提交
145
    author = Author.scoped(:includes => { :posts => [ :special_comments , :very_special_comment ] }, :order => ['authors.name', 'comments.body', 'very_special_comments_posts.body'], :where => 'posts.id = 4').first
146 147 148 149 150 151
    assert_equal authors(:david), author
    assert_no_queries do
      author.posts.first.special_comments
      author.posts.first.very_special_comment
    end
  end
J
Jeremy Kemper 已提交
152

153
  def test_eager_association_loading_of_stis_with_multiple_references
154
    authors = Author.scoped(:includes => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :where => 'posts.id = 4').to_a
155 156 157 158 159 160
    assert_equal [authors(:david)], authors
    assert_no_queries do
      authors.first.posts.first.special_comments.first.post.special_comments
      authors.first.posts.first.special_comments.first.post.very_special_comment
    end
  end
161

162
  def test_eager_association_loading_where_first_level_returns_nil
163
    authors = Author.scoped(:includes => {:post_about_thinking => :comments}, :order => 'authors.id DESC').to_a
164
    assert_equal [authors(:bob), authors(:mary), authors(:david)], authors
165
    assert_no_queries do
166
      authors[2].post_about_thinking.comments.first
167 168
    end
  end
J
Jeremy Kemper 已提交
169 170

  def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
J
Jon Leighton 已提交
171
    source = Vertex.scoped(:includes=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id').first
J
Jeremy Kemper 已提交
172 173 174 175
    assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first }
  end

  def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
J
Jon Leighton 已提交
176
    sink = Vertex.scoped(:includes=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC').first
J
Jeremy Kemper 已提交
177 178
    assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first }
  end
179
end