cascaded_eager_loading_test.rb 8.7 KB
Newer Older
1 2
# frozen_string_literal: true

3
require "cases/helper"
4 5 6 7 8 9 10 11 12 13 14
require "models/post"
require "models/comment"
require "models/author"
require "models/categorization"
require "models/category"
require "models/company"
require "models/topic"
require "models/reply"
require "models/person"
require "models/vertex"
require "models/edge"
15

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

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

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

38
  def test_eager_association_loading_with_hmt_does_not_table_name_collide_when_joining_associations
39
    authors = Author.joins(:posts).eager_load(:comments).where(posts: { tags_count: 1 }).order(:id).to_a
40 41
    assert_equal 3, assert_queries(0) { authors.size }
    assert_equal 10, assert_queries(0) { authors[0].comments.size }
42 43
  end

44
  def test_eager_association_loading_grafts_stashed_associations_to_correct_parent
45
    assert_equal people(:michael), Person.eager_load(primary_contact: :primary_contact).where("primary_contacts_people_2.first_name = ?", "Susan").order("people.id").first
46 47
  end

48
  def test_cascaded_eager_association_loading_with_join_for_count
49
    categories = Category.joins(:categorizations).includes([{ posts: :comments }, :authors])
50

51 52 53 54
    assert_equal 4, categories.count
    assert_equal 4, categories.to_a.count
    assert_equal 3, categories.distinct.count
    assert_equal 3, categories.to_a.uniq.size # Must uniq since instantiating with inner joins will get dupes
55 56 57
  end

  def test_cascaded_eager_association_loading_with_duplicated_includes
58
    categories = Category.includes(:categorizations).includes(categorizations: :author).where("categorizations.id is not null").references(:categorizations)
59
    assert_nothing_raised do
60
      assert_equal 3, categories.count
61
      assert_equal 3, categories.to_a.size
62 63 64
    end
  end

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

A
Aaron Patterson 已提交
73
  def test_eager_association_loading_with_join_for_count
74 75 76
    authors = Author.joins(:special_posts).includes([:posts, :categorizations])

    assert_nothing_raised { authors.count }
77
    assert_queries(3) { authors.to_a }
78 79
  end

80
  def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
81
    authors = Author.all.merge!(includes: { posts: [:comments, :categorizations] }, order: "authors.id").to_a
82
    assert_equal 3, authors.size
83
    assert_equal 5, authors[0].posts.size
84
    assert_equal 3, authors[1].posts.size
85
    assert_equal 10, authors[0].posts.collect { |post| post.comments.size }.inject(0) { |sum, i| sum + i }
86 87 88
  end

  def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
89
    authors = Author.all.merge!(includes: { posts: [:comments, :author] }, order: "authors.id").to_a
90
    assert_equal 3, authors.size
91 92
    assert_equal 5, authors[0].posts.size
    assert_equal authors(:david).name, authors[0].name
93
    assert_equal [authors(:david).name], authors[0].posts.collect { |post| post.author.name }.uniq
94 95 96
  end

  def test_eager_association_loading_with_cascaded_two_levels_with_condition
97
    authors = Author.all.merge!(includes: { posts: :comments }, where: "authors.id=1", order: "authors.id").to_a
98 99 100 101 102
    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
103
    firms = Firm.all.merge!(includes: { account: { firm: :account } }, order: "companies.id").to_a
104 105
    assert_equal 2, firms.size
    assert_equal firms.first.account, firms.first.account.firm.account
106 107
    assert_equal companies(:first_firm).account, assert_queries(0) { firms.first.account.firm.account }
    assert_equal companies(:first_firm).account.firm.account, assert_queries(0) { firms.first.account.firm.account }
108 109
  end

110
  def test_eager_association_loading_with_has_many_sti
111
    topics = Topic.all.merge!(includes: :replies, order: "topics.id").to_a
112
    first, second, = topics(:first).replies.size, topics(:second).replies.size
113
    assert_queries(0) do
114 115
      assert_equal first, topics[0].replies.size
      assert_equal second, topics[1].replies.size
116 117 118
    end
  end

