~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activerecord/test/cases/associations/eager_test.rb

  • Committer: Michael Forrest
  • Date: 2010-10-15 16:28:50 UTC
  • Revision ID: michael.forrest@canonical.com-20101015162850-tj2vchanv0kr0dun
refrozeĀ gems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require "cases/helper"
 
2
require 'models/post'
 
3
require 'models/tagging'
 
4
require 'models/tag'
 
5
require 'models/comment'
 
6
require 'models/author'
 
7
require 'models/category'
 
8
require 'models/company'
 
9
require 'models/person'
 
10
require 'models/reader'
 
11
require 'models/owner'
 
12
require 'models/pet'
 
13
require 'models/reference'
 
14
require 'models/job'
 
15
require 'models/subscriber'
 
16
require 'models/subscription'
 
17
require 'models/book'
 
18
require 'models/developer'
 
19
require 'models/project'
 
20
 
 
21
class EagerAssociationTest < ActiveRecord::TestCase
 
22
  fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
 
23
            :companies, :accounts, :tags, :taggings, :people, :readers,
 
24
            :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
 
25
            :developers, :projects, :developers_projects
 
26
 
 
27
  def test_loading_with_one_association
 
28
    posts = Post.find(:all, :include => :comments)
 
29
    post = posts.find { |p| p.id == 1 }
 
30
    assert_equal 2, post.comments.size
 
31
    assert post.comments.include?(comments(:greetings))
 
32
 
 
33
    post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
 
34
    assert_equal 2, post.comments.size
 
35
    assert post.comments.include?(comments(:greetings))
 
36
 
 
37
    posts = Post.find(:all, :include => :last_comment)
 
38
    post = posts.find { |p| p.id == 1 }
 
39
    assert_equal Post.find(1).last_comment, post.last_comment
 
40
  end
 
41
 
 
42
  def test_loading_with_one_association_with_non_preload
 
43
    posts = Post.find(:all, :include => :last_comment, :order => 'comments.id DESC')
 
44
    post = posts.find { |p| p.id == 1 }
 
45
    assert_equal Post.find(1).last_comment, post.last_comment
 
46
  end
 
47
 
 
48
  def test_loading_conditions_with_or
 
