-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
where_chain_test.rb
314 lines (243 loc) · 12.3 KB
/
where_chain_test.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
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
# frozen_string_literal: true
require "cases/helper"
require "models/post"
require "models/author"
require "models/human"
require "models/essay"
require "models/comment"
require "models/categorization"
require "models/book"
module ActiveRecord
class WhereChainTest < ActiveRecord::TestCase
fixtures :posts, :comments, :authors, :humans, :essays, :author_addresses, :books
def test_associated_with_association
Post.where.associated(:author).tap do |relation|
assert_includes relation, posts(:welcome)
assert_includes relation, posts(:sti_habtm)
assert_not_includes relation, posts(:authorless)
end
end
def test_associated_with_child_association
Comment.where.associated(:children).tap do |relation|
assert_includes relation, comments(:greetings)
assert_not_includes relation, comments(:more_greetings)
end
end
def test_associated_with_multiple_associations
Post.where.associated(:author, :comments).tap do |relation|
assert_includes relation, posts(:welcome)
assert_not_includes relation, posts(:sti_habtm)
assert_not_includes relation, posts(:authorless)
end
end
def test_associated_with_invalid_association_name
e = assert_raises(ArgumentError) do
Post.where.associated(:cars).to_a
end
assert_match(/An association named `:cars` does not exist on the model `Post`\./, e.message)
end
def test_associated_merged_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.where.associated(:author).merge(Author.where(id: 1)).count
end
def test_associated_unscoped_merged_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.unscope(:where).where.associated(:author).merge(Author.where(id: 1)).count
end
def test_associated_unscoped_merged_joined_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.joins(:author).unscope(:where).where.associated(:author).merge(Author.where(id: 1)).count
end
def test_associated_unscoped_merged_joined_extended_early_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.extending(Post::NamedExtension).joins(:author).unscope(:where).where.associated(:author).merge(Author.where(id: 1)).count
end
def test_associated_unscoped_merged_joined_extended_late_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.joins(:author).unscope(:where).where.associated(:author).merge(Author.where(id: 1)).extending(Post::NamedExtension).count
end
def test_associated_ordered_merged_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.order(created_at: :desc).where.associated(:author).merge(Author.where(id: 1)).count
end
def test_associated_ordered_merged_joined_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.joins(:author).order(created_at: :desc).where.associated(:author).merge(Author.where(id: 1)).count
end
def test_associated_with_enum
assert_equal Author.find(2), Author.joins(:reading_listing).where.associated(:reading_listing).first
end
def test_associated_with_enum_ordered
assert_equal Author.find(2), Author.order(id: :desc).joins(:reading_listing).where.associated(:reading_listing).first
end
def test_associated_with_enum_unscoped
assert_equal Author.find(2), Author.unscope(:where).joins(:reading_listing).where.associated(:reading_listing).first
end
def test_associated_with_enum_extended_early
assert_equal Author.find(2), Author.extending(Author::NamedExtension).order(id: :desc).joins(:reading_listing).where.associated(:reading_listing).first
end
def test_associated_with_enum_extended_late
assert_equal Author.find(2), Author.order(id: :desc).joins(:reading_listing).where.associated(:reading_listing).extending(Author::NamedExtension).first
end
def test_associated_with_add_joins_before
Comment.joins(:children).where.associated(:children).tap do |relation|
assert_includes relation, comments(:greetings)
assert_not_includes relation, comments(:more_greetings)
end
end
def test_associated_with_add_left_joins_before
Comment.left_joins(:children).where.associated(:children).tap do |relation|
assert_includes relation, comments(:greetings)
assert_not_includes relation, comments(:more_greetings)
end
end
def test_associated_with_add_left_outer_joins_before
Comment.left_outer_joins(:children).where.associated(:children).tap do |relation|
assert_includes relation, comments(:greetings)
assert_not_includes relation, comments(:more_greetings)
end
end
def test_missing_with_association
assert_predicate posts(:authorless).author, :blank?
assert_equal [posts(:authorless)], Post.where.missing(:author).to_a
end
def test_missing_with_child_association
Comment.where.missing(:children).tap do |relation|
assert_includes relation, comments(:more_greetings)
assert_not_includes relation, comments(:greetings)
end
end
def test_missing_with_invalid_association_name
e = assert_raises(ArgumentError) do
Post.where.missing(:cars).to_a
end
assert_match(/An association named `:cars` does not exist on the model `Post`\./, e.message)
end
def test_missing_with_multiple_association
assert_predicate posts(:authorless).comments, :empty?
assert_equal [posts(:authorless)], Post.where.missing(:author, :comments).to_a
end
def test_missing_merged_with_scope_on_association
# This query does not make much logical sense, but it is testing
# that the generated SQL query is valid.
assert_equal Author.find(1).posts.count, Post.where.missing(:author).merge(Author.where(id: 1)).count
end
def test_missing_unscoped_merged_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.joins(:author).unscope(:where).where.missing(:author).merge(Author.where(id: 1)).count
end
def test_missing_unscoped_merged_joined_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.unscope(:where).where.missing(:author).merge(Author.where(id: 1)).count
end
def test_missing_ordered_merged_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.order(created_at: :desc).where.missing(:author).merge(Author.where(id: 1)).count
end
def test_missing_ordered_merged_joined_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.joins(:author).order(created_at: :desc).where.missing(:author).merge(Author.where(id: 1)).count
end
def test_missing_unscoped_merged_joined_extended_early_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.extending(Post::NamedExtension).joins(:author).unscope(:where).where.missing(:author).merge(Author.where(id: 1)).count
end
def test_missing_unscoped_merged_joined_extended_late_with_scope_on_association
assert_equal Author.find(1).posts.count, Post.joins(:author).unscope(:where).where.missing(:author).merge(Author.where(id: 1)).extending(Post::NamedExtension).count
end
def test_missing_with_enum
assert_equal Author.find(2), Author.joins(:reading_listing).where.missing(:unread_listing).first
end
def test_missing_with_enum_ordered
assert_equal Author.find(2), Author.order(id: :desc).joins(:reading_listing).where.missing(:unread_listing).first
end
def test_missing_with_enum_unscoped
assert_equal Author.find(2), Author.unscope(:where).joins(:reading_listing).where.missing(:unread_listing).first
end
def test_missing_with_enum_extended_early
assert_equal Author.find(2), Author.extending(Author::NamedExtension).order(id: :desc).joins(:reading_listing).where.missing(:unread_listing).first
end
def test_missing_with_enum_extended_late
assert_equal Author.find(2), Author.order(id: :desc).joins(:reading_listing).where.missing(:unread_listing).extending(Author::NamedExtension).first
end
def test_not_inverts_where_clause
relation = Post.where.not(title: "hello")
expected_where_clause = Post.where(title: "hello").where_clause.invert
assert_equal expected_where_clause, relation.where_clause
end
def test_not_with_nil
assert_raise ArgumentError do
Post.where.not(nil)
end
end
def test_association_not_eq
expected = Comment.arel_table["title"].not_eq(Arel::Nodes::BindParam.new(1))
relation = Post.joins(:comments).where.not(comments: { title: "hello" })
assert_equal(expected.to_sql, relation.where_clause.ast.to_sql)
end
def test_not_eq_with_preceding_where
relation = Post.where(title: "hello").where.not(title: "world")
expected_where_clause =
Post.where(title: "hello").where_clause +
Post.where(title: "world").where_clause.invert
assert_equal expected_where_clause, relation.where_clause
end
def test_not_eq_with_succeeding_where
relation = Post.where.not(title: "hello").where(title: "world")
expected_where_clause =
Post.where(title: "hello").where_clause.invert +
Post.where(title: "world").where_clause
assert_equal expected_where_clause, relation.where_clause
end
def test_chaining_multiple
relation = Post.where.not(author_id: [1, 2]).where.not(title: "ruby on rails")
expected_where_clause =
Post.where(author_id: [1, 2]).where_clause.invert +
Post.where(title: "ruby on rails").where_clause.invert
assert_equal expected_where_clause, relation.where_clause
end
def test_rewhere_with_one_condition
relation = Post.where(body: "hello").where(body: "world").rewhere(body: "hullo")
expected = Post.where(body: "hullo")
assert_equal expected.to_a, relation.to_a
end
def test_rewhere_with_multiple_overwriting_conditions
relation = Post.where(body: "hello").where(type: "StiPost").rewhere(body: "hullo", type: "Post")
expected = Post.where(body: "hullo", type: "Post")
assert_equal expected.to_a, relation.to_a
end
def test_rewhere_with_one_overwriting_condition_and_one_unrelated
relation = Post.where(body: "hello").where(type: "Post").rewhere(body: "hullo")
expected = Post.where(body: "hullo", type: "Post")
assert_equal expected.to_a, relation.to_a
end
def test_rewhere_with_alias_condition
relation = Post.where(text: "hello").where(text: "world").rewhere(text: "hullo")
expected = Post.where(text: "hullo")
assert_equal expected.to_a, relation.to_a
end
def test_rewhere_with_nested_condition
relation = Post.where.missing(:comments).rewhere("comments.id": comments(:does_it_hurt))
expected = Post.left_joins(:comments).where("comments.id": comments(:does_it_hurt))
assert_equal expected.to_a, relation.to_a
end
def test_rewhere_with_polymorphic_association
relation = Essay.where(writer: authors(:david)).rewhere(writer: humans(:steve))
expected = Essay.where(writer: humans(:steve))
assert_equal expected.to_a, relation.to_a
end
def test_rewhere_with_range
relation = Post.where(comments_count: 1..3).rewhere(comments_count: 3..5)
expected = Post.where(comments_count: 3..5)
assert_equal expected.to_a, relation.to_a
end
def test_rewhere_with_infinite_upper_bound_range
relation = Post.where(comments_count: 1..Float::INFINITY).rewhere(comments_count: 3..5)
expected = Post.where(comments_count: 3..5)
assert_equal expected.to_a, relation.to_a
end
def test_rewhere_with_infinite_lower_bound_range
relation = Post.where(comments_count: -Float::INFINITY..1).rewhere(comments_count: 3..5)
expected = Post.where(comments_count: 3..5)
assert_equal expected.to_a, relation.to_a
end
def test_rewhere_with_infinite_range
relation = Post.where(comments_count: -Float::INFINITY..Float::INFINITY).rewhere(comments_count: 3..5)
expected = Post.where(comments_count: 3..5)
assert_equal expected.to_a, relation.to_a
end
def test_rewhere_with_nil
relation = Post.where(comments_count: 16).rewhere(nil)
expected = Post.all
assert_equal expected.to_a, relation.to_a
end
end
end