aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/nested_through_associations_test.rb
blob: 90b2d4bc07fa31ea31978bb632164be954834243 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# frozen_string_literal: true
require "cases/helper"
require "models/author"
require "models/post"
require "models/person"
require "models/reference"
require "models/job"
require "models/reader"
require "models/comment"
require "models/tag"
require "models/tagging"
require "models/subscriber"
require "models/book"
require "models/subscription"
require "models/rating"
require "models/member"
require "models/member_detail"
require "models/member_type"
require "models/sponsor"
require "models/club"
require "models/organization"
require "models/category"
require "models/categorization"
require "models/membership"
require "models/essay"

class NestedThroughAssociationsTest < ActiveRecord::TestCase
  fixtures :authors, :author_addresses, :books, :posts, :subscriptions, :subscribers, :tags, :taggings,
           :people, :readers, :references, :jobs, :ratings, :comments, :members, :member_details,
           :member_types, :sponsors, :clubs, :organizations, :categories, :categories_posts,
           :categorizations, :memberships, :essays

  # Through associations can either use the has_many or has_one macros.
  #
  # has_many
  #   - Source reflection can be has_many, has_one, belongs_to or has_and_belongs_to_many
  #   - Through reflection can be has_many, has_one, belongs_to or has_and_belongs_to_many
  #
  # has_one
  #   - Source reflection can be has_one or belongs_to
  #   - Through reflection can be has_one or belongs_to
  #
  # Additionally, the source reflection and/or through reflection may be subject to
  # polymorphism and/or STI.
  #
  # When testing these, we need to make sure it works via loading the association directly, or
  # joining the association, or including the association. We also need to ensure that associations
  # are readonly where relevant.

  # has_many through
  # Source: has_many through
  # Through: has_many
  def test_has_many_through_has_many_with_has_many_through_source_reflection
    general = tags(:general)
    assert_equal [general, general], authors(:david).tags
  end

  def test_has_many_through_has_many_with_has_many_through_source_reflection_preload
    authors = assert_queries(5) { Author.includes(:tags).to_a }
    general = tags(:general)

    assert_no_queries do
      assert_equal [general, general], authors.first.tags
    end
  end

  def test_has_many_through_has_many_with_has_many_through_source_reflection_preload_via_joins
    assert_includes_and_joins_equal(
      Author.where("tags.id" => tags(:general).id),
      [authors(:david)], :tags
    )

    # This ensures that the polymorphism of taggings is being observed correctly
    authors = Author.joins(:tags).where("taggings.taggable_type" => "FakeModel")
    assert authors.empty?
  end

  # has_many through
  # Source: has_many
  # Through: has_many through
  def test_has_many_through_has_many_through_with_has_many_source_reflection
    luke, david = subscribers(:first), subscribers(:second)
    assert_equal [luke, david, david], authors(:david).subscribers.order("subscribers.nick")
  end

  def test_has_many_through_has_many_through_with_has_many_source_reflection_preload
    luke, david = subscribers(:first), subscribers(:second)
    authors = assert_queries(4) { Author.includes(:subscribers).to_a }
    assert_no_queries do
      assert_equal [luke, david, david], authors.first.subscribers.sort_by(&:nick)
    end
  end

  def test_has_many_through_has_many_through_with_has_many_source_reflection_preload_via_joins
    # All authors with subscribers where one of the subscribers' nick is 'alterself'
    assert_includes_and_joins_equal(
      Author.where("subscribers.nick" => "alterself"),
      [authors(:david)], :subscribers
    )
  end

  # has_many through
  # Source: has_one through
  # Through: has_one
  def test_has_many_through_has_one_with_has_one_through_source_reflection
    assert_equal [member_types(:founding)], members(:groucho).nested_member_types
  end

  def test_has_many_through_has_one_with_has_one_through_source_reflection_preload
    members = assert_queries(4) { Member.includes(:nested_member_types).to_a }
    founding = member_types(:founding)
    assert_no_queries do
      assert_equal [founding], members.first.nested_member_types
    end
  end

  def test_has_many_through_has_one_with_has_one_through_source_reflection_preload_via_joins
    assert_includes_and_joins_equal(
      Member.where("member_types.id" => member_types(:founding).id),
      [members(:groucho)], :nested_member_types
    )
  end

  # has_many through
  # Source: has_one
  # Through: has_one through
  def test_has_many_through_has_one_through_with_has_one_source_reflection
    assert_equal [sponsors(:moustache_club_sponsor_for_groucho)], members(:groucho).nested_sponsors
  end

  def test_has_many_through_has_one_through_with_has_one_source_reflection_preload
    members = assert_queries(4) { Member.includes(:nested_sponsors).to_a }
    mustache = sponsors(:moustache_club_sponsor_for_groucho)
    assert_no_queries(ignore_none: false) do
      assert_equal [mustache], members.first.nested_sponsors
    end
  end

  def test_has_many_through_has_one_through_with_has_one_source_reflection_preload_via_joins
    assert_includes_and_joins_equal(
      Member.where("sponsors.id" => sponsors(:moustache_club_sponsor_for_groucho).id),
      [members(:groucho)], :nested_sponsors
    )
  end

  # has_many through
  # Source: has_many through
  # Through: has_one
  def test_has_many_through_has_one_with_has_many_through_source_reflection
    groucho_details, other_details = member_details(:groucho), member_details(:some_other_guy)

    assert_equal [groucho_details, other_details],
                 members(:groucho).organization_member_details.order("member_details.id")
  end

  def test_has_many_through_has_one_with_has_many_through_source_reflection_preload
    ActiveRecord::Base.connection.table_alias_length  # preheat cache
    members = assert_queries(4) { Member.includes(:organization_member_details).to_a.sort_by(&:id) }
    groucho_details, other_details = member_details(:groucho), member_details(:some_other_guy)

    assert_no_queries do
      assert_equal [groucho_details, other_details], members.first.organization_member_details.sort_by(&:id)
    end
  end

  def test_has_many_through_has_one_with_has_many_through_source_reflection_preload_via_joins
    assert_includes_and_joins_equal(
      Member.where("member_details.id" => member_details(:groucho).id).order("member_details.id"),
      [members(:groucho), members(:some_other_guy)], :organization_member_details
    )

    members = Member.joins(:organization_member_details).
                     where("member_details.id" => 9)
    assert members.empty?
  end

  # has_many through
  # Source: has_many
  # Through: has_one through
  def test_has_many_through_has_one_through_with_has_many_source_reflection
    groucho_details, other_details = member_details(:groucho), member_details(:some_other_guy)

    assert_equal [groucho_details, other_details],
                 members(:groucho).organization_member_details_2.order("member_details.id")
  end

  def test_has_many_through_has_one_through_with_has_many_source_reflection_preload
    members = assert_queries(4) { Member.includes(:organization_member_details_2).to_a.sort_by(&:id) }
    groucho_details, other_details = member_details(:groucho), member_details(:some_other_guy)

    # postgresql test if randomly executed then executes "SHOW max_identifier_length". Hence
    # the need to ignore certain predefined sqls that deal with system calls.
    assert_no_queries(ignore_none: false) do
      assert_equal [groucho_details, other_details], members.first.organization_member_details_2.sort_by(&:id)
    end
  end

  def test_has_many_through_has_one_through_with_has_many_source_reflection_preload_via_joins
    assert_includes_and_joins_equal(
      Member.where("member_details.id" => member_details(:groucho).id).order("member_details.id"),
      [members(:groucho), members(:some_other_guy)], :organization_member_details_2
    )

    members = Member.joins(:organization_member_details_2).
                     where("member_details.id" => 9)
    assert members.empty?
  end

  # has_many through
  # Source: has_and_belongs_to_many
  # Through: has_many
  def test_has_many_through_has_many_with_has_and_belongs_to_many_source_reflection
    general, cooking = categories(:general), categories(:cooking)

    assert_equal [general, cooking], authors(:bob).post_categories.order("categories.id")
  end

  def test_has_many_through_has_many_with_has_and_belongs_to_many_source_reflection_preload
    authors = assert_queries(4) { Author.includes(:post_categories).to_a.sort_by(&:id) }
    general, cooking = categories(:general), categories(:cooking)

    assert_no_queries do
      assert_equal [general, cooking], authors[2].post_categories.sort_by(&:id)
    end
  end

  def test_has_many_through_has_many_with_has_and_belongs_to_many_source_reflection_preload_via_joins
    # preload table schemas
    Author.joins(:post_categories).first

    assert_includes_and_joins_equal(
      Author.where("categories.id" => categories(:cooking).id),
      [authors(:bob)], :post_categories
    )
  end

  # has_many through
  # Source: has_many
  # Through: has_and_belongs_to_many
  def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection
    greetings, more = comments(:greetings), comments(:more_greetings)

    assert_equal [greetings, more], categories(:technology).post_comments.order("comments.id")
  end

  def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload
    Category.includes(:post_comments).to_a # preheat cache
    categories = assert_queries(4) { Category.includes(:post_comments).to_a.sort_by(&:id) }
    greetings, more = comments(:greetings), comments(:more_greetings)

    assert_no_queries do
      assert_equal [greetings, more], categories[1].post_comments.sort_by(&:id)
    end
  end

  def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload_via_joins
    # preload table schemas
    Category.joins(:post_comments).first

    assert_includes_and_joins_equal(
      Category.where("comments.id" => comments(:more_greetings).id).order("categories.id"),
      [categories(:general), categories(:technology)], :post_comments
    )
  end

  # has_many through
  # Source: has_many through a habtm
  # Through: has_many through
  def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection
    greetings, more = comments(:greetings), comments(:more_greetings)

    assert_equal [greetings, more], authors(:bob).category_post_comments.order("comments.id")
  end

  def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload
    authors = assert_queries(6) { Author.includes(:category_post_comments).to_a.sort_by(&:id) }
    greetings, more = comments(:greetings), comments(:more_greetings)

    assert_no_queries do
      assert_equal [greetings, more], authors[2].category_post_comments.sort_by(&:id)
    end
  end

  def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload_via_joins
    # preload table schemas
    Author.joins(:category_post_comments).first

    assert_includes_and_joins_equal(
      Author.where("comments.id" => comments(:does_it_hurt).id).order("authors.id"),
      [authors(:david), authors(:mary)], :category_post_comments
    )
  end

  # has_many through
  # Source: belongs_to
  # Through: has_many through
  def test_has_many_through_has_many_through_with_belongs_to_source_reflection
    assert_equal [tags(:general), tags(:general)], authors(:david).tagging_tags
  end

  def test_has_many_through_has_many_through_with_belongs_to_source_reflection_preload
    authors = assert_queries(5) { Author.includes(:tagging_tags).to_a }
    general = tags(:general)

    assert_no_queries do
      assert_equal [general, general], authors.first.tagging_tags
    end
  end

  def test_has_many_through_has_many_through_with_belongs_to_source_reflection_preload_via_joins
    assert_includes_and_joins_equal(
      Author.where("tags.id" => tags(:general).id),
      [authors(:david)], :tagging_tags
    )
  end

  # has_many through
  # Source: has_many through
  # Through: belongs_to
  def test_has_many_through_belongs_to_with_has_many_through_source_reflection
    welcome_general, thinking_general = taggings(:welcome_general), taggings(:thinking_general)

    assert_equal [welcome_general, thinking_general],
                 categorizations(:david_welcome_general).post_taggings.order("taggings.id")
  end

  def test_has_many_through_belongs_to_with_has_many_through_source_reflection_preload
    categorizations = assert_queries(4) { Categorization.includes(:post_taggings).to_a.sort_by(&:id) }
    welcome_general, thinking_general = taggings(:welcome_general), taggings(:thinking_general)

    assert_no_queries do
      assert_equal [welcome_general, thinking_general], categorizations.first.post_taggings.sort_by(&:id)
    end
  end

  def test_has_many_through_belongs_to_with_has_many_through_source_reflection_preload_via_joins
    assert_includes_and_joins_equal(
      Categorization.where("taggings.id" => taggings(:welcome_general).id).order("taggings.id"),
      [categorizations(:david_welcome_general)], :post_taggings
    )
  end

  # has_one through
  # Source: has_one through
  # Through: has_one
  def test_has_one_through_has_one_with_has_one_through_source_reflection
    assert_equal member_types(:founding), members(:groucho).nested_member_type
  end

  def test_has_one_through_has_one_with_has_one_through_source_reflection_preload
    members = assert_queries(4) { Member.includes(:nested_member_type).to_a.sort_by(&:id) }
    founding = member_types(:founding)

    assert_no_queries do
      assert_equal founding, members.first.nested_member_type
    end
  end

  def test_has_one_through_has_one_with_has_one_through_source_reflection_preload_via_joins
    assert_includes_and_joins_equal(
      Member.where("member_types.id" => member_types(:founding).id),
      [members(:groucho)], :nested_member_type
    )
  end

  # has_one through
  # Source: belongs_to
  # Through: has_one through
  def test_has_one_through_has_one_through_with_belongs_to_source_reflection
    assert_equal categories(:general), members(:groucho).club_category
  end

  def test_joins_and_includes_from_through_models_not_included_in_association
    prev_default_scope = Club.default_scopes

    [:includes, :preload, :joins, :eager_load].each do |q|
      Club.default_scopes = [proc { Club.send(q, :category) }]
      assert_equal categories(:general), members(:groucho).reload.club_category
    end
  ensure
    Club.default_scopes = prev_default_scope
  end

  def test_has_one_through_has_one_through_with_belongs_to_source_reflection_preload
    members = assert_queries(4) { Member.includes(:club_category).to_a.sort_by(&:id) }
    general = categories(:general)

    assert_no_queries do
      assert_equal general, members.first.club_category
    end
  end

  def test_has_one_through_has_one_through_with_belongs_to_source_reflection_preload_via_joins
    assert_includes_and_joins_equal(
      Member.where("categories.id" => categories(:technology).id),
      [members(:blarpy_winkup)], :club_category
    )
  end

  def test_distinct_has_many_through_a_has_many_through_association_on_source_reflection
    author = authors(:david)
    assert_equal [tags(:general)], author.distinct_tags
  end

  def test_distinct_has_many_through_a_has_many_through_association_on_through_reflection
    author = authors(:david)
    assert_equal [subscribers(:first), subscribers(:second)],
                 author.distinct_subscribers.order("subscribers.nick")
  end

  def test_nested_has_many_through_with_a_table_referenced_multiple_times
    author = authors(:bob)
    assert_equal [posts(:misc_by_bob), posts(:misc_by_mary), posts(:other_by_bob), posts(:other_by_mary)],
                 author.similar_posts.sort_by(&:id)

    # Mary and Bob both have posts in misc, but they are the only ones.
    authors = Author.joins(:similar_posts).where("posts.id" => posts(:misc_by_bob).id)
    assert_equal [authors(:mary), authors(:bob)], authors.distinct.sort_by(&:id)

    # Check the polymorphism of taggings is being observed correctly (in both joins)
    authors = Author.joins(:similar_posts).where("taggings.taggable_type" => "FakeModel")
    assert authors.empty?
    authors = Author.joins(:similar_posts).where("taggings_authors_join.taggable_type" => "FakeModel")
    assert authors.empty?
  end

  def test_has_many_through_with_foreign_key_option_on_through_reflection
    assert_equal [posts(:welcome), posts(:authorless)], people(:david).agents_posts.order("posts.id")
    assert_equal [authors(:david)], references(:david_unicyclist).agents_posts_authors

    references = Reference.joins(:agents_posts_authors).where("authors.id" => authors(:david).id)
    assert_equal [references(:david_unicyclist)], references
  end

  def test_has_many_through_with_foreign_key_option_on_source_reflection
    assert_equal [people(:michael), people(:susan)], jobs(:unicyclist).agents.order("people.id")

    jobs = Job.joins(:agents)
    assert_equal [jobs(:unicyclist), jobs(:unicyclist)], jobs
  end

  def test_has_many_through_with_sti_on_through_reflection
    ratings = posts(:sti_comments).special_comments_ratings.sort_by(&:id)
    assert_equal [ratings(:special_comment_rating), ratings(:sub_special_comment_rating)], ratings

    # Ensure STI is respected in the join
    scope = Post.joins(:special_comments_ratings).where(id: posts(:sti_comments).id)
    assert scope.where("comments.type" => "Comment").empty?
    assert !scope.where("comments.type" => "SpecialComment").empty?
    assert !scope.where("comments.type" => "SubSpecialComment").empty?
  end

  def test_has_many_through_with_sti_on_nested_through_reflection
    taggings = posts(:sti_comments).special_comments_ratings_taggings
    assert_equal [taggings(:special_comment_rating)], taggings

    scope = Post.joins(:special_comments_ratings_taggings).where(id: posts(:sti_comments).id)
    assert scope.where("comments.type" => "Comment").empty?
    assert !scope.where("comments.type" => "SpecialComment").empty?
  end

  def test_nested_has_many_through_writers_should_raise_error
    david = authors(:david)
    subscriber = subscribers(:first)

    assert_raises(ActiveRecord::HasManyThroughNestedAssociationsAreReadonly) do
      david.subscribers = [subscriber]
    end

    assert_raises(ActiveRecord::HasManyThroughNestedAssociationsAreReadonly) do
      david.subscriber_ids = [subscriber.id]
    end

    assert_raises(ActiveRecord::HasManyThroughNestedAssociationsAreReadonly) do
      david.subscribers << subscriber
    end

    assert_raises(ActiveRecord::HasManyThroughNestedAssociationsAreReadonly) do
      david.subscribers.delete(subscriber)
    end

    assert_raises(ActiveRecord::HasManyThroughNestedAssociationsAreReadonly) do
      david.subscribers.clear
    end

    assert_raises(ActiveRecord::HasManyThroughNestedAssociationsAreReadonly) do
      david.subscribers.build
    end

    assert_raises(ActiveRecord::HasManyThroughNestedAssociationsAreReadonly) do
      david.subscribers.create
    end
  end

  def test_nested_has_one_through_writers_should_raise_error
    groucho = members(:groucho)
    founding = member_types(:founding)

    assert_raises(ActiveRecord::HasOneThroughNestedAssociationsAreReadonly) do
      groucho.nested_member_type = founding
    end
  end

  def test_nested_has_many_through_with_conditions_on_through_associations
    assert_equal [tags(:blue)], authors(:bob).misc_post_first_blue_tags
  end

  def test_nested_has_many_through_with_conditions_on_through_associations_preload
    assert Author.where("tags.id" => 100).joins(:misc_post_first_blue_tags).empty?

    authors = assert_queries(3) { Author.includes(:misc_post_first_blue_tags).to_a.sort_by(&:id) }
    blue = tags(:blue)

    assert_no_queries do
      assert_equal [blue], authors[2].misc_post_first_blue_tags
    end
  end

  def test_nested_has_many_through_with_conditions_on_through_associations_preload_via_joins
    # Pointless condition to force single-query loading
    assert_includes_and_joins_equal(
      Author.where("tags.id = tags.id").references(:tags),
      [authors(:bob)], :misc_post_first_blue_tags
    )
  end

  def test_nested_has_many_through_with_conditions_on_source_associations
    assert_equal [tags(:blue)], authors(:bob).misc_post_first_blue_tags_2
  end

  def test_nested_has_many_through_with_conditions_on_source_associations_preload
    authors = assert_queries(4) { Author.includes(:misc_post_first_blue_tags_2).to_a.sort_by(&:id) }
    blue = tags(:blue)

    assert_no_queries do
      assert_equal [blue], authors[2].misc_post_first_blue_tags_2
    end
  end

  def test_nested_has_many_through_with_conditions_on_source_associations_preload_via_joins
    # Pointless condition to force single-query loading
    assert_includes_and_joins_equal(
      Author.where("tags.id = tags.id").references(:tags),
      [authors(:bob)], :misc_post_first_blue_tags_2
    )
  end

  def test_nested_has_many_through_with_foreign_key_option_on_the_source_reflection_through_reflection
    assert_equal [categories(:general)], organizations(:nsa).author_essay_categories

    organizations = Organization.joins(:author_essay_categories).
                                 where("categories.id" => categories(:general).id)
    assert_equal [organizations(:nsa)], organizations

    assert_equal categories(:general), organizations(:nsa).author_owned_essay_category

    organizations = Organization.joins(:author_owned_essay_category).
                                 where("categories.id" => categories(:general).id)
    assert_equal [organizations(:nsa)], organizations
  end

  def test_nested_has_many_through_should_not_be_autosaved
    c = Categorization.new
    c.author = authors(:david)
    c.post_taggings.to_a
    assert !c.post_taggings.empty?
    c.save
    assert !c.post_taggings.empty?
  end

  private

    def assert_includes_and_joins_equal(query, expected, association)
      actual = assert_queries(1) { query.joins(association).to_a.uniq }
      assert_equal expected, actual

      actual = assert_queries(1) { query.includes(association).to_a.uniq }
      assert_equal expected, actual
    end
end