a3054cbd522eb73d750ad8cdee463564f282ad7c
[dbsrgits/DBIx-Class-Tree.git] / t / lib / TreeTest.pm
1 package TreeTest;
2 use strict;
3 use warnings;
4
5 use Test::More;
6 use TreeTest::Schema;
7
8 our $NODE_COUNT = 80;
9
10 sub count_tests {
11     my $count = 22;
12     if( TreeTest::Schema::Node->can('position_column') ){
13         $count ++;
14     }
15     return $count;
16 }
17
18 sub run_tests {
19     my $schema = TreeTest::Schema->connect();
20     my $nodes = $schema->resultset('Node');
21     my $root = $nodes->create({ name=>'root' });
22     my @parents = (
23         1,1,3,4,4,3,3,8,8,10,10,8,8,3,3,16,3,3,1,20,1,22,22,24,24,22,27,27,29,29,27,32,32,34,34,36,34,38,38,40,40,42,42,44,44,46,44,44,49,44,51,51,53,51,55,55,57,57,55,60,55,62,55,64,64,55,67,67,55,70,70,55,55,51,51,76,76,78,78,76
24     );
25
26     foreach my $parent_id (@parents) {
27         my $node = $nodes->create({ name=>'child' });
28         $node->parent( $parent_id );
29     }
30     ok( ($nodes->count()==81), 'correct number of nodes in random tree' );
31     ok( ($nodes->find(3)->children->count()==7), 'node 3 has correct number of children' );
32     ok( ($nodes->find(22)->children->count()==3), 'node 22 has correct number of children' );
33
34     my $child = ($nodes->find(22)->children->all())[0];
35     $child->parent( $nodes->find(3) );
36     ok( ($nodes->find(3)->children->count()==8), 'node 3 has correct number of children' );
37     ok( ($nodes->find(3)->siblings->count()==3), 'node 3 has correct number of siblings' );
38     ok( ($nodes->find(22)->children->count()==2), 'node 22 has correct number of children' );
39     ok( ($nodes->find(22)->siblings->count()==3), 'node 22 has correct number of siblings' );
40
41     $nodes->find(22)->add_child( $nodes->find(3) );
42     ok( ($nodes->find(22)->children->count()==3), 'node 22 has correct number of children' );
43     ok( ($nodes->find(22)->siblings->count()==2), 'node 22 has correct number of siblings' );
44
45     $nodes->find(22)->add_sibling( $nodes->find(3) );
46     ok( ($nodes->find(22)->children->count()==2), 'node 22 has correct number of children' );
47     ok( ($nodes->find(22)->siblings->count()==3), 'node 22 has correct number of siblings' );
48
49     ok( ($nodes->find(22)->parents->count()==1), 'node 22 has correct number of parents' );
50     ok( (($nodes->find(22)->parents->all())[0]->id()==$nodes->find(22)->parent->id()), 'node 22 parent matches parents' );
51
52     if( TreeTest::Schema::Node->can('position_column') ){
53         ok( check_positions(scalar $root->children()), 'positions are correct' );
54     }
55
56     #$nodes->delete;
57     $schema = TreeTest::Schema->connect();
58     $nodes = $schema->resultset('Node');
59     $root = $nodes->create({ name=>'root' });
60     @parents = (
61         1,1,3,2,3,3,6,6,2,5,3,5,12,1
62     );
63     foreach my $parent_id (@parents) {
64         my $node = $nodes->create({ name=>'child' });
65         $node->parent( $parent_id );
66     }
67
68     ok ($nodes->find(1)->descendents == 14, 'root node has 14 descendents');
69     ok ($nodes->find(2)->descendents == 4, 'node 2 has 4 descendents');
70     ok ($nodes->find(6)->descendents == 2, 'node 6 has 2 descendents');
71     ok ($nodes->find(10)->descendents == 0, 'node 10 has no descendents');
72     
73     $nodes->find(2)->pharaoh_delete;
74     ok ($nodes->find(1)->descendents == 9, 'root node has 9 descendents after pharaohing node 2');
75
76     ok ($nodes->find(3)->children == 4, 'node 3 has 4 children');
77     $nodes->find(6)->grandmother_delete;
78     ok ($nodes->find(3)->children == 5, 'node 3 has 5 children after node 6 dies');
79
80     $nodes->find(3)->replace_with_and_delete($nodes->find(12));
81     ok ($nodes->find(12)->children == 5, 'node 12 has 5 children after taking over from node 3');
82     ok ($nodes->find(7)->parent->id == 12, 'node 7\'s parent is node 12');
83     
84     
85     
86
87 }
88
89 sub check_positions {
90     my $nodes = shift;
91     my $position = 0;
92     while (my $node = $nodes->next()) {
93         $position ++;
94         return 0 if ($node->position() != $position);
95         return 0 if ( !check_positions(scalar $node->children()) );
96     }
97     return 1;
98 }
99
100 1;