49
    posts = authors(:david).posts.find(:all, :include => :comments, :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'")
 
50
    assert_nil posts.detect { |p| p.author_id != authors(:david).id },
 
51
      "expected to find only david's posts"
 
52
  end
 
53
 
 
54
  def test_with_ordering
 
55
    list = Post.find(:all, :include => :comments, :order => "posts.id DESC")
 
56
    [:eager_other, :sti_habtm, :sti_post_and_comments, :sti_comments,
 
57
     :authorless, :thinking, :welcome
 
58
    ].each_with_index do |post, index|
 
59
      assert_equal posts(post), list[index]
 
60
    end
 
61
  end
 
62
 
 
63
  def test_with_two_tables_in_from_without_getting_double_quoted
 
64
    posts = Post.find(:all,
 
65
      :select     => "posts.*",
 
66
      :from       => "authors, posts",
 
67
      :include    => :comments,
 
68
      :conditions => "posts.author_id = authors.id",
 
69
      :order      => "posts.id"
 
70
    )
 
71
 
 
72
    assert_equal 2, posts.first.comments.size
 
73
  end
 
74
 
 
75
  def test_loading_with_multiple_associations
 
76
    posts = Post.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id")
 
77
    assert_equal 2, posts.first.comments.size
 
78
    assert_equal 2, posts.first.categories.size
 
79
    assert posts.first.comments.include?(comments(:greetings))
 
80
  end
 
81
 
 
82
  def test_duplicate_middle_objects
 
83
    comments = Comment.find :all, :conditions => 'post_id = 1', :include => [:post => :author]
 
84
    assert_no_queries do
 
85
      comments.each {|comment| comment.post.author.name}
 
86
    end
 
87
  end
 
88
 
 
89
  def test_including_duplicate_objects_from_belongs_to
 
90
    popular_post = Post.create!(:title => 'foo', :body => "I like cars!")
 
91
    comment = popular_post.comments.create!(:body => "lol")
 
92
    popular_post.readers.create!(:person => people(:michael))
 
93
    popular_post.readers.create!(:person => people(:david))
 
94
 
 
95
    readers = Reader.find(:all, :conditions => ["post_id = ?", popular_post.id],
 
96
                                :include => {:post => :comments})
 
97
    readers.each do |reader|
 
98
      assert_equal [comment], reader.post.comments
 
99
    end
 
100
  end
 
101
 
 
102
  def test_including_duplicate_objects_from_has_many
 
103
    car_post = Post.create!(:title => 'foo', :body => "I like cars!")
 
104
    car_post.categories << categories(:general)
 
105
    car_post.categories << categories(:technology)
 
106
 
 
107
    comment = car_post.comments.create!(:body => "hmm")
 
108
    categories = Category.find(:all, :conditions => ["posts.id=?", car_post.id],
 
109
                                 :include => {:posts => :comments})
 
110
    categories.each do |category|
 
111
      assert_equal [comment], category.posts[0].comments
 
112
    end
 
113
  end
 
114
 
 
115
  def test_finding_with_includes_on_has_many_association_with_same_include_includes_only_once
 
116
    author_id = authors(:david).id
 
117
    author = assert_queries(3) { Author.find(author_id, :include => {:posts_with_comments => :comments}) } # find the author, then find the posts, then find the comments
 
118
    author.posts_with_comments.each do |post_with_comments|
 
119
      assert_equal post_with_comments.comments.length, post_with_comments.comments.count
 
120
      assert_equal nil, post_with_comments.comments.uniq!
 
121
    end
 
122
  end
 
123
 
 
124
  def test_finding_with_includes_on_has_one_assocation_with_same_include_includes_only_once
 
125
    author = authors(:david)
 
126
    post = author.post_about_thinking_with_last_comment
 
127
    last_comment = post.last_comment
 
128
    author = assert_queries(3) { Author.find(author.id, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments
 
129
    assert_no_queries do
 
130
      assert_equal post, author.post_about_thinking_with_last_comment
 
131
      assert_equal last_comment, author.post_about_thinking_with_last_comment.last_comment
 
132
    end
 
133
  end
 
134
 
 
135
  def test_finding_with_includes_on_belongs_to_association_with_same_include_includes_only_once
 
136
    post = posts(:welcome)
 
137
    author = post.author
 
138
    author_address = author.author_address
 
139
    post = assert_queries(3) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address
 
140
    assert_no_queries do
 
141
      assert_equal author, post.author_with_address
 
142
      assert_equal author_address, post.author_with_address.author_address
 
143
    end
 
144
  end
 
145
 
 
146
  def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
 
147
    post = posts(:welcome)
 
148
    post.update_attributes!(:author => nil)
 
149
    post = assert_queries(1) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author which is null so no query for the author or address
 
150
    assert_no_queries do
 
151
      assert_equal nil, post.author_with_address
 
152
    end
 
153
  end
 
154
 
 
155
  def test_loading_from_an_association
 
156
    posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
 
157
    assert_equal 2, posts.first.comments.size
 
158
  end
 
159
 
 
160
  def test_loading_from_an_association_that_has_a_hash_of_conditions
 
161
    assert_nothing_raised do
 
162
      Author.find(:all, :include => :hello_posts_with_hash_conditions)
 
163
    end
 
164
    assert !Author.find(authors(:david).id, :include => :hello_posts_with_hash_conditions).hello_posts.empty?
 
165
  end
 
166
 
 
167
  def test_loading_with_no_associations
 
168
    assert_nil Post.find(posts(:authorless).id, :include => :author).author
 
169
  end
 
170
 
 
171
  def test_nested_loading_with_no_associations
 
172
    assert_nothing_raised do
 
173
      Post.find(posts(:authorless).id, :include => {:author => :author_addresss})
 
174
    end
 
175
  end
 
176
 
 
177
  def test_eager_association_loading_with_belongs_to_and_foreign_keys
 
178
    pets = Pet.find(:all, :include => :owner)
 
179
    assert_equal 3, pets.length
 
180
  end
 
181
 
 
182
  def test_eager_association_loading_with_belongs_to
 
183
    comments = Comment.find(:all, :include => :post)
 
184
    assert_equal 10, comments.length
 
185
    titles = comments.map { |c| c.post.title }
 
186
    assert titles.include?(posts(:welcome).title)
 
187
    assert titles.include?(posts(:sti_post_and_comments).title)
 
188
  end
 
189
 
 
190
  def test_eager_association_loading_with_belongs_to_and_limit
 
191
    comments = Comment.find(:all, :include => :post, :limit => 5, :order => 'comments.id')
 
192
    assert_equal 5, comments.length
 
193
    assert_equal [1,2,3,5,6], comments.collect { |c| c.id }
 
194
  end
 
195
 
 
196
  def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
 
197
    comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :order => 'comments.id')
 
198
    assert_equal 3, comments.length
 
199
    assert_equal [5,6,7], comments.collect { |c| c.id }
 
200
  end
 
201
 
 
202
  def test_eager_association_loading_with_belongs_to_and_limit_and_offset
 
203
    comments = Comment.find(:all, :include => :post, :limit => 3, :offset => 2, :order => 'comments.id')
 
204
    assert_equal 3, comments.length
 
205
    assert_equal [3,5,6], comments.collect { |c| c.id }
 
206
  end
 
207
 
 
208
  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
 
209
    comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :offset => 1, :order => 'comments.id')
 
210
    assert_equal 3, comments.length
 
211
    assert_equal [6,7,8], comments.collect { |c| c.id }
 
212
  end
 
213
 
 
214
  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
 
215
    comments = Comment.find(:all, :include => :post, :conditions => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id')
 
216
    assert_equal 3, comments.length
 
217
    assert_equal [6,7,8], comments.collect { |c| c.id }
 
218
  end
 
219
 
 
220
  def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
 
221
    assert_nothing_raised do
 
222
      Comment.find(:all, :include => :post, :conditions => ['posts.id = ?',4])
 
223
    end
 
224
  end
 
225
 
 
226
  def test_eager_association_loading_with_belongs_to_and_conditions_hash
 
227
    comments = []
 
228
    assert_nothing_raised do
 
229
      comments = Comment.find(:all, :include => :post, :conditions => {:posts => {:id => 4}}, :limit => 3, :order => 'comments.id')
 
230
    end
 
231
    assert_equal 3, comments.length
 
232
    assert_equal [5,6,7], comments.collect { |c| c.id }
 
233
    assert_no_queries do
 
234
      comments.first.post
 
235
    end
 
236
  end
 
237
 
 
238
  def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
 
239
    quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
 
240
    assert_nothing_raised do
 
241
      Comment.find(:all, :include => :post, :conditions => ["#{quoted_posts_id} = ?",4])
 
242
    end
 
243
  end
 
244
 
 
245
  def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
 
246
    assert_nothing_raised do
 
247
      Comment.find(:all, :include => :post, :order => 'posts.id')
 
248
    end
 
249
  end
 
250
 
 
251
  def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
 
252
    quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
 
253
    assert_nothing_raised do
 
254
      Comment.find(:all, :include => :post, :order => quoted_posts_id)
 
255
    end
 
256
  end
 
257
 
 
258
  def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
 
259
    posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :order => 'posts.id')
 
260
    assert_equal 1, posts.length
 
261
    assert_equal [1], posts.collect { |p| p.id }
 
262
  end
 
263
 
 
264
  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
 
265
    posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id')
 
266
    assert_equal 1, posts.length
 
267
    assert_equal [2], posts.collect { |p| p.id }
 
268
  end
 
269
 
 
270
  def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
 
271
    author_favorite = AuthorFavorite.find(:first, :include => :favorite_author)
 
272
    assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author }
 
