Tests for adjacency list as well as a bunch of fixes.
[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     return 11;
12 }
13
14 sub run_tests {
15     my $schema = TreeTest::Schema->connect();
16     my $nodes = $schema->resultset('Node');
17     my $root = $nodes->create({ name=>'root' });
18     my @parents = (
19         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
20     );
21
22     foreach my $parent_id (@parents) {
23         my $node = $nodes->create({ name=>'child' });
24         $node->parent( $parent_id );
25     }
26     ok( ($nodes->count()==81), 'correct number of nodes in random tree' );
27     ok( ($nodes->find(3)->children->count()==7), 'node 3 has correct number of children' );
28     ok( ($nodes->find(22)->children->count()==3), 'node 22 has correct number of children' );
29
30     my $child = ($nodes->find(22)->children->all())[0];
31     $child->parent( $nodes->find(3) );
32     ok( ($nodes->find(3)->children->count()==8), 'node 3 has correct number of children' );
33     ok( ($nodes->find(3)->siblings->count()==3), 'node 3 has correct number of siblings' );
34     ok( ($nodes->find(22)->children->count()==2), 'node 22 has correct number of children' );
35     ok( ($nodes->find(22)->siblings->count()==3), 'node 22 has correct number of siblings' );
36
37     $nodes->find(22)->attach_child( $nodes->find(3) );
38     ok( ($nodes->find(22)->children->count()==3), 'node 22 has correct number of children' );
39     ok( ($nodes->find(22)->siblings->count()==2), 'node 22 has correct number of siblings' );
40
41     $nodes->find(22)->attach_sibling( $nodes->find(3) );
42     ok( ($nodes->find(22)->children->count()==2), 'node 22 has correct number of children' );
43     ok( ($nodes->find(22)->siblings->count()==3), 'node 22 has correct number of siblings' );
44 }
45
46 1;