aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/eager_singularization_test.rb
blob: 16eff15026c8cdf8bf2665715468948d8abd7175 (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
require "cases/helper"

class EagerSingularizationTest < ActiveRecord::TestCase
  class Virus < ActiveRecord::Base
    belongs_to :octopus
  end

  class Octopus < ActiveRecord::Base
    has_one :virus
  end

  class Pass < ActiveRecord::Base
    belongs_to :bus
  end

  class Bus < ActiveRecord::Base
    has_many :passes
  end

  class Mess < ActiveRecord::Base
    has_and_belongs_to_many :crises
  end

  class Crisis < ActiveRecord::Base
    has_and_belongs_to_many :messes
    has_many :analyses, dependent: :destroy
    has_many :successes, through: :analyses
    has_many :dresses, dependent: :destroy
    has_many :compresses, through: :dresses
  end

  class Analysis < ActiveRecord::Base
    belongs_to :crisis
    belongs_to :success
  end

  class Success < ActiveRecord::Base
    has_many :analyses, dependent: :destroy
    has_many :crises, through: :analyses
  end

  class Dress < ActiveRecord::Base
    belongs_to :crisis
    has_many :compresses
  end

  class Compress < ActiveRecord::Base
    belongs_to :dress
  end

  def setup
    connection.create_table :viri do |t|
      t.column :octopus_id, :integer
      t.column :species, :string
    end
    connection.create_table :octopi do |t|
      t.column :species, :string
    end
    connection.create_table :passes do |t|
      t.column :bus_id, :integer
      t.column :rides, :integer
    end
    connection.create_table :buses do |t|
      t.column :name, :string
    end
    connection.create_table :crises_messes, id: false do |t|
      t.column :crisis_id, :integer
      t.column :mess_id, :integer
    end
    connection.create_table :messes do |t|
      t.column :name, :string
    end
    connection.create_table :crises do |t|
      t.column :name, :string
    end
    connection.create_table :successes do |t|
      t.column :name, :string
    end
    connection.create_table :analyses do |t|
      t.column :crisis_id, :integer
      t.column :success_id, :integer
    end
    connection.create_table :dresses do |t|
      t.column :crisis_id, :integer
    end
    connection.create_table :compresses do |t|
      t.column :dress_id, :integer
    end
  end

  teardown do
    connection.drop_table :viri
    connection.drop_table :octopi
    connection.drop_table :passes
    connection.drop_table :buses
    connection.drop_table :crises_messes
    connection.drop_table :messes
    connection.drop_table :crises
    connection.drop_table :successes
    connection.drop_table :analyses
    connection.drop_table :dresses
    connection.drop_table :compresses
  end

  def test_eager_no_extra_singularization_belongs_to
    assert_nothing_raised do
      Virus.all.merge!(includes: :octopus).to_a
    end
  end

  def test_eager_no_extra_singularization_has_one
    assert_nothing_raised do
      Octopus.all.merge!(includes: :virus).to_a
    end
  end

  def test_eager_no_extra_singularization_has_many
    assert_nothing_raised do
      Bus.all.merge!(includes: :passes).to_a
    end
  end

  def test_eager_no_extra_singularization_has_and_belongs_to_many
    assert_nothing_raised do
      Crisis.all.merge!(includes: :messes).to_a
      Mess.all.merge!(includes: :crises).to_a
    end
  end

  def test_eager_no_extra_singularization_has_many_through_belongs_to
    assert_nothing_raised do
      Crisis.all.merge!(includes: :successes).to_a
    end
  end

  def test_eager_no_extra_singularization_has_many_through_has_many
    assert_nothing_raised do
      Crisis.all.merge!(includes: :compresses).to_a
    end
  end

  private
    def connection
      ActiveRecord::Base.connection
    end
end