273
  end
 
274
 
 
275
  def test_eager_load_belongs_to_quotes_table_and_column_names
 
276
    job = Job.find jobs(:unicyclist).id, :include => :ideal_reference
 
277
    references(:michael_unicyclist)
 
278
    assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference}
 
279
  end
 
280
 
 
281
  def test_eager_load_has_one_quotes_table_and_column_names
 
282
    michael = Person.find(people(:michael), :include => :favourite_reference)
 
283
    references(:michael_unicyclist)
 
284
    assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
 
285
  end
 
286
 
 
287
  def test_eager_load_has_many_quotes_table_and_column_names
 
288
    michael = Person.find(people(:michael), :include => :references)
 
289
    references(:michael_magician,:michael_unicyclist)
 
290
    assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&:id) }
 
291
  end
 
292
 
 
293
  def test_eager_load_has_many_through_quotes_table_and_column_names
 
294
    michael = Person.find(people(:michael), :include => :jobs)
 
295
    jobs(:magician, :unicyclist)
 
296
    assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&:id) }
 
297
  end
 
298
 
 
299
  def test_eager_load_has_many_with_string_keys
 
300
    subscriptions = subscriptions(:webster_awdr, :webster_rfr)
 
301
    subscriber =Subscriber.find(subscribers(:second).id, :include => :subscriptions)
 
302
    assert_equal subscriptions, subscriber.subscriptions.sort_by(&:id)
 