119
  def test_eager_association_loading_with_has_many_sti_and_subclasses
120 121
    reply = Reply.new(title: "gaga", content: "boo-boo", parent_id: 1)
    assert reply.save
122

123
    topics = Topic.all.merge!(includes: :replies, order: ["topics.id", "replies_topics.id"]).to_a
124
    assert_queries(0) do
125 126 127 128 129
      assert_equal 2, topics[0].replies.size
      assert_equal 0, topics[1].replies.size
    end
  end

130
  def test_eager_association_loading_with_belongs_to_sti
131
    replies = Reply.all.merge!(includes: :topic, order: "topics.id").to_a
132 133
    assert_includes replies, topics(:second)
    assert_not_includes replies, topics(:first)
134
    assert_equal topics(:first), assert_queries(0) { replies.first.topic }
135
  end
136 137

  def test_eager_association_loading_with_multiple_stis_and_order
138
    author = Author.all.merge!(includes: { posts: [ :special_comments, :very_special_comment ] }, order: ["authors.name", "comments.body", "very_special_comments_posts.body"], where: "posts.id = 4").first
139
    assert_equal authors(:david), author
140
    assert_queries(0) do
141 142 143 144
      author.posts.first.special_comments
      author.posts.first.very_special_comment
    end
  end
J
Jeremy Kemper 已提交
145

146
  def test_eager_association_loading_of_stis_with_multiple_references
147
    authors = Author.all.merge!(includes: { posts: { special_comments: { post: [ :special_comments, :very_special_comment ] } } }, order: "comments.body, very_special_comments_posts.body", where: "posts.id = 4").to_a
148
    assert_equal [authors(:david)], authors
149
    assert_queries(0) do
150 151 152 153
      authors.first.posts.first.special_comments.first.post.special_comments
      authors.first.posts.first.special_comments.first.post.very_special_comment
    end
  end
154

155
  def test_eager_association_loading_where_first_level_returns_nil
156
    authors = Author.all.merge!(includes: { post_about_thinking: :comments }, order: "authors.id DESC").to_a
157
    assert_equal [authors(:bob), authors(:mary), authors(:david)], authors
158
    assert_queries(0) do
159
      authors[2].post_about_thinking.comments.first
160 161
    end
  end
J
Jeremy Kemper 已提交
162

163 164
  def test_preload_through_missing_records
    post = Post.where.not(author_id: Author.select(:id)).preload(author: { comments: :post }).first!
165
    assert_queries(0) { assert_nil post.author }
166 167
  end

M
Matt Jones 已提交
168 169 170 171 172
  def test_eager_association_loading_with_missing_first_record
    posts = Post.where(id: 3).preload(author: { comments: :post }).to_a
    assert_equal posts.size, 1
  end

J
Jeremy Kemper 已提交
173
  def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
174
    source = Vertex.all.merge!(includes: { sinks: { sinks: { sinks: :sinks } } }, order: "vertices.id").first
175
    assert_equal vertices(:vertex_4), assert_queries(0) { source.sinks.first.sinks.first.sinks.first }
J
Jeremy Kemper 已提交
176 177 178
  end

  def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
179
    sink = Vertex.all.merge!(includes: { sources: { sources: { sources: :sources } } }, order: "vertices.id DESC").first
180
    assert_equal vertices(:vertex_1), assert_queries(0) { sink.sources.first.sources.first.sources.first.sources.first }
J
Jeremy Kemper 已提交
181
  end
182 183

  def test_eager_association_loading_with_cascaded_interdependent_one_level_and_two_levels
184
    authors_relation = Author.all.merge!(includes: [:comments, { posts: :categorizations }], order: "authors.id")
185 186 187 188 189 190
    authors = authors_relation.to_a
    assert_equal 3, authors.size
    assert_equal 10, authors[0].comments.size
    assert_equal 1, authors[1].comments.size
    assert_equal 5, authors[0].posts.size
    assert_equal 3, authors[1].posts.size
191
    assert_equal 3, authors[0].posts.collect { |post| post.categorizations.size }.inject(0) { |sum, i| sum + i }
192
  end
193
end