Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 609 lines (533 sloc) 22.409 kb
1282d2e @stefankroes Initial commit
authored
1 require File.dirname(__FILE__) + '/test_helper.rb'
2
ecea76f @stefankroes Version 1.2.0
authored
3 # Setup the required models for all test cases
4
1282d2e @stefankroes Initial commit
authored
5 class TestNode < ActiveRecord::Base
ecea76f @stefankroes Version 1.2.0
authored
6 has_ancestry :cache_depth => true, :depth_cache_column => :depth_cache
1282d2e @stefankroes Initial commit
authored
7 end
8
9 class AlternativeTestNode < ActiveRecord::Base
ecea76f @stefankroes Version 1.2.0
authored
10 has_ancestry :ancestry_column => :alternative_ancestry, :orphan_strategy => :rootify
11 end
12
13 class ActsAsTreeTestNode < ActiveRecord::Base
14 acts_as_tree
1282d2e @stefankroes Initial commit
authored
15 end
16
df705c3 @stefankroes Version 1.1.0 done!
authored
17 class ParentIdTestNode < ActiveRecord::Base
18 end
19
e3ea26a @stefankroes - Version 1.1.2 (2009-10-29)
authored
20 class TestNodeSub1 < TestNode
21 end
22
23 class TestNodeSub2 < TestNode
24 end
25
1282d2e @stefankroes Initial commit
authored
26 class ActsAsTreeTest < ActiveSupport::TestCase
27 load_schema
28
29 def setup_test_nodes model, level, quantity
30 model.delete_all
31 create_test_nodes model, nil, level, quantity
32 end
33
34 def create_test_nodes model, parent, level, quantity
35 unless level == 0
36 (1..quantity).map do |i|
37 node = model.create!(:parent => parent)
38 [node, create_test_nodes(model, node, level - 1, quantity)]
39 end
40 else; []; end
41 end
42
43 def test_default_ancestry_column
44 assert_equal :ancestry, TestNode.ancestry_column
45 end
46
47 def test_non_default_ancestry_column
48 assert_equal :alternative_ancestry, AlternativeTestNode.ancestry_column
49 end
50
51 def test_setting_ancestry_column
52 TestNode.ancestry_column = :ancestors
53 assert_equal :ancestors, TestNode.ancestry_column
54 TestNode.ancestry_column = :ancestry
55 assert_equal :ancestry, TestNode.ancestry_column
56 end
57
58 def test_default_orphan_strategy
59 assert_equal :destroy, TestNode.orphan_strategy
60 end
61
62 def test_non_default_orphan_strategy
63 assert_equal :rootify, AlternativeTestNode.orphan_strategy
64 end
65
66 def test_setting_orphan_strategy
67 TestNode.orphan_strategy = :rootify
68 assert_equal :rootify, TestNode.orphan_strategy
69 TestNode.orphan_strategy = :destroy
70 assert_equal :destroy, TestNode.orphan_strategy
71 end
72
73 def test_setting_invalid_orphan_strategy
74 assert_raise Ancestry::AncestryException do
75 TestNode.orphan_strategy = :non_existent_orphan_strategy
76 end
77 end
78
79 def test_setup_test_nodes
ecea76f @stefankroes Version 1.2.0
authored
80 [TestNode, AlternativeTestNode, ActsAsTreeTestNode].each do |model|
1282d2e @stefankroes Initial commit
authored
81 roots = setup_test_nodes model, 3, 3
82 assert_equal Array, roots.class
83 assert_equal 3, roots.length
84 roots.each do |node, children|
85 assert_equal model, node.class
86 assert_equal Array, children.class
87 assert_equal 3, children.length
88 children.each do |node, children|
89 assert_equal model, node.class
90 assert_equal Array, children.class
91 assert_equal 3, children.length
92 children.each do |node, children|
93 assert_equal model, node.class
94 assert_equal Array, children.class
95 assert_equal 0, children.length
96 end
97 end
98 end
99 end
100 end
101
102 def test_tree_navigation
103 roots = setup_test_nodes TestNode, 3, 3
104 roots.each do |lvl0_node, lvl0_children|
105 # Ancestors assertions
106 assert_equal [], lvl0_node.ancestor_ids
107 assert_equal [], lvl0_node.ancestors
108 assert_equal [lvl0_node.id], lvl0_node.path_ids
109 assert_equal [lvl0_node], lvl0_node.path
df705c3 @stefankroes Version 1.1.0 done!
authored
110 assert_equal 0, lvl0_node.depth
1282d2e @stefankroes Initial commit
authored
111 # Parent assertions
112 assert_equal nil, lvl0_node.parent_id
113 assert_equal nil, lvl0_node.parent
114 # Root assertions
115 assert_equal lvl0_node.id, lvl0_node.root_id
116 assert_equal lvl0_node, lvl0_node.root
117 assert lvl0_node.is_root?
118 # Children assertions
119 assert_equal lvl0_children.map(&:first).map(&:id), lvl0_node.child_ids
120 assert_equal lvl0_children.map(&:first), lvl0_node.children
121 assert lvl0_node.has_children?
122 assert !lvl0_node.is_childless?
123 # Siblings assertions
124 assert_equal roots.map(&:first).map(&:id), lvl0_node.sibling_ids
125 assert_equal roots.map(&:first), lvl0_node.siblings
126 assert lvl0_node.has_siblings?
127 assert !lvl0_node.is_only_child?
128 # Descendants assertions
129 descendants = TestNode.all.find_all do |node|
130 node.ancestor_ids.include? lvl0_node.id
131 end
132 assert_equal descendants.map(&:id), lvl0_node.descendant_ids
133 assert_equal descendants, lvl0_node.descendants
134 assert_equal [lvl0_node] + descendants, lvl0_node.subtree
135
136 lvl0_children.each do |lvl1_node, lvl1_children|
137 # Ancestors assertions
138 assert_equal [lvl0_node.id], lvl1_node.ancestor_ids
139 assert_equal [lvl0_node], lvl1_node.ancestors
140 assert_equal [lvl0_node.id, lvl1_node.id], lvl1_node.path_ids
141 assert_equal [lvl0_node, lvl1_node], lvl1_node.path
df705c3 @stefankroes Version 1.1.0 done!
authored
142 assert_equal 1, lvl1_node.depth
1282d2e @stefankroes Initial commit
authored
143 # Parent assertions
144 assert_equal lvl0_node.id, lvl1_node.parent_id
145 assert_equal lvl0_node, lvl1_node.parent
146 # Root assertions
147 assert_equal lvl0_node.id, lvl1_node.root_id
148 assert_equal lvl0_node, lvl1_node.root
149 assert !lvl1_node.is_root?
150 # Children assertions
151 assert_equal lvl1_children.map(&:first).map(&:id), lvl1_node.child_ids
152 assert_equal lvl1_children.map(&:first), lvl1_node.children
153 assert lvl1_node.has_children?
154 assert !lvl1_node.is_childless?
155 # Siblings assertions
156 assert_equal lvl0_children.map(&:first).map(&:id), lvl1_node.sibling_ids
157 assert_equal lvl0_children.map(&:first), lvl1_node.siblings
158 assert lvl1_node.has_siblings?
159 assert !lvl1_node.is_only_child?
160 # Descendants assertions
161 descendants = TestNode.all.find_all do |node|
162 node.ancestor_ids.include? lvl1_node.id
163 end
164 assert_equal descendants.map(&:id), lvl1_node.descendant_ids
165 assert_equal descendants, lvl1_node.descendants
166 assert_equal [lvl1_node] + descendants, lvl1_node.subtree
167
168 lvl1_children.each do |lvl2_node, lvl2_children|
169 # Ancestors assertions
170 assert_equal [lvl0_node.id, lvl1_node.id], lvl2_node.ancestor_ids
171 assert_equal [lvl0_node, lvl1_node], lvl2_node.ancestors
172 assert_equal [lvl0_node.id, lvl1_node.id, lvl2_node.id], lvl2_node.path_ids
173 assert_equal [lvl0_node, lvl1_node, lvl2_node], lvl2_node.path
df705c3 @stefankroes Version 1.1.0 done!
authored
174 assert_equal 2, lvl2_node.depth
1282d2e @stefankroes Initial commit
authored
175 # Parent assertions
176 assert_equal lvl1_node.id, lvl2_node.parent_id
177 assert_equal lvl1_node, lvl2_node.parent
178 # Root assertions
179 assert_equal lvl0_node.id, lvl2_node.root_id
180 assert_equal lvl0_node, lvl2_node.root
181 assert !lvl2_node.is_root?
182 # Children assertions
183 assert_equal [], lvl2_node.child_ids
184 assert_equal [], lvl2_node.children
185 assert !lvl2_node.has_children?
186 assert lvl2_node.is_childless?
187 # Siblings assertions
188 assert_equal lvl1_children.map(&:first).map(&:id), lvl2_node.sibling_ids
189 assert_equal lvl1_children.map(&:first), lvl2_node.siblings
190 assert lvl2_node.has_siblings?
191 assert !lvl2_node.is_only_child?
192 # Descendants assertions
193 descendants = TestNode.all.find_all do |node|
194 node.ancestor_ids.include? lvl2_node.id
195 end
196 assert_equal descendants.map(&:id), lvl2_node.descendant_ids
197 assert_equal descendants, lvl2_node.descendants
198 assert_equal [lvl2_node] + descendants, lvl2_node.subtree
199 end
200 end
201 end
202 end
203
204 def test_named_scopes
205 roots = setup_test_nodes TestNode, 3, 3
206
207 # Roots assertion
22a1984 - Added some tests for node creation through scopes
Stefan Kroes authored
208 assert_equal roots.map(&:first), TestNode.roots.all
1282d2e @stefankroes Initial commit
authored
209
210 TestNode.all.each do |test_node|
22a1984 - Added some tests for node creation through scopes
Stefan Kroes authored
211 # Assertions for ancestors_of named scope
212 assert_equal test_node.ancestors, TestNode.ancestors_of(test_node)
213 assert_equal test_node.ancestors, TestNode.ancestors_of(test_node.id)
214 # Assertions for children_of named scope
215 assert_equal test_node.children, TestNode.children_of(test_node)
216 assert_equal test_node.children, TestNode.children_of(test_node.id)
217 # Assertions for descendants_of named scope
218 assert_equal test_node.descendants, TestNode.descendants_of(test_node)
219 assert_equal test_node.descendants, TestNode.descendants_of(test_node.id)
ecea76f @stefankroes Version 1.2.0
authored
220 # Assertions for subtree_of named scope
221 assert_equal test_node.subtree, TestNode.subtree_of(test_node)
222 assert_equal test_node.subtree, TestNode.subtree_of(test_node.id)
22a1984 - Added some tests for node creation through scopes
Stefan Kroes authored
223 # Assertions for siblings_of named scope
224 assert_equal test_node.siblings, TestNode.siblings_of(test_node)
225 assert_equal test_node.siblings, TestNode.siblings_of(test_node.id)
1282d2e @stefankroes Initial commit
authored
226 end
227 end
228
229 def test_ancestroy_column_validation
488e752 @stefankroes Added validation to detect cycles in record ancestry
authored
230 node = TestNode.create
1282d2e @stefankroes Initial commit
authored
231 ['3', '10/2', '1/4/30', nil].each do |value|
232 node.write_attribute TestNode.ancestry_column, value
488e752 @stefankroes Added validation to detect cycles in record ancestry
authored
233 node.valid?; assert !node.errors.invalid?(TestNode.ancestry_column)
1282d2e @stefankroes Initial commit
authored
234 end
235 ['1/3/', '/2/3', 'a', 'a/b', '-34', '/54'].each do |value|
236 node.write_attribute TestNode.ancestry_column, value
488e752 @stefankroes Added validation to detect cycles in record ancestry
authored
237 node.valid?; assert node.errors.invalid?(TestNode.ancestry_column)
1282d2e @stefankroes Initial commit
authored
238 end
239 end
240
241 def test_descendants_move_with_node
242 root1, root2, root3 = setup_test_nodes(TestNode, 3, 3).map(&:first)
243 assert_no_difference 'root1.descendants.size' do
244 assert_difference 'root2.descendants.size', root1.subtree.size do
245 root1.parent = root2
246 root1.save!
247 end
248 end
249 assert_no_difference 'root2.descendants.size' do
250 assert_difference 'root3.descendants.size', root2.subtree.size do
251 root2.parent = root3
252 root2.save!
253 end
254 end
255 assert_no_difference 'root1.descendants.size' do
256 assert_difference 'root2.descendants.size', -root1.subtree.size do
257 assert_difference 'root3.descendants.size', -root1.subtree.size do
258 root1.parent = nil
259 root1.save!
260 end
261 end
262 end
263 end
264
265 def test_orphan_rootify_strategy
266 TestNode.orphan_strategy = :rootify
267 root = setup_test_nodes(TestNode, 3, 3).first.first
268 children = root.children.all
269 root.destroy
270 children.each do |child|
271 child.reload
272 assert child.is_root?
273 assert_equal 3, child.children.size
274 end
275 end
276
277 def test_orphan_destroy_strategy
278 TestNode.orphan_strategy = :destroy
279 root = setup_test_nodes(TestNode, 3, 3).first.first
280 assert_difference 'TestNode.count', -root.subtree.size do
281 root.destroy
282 end
22a1984 - Added some tests for node creation through scopes
Stefan Kroes authored
283 node = TestNode.roots.first.children.first
1282d2e @stefankroes Initial commit
authored
284 assert_difference 'TestNode.count', -node.subtree.size do
285 node.destroy
286 end
287 end
288
289 def test_orphan_restrict_strategy
290 TestNode.orphan_strategy = :restrict
291 setup_test_nodes(TestNode, 3, 3)
22a1984 - Added some tests for node creation through scopes
Stefan Kroes authored
292 root = TestNode.roots.first
1282d2e @stefankroes Initial commit
authored
293 assert_raise Ancestry::AncestryException do
294 root.destroy
295 end
296 assert_nothing_raised Ancestry::AncestryException do
297 root.children.first.children.first.destroy
298 end
299
300 end
301
302 def test_integrity_checking
303 # Check that there are no errors on a valid data set
304 setup_test_nodes(TestNode, 3, 3)
305 assert_nothing_raised do
df705c3 @stefankroes Version 1.1.0 done!
authored
306 TestNode.check_ancestry_integrity!
1282d2e @stefankroes Initial commit
authored
307 end
308
309 # Check detection of invalid format for ancestry column
310 setup_test_nodes(TestNode, 3, 3).first.first.update_attribute TestNode.ancestry_column, 'invalid_ancestry'
311 assert_raise Ancestry::AncestryIntegrityException do
df705c3 @stefankroes Version 1.1.0 done!
authored
312 TestNode.check_ancestry_integrity!
1282d2e @stefankroes Initial commit
authored
313 end
314
315 # Check detection of non-existent ancestor
316 setup_test_nodes(TestNode, 3, 3).first.first.update_attribute TestNode.ancestry_column, 35
317 assert_raise Ancestry::AncestryIntegrityException do
df705c3 @stefankroes Version 1.1.0 done!
authored
318 TestNode.check_ancestry_integrity!
1282d2e @stefankroes Initial commit
authored
319 end
320
321 # Check detection of cyclic ancestry
322 node = setup_test_nodes(TestNode, 3, 3).first.first
323 node.update_attribute TestNode.ancestry_column, node.id
324 assert_raise Ancestry::AncestryIntegrityException do
df705c3 @stefankroes Version 1.1.0 done!
authored
325 TestNode.check_ancestry_integrity!
1282d2e @stefankroes Initial commit
authored
326 end
327
328 # Check detection of conflicting parent id
329 TestNode.destroy_all
330 TestNode.create!(TestNode.ancestry_column => TestNode.create!(TestNode.ancestry_column => TestNode.create!(TestNode.ancestry_column => nil).id).id)
331 assert_raise Ancestry::AncestryIntegrityException do
df705c3 @stefankroes Version 1.1.0 done!
authored
332 TestNode.check_ancestry_integrity!
1282d2e @stefankroes Initial commit
authored
333 end
334 end
335
336 def assert_integrity_restoration
337 assert_raise Ancestry::AncestryIntegrityException do
df705c3 @stefankroes Version 1.1.0 done!
authored
338 TestNode.check_ancestry_integrity!
1282d2e @stefankroes Initial commit
authored
339 end
df705c3 @stefankroes Version 1.1.0 done!
authored
340 TestNode.restore_ancestry_integrity!
1282d2e @stefankroes Initial commit
authored
341 assert_nothing_raised do
df705c3 @stefankroes Version 1.1.0 done!
authored
342 TestNode.check_ancestry_integrity!
1282d2e @stefankroes Initial commit
authored
343 end
344 end
345
346 def test_integrity_restoration
347 # Check that integrity is restored for invalid format for ancestry column
348 setup_test_nodes(TestNode, 3, 3).first.first.update_attribute TestNode.ancestry_column, 'invalid_ancestry'
349 assert_integrity_restoration
350
351 # Check that integrity is restored for non-existent ancestor
352 setup_test_nodes(TestNode, 3, 3).first.first.update_attribute TestNode.ancestry_column, 35
353 assert_integrity_restoration
354
355 # Check that integrity is restored for cyclic ancestry
356 node = setup_test_nodes(TestNode, 3, 3).first.first
357 node.update_attribute TestNode.ancestry_column, node.id
358 assert_integrity_restoration
359
360 # Check that integrity is restored for conflicting parent id
361 TestNode.destroy_all
362 TestNode.create!(TestNode.ancestry_column => TestNode.create!(TestNode.ancestry_column => TestNode.create!(TestNode.ancestry_column => nil).id).id)
363 assert_integrity_restoration
364 end
365
366 def test_arrangement
367 id_sorter = Proc.new do |a, b|; a.id <=> b.id; end
368 setup_test_nodes(TestNode, 3, 3)
369 arranged_nodes = TestNode.arrange
370 assert_equal 3, arranged_nodes.size
371 arranged_nodes.each do |node, children|
372 assert_equal node.children.sort(&id_sorter), children.keys.sort(&id_sorter)
373 children.each do |node, children|
374 assert_equal node.children.sort(&id_sorter), children.keys.sort(&id_sorter)
375 children.each do |node, children|
376 assert_equal 0, children.size
377 end
378 end
379 end
380 end
22a1984 - Added some tests for node creation through scopes
Stefan Kroes authored
381
382 def test_node_creation_though_scope
383 node = TestNode.create!
384 child = node.children.create
385 assert_equal node, child.parent
386
387 other_child = child.siblings.create!
388 assert_equal node, other_child.parent
389
390 grandchild = TestNode.children_of(child).new
391 grandchild.save
392 assert_equal child, grandchild.parent
393
394 other_grandchild = TestNode.siblings_of(grandchild).new
395 other_grandchild.save!
396 assert_equal child, other_grandchild.parent
397 end
488e752 @stefankroes Added validation to detect cycles in record ancestry
authored
398
399 def test_validate_ancestry_exclude_self
400 parent = TestNode.create!
401 child = parent.children.create!
402 assert_raise ActiveRecord::RecordInvalid do
403 parent.update_attributes! :parent => child
404 end
405 end
df705c3 @stefankroes Version 1.1.0 done!
authored
406
407 def test_depth_caching
408 roots = setup_test_nodes TestNode, 3, 3
409 roots.each do |lvl0_node, lvl0_children|
410 assert_equal 0, lvl0_node.depth_cache
411 lvl0_children.each do |lvl1_node, lvl1_children|
412 assert_equal 1, lvl1_node.depth_cache
413 lvl1_children.each do |lvl2_node, lvl2_children|
414 assert_equal 2, lvl2_node.depth_cache
415 end
416 end
417 end
418 end
419
420 def test_depth_scopes
421 setup_test_nodes TestNode, 4, 4
422 TestNode.before_depth(2).all? { |node| assert node.depth < 2 }
423 TestNode.to_depth(2).all? { |node| assert node.depth <= 2 }
424 TestNode.at_depth(2).all? { |node| assert node.depth == 2 }
425 TestNode.from_depth(2).all? { |node| assert node.depth >= 2 }
426 TestNode.after_depth(2).all? { |node| assert node.depth > 2 }
427 end
428
429 def test_depth_scopes_unavailable
430 assert_raise Ancestry::AncestryException do
431 AlternativeTestNode.before_depth(1)
432 AlternativeTestNode.to_depth(1)
433 AlternativeTestNode.at_depth(1)
434 AlternativeTestNode.from_depth(1)
435 AlternativeTestNode.after_depth(1)
436 end
437 end
438
ecea76f @stefankroes Version 1.2.0
authored
439 def test_invalid_has_ancestry_options
df705c3 @stefankroes Version 1.1.0 done!
authored
440 assert_raise Ancestry::AncestryException do
ecea76f @stefankroes Version 1.2.0
authored
441 Class.new(ActiveRecord::Base).has_ancestry :this_option_doesnt_exist => 42
df705c3 @stefankroes Version 1.1.0 done!
authored
442 end
443 assert_raise Ancestry::AncestryException do
ecea76f @stefankroes Version 1.2.0
authored
444 Class.new(ActiveRecord::Base).has_ancestry :not_a_hash
df705c3 @stefankroes Version 1.1.0 done!
authored
445 end
446 end
447
448 def test_build_ancestry_from_parent_ids
449 [ParentIdTestNode.create!].each do |parent|
450 (Array.new(5) { ParentIdTestNode.create! :parent_id => parent.id }).each do |parent|
451 (Array.new(5) { ParentIdTestNode.create! :parent_id => parent.id }).each do |parent|
452 (Array.new(5) { ParentIdTestNode.create! :parent_id => parent.id })
453 end
454 end
455 end
456
457 # Assert all nodes where created
458 assert_equal 156, ParentIdTestNode.count
459
ecea76f @stefankroes Version 1.2.0
authored
460 ParentIdTestNode.has_ancestry
df705c3 @stefankroes Version 1.1.0 done!
authored
461 ParentIdTestNode.build_ancestry_from_parent_ids!
462
463 # Assert ancestry integirty
464 assert_nothing_raised do
465 ParentIdTestNode.check_ancestry_integrity!
466 end
467
468 roots = ParentIdTestNode.roots.all
469 # Assert single root node
470 assert_equal 1, roots.size
471
472 # Assert it has 5 children
473 roots.each do |parent|
474 assert 5, parent.children.count
475 parent.children.each do |parent|
476 assert 5, parent.children.count
477 parent.children.each do |parent|
478 assert 5, parent.children.count
479 parent.children.each do |parent|
480 assert 0, parent.children.count
481 end
482 end
483 end
484 end
485 end
486
487 def test_rebuild_depth_cache
488 setup_test_nodes TestNode, 3, 3
ecea76f @stefankroes Version 1.2.0
authored
489 TestNode.connection.execute("update test_nodes set depth_cache = null;")
df705c3 @stefankroes Version 1.1.0 done!
authored
490
491 # Assert cache was emptied correctly
492 TestNode.all.each do |test_node|
493 assert_equal nil, test_node.depth_cache
494 end
495
496 # Rebuild cache
497 TestNode.rebuild_depth_cache!
498
499 # Assert cache was rebuild correctly
500 TestNode.all.each do |test_node|
501 assert_equal test_node.depth, test_node.depth_cache
502 end
503 end
504
505 def test_exception_when_rebuilding_depth_cache_for_model_without_depth_caching
506 assert_raise Ancestry::AncestryException do
507 AlternativeTestNode.rebuild_depth_cache!
508 end
509 end
510
511 def test_descendants_with_depth_constraints
512 setup_test_nodes TestNode, 4, 4
513
514 assert_equal 4, TestNode.roots.first.descendants(:before_depth => 2).count
515 assert_equal 20, TestNode.roots.first.descendants(:to_depth => 2).count
516 assert_equal 16, TestNode.roots.first.descendants(:at_depth => 2).count
517 assert_equal 80, TestNode.roots.first.descendants(:from_depth => 2).count
518 assert_equal 64, TestNode.roots.first.descendants(:after_depth => 2).count
519 end
520
521 def test_subtree_with_depth_constraints
522 setup_test_nodes TestNode, 4, 4
523
524 assert_equal 5, TestNode.roots.first.subtree(:before_depth => 2).count
525 assert_equal 21, TestNode.roots.first.subtree(:to_depth => 2).count
526 assert_equal 16, TestNode.roots.first.subtree(:at_depth => 2).count
527 assert_equal 80, TestNode.roots.first.subtree(:from_depth => 2).count
528 assert_equal 64, TestNode.roots.first.subtree(:after_depth => 2).count
529 end
530
531
532 def test_ancestors_with_depth_constraints
533 node1 = TestNode.create!
534 node2 = node1.children.create!
535 node3 = node2.children.create!
536 node4 = node3.children.create!
537 node5 = node4.children.create!
538 leaf = node5.children.create!
539
540 assert_equal [node1, node2, node3], leaf.ancestors(:before_depth => -2)
541 assert_equal [node1, node2, node3, node4], leaf.ancestors(:to_depth => -2)
542 assert_equal [node4], leaf.ancestors(:at_depth => -2)
543 assert_equal [node4, node5], leaf.ancestors(:from_depth => -2)
544 assert_equal [node5], leaf.ancestors(:after_depth => -2)
545 end
546
547 def test_path_with_depth_constraints
548 node1 = TestNode.create!
549 node2 = node1.children.create!
550 node3 = node2.children.create!
551 node4 = node3.children.create!
552 node5 = node4.children.create!
553 leaf = node5.children.create!
554
555 assert_equal [node1, node2, node3], leaf.path(:before_depth => -2)
556 assert_equal [node1, node2, node3, node4], leaf.path(:to_depth => -2)
557 assert_equal [node4], leaf.path(:at_depth => -2)
558 assert_equal [node4, node5, leaf], leaf.path(:from_depth => -2)
559 assert_equal [node5, leaf], leaf.path(:after_depth => -2)
560 end
561
562 def test_exception_on_unknown_depth_column
563 assert_raise Ancestry::AncestryException do
564 TestNode.create!.subtree(:this_is_not_a_valid_depth_option => 42)
565 end
566 end
e3ea26a @stefankroes - Version 1.1.2 (2009-10-29)
authored
567
568 def test_sti_support
569 node1 = TestNodeSub1.create!
570 node2 = TestNodeSub2.create! :parent => node1
571 node3 = TestNodeSub1.create! :parent => node2
572 node4 = TestNodeSub2.create! :parent => node3
573 node5 = TestNodeSub1.create! :parent => node4
574
575 assert_equal [node2, node3, node4, node5], node1.descendants
576 assert_equal [node1, node2, node3, node4, node5], node1.subtree
577 assert_equal [node1, node2, node3, node4], node5.ancestors
578 assert_equal [node1, node2, node3, node4, node5], node5.path
579 end
ecea76f @stefankroes Version 1.2.0
authored
580
581 def test_arrange_order_option
582 # In Ruby versions before 1.9 hashes aren't ordered so this doesn't make sense
583 unless RUBY_VERSION =~ /^1\.8/
584 roots = setup_test_nodes TestNode, 3, 3
585 descending_nodes_lvl0 = TestNode.arrange :order => 'id desc'
586 ascending_nodes_lvl0 = TestNode.arrange :order => 'id asc'
587
588 descending_nodes_lvl0.keys.zip(ascending_nodes_lvl0.keys.reverse).each do |descending_node, ascending_node|
589 assert_equal descending_node, ascending_node
590 descending_nodes_lvl1 = descending_nodes_lvl0[descending_node]
591 ascending_nodes_lvl1 = ascending_nodes_lvl0[ascending_node]
592 descending_nodes_lvl1.keys.zip(ascending_nodes_lvl1.keys.reverse).each do |descending_node, ascending_node|
593 assert_equal descending_node, ascending_node
594 descending_nodes_lvl2 = descending_nodes_lvl1[descending_node]
595 ascending_nodes_lvl2 = ascending_nodes_lvl1[ascending_node]
596 descending_nodes_lvl2.keys.zip(ascending_nodes_lvl2.keys.reverse).each do |descending_node, ascending_node|
597 assert_equal descending_node, ascending_node
598 descending_nodes_lvl3 = descending_nodes_lvl2[descending_node]
599 ascending_nodes_lvl3 = ascending_nodes_lvl2[ascending_node]
600 descending_nodes_lvl3.keys.zip(ascending_nodes_lvl3.keys.reverse).each do |descending_node, ascending_node|
601 assert_equal descending_node, ascending_node
602 end
603 end
604 end
605 end
606 end
607 end
1282d2e @stefankroes Initial commit
authored
608 end
Something went wrong with that request. Please try again.