303
  end
 
304
  
 
305
  def test_eager_load_has_many_through_with_string_keys
 
306
    books = books(:awdr, :rfr)
 
307
    subscriber = Subscriber.find(subscribers(:second).id, :include => :books)
 
308
    assert_equal books, subscriber.books.sort_by(&:id)
 
309
  end
 
310
  
 
311
  def test_eager_load_belongs_to_with_string_keys
 
312
    subscriber = subscribers(:second)
 
313
    subscription = Subscription.find(subscriptions(:webster_awdr).id, :include => :subscriber)
 
314
    assert_equal subscriber, subscription.subscriber
 
315
  end
 
316
 
 
317
  def test_eager_association_loading_with_explicit_join
 
318
    posts = Post.find(:all, :include => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id')
 
319
    assert_equal 1, posts.length
 
320
  end
 
321
 
 
322
  def test_eager_with_has_many_through
 
323
    posts_with_comments = people(:michael).posts.find(:all, :include => :comments, :order => 'posts.id')
 
324
    posts_with_author = people(:michael).posts.find(:all, :include => :author, :order => 'posts.id')
 
325
    posts_with_comments_and_author = people(:michael).posts.find(:all, :include => [ :comments, :author ], :order => 'posts.id')
 
326
    assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size }
 
327
    assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
 
328
    assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
 
329
  end
 
330
 
 
331
  def test_eager_with_has_many_through_a_belongs_to_association
 
332
    author = authors(:mary)
 
333
    post = Post.create!(:author => author, :title => "TITLE", :body => "BODY")
 
334
    author.author_favorites.create(:favorite_author_id => 1)
 
335
    author.author_favorites.create(:favorite_author_id => 2)
 
336
    posts_with_author_favorites = author.posts.find(:all, :include => :author_favorites)
 
337
    assert_no_queries { posts_with_author_favorites.first.author_favorites.first.author_id }
 
338
  end
 
339
 
 
340
  def test_eager_with_has_many_through_an_sti_join_model
 
341
    author = Author.find(:first, :include => :special_post_comments, :order => 'authors.id')
 
342
    assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
 
343
  end
 
344
 
 
345
  def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
 
346
    author = Author.find(:first, :include => :special_nonexistant_post_comments, :order => 'authors.id')
 
347
    assert_equal [], author.special_nonexistant_post_comments
 
348
  end
 
349
 
 
350
  def test_eager_with_has_many_through_join_model_with_conditions
 
351
    assert_equal Author.find(:first, :include => :hello_post_comments,
 
352
                             :order => 'authors.id').hello_post_comments.sort_by(&:id),
 
353
                 Author.find(:first, :order => 'authors.id').hello_post_comments.sort_by(&:id)
 
354
  end
 
355
 
 
356
  def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
 
357
    assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions.first
 
358
  end
 
359
 
 
360
  def test_eager_with_has_many_through_join_model_with_include
 
361
    author_comments = Author.find(authors(:david).id, :include => :comments_with_include).comments_with_include.to_a
 
362
    assert_no_queries do
 
363
      author_comments.first.post.title
 
364
    end
 
365
  end
 
366
 
 
367
  def test_eager_with_has_many_and_limit
 
368
    posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
 
369
    assert_equal 2, posts.size
 
370
    assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
 
371
  end
 
372
 
 
373
  def test_eager_with_has_many_and_limit_and_conditions
 
374
    if current_adapter?(:OpenBaseAdapter)
 
375
      posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "FETCHBLOB(posts.body) = 'hello'", :order => "posts.id")
 
376
    else
 
377
      posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.body = 'hello'", :order => "posts.id")
 
378
    end
 
379
    assert_equal 2, posts.size
 
380
    assert_equal [4,5], posts.collect { |p| p.id }
 
381
  end
 
382
 
 
383
  def test_eager_with_has_many_and_limit_and_conditions_array
 
384
    if current_adapter?(:OpenBaseAdapter)
 
385
      posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "FETCHBLOB(posts.body) = ?", 'hello' ], :order => "posts.id")
 
386
    else
 
387
      posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "posts.body = ?", 'hello' ], :order => "posts.id")
 
388
    end
 
389
    assert_equal 2, posts.size
 
390
    assert_equal [4,5], posts.collect { |p| p.id }
 
391
  end
 
392
 
 
393
  def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
 
394
    posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
 
395
    assert_equal 2, posts.size
 
396
 
 
397
    count = Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
 
