forked from thoughtbot/shoulda-matchers
/
model_reflection_spec.rb
267 lines (232 loc) · 10.5 KB
/
model_reflection_spec.rb
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
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveRecord::AssociationMatchers::ModelReflection do
it 'delegates other methods to the given Reflection object' do
define_model(:country)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country
end
delegate_reflection = person_model.reflect_on_association(:country)
allow(delegate_reflection).to receive(:foo).and_return('bar')
reflection = described_class.new(delegate_reflection)
expect(reflection.foo).to eq 'bar'
end
describe '#associated_class' do
it 'returns the model that the association refers to' do
define_model(:country)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country
end
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
expect(reflection.associated_class).to be Country
end
end
describe '#through?' do
it 'returns true if the reflection is for a has_many :through association' do
define_model(:city, person_id: :integer)
define_model(:person, country_id: :integer) do
has_many :cities
end
country_model = define_model(:country) do
has_many :people
has_many :cities, through: :people
end
delegate_reflection = country_model.reflect_on_association(:cities)
reflection = described_class.new(delegate_reflection)
expect(reflection).to be_through
end
it 'returns false if not' do
define_model(:person, country_id: :integer)
country_model = define_model(:country) do
has_many :people
end
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
expect(reflection).not_to be_through
end
end
describe '#join_table_name' do
context 'when the association was defined with a :join_table option' do
it 'returns the value of the option' do
create_table :foos, id: false do |t|
t.integer :person_id
t.integer :country_id
end
define_model(:person, country_id: :integer)
country_model = define_model(:country) do
has_and_belongs_to_many :people, join_table: 'foos'
end
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
expect(reflection.join_table_name).to eq 'foos'
end
end
context 'when the association was not defined with :join_table' do
it 'returns the default join_table that ActiveRecord generates' do
define_model(:person, country_id: :integer)
country_model = define_model(:country) do
has_and_belongs_to_many :people
end
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
expect(reflection.join_table_name).to eq 'countries_people'
end
end
end
describe '#association_relation' do
if rails_4_x?
context 'when the reflection object has a #scope method' do
context 'when the scope is a block' do
it 'executes the block in the context of an empty scope' do
define_model(:country, mood: :string)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country, -> { where(mood: 'nice') }
end
person_instance = person_model.new(spirit: 'nice')
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation(person_instance).to_sql
expected_sql = Country.where(mood: 'nice').to_sql
expect(actual_sql).to eq expected_sql
end
it 'pass instance of association source to block' do
spy = spy('spy')
define_model(:country, mood: :string)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country, ->(person) { spy.who(person) }
end
person_instance = person_model.new(spirit: 'nice')
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
reflection.association_relation(person_instance)
expect(spy).to have_received(:who).with(person_instance)
end
end
context 'when the scope is nil' do
it 'returns an empty scope' do
define_model(:country)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country
end
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation(person_model).to_sql
expected_sql = Country.all.to_sql
expect(actual_sql).to eq expected_sql
end
end
end
end
if rails_3_x?
context 'when the reflection object does not have a #scope method' do
context 'when the reflection options contain :conditions' do
it 'creates an ActiveRecord::Relation from the conditions' do
define_model(:country, mood: :string)
person_model = define_model(:person, country_id: :integer) do
belongs_to :country, conditions: { mood: 'nice' }
end
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Country.where(mood: 'nice').to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :order' do
it 'creates an ActiveRecord::Relation from the order' do
define_model(:person, country_id: :integer, age: :integer)
country_model = define_model(:country) do
has_many :people, order: 'age'
end
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.order('age').to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :include' do
it 'creates an ActiveRecord::Relation from the include' do
define_model(:city, country_id: :integer)
define_model(:country) do
has_many :cities
end
person_model = define_model(:person, country_id: :integer) do
belongs_to :country, include: :cities
end
delegate_reflection = person_model.reflect_on_association(:country)
reflection = described_class.new(delegate_reflection)
actual_includes = reflection.association_relation.includes_values
expected_includes = Country.includes(:cities).includes_values
expect(actual_includes).to eq expected_includes
end
end
context 'when the reflection options contain :group' do
it 'creates an ActiveRecord::Relation from the group' do
country_model = define_model(:country, mood: :string) do
has_many :people, group: 'age'
end
define_model(:person, country_id: :integer, age: :integer)
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.group('age').to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :having' do
it 'creates an ActiveRecord::Relation from the having' do
country_model = define_model(:country) do
has_many :people, having: 'country_id > 1'
end
define_model(:person, country_id: :integer)
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.having('country_id > 1').to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :limit' do
it 'creates an ActiveRecord::Relation from the limit' do
country_model = define_model(:country) do
has_many :people, limit: 10
end
define_model(:person, country_id: :integer)
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.limit(10).to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :offset' do
it 'creates an ActiveRecord::Relation from the offset' do
country_model = define_model(:country) do
has_many :people, offset: 5
end
define_model(:person, country_id: :integer)
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.offset(5).to_sql
expect(actual_sql).to eq expected_sql
end
end
context 'when the reflection options contain :select' do
it 'creates an ActiveRecord::Relation from the select' do
country_model = define_model(:country) do
has_many :people, select: 'age'
end
define_model(:person, country_id: :integer, age: :integer)
delegate_reflection = country_model.reflect_on_association(:people)
reflection = described_class.new(delegate_reflection)
actual_sql = reflection.association_relation.to_sql
expected_sql = Person.select('age').to_sql
expect(actual_sql).to eq expected_sql
end
end
end
end
end
end