aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/has_one_through_associations_test.rb
blob: 28b883586d2044030f7b0555fd7181b13d4fdc5f (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
require "cases/helper"
require "models/club"
require "models/member_type"
require "models/member"
require "models/membership"
require "models/sponsor"
require "models/organization"
require "models/member_detail"
require "models/minivan"
require "models/dashboard"
require "models/speedometer"
require "models/category"
require "models/author"
require "models/essay"
require "models/owner"
require "models/post"
require "models/comment"
require "models/categorization"
require "models/customer"
require "models/carrier"
require "models/shop_account"
require "models/customer_carrier"

class HasOneThroughAssociationsTest < ActiveRecord::TestCase
  fixtures :member_types, :members, :clubs, :memberships, :sponsors, :organizations, :minivans,
           :dashboards, :speedometers, :authors, :author_addresses, :posts, :comments, :categories, :essays, :owners

  def setup
    @member = members(:groucho)
  end

  def test_has_one_through_with_has_one
    assert_equal clubs(:boring_club), @member.club
  end

  def test_creating_association_creates_through_record
    new_member = Member.create(name: "Chris")
    new_member.club = Club.create(name: "LRUG")
    assert_not_nil new_member.current_membership
    assert_not_nil new_member.club
  end

  def test_creating_association_builds_through_record_for_new
    new_member = Member.new(name: "Jane")
    new_member.club = clubs(:moustache_club)
    assert new_member.current_membership
    assert_equal clubs(:moustache_club), new_member.current_membership.club
    assert_equal clubs(:moustache_club), new_member.club
    assert new_member.save
    assert_equal clubs(:moustache_club), new_member.club
  end

  def test_creating_association_sets_both_parent_ids_for_new
    member = Member.new(name: "Sean Griffin")
    club = Club.new(name: "Da Club")

    member.club = club

    member.save!

    assert member.id
    assert club.id
    assert_equal member.id, member.current_membership.member_id
    assert_equal club.id, member.current_membership.club_id
  end

  def test_replace_target_record
    new_club = Club.create(name: "Marx Bros")
    @member.club = new_club
    @member.reload
    assert_equal new_club, @member.club
  end

  def test_replacing_target_record_deletes_old_association
    assert_no_difference "Membership.count" do
      new_club = Club.create(name: "Bananarama")
      @member.club = new_club
      @member.reload
    end
  end

  def test_set_record_to_nil_should_delete_association
    @member.club = nil
    @member.reload
    assert_nil @member.current_membership
    assert_nil @member.club
  end

  def test_set_record_after_delete_association
    @member.club = nil
    @member.club = clubs(:moustache_club)
    @member.reload
    assert_equal clubs(:moustache_club), @member.club
  end

  def test_has_one_through_polymorphic
    assert_equal clubs(:moustache_club), @member.sponsor_club
  end

  def test_has_one_through_eager_loading
    members = assert_queries(3) do #base table, through table, clubs table
      Member.all.merge!(includes: :club, where: ["name = ?", "Groucho Marx"]).to_a
    end
    assert_equal 1, members.size
    assert_not_nil assert_no_queries { members[0].club }
  end

  def test_has_one_through_eager_loading_through_polymorphic
    members = assert_queries(3) do #base table, through table, clubs table
      Member.all.merge!(includes: :sponsor_club, where: ["name = ?", "Groucho Marx"]).to_a
    end
    assert_equal 1, members.size
    assert_not_nil assert_no_queries { members[0].sponsor_club }
  end

  def test_has_one_through_with_conditions_eager_loading
    # conditions on the through table
    assert_equal clubs(:moustache_club), Member.all.merge!(includes: :favourite_club).find(@member.id).favourite_club
    memberships(:membership_of_favourite_club).update_columns(favourite: false)
    assert_nil Member.all.merge!(includes: :favourite_club).find(@member.id).reload.favourite_club

    # conditions on the source table
    assert_equal clubs(:moustache_club), Member.all.merge!(includes: :hairy_club).find(@member.id).hairy_club
    clubs(:moustache_club).update_columns(name: "Association of Clean-Shaven Persons")
    assert_nil Member.all.merge!(includes: :hairy_club).find(@member.id).reload.hairy_club
  end

  def test_has_one_through_polymorphic_with_source_type
    assert_equal members(:groucho), clubs(:moustache_club).sponsored_member
  end

  def test_eager_has_one_through_polymorphic_with_source_type
    clubs = Club.all.merge!(includes: :sponsored_member, where: ["name = ?", "Moustache and Eyebrow Fancier Club"]).to_a
    # Only the eyebrow fanciers club has a sponsored_member
    assert_not_nil assert_no_queries { clubs[0].sponsored_member }
  end

  def test_has_one_through_nonpreload_eagerloading
    members = assert_queries(1) do
      Member.all.merge!(includes: :club, where: ["members.name = ?", "Groucho Marx"], order: "clubs.name").to_a #force fallback
    end
    assert_equal 1, members.size
    assert_not_nil assert_no_queries { members[0].club }
  end

  def test_has_one_through_nonpreload_eager_loading_through_polymorphic
    members = assert_queries(1) do
      Member.all.merge!(includes: :sponsor_club, where: ["members.name = ?", "Groucho Marx"], order: "clubs.name").to_a #force fallback
    end
    assert_equal 1, members.size
    assert_not_nil assert_no_queries { members[0].sponsor_club }
  end

  def test_has_one_through_nonpreload_eager_loading_through_polymorphic_with_more_than_one_through_record
    Sponsor.new(sponsor_club: clubs(:crazy_club), sponsorable: members(:groucho)).save!
    members = assert_queries(1) do
      Member.all.merge!(includes: :sponsor_club, where: ["members.name = ?", "Groucho Marx"], order: "clubs.name DESC").to_a #force fallback
    end
    assert_equal 1, members.size
    assert_not_nil assert_no_queries { members[0].sponsor_club }
    assert_equal clubs(:crazy_club), members[0].sponsor_club
  end

  def test_uninitialized_has_one_through_should_return_nil_for_unsaved_record
    assert_nil Member.new.club
  end

  def test_assigning_association_correctly_assigns_target
    new_member = Member.create(name: "Chris")
    new_member.club = new_club = Club.create(name: "LRUG")
    assert_equal new_club, new_member.association(:club).target
  end

  def test_has_one_through_proxy_should_not_respond_to_private_methods
    assert_raise(NoMethodError) { clubs(:moustache_club).private_method }
    assert_raise(NoMethodError) { @member.club.private_method }
  end

  def test_has_one_through_proxy_should_respond_to_private_methods_via_send
    clubs(:moustache_club).send(:private_method)
    @member.club.send(:private_method)
  end

  def test_assigning_to_has_one_through_preserves_decorated_join_record
    @organization = organizations(:nsa)
    assert_difference "MemberDetail.count", 1 do
      @member_detail = MemberDetail.new(extra_data: "Extra")
      @member.member_detail = @member_detail
      @member.organization = @organization
    end
    assert_equal @organization, @member.organization
    assert_includes @organization.members, @member
    assert_equal "Extra", @member.member_detail.extra_data
  end

  def test_reassigning_has_one_through
    @organization = organizations(:nsa)
    @new_organization = organizations(:discordians)

    assert_difference "MemberDetail.count", 1 do
      @member_detail = MemberDetail.new(extra_data: "Extra")
      @member.member_detail = @member_detail
      @member.organization = @organization
    end
    assert_equal @organization, @member.organization
    assert_equal "Extra", @member.member_detail.extra_data
    assert_includes @organization.members, @member
    assert_not_includes @new_organization.members, @member

    assert_no_difference "MemberDetail.count" do
      @member.organization = @new_organization
    end
    assert_equal @new_organization, @member.organization
    assert_equal "Extra", @member.member_detail.extra_data
    assert_not_includes @organization.members, @member
    assert_includes @new_organization.members, @member
  end

  def test_preloading_has_one_through_on_belongs_to
    MemberDetail.delete_all
    assert_not_nil @member.member_type
    @organization = organizations(:nsa)
    @member_detail = MemberDetail.new
    @member.member_detail = @member_detail
    @member.organization = @organization
    @member_details = assert_queries(3) do
      MemberDetail.all.merge!(includes: :member_type).to_a
    end
    @new_detail = @member_details[0]
    assert @new_detail.send(:association, :member_type).loaded?
    assert_no_queries { @new_detail.member_type }
  end

  def test_save_of_record_with_loaded_has_one_through
    @club = @member.club
    assert_not_nil @club.sponsored_member

    assert_nothing_raised do
      Club.find(@club.id).save!
      Club.all.merge!(includes: :sponsored_member).find(@club.id).save!
    end

    @club.sponsor.destroy

    assert_nothing_raised do
      Club.find(@club.id).save!
      Club.all.merge!(includes: :sponsored_member).find(@club.id).save!
    end
  end

  def test_through_belongs_to_after_destroy
    @member_detail = MemberDetail.new(extra_data: "Extra")
    @member.member_detail = @member_detail
    @member.save!

    assert_not_nil @member_detail.member_type
    @member_detail.destroy
    assert_queries(1) do
      @member_detail.association(:member_type).reload
      assert_not_nil @member_detail.member_type
    end

    @member_detail.member.destroy
    assert_queries(1) do
      @member_detail.association(:member_type).reload
      assert_nil @member_detail.member_type
    end
  end

  def test_value_is_properly_quoted
    minivan = Minivan.find("m1")
    assert_nothing_raised do
      minivan.dashboard
    end
  end

  def test_has_one_through_polymorphic_with_primary_key_option
    assert_equal categories(:general), authors(:david).essay_category

    authors = Author.joins(:essay_category).where("categories.id" => categories(:general).id)
    assert_equal authors(:david), authors.first

    assert_equal owners(:blackbeard), authors(:david).essay_owner

    authors = Author.joins(:essay_owner).where("owners.name = 'blackbeard'")
    assert_equal authors(:david), authors.first
  end

  def test_has_one_through_with_primary_key_option
    assert_equal categories(:general), authors(:david).essay_category_2

    authors = Author.joins(:essay_category_2).where("categories.id" => categories(:general).id)
    assert_equal authors(:david), authors.first
  end

  def test_has_one_through_with_default_scope_on_join_model
    assert_equal posts(:welcome).comments.order("id").first, authors(:david).comment_on_first_post
  end

  def test_has_one_through_many_raises_exception
    assert_raise(ActiveRecord::HasOneThroughCantAssociateThroughCollection) do
      members(:groucho).club_through_many
    end
  end

  def test_has_one_through_polymorphic_association
    assert_raise(ActiveRecord::HasOneAssociationPolymorphicThroughError) do
      @member.premium_club
    end
  end

  def test_has_one_through_belongs_to_should_update_when_the_through_foreign_key_changes
    minivan = minivans(:cool_first)

    minivan.dashboard
    proxy = minivan.send(:association_instance_get, :dashboard)

    assert !proxy.stale_target?
    assert_equal dashboards(:cool_first), minivan.dashboard

    minivan.speedometer_id = speedometers(:second).id

    assert proxy.stale_target?
    assert_equal dashboards(:second), minivan.dashboard
  end

  def test_has_one_through_belongs_to_setting_belongs_to_foreign_key_after_nil_target_loaded
    minivan = Minivan.new

    minivan.dashboard
    proxy = minivan.send(:association_instance_get, :dashboard)

    minivan.speedometer_id = speedometers(:second).id

    assert proxy.stale_target?
    assert_equal dashboards(:second), minivan.dashboard
  end

  def test_assigning_has_one_through_belongs_to_with_new_record_owner
    minivan   = Minivan.new
    dashboard = dashboards(:cool_first)

    minivan.dashboard = dashboard

    assert_equal dashboard, minivan.dashboard
    assert_equal dashboard, minivan.speedometer.dashboard
  end

  def test_has_one_through_with_custom_select_on_join_model_default_scope
    assert_equal clubs(:boring_club), members(:groucho).selected_club
  end

  def test_has_one_through_relationship_cannot_have_a_counter_cache
    assert_raise(ArgumentError) do
      Class.new(ActiveRecord::Base) do
        has_one :thing, through: :other_thing, counter_cache: true
      end
    end
  end

  def test_has_one_through_do_not_cache_association_reader_if_the_though_method_has_default_scopes
    customer = Customer.create!
    carrier = Carrier.create!
    customer_carrier = CustomerCarrier.create!(
      customer: customer,
      carrier: carrier,
    )
    account = ShopAccount.create!(customer_carrier: customer_carrier)

    CustomerCarrier.current_customer = customer

    account_carrier = account.carrier
    assert_equal carrier, account_carrier

    CustomerCarrier.current_customer = nil

    other_carrier = Carrier.create!
    other_customer = Customer.create!
    other_customer_carrier = CustomerCarrier.create!(
      customer: other_customer,
      carrier: other_carrier,
    )
    other_account = ShopAccount.create!(customer_carrier: other_customer_carrier)

    account_carrier = other_account.carrier
    assert_equal other_carrier, account_carrier
  ensure
    CustomerCarrier.current_customer = nil
  end
end