398
    assert_equal count, posts.size
 
399
  end
 
400
 
 
401
  def test_eager_with_has_many_and_limit_and_high_offset
 
402
    posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
 
403
    assert_equal 0, posts.size
 
404
  end
 
405
 
 
406
  def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions
 
407
    assert_queries(1) do
 
408
      posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
 
409
        :conditions => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ])
 
410
      assert_equal 0, posts.size
 
411
    end
 
412
  end
 
413
 
 
414
  def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions
 
415
    assert_queries(1) do
 
416
      posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
 
417
        :conditions => { 'authors.name' => 'David', 'comments.body' => 'go crazy' })
 
418
      assert_equal 0, posts.size
 
419
    end
 
420
  end
 
421
 
 
422
  def test_count_eager_with_has_many_and_limit_and_high_offset
 
423
    posts = Post.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
 
424
    assert_equal 0, posts
 
425
  end
 
426
 
 
427
  def test_eager_with_has_many_and_limit_with_no_results
 
428
    posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'")
 
429
    assert_equal 0, posts.size
 
430
  end
 
431
 
 
432
  def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
 
433
    author = authors(:david)
 
434
    author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
 
435
    assert_equal author_posts_without_comments.size, author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null')
 
436
  end
 
437
  
 
438
  def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
 
439
    person = people(:michael)
 
440
    person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
 
441
    assert_equal person_posts_without_comments.size, person.posts_with_no_comments.count
 
442
  end
 
443
 
 
444
  def test_eager_with_has_and_belongs_to_many_and_limit
 
445
    posts = Post.find(:all, :include => :categories, :order => "posts.id", :limit => 3)
 
446
    assert_equal 3, posts.size
 
447
    assert_equal 2, posts[0].categories.size
 
448
    assert_equal 1, posts[1].categories.size
 
449
    assert_equal 0, posts[2].categories.size
 
450
    assert posts[0].categories.include?(categories(:technology))
 
451
    assert posts[1].categories.include?(categories(:general))
 
452
  end
 
453
 
 
454
  def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
 
455
    posts = authors(:david).posts.find(:all,
 
456
      :include    => :comments,
 
457
      :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
 
458
      :limit      => 2
 
459
    )
 
460
    assert_equal 2, posts.size
 
461
 
 
462
    count = Post.count(
 
463
      :include    => [ :comments, :author ],
 
464
      :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
 
465
      :limit      => 2
 
466
    )
 
467
    assert_equal count, posts.size
 
468
  end
 
469
 
 
470
  def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
 
471
    posts = nil
 
472
    Post.with_scope(:find => {
 
473
      :include    => :comments,
 
474
      :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
 
475
    }) do
 
476
      posts = authors(:david).posts.find(:all, :limit => 2)
 
477
      assert_equal 2, posts.size
 
478
    end
 
479
 
 
480
    Post.with_scope(:find => {
 
481
      :include    => [ :comments, :author ],
 
482
      :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
 
483
    }) do
 
484
      count = Post.count(:limit => 2)
 
485
      assert_equal count, posts.size
 
486
    end
 
487
  end
 
488
 
 
489
  def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
 
490
    Post.with_scope(:find => { :conditions => "1=1" }) do
 
491
      posts = authors(:david).posts.find(:all,
 
492
        :include    => :comments,
 
493
        :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
 
494
        :limit      => 2
 
495
      )
 
496
      assert_equal 2, posts.size
 
497
 
 
498
      count = Post.count(
 
499
        :include    => [ :comments, :author ],
 
500
        :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
 
501
        :limit      => 2
 
502
      )
 
503
      assert_equal count, posts.size
 
504
    end
 
505
  end
 
506
 
 
507
  def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
 
508
    posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2)
 
509
    posts_with_scoped_order = Post.with_scope(:find => {:order => 'posts.id DESC'}) do
 
510
      Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
 
511
    end
 
512
    assert_equal posts_with_explicit_order, posts_with_scoped_order
 
513
  end
 
514
 
 
515
  def test_eager_association_loading_with_habtm
 
516
    posts = Post.find(:all, :include => :categories, :order => "posts.id")
 
517
    assert_equal 2, posts[0].categories.size
 
518
    assert_equal 1, posts[1].categories.size
 
519
    assert_equal 0, posts[2].categories.size
 
520
    assert posts[0].categories.include?(categories(:technology))
 
521
    assert posts[1].categories.include?(categories(:general))
 
522
  end
 
