-
Notifications
You must be signed in to change notification settings - Fork 20
/
order_query_spec.rb
553 lines (491 loc) · 18.4 KB
/
order_query_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
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
# frozen_string_literal: true
require 'spec_helper'
# Bare model
class User < ActiveRecord::Base
include OrderQuery
end
# Simple model
class Post < ActiveRecord::Base
include OrderQuery
order_query :order_list,
[:pinned, [true, false]],
%i[published_at desc],
%i[id desc]
end
def create_post(attr = {})
Post.create!({ pinned: false, published_at: Time.now }.merge(attr))
end
# Advanced model
class Issue < ActiveRecord::Base
DISPLAY_ORDER = [
[:pinned, [true, false]],
[:priority, %w[high medium low]],
[:valid_votes_count, :desc, sql: '(votes - suspicious_votes)'],
%i[updated_at desc],
%i[id desc]
].freeze
def valid_votes_count
votes - suspicious_votes
end
include OrderQuery
order_query :display_order, DISPLAY_ORDER
order_query :id_order_asc, [%i[id asc]]
end
def create_issue(attr = {})
Issue.create!(
{ priority: 'high', votes: 3, suspicious_votes: 0, updated_at: Time.now }
.merge(attr)
)
end
def wrap_top_level_or(value)
conf = ::OrderQuery
around do |ex|
was = conf.wrap_top_level_or
begin
conf.wrap_top_level_or = value
ex.run
ensure
conf.wrap_top_level_or = was
end
end
end
RSpec.describe 'OrderQuery' do
context 'Column' do
it 'fails with ArgumentError if invalid vals_and_or_dir is passed' do
expect do
OrderQuery::Column.new(Post.all, :pinned, :desc, :extra)
end.to raise_error(ArgumentError)
end
end
context 'Point' do
context '#value' do
it 'fails if nil on non-nullable column' do
post = OpenStruct.new
post.pinned = nil
space = Post.seek([:pinned])
expect do
OrderQuery::Point.new(post, space)
.value(space.columns.find { |c| c.name == :pinned })
end.to raise_error(OrderQuery::Errors::NonNullableColumnIsNullError)
end
end
end
[false, true].each do |wrap_top_level_or|
context "(wtlo: #{wrap_top_level_or})" do
wrap_top_level_or wrap_top_level_or
context 'Issue test model' do
datasets = lambda {
t = Time.now
[
[
['high', 5, 0, t, true],
['high', 5, 1, t, true],
['high', 5, 0, t],
['high', 5, 0, t - 1.day],
['high', 5, 1, t],
['medium', 10, 0, t],
['medium', 10, 5, t - 12.hours],
['low', 30, 0, t + 1.day]
],
[
['high', 5, 0, t],
['high', 5, 1, t],
['high', 5, 1, t - 1.day],
['low', 30, 0, t + 1.day]
],
[
['high', 5, 1, t - 1.day],
['low', 30, 0, t + 1.day]
]
]
}.call
datasets.each_with_index do |ds, i|
it "is ordered correctly (test data #{i})" do
issues = ds.map do |attr|
Issue.new(priority: attr[0], votes: attr[1],
suspicious_votes: attr[2], updated_at: attr[3],
pinned: attr[4] || false)
end
issues.shuffle.reverse_each(&:save!)
expect(Issue.display_order.to_a).to eq(issues)
expect(Issue.display_order_reverse.to_a).to eq(issues.reverse)
issues.zip(issues.rotate).each_with_index do |(cur, nxt), j|
expect(cur.display_order.position).to eq(j + 1)
expect(cur.display_order.next).to eq(nxt)
expect(Issue.display_order_at(cur).next).to eq nxt
expect(cur.display_order.space.count).to eq(Issue.count)
expect(
cur.display_order.before.count + 1 +
cur.display_order.after.count
).to eq(nxt.display_order.count)
expect(nxt.display_order.previous).to eq(cur)
expect(
nxt.display_order.before.to_a.reverse + [nxt] +
nxt.display_order.after.to_a
).to eq(Issue.display_order.to_a)
end
end
end
it '#next returns nil when there is only 1 record' do
p = create_issue.display_order
expect(p.next).to be_nil
expect(p.next(true)).to be_nil
end
it 'is ordered correctly for order query [[:id, :asc]]' do
a = create_issue
b = create_issue
expect(a.id_order_asc.next).to eq b
expect(b.id_order_asc.previous).to eq a
expect([a] + a.id_order_asc.after.to_a).to eq(Issue.id_order_asc.to_a)
expect(b.id_order_asc.before.reverse.to_a + [b]).to(
eq Issue.id_order_asc.to_a
)
expect(Issue.id_order_asc.count).to eq(2)
end
it '.seek works on a list of ids' do
ids = Array.new(3) { create_issue.id }
expect(Issue.seek([[:id, ids]]).count).to eq ids.length
expect(Issue.seek([:id, ids]).count).to eq ids.length
expect(Issue.seek([:id, ids]).scope.pluck(:id)).to eq ids
expect(Issue.seek([:id, ids]).scope_reverse.pluck(:id)).to(
eq(ids.reverse)
)
end
context 'partitioned on a boolean flag' do
before do
create_issue(active: true)
create_issue(active: false)
create_issue(active: true)
end
let!(:order) { [%i[id desc]] }
let!(:active) { Issue.where(active: true).seek(order) }
let!(:inactive) { Issue.where(active: false).seek(order) }
it '.seek preserves scope' do
expect(inactive.count).to eq 1
expect(inactive.scope.count).to eq 1
expect(inactive.scope_reverse.count).to eq 1
expect(active.count).to eq 2
expect(active.scope.count).to eq 2
expect(active.scope_reverse.count).to eq 2
end
it 'gives a valid result if at argument is outside of the space' do
expect(inactive.at(active.first).next).to_not be_active
expect(inactive.at(active.last).next).to_not be_active
expect(active.at(inactive.first).next).to be_active
expect(active.at(inactive.last).next).to be_active
end
it 'next/previous(false)' do
expect(active.at(active.first).next(false)).to_not be_nil
expect(active.at(active.last).next(false)).to be_nil
expect(inactive.at(inactive.first).previous(false)).to be_nil
# there is only one, so previous(last) is also nil
expect(inactive.at(inactive.last).previous(false)).to be_nil
end
it 'previous(true) with only 1 record' do
expect(inactive.at(inactive.last).previous(true)).to be_nil
end
end
it '#seek falls back to scope when order column is missing self' do
a = create_issue(priority: 'medium')
b = create_issue(priority: 'high')
expect(
a.seek(
Issue.display_order,
[[:priority, %w[wontfix askbob]], %i[id desc]]
).next
).to eq(b)
end
context 'nil in string enum' do
priorities = [nil, 'low', 'medium', 'high']
let!(:issues) { priorities.map { |p| create_issue(priority: p) } }
priorities.permutation do |p|
it "works for #{p} (desc)" do
scope = Issue.seek([:priority, p]).scope
actual = scope.all.map(&:priority)
expected = p
expect(actual).to eq(expected), scope.to_sql
end
it "works for #{p} (asc)" do
scope = Issue.seek([:priority, p, :asc]).scope
actual = scope.all.map(&:priority)
expected = p.reverse
expect(actual).to eq(expected), scope.to_sql
end
end
end
before do
Issue.delete_all
end
before :all do
ActiveRecord::Schema.define do
self.verbose = false
create_table :issues do |t|
t.column :pinned, :boolean, null: false, default: false
t.column :priority, :string
t.column :votes, :integer
t.column :suspicious_votes, :integer
t.column :announced_at, :datetime
t.column :updated_at, :datetime
t.column :active, :boolean, null: false, default: true
end
end
Issue.reset_column_information
end
after :all do
ActiveRecord::Migration.drop_table :issues
end
end
context 'Post test model' do
it '#next works' do
p1 = create_post(pinned: true)
o1 = p1.order_list
expect(o1.next).to be_nil
expect(o1.next(true)).to be_nil
p2 = create_post(pinned: false)
o2 = p2.order_list
expect(o1.next(false)).to eq(p2)
expect(o2.next(false)).to be_nil
expect(o2.next(true)).to eq(p1)
end
context '#inspect' do
it 'Column' do
expect(OrderQuery::Column.new(Post, :id, :desc).inspect)
.to eq '(id unique desc)'
expect(
OrderQuery::Column.new(Post, :virtual, :desc, sql: 'SIN(id)')
.inspect
).to eq '(virtual SIN(id) desc)'
end
let(:space) do
OrderQuery::Space.new(Post, [[:pinned, [true, false]]])
end
it 'Point' do
post = create_post
point = OrderQuery::Point.new(post, space)
# rubocop:disable Metrics/LineLength
expect(point.inspect).to eq %(#<OrderQuery::Point @record=#<Post id: #{post.id}, title: nil, pinned: false, published_at: #{post.attribute_for_inspect(:published_at)}> @space=#<OrderQuery::Space @columns=[(pinned [true, false] desc), (id unique asc)] @base_scope=Post(id: integer, title: string, pinned: boolean, published_at: datetime)>>)
# rubocop:enable Metrics/LineLength
end
it 'Space' do
# rubocop:disable Metrics/LineLength
expect(space.inspect).to eq '#<OrderQuery::Space @columns=[(pinned [true, false] desc), (id unique asc)] @base_scope=Post(id: integer, title: string, pinned: boolean, published_at: datetime)>'
# rubocop:enable Metrics/LineLength
end
end
context 'boolean enum order' do
before do
create_post pinned: true
create_post pinned: false
end
after do
Post.delete_all
end
it 'ORDER BY is collapsed' do
expect(Post.seek([:pinned, [true, false]]).scope.to_sql).to(
match(/ORDER BY .posts.\..pinned. DESC/)
)
end
it 'enum asc' do
expect(
Post.seek([:pinned, [false, true], :asc]).scope.pluck(:pinned)
).to eq([true, false])
expect(
Post.seek([:pinned, [true, false], :asc]).scope.pluck(:pinned)
).to eq([false, true])
end
it 'enum desc' do
expect(
Post.seek([:pinned, [false, true], :desc]).scope.pluck(:pinned)
).to eq([false, true])
expect(
Post.seek([:pinned, [true, false], :desc]).scope.pluck(:pinned)
).to eq([true, false])
end
end
context 'nil in boolean enum' do
states = [nil, false, true]
let!(:posts) { states.map { |state| create_post(pinned: state) } }
states.permutation do |p|
it "works for #{p} (desc)" do
scope = Post.seek([:pinned, p]).scope
actual = scope.all.map(&:pinned)
expected = p
expect(actual).to eq(expected), scope.to_sql
end
it "works for #{p} (asc)" do
scope = Post.seek([:pinned, p, :asc]).scope
actual = scope.all.map(&:pinned)
expected = p.reverse
expect(actual).to eq(expected), scope.to_sql
end
end
end
context 'nil published_at' do
# rubocop:disable Metrics/AbcSize
def expect_next(space, post, next_post)
point = space.at(post)
actual = point.next
failure_message =
"expected: #{post.title}.next == #{next_post.title}\n" \
" got: #{actual ? actual.title : 'nil'}\n" \
" all: #{space.scope.all.map(&:title)}\n" \
" sql: #{space.at(post).after.limit(1).to_sql}"
expect(actual ? actual.title : nil).to eq(next_post.title),
failure_message
end
def expect_prev(space, post, prev_post)
point = space.at(post)
actual = point.previous
failure_message =
"expected: #{post.title}.previous == #{prev_post.title}\n" \
" got: #{actual ? actual.title : 'nil'}\n" \
" all: #{space.scope.all.map(&:title)}\n" \
" sql: #{space.at(post).before.limit(1).to_sql}"
expect(actual ? actual.title : nil).to eq(prev_post.title),
failure_message
end
def expect_order(space, ordered)
actual = space.scope.all.map(&:title)
expected = ordered.map(&:title)
failure_message =
"expected: #{expected * ', '}\n"\
" got: #{actual * ', '}\n"\
" sql: #{space.scope.to_sql}"
expect(actual).to eq(expected), failure_message
ordered.each_cons(2) do |post, next_post|
expect_next space, post, next_post
expect_prev space, next_post, post
end
expect_next space, ordered.last, ordered.first
expect_prev space, ordered.first, ordered.last
end
# rubocop:enable Metrics/AbcSize
let! :null_1 do
Post.create!(title: 'null_1', published_at: nil).reload
end
let! :null_2 do
Post.create!(title: 'null_2', published_at: nil).reload
end
let! :older do
Post.create!(title: 'older', published_at: Time.now + 1.hour)
end
let! :newer do
Post.create!(title: 'newer', published_at: Time.now - 1.hour)
end
it 'orders nulls first (desc)' do
space = Post.seek([:published_at, :desc, nulls: :first])
expect_order space, [null_1, null_2, older, newer]
end
it 'orders nulls first (asc)' do
space = Post.seek([:published_at, :asc, nulls: :first])
expect_order space, [null_1, null_2, newer, older]
end
it 'orders nulls last (desc)' do
space = Post.seek([:published_at, :desc, nulls: :last])
expect_order space, [older, newer, null_1, null_2]
end
it 'orders nulls last (asc)' do
space = Post.seek([:published_at, :asc, nulls: :last])
expect_order space, [newer, older, null_1, null_2]
end
end
context 'after/before no strict' do
context 'by middle attribute in search order' do
let! :base do
Post.create! pinned: true, published_at: Time.now
end
let! :older do
Post.create! pinned: true, published_at: Time.now + 1.hour
end
let! :newer do
Post.create! pinned: true, published_at: Time.now - 1.hour
end
it 'includes first element' do
point = Post.order_list_at(base)
expect(point.after.count).to eq 1
expect(point.after.to_a).to eq [newer]
expect(point.after(false).count).to eq 2
expect(point.after(false).to_a).to eq [base, newer]
expect(point.before(false).to_a).to eq [base, older]
end
end
context 'by last attribute in search order' do
let!(:base) do
Post.create! pinned: true,
published_at: Time.new(2016, 5, 1, 5, 4, 3),
id: 6
end
let!(:previous) do
Post.create! pinned: true,
published_at: Time.new(2016, 5, 1, 5, 4, 3),
id: 4
end
let!(:next_one) do
Post.create! pinned: true,
published_at: Time.new(2016, 5, 1, 5, 4, 3),
id: 9
end
it 'includes first element' do
point = Post.order_list_at(base)
expect(point.after.count).to eq 1
expect(point.after.to_a).to eq [previous]
expect(point.after(false).count).to eq 2
expect(point.after(false).to_a).to eq [base, previous]
expect(point.before(false).to_a).to eq [base, next_one]
end
end
end
before do
Post.delete_all
end
before :all do
ActiveRecord::Schema.define do
self.verbose = false
create_table :posts do |t|
t.string :title
t.boolean :pinned
t.datetime :published_at
end
end
Post.reset_column_information
end
after :all do
ActiveRecord::Migration.drop_table :posts
end
end
end
end
context 'SQL generation' do
context 'wrap top-level OR on' do
wrap_top_level_or true
it 'wraps top-level OR' do
after_scope = User.create!(updated_at: Date.parse('2014-09-06'))
.seek([%i[updated_at desc], %i[id desc]]).after
expect(after_scope.to_sql).to include('<=')
end
end
context 'wrap top-level OR off' do
wrap_top_level_or false
it 'does not wrap top-level OR' do
after_scope = User.create!(updated_at: Date.parse('2014-09-06'))
.seek([%i[updated_at desc], %i[id desc]]).after
expect(after_scope.to_sql).to_not include('<=')
end
end
before do
User.delete_all
end
before :all do
ActiveRecord::Schema.define do
self.verbose = false
create_table :users do |t|
t.datetime :updated_at, null: false
end
end
User.reset_column_information
end
after :all do
ActiveRecord::Migration.drop_table :users
end
end
end