@@ -37,7 +37,7 @@ def test_sort_by_ancestry_full_tree
37
37
AncestryTestDatabase . with_model do |model |
38
38
n1 , n2 , n3 , n4 , n5 , n6 = build_tree ( model )
39
39
40
- records = model . sort_by_ancestry ( model . all . order ( :id ) . reverse )
40
+ records = model . sort_by_ancestry ( model . all . ordered_by_ancestry_and ( :id => :desc ) )
41
41
assert_equal [ n1 , n5 , n6 , n2 , n4 , n3 ] . map ( &:id ) , records . map ( &:id )
42
42
end
43
43
end
@@ -51,7 +51,8 @@ def test_sort_by_ancestry_no_parents_siblings
51
51
AncestryTestDatabase . with_model do |model |
52
52
_ , _ , n3 , n4 , _ , _ = build_tree ( model )
53
53
54
- assert_equal [ n4 , n3 ] . map ( &:id ) , model . sort_by_ancestry ( [ n4 , n3 ] ) . map ( &:id )
54
+ records = model . sort_by_ancestry ( model . all . ordered_by_ancestry_and ( :id => :desc ) . offset ( 3 ) . take ( 2 ) )
55
+ assert_equal [ n4 , n3 ] . map ( &:id ) , records . map ( &:id )
55
56
end
56
57
end
57
58
@@ -61,15 +62,18 @@ def test_sort_by_ancestry_no_parents_same_level
61
62
AncestryTestDatabase . with_model do |model |
62
63
_ , _ , n3 , n4 , n5 , _ = build_tree ( model )
63
64
64
- assert_equal [ n5 , n4 , n3 ] . map ( &:id ) , model . sort_by_ancestry ( [ n5 , n4 , n3 ] ) . map ( &:id )
65
+ records = [ n5 , n4 , n3 ]
66
+ # records = model.sort_by_ancestry(model.all.ordered_by_ancestry_and(:id => :desc).offset(3).take(3))
67
+ assert_equal [ n5 , n4 , n3 ] . map ( &:id ) , records . map ( &:id )
65
68
end
66
69
end
67
70
68
71
def test_sort_by_ancestry_partial_tree
69
72
AncestryTestDatabase . with_model do |model |
70
73
n1 , n2 , _ , _ , n5 , _ = build_tree ( model )
71
74
72
- assert_equal [ n1 , n5 , n2 ] . map ( &:id ) , model . sort_by_ancestry ( [ n5 , n2 , n1 ] ) . map ( &:id )
75
+ records = model . sort_by_ancestry ( model . all . ordered_by_ancestry_and ( :id => :desc ) . offset ( 0 ) . take ( 3 ) )
76
+ assert_equal [ n1 , n5 , n2 ] . map ( &:id ) , records . map ( &:id )
73
77
end
74
78
end
75
79
@@ -142,7 +146,7 @@ def test_sort_by_ancestry_with_block_full_tree_sql_sort
142
146
AncestryTestDatabase . with_model :extra_columns => { :rank => :integer } do |model |
143
147
n1 , n2 , n3 , n4 , n5 , n6 = build_ranked_tree ( model )
144
148
145
- records = model . sort_by_ancestry ( model . all . order ( :rank ) )
149
+ records = model . sort_by_ancestry ( model . all . ordered_by_ancestry_and ( :rank ) )
146
150
assert_equal [ n1 , n5 , n3 , n2 , n4 , n6 ] . map ( &:id ) , records . map ( &:id )
147
151
end
148
152
end
@@ -151,7 +155,8 @@ def test_sort_by_ancestry_with_block_all_parents_some_children
151
155
AncestryTestDatabase . with_model :extra_columns => { :rank => :integer } do |model |
152
156
n1 , n2 , _ , _ , n5 , _ = build_ranked_tree ( model )
153
157
154
- assert_equal [ n1 , n5 , n2 ] . map ( &:id ) , model . sort_by_ancestry ( [ n1 , n2 , n5 ] , &RANK_SORT ) . map ( &:id )
158
+ records = model . sort_by_ancestry ( model . all . ordered_by_ancestry_and ( :rank ) . take ( 3 ) , &RANK_SORT )
159
+ assert_equal [ n1 , n5 , n2 ] . map ( &:id ) , records . map ( &:id )
155
160
end
156
161
end
157
162
@@ -176,7 +181,7 @@ def test_sort_by_ancestry_with_block_no_parents_all_children
176
181
AncestryTestDatabase . with_model :extra_columns => { :rank => :integer } do |model |
177
182
_ , _ , n3 , n4 , n5 , n6 = build_ranked_tree ( model )
178
183
179
- records = model . sort_by_ancestry ( [ n3 , n4 , n5 , n6 ] , &RANK_SORT )
184
+ records = model . sort_by_ancestry ( model . all . ordered_by_ancestry_and ( :rank ) . offset ( 2 ) , &RANK_SORT )
180
185
if CORRECT || records [ 0 ] == n5
181
186
assert_equal [ n5 , n3 , n4 , n6 ] . map ( &:id ) , records . map ( &:id )
182
187
else
@@ -191,20 +196,20 @@ def test_sort_by_ancestry_with_block_no_parents_all_children
191
196
# - n2 (1)
192
197
# - n4 (0)
193
198
# - x
194
- # Issue: n2 will always go before n4.
199
+ # Issue: n2 will always go before n4, n5 .
195
200
# But n1 is not available to put n3 before the n2 tree.
196
201
# not sure why it doesn't follow the input order
197
202
#
198
203
# NOTE: even for partial trees, if the input records are ranked, the output works
199
204
def test_sort_by_ancestry_with_sql_sort_paginated_missing_parents_and_children
200
205
AncestryTestDatabase . with_model :extra_columns => { :rank => :integer } do |model |
201
- _ , n2 , n3 , n4 , _ , _ = build_ranked_tree ( model )
206
+ _ , n2 , n3 , n4 , n5 , _ = build_ranked_tree ( model )
202
207
203
- records = model . sort_by_ancestry ( [ n2 , n4 , n3 ] )
208
+ records = model . sort_by_ancestry ( model . all . ordered_by_ancestry_and ( :rank ) . offset ( 1 ) . take ( 4 ) )
204
209
if CORRECT
205
- assert_equal [ n3 , n2 , n4 ] . map ( &:id ) , records . map ( &:id )
210
+ assert_equal [ n3 , n2 , n4 , n5 ] . map ( &:id ) , records . map ( &:id )
206
211
else
207
- assert_equal [ n2 , n4 , n3 ] . map ( &:id ) , records . map ( &:id )
212
+ assert_equal [ n2 , n4 , n5 , n3 ] . map ( &:id ) , records . map ( &:id )
208
213
end
209
214
end
210
215
end
@@ -218,13 +223,13 @@ def test_sort_by_ancestry_with_sql_sort_paginated_missing_parents_and_children
218
223
# - n5
219
224
def test_sort_by_ancestry_with_block_paginated_missing_parents_and_children
220
225
AncestryTestDatabase . with_model :extra_columns => { :rank => :integer } do |model |
221
- _ , n2 , n3 , n4 , _ , _ = build_ranked_tree ( model )
226
+ _ , n2 , n3 , n4 , n5 , _ = build_ranked_tree ( model )
222
227
223
- records = model . sort_by_ancestry ( [ n2 , n4 , n3 ] , &RANK_SORT )
228
+ records = model . sort_by_ancestry ( model . all . ordered_by_ancestry_and ( :rank ) . offset ( 1 ) . take ( 4 ) , &RANK_SORT )
224
229
if CORRECT
225
- assert_equal [ n3 , n2 , n4 ] . map ( &:id ) , records . map ( &:id )
230
+ assert_equal [ n3 , n2 , n4 , n5 ] . map ( &:id ) , records . map ( &:id )
226
231
else
227
- assert_equal [ n2 , n4 , n3 ] . map ( &:id ) , records . map ( &:id )
232
+ assert_equal [ n2 , n4 , n5 , n3 ] . map ( &:id ) , records . map ( &:id )
228
233
end
229
234
end
230
235
end
0 commit comments