523
 
 
524
  def test_eager_with_inheritance
 
525
    posts = SpecialPost.find(:all, :include => [ :comments ])
 
526
  end
 
527
 
 
528
  def test_eager_has_one_with_association_inheritance
 
529
    post = Post.find(4, :include => [ :very_special_comment ])
 
530
    assert_equal "VerySpecialComment", post.very_special_comment.class.to_s
 
531
  end
 
532
 
 
533
  def test_eager_has_many_with_association_inheritance
 
534
    post = Post.find(4, :include => [ :special_comments ])
 
535
    post.special_comments.each do |special_comment|
 
536
      assert_equal "SpecialComment", special_comment.class.to_s
 
537
    end
 
538
  end
 
539
 
 
540
  def test_eager_habtm_with_association_inheritance
 
541
    post = Post.find(6, :include => [ :special_categories ])
 
542
    assert_equal 1, post.special_categories.size
 
543
    post.special_categories.each do |special_category|
 
544
      assert_equal "SpecialCategory", special_category.class.to_s
 
545
    end
 
546
  end
 
547
 
 
548
  def test_eager_with_has_one_dependent_does_not_destroy_dependent
 
549
    assert_not_nil companies(:first_firm).account
 
550
    f = Firm.find(:first, :include => :account,
 
551
            :conditions => ["companies.name = ?", "37signals"])
 
552
    assert_not_nil f.account
 
553
    assert_equal companies(:first_firm, :reload).account, f.account
 
554
  end
 
555
 
 
556
  def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
 
557
    author = authors(:david)
 
558
    posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
 
559
    assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size
 
560
    assert_equal posts_with_no_comments, author.posts_with_no_comments
 
561
  end
 
562
 
 
563
  def test_eager_with_invalid_association_reference
 
564
    assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys") {
 
565
      post = Post.find(6, :include=> :monkeys )
 
566
    }
 
567
    assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys") {
 
568
      post = Post.find(6, :include=>[ :monkeys ])
 
569
    }
 
570
    assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys") {
 
571
      post = Post.find(6, :include=>[ 'monkeys' ])
 
572
    }
 
573
    assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys, :elephants") {
 
574
      post = Post.find(6, :include=>[ :monkeys, :elephants ])
 
575
    }
 
576
  end
 
577
 
 
578
  def find_all_ordered(className, include=nil)
 
579
    className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
 
580
  end
 
581
 
 
582
  def test_limited_eager_with_order
 
583
    assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title)', :limit => 2, :offset => 1)
 
584
    assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC', :limit => 2, :offset => 1)
 
585
  end
 
586
 
 
587
  def test_limited_eager_with_multiple_order_columns
 
588
    assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title), posts.id', :limit => 2, :offset => 1)
 
589
    assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC, posts.id', :limit => 2, :offset => 1)
 
590
  end
 
591
 
 
592
  def test_preload_with_interpolation
 
593
    assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions
 
594
  end
 
595
 
 
596
  def test_polymorphic_type_condition
 
597
    post = Post.find(posts(:thinking).id, :include => :taggings)
 
598
    assert post.taggings.include?(taggings(:thinking_general))
 
599
    post = SpecialPost.find(posts(:thinking).id, :include => :taggings)
 
600
    assert post.taggings.include?(taggings(:thinking_general))
 
601
  end
 
602
 
 
603
  def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
 
604
    # Eager includes of has many and habtm associations aren't necessarily sorted in the same way
 
605
    def assert_equal_after_sort(item1, item2, item3 = nil)
 
606
      assert_equal(item1.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id})
 
607
      assert_equal(item3.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id}) if item3
 
608
    end
 
609
    # Test regular association, association with conditions, association with
 
610
    # STI, and association with conditions assured not to be true
 
611
    post_types = [:posts, :other_posts, :special_posts]
 
612
    # test both has_many and has_and_belongs_to_many
 
613
    [Author, Category].each do |className|
 
614
      d1 = find_all_ordered(className)
 
615
      # test including all post types at once
 
616
      d2 = find_all_ordered(className, post_types)
 
617
      d1.each_index do |i|
 
618
        assert_equal(d1[i], d2[i])
 
619
        assert_equal_after_sort(d1[i].posts, d2[i].posts)
 
620
        post_types[1..-1].each do |post_type|
 
621
          # test including post_types together
 
622
          d3 = find_all_ordered(className, [:posts, post_type])
 
623
          assert_equal(d1[i], d3[i])
 
