forked from brendon/ranked-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lots_of_ducks_spec.rb
117 lines (79 loc) · 2.93 KB
/
lots_of_ducks_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
require 'spec_helper'
describe Duck do
before {
200.times do |i|
Duck.create \
:name => "Duck #{i}"
end
}
describe "setting and fetching by position" do
describe '137' do
before {
@last = Duck.last
@last.update_attribute :row_position, 137
}
subject { Duck.ranker(:row).with(Duck.new).current_at_position(137).instance }
its(:id) { should == @last.id }
end
describe '2' do
before {
@last = Duck.last
@last.update_attribute :row_position, 2
}
subject { Duck.ranker(:row).with(Duck.new).current_at_position(2).instance }
its(:id) { should == @last.id }
end
end
describe "a rearrangement" do
describe "with max value" do
before {
@first = Duck.first
@second = Duck.offset(1).first
@ordered = Duck.rank(:row).where(Duck.arel_table[:id].not_in([@first.id, @second.id])).collect {|d| d.id }
@first.update_attribute :row, RankedModel::MAX_RANK_VALUE
@second.update_attribute :row, RankedModel::MAX_RANK_VALUE
}
context {
subject { Duck.rank(:row).collect {|d| d.id } }
it { should == (@ordered[0..-2] + [@first.id, @second.id, @ordered[-1]]) }
}
end
describe "with min value" do
before {
@first = Duck.first
@second = Duck.offset(1).first
@ordered = Duck.rank(:row).where(Duck.arel_table[:id].not_in([@first.id, @second.id])).collect {|d| d.id }
@first.update_attribute :row, RankedModel::MIN_RANK_VALUE
@second.update_attribute :row, RankedModel::MIN_RANK_VALUE
}
context {
subject { Duck.rank(:row).collect {|d| d.id } }
it { should == ([@second.id, @first.id] + @ordered) }
}
end
describe "with no more gaps" do
before {
@first = Duck.first
@second = Duck.where(:row => RankedModel::MAX_RANK_VALUE).first || Duck.offset(1).first
@third = Duck.offset(2).first
@fourth = Duck.offset(4).first
@lower = Duck.rank(:row).
where(Duck.arel_table[:id].not_in([@first.id, @second.id, @third.id, @fourth.id])).
where(Duck.arel_table[:row].lt(RankedModel::MAX_RANK_VALUE / 2)).
collect {|d| d.id }
@upper = Duck.rank(:row).
where(Duck.arel_table[:id].not_in([@first.id, @second.id, @third.id, @fourth.id])).
where(Duck.arel_table[:row].gteq(RankedModel::MAX_RANK_VALUE / 2)).
collect {|d| d.id }
@first.update_attribute :row, RankedModel::MIN_RANK_VALUE
@second.update_attribute :row, RankedModel::MAX_RANK_VALUE
@third.update_attribute :row, (RankedModel::MAX_RANK_VALUE / 2)
@fourth.update_attribute :row, @third.row
}
context {
subject { Duck.rank(:row).collect {|d| d.id } }
it { should == ([@first.id] + @lower + [@fourth.id, @third.id] + @upper + [@second.id]) }
}
end
end
end