624
          assert_equal_after_sort(d1[i].posts, d3[i].posts)
 
625
          assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
 
626
        end
 
627
      end
 
628
    end
 
629
  end
 
630
 
 
631
  def test_eager_with_multiple_associations_with_same_table_has_one
 
632
    d1 = find_all_ordered(Firm)
 
633
    d2 = find_all_ordered(Firm, :account)
 
634
    d1.each_index do |i|
 
635
      assert_equal(d1[i], d2[i])
 
636
      assert_equal(d1[i].account, d2[i].account)
 
637
    end
 
638
  end
 
639
 
 
640
  def test_eager_with_multiple_associations_with_same_table_belongs_to
 
641
    firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
 
642
    d1 = find_all_ordered(Client)
 
643
    d2 = find_all_ordered(Client, firm_types)
 
644
    d1.each_index do |i|
 
645
      assert_equal(d1[i], d2[i])
 
646
      firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
 
647
    end
 
648
  end
 
649
  def test_eager_with_valid_association_as_string_not_symbol
 
650
    assert_nothing_raised { Post.find(:all, :include => 'comments') }
 
651
  end
 
652
 
 
653
  def test_eager_with_floating_point_numbers
 
654
    assert_queries(2) do
 
655
      # Before changes, the floating point numbers will be interpreted as table names and will cause this to run in one query
 
656
      Comment.find :all, :conditions => "123.456 = 123.456", :include => :post
 
657
    end
 
658
  end
 
659
 
 
660
  def test_preconfigured_includes_with_belongs_to
 
661
    author = posts(:welcome).author_with_posts
 
662
    assert_no_queries {assert_equal 5, author.posts.size}
 
663
  end
 
664
 
 
665
  def test_preconfigured_includes_with_has_one
 
666
    comment = posts(:sti_comments).very_special_comment_with_post
 
667
    assert_no_queries {assert_equal posts(:sti_comments), comment.post}
 
668
  end
 
669
 
 
670
  def test_preconfigured_includes_with_has_many
 
671
    posts = authors(:david).posts_with_comments
 
672
    one = posts.detect { |p| p.id == 1 }
 
673
    assert_no_queries do
 
674
      assert_equal 5, posts.size
 
675
      assert_equal 2, one.comments.size
 
676
    end
 
677
  end
 
678
 
 
679
  def test_preconfigured_includes_with_habtm
 
680
    posts = authors(:david).posts_with_categories
 
681
    one = posts.detect { |p| p.id == 1 }
 
682
    assert_no_queries do
 
683
      assert_equal 5, posts.size
 
684
      assert_equal 2, one.categories.size
 
685
    end
 
686
  end
 
687
 
 
688
  def test_preconfigured_includes_with_has_many_and_habtm
 
689
    posts = authors(:david).posts_with_comments_and_categories
 
690
    one = posts.detect { |p| p.id == 1 }
 
691
    assert_no_queries do
 
692
      assert_equal 5, posts.size
 
693
      assert_equal 2, one.comments.size
 
694
      assert_equal 2, one.categories.size
 
695
    end
 
696
  end
 
697
 
 
698
  def test_count_with_include
 
699
    if current_adapter?(:SybaseAdapter)
 
700
      assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15")
 
701
    elsif current_adapter?(:OpenBaseAdapter)
 
702
      assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15")
 
703
    else
 
704
      assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15")
 
705
    end
 
706
  end
 
707
 
 
708
  def test_load_with_sti_sharing_association
 
709
    assert_queries(2) do #should not do 1 query per subclass
 
710
      Comment.find :all, :include => :post
 
711
    end
 
712
  end
 
713
 
 
714
  def test_conditions_on_join_table_with_include_and_limit
 
715
    assert_equal 3, Developer.find(:all, :include => 'projects', :conditions => 'developers_projects.access_level = 1', :limit => 5).size
 
716
  end
 
717
 
 
718
  def test_order_on_join_table_with_include_and_limit
 
719
    assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size
 
720
  end
 
721
 
 
722
  def test_eager_loading_with_order_on_joined_table_preloads
 
723
    posts = assert_queries(2) do
 
724
      Post.find(:all, :joins => :comments, :include => :author, :order => 'comments.id DESC')
 
725
    end
 
726
    assert_equal posts(:eager_other), posts[0]
 
727
    assert_equal authors(:mary), assert_no_queries { posts[0].author}
 
728
  end
 
729
 
 
730
  def test_eager_loading_with_conditions_on_joined_table_preloads
 
731
    posts = assert_queries(2) do
 
732
      Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
 
733
    end
 
734
    assert_equal [posts(:welcome)], posts
 
735
    assert_equal authors(:david), assert_no_queries { posts[0].author}
 
736
 
 
737
    posts = assert_queries(2) do
 
738
      Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
 
739
    end
 
740
    assert_equal [posts(:welcome)], posts
 
741
    assert_equal authors(:david), assert_no_queries { posts[0].author}
 
742
 
 
743
    posts = assert_queries(2) do
 
744
      Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id')
 
745
    end
 
746
    assert_equal posts(:welcome, :thinking), posts
 
747
 
 
748
    posts = assert_queries(2) do
 
749
      Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id')
 
750
    end
 
751
    assert_equal posts(:welcome, :thinking), posts
 
752
 
 
753
  end
 
754
 
 
755
  def test_eager_loading_with_conditions_on_string_joined_table_preloads
 
756
    posts = assert_queries(2) do
 
757
      Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
 
758
    end
 
759
    assert_equal [posts(:welcome)], posts
 
760
    assert_equal authors(:david), assert_no_queries { posts[0].author}
 
761
 
 
762
    posts = assert_queries(2) do
 
763
      Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
 
764
    end
 
765
    assert_equal [posts(:welcome)], posts
 
766
    assert_equal authors(:david), assert_no_queries { posts[0].author}
 
767
 
 
768
  end
 
769
 
 
770
  def test_eager_loading_with_select_on_joined_table_preloads
 
771
    posts = assert_queries(2) do
 
772
      Post.find(:all, :select => 'posts.*, authors.name as author_name', :include => :comments, :joins => :author, :order => 'posts.id')
 
773
    end
 
774
    assert_equal 'David', posts[0].author_name
 
775
    assert_equal posts(:welcome).comments, assert_no_queries { posts[0].comments}
 
776
  end
 
777
 
 
778
  def test_eager_loading_with_conditions_on_join_model_preloads
 
779
    authors = assert_queries(2) do
 
780
      Author.find(:all, :include => :author_address, :joins => :comments, :conditions => "posts.title like 'Welcome%'")
 
781
    end
 
782
    assert_equal authors(:david), authors[0]
 
783
    assert_equal author_addresses(:david_address), authors[0].author_address
 
784
  end
 
785
 
 
786
  def test_preload_belongs_to_uses_exclusive_scope
 
787
    people = Person.males.find(:all, :include => :primary_contact)
 
788
    assert_not_equal people.length, 0
 
789
    people.each do |person|
 
790
      assert_no_queries {assert_not_nil person.primary_contact}
 
791
      assert_equal Person.find(person.id).primary_contact, person.primary_contact
 
792
    end
 
793
  end
 
794
 
 
795
  def test_preload_has_many_uses_exclusive_scope
 
796
    people = Person.males.find :all, :include => :agents
 
797
    people.each do |person|
 
798
      assert_equal Person.find(person.id).agents, person.agents
 
799
    end
 
800
  end
 
801
 
 
802
  def test_preload_has_many_using_primary_key
 
803
    expected = Firm.find(:first).clients_using_primary_key.to_a
 
804
    firm = Firm.find :first, :include => :clients_using_primary_key
 
805
    assert_no_queries do
 
806
      assert_equal expected, firm.clients_using_primary_key
 
807
    end
 
808
  end
 
809
 
 
810
  def test_include_has_many_using_primary_key
 
811
    expected = Firm.find(1).clients_using_primary_key.sort_by &:name
 
812
    firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
 
813
    assert_no_queries do
 
814
      assert_equal expected, firm.clients_using_primary_key
 
815
    end
 
816
  end
 
817
  
 
818
  def test_preload_has_one_using_primary_key
 
819
    expected = Firm.find(:first).account_using_primary_key
 
820
    firm = Firm.find :first, :include => :account_using_primary_key
 
821
    assert_no_queries do
 
822
      assert_equal expected, firm.account_using_primary_key
 
823
    end
 
824
  end
 
825
 
 
826
  def test_include_has_one_using_primary_key
 
827
    expected = Firm.find(1).account_using_primary_key
 
828
    firm = Firm.find(:all, :include => :account_using_primary_key, :order => 'accounts.id').detect {|f| f.id == 1}
 
829
    assert_no_queries do
 
830
      assert_equal expected, firm.account_using_primary_key
 
831
    end
 
832
  end
 
833
  
 
834
end