small docpatch (nebulous)
[dbsrgits/DBIx-Class-Tree.git] / t / lib / TreeTest.pm
CommitLineData
82958127 1package TreeTest;
2use strict;
3use warnings;
4
5use Test::More;
0179986e 6use Test::Exception;
82958127 7use TreeTest::Schema;
8
9our $NODE_COUNT = 80;
10
11sub count_tests {
3c94ae56 12 my $count = 17;
c11aa533 13 if( TreeTest::Schema::Node->can('position_column') ){
14 $count ++;
15 }
16 return $count;
82958127 17}
18
19sub run_tests {
20 my $schema = TreeTest::Schema->connect();
21 my $nodes = $schema->resultset('Node');
22 my $root = $nodes->create({ name=>'root' });
23 my @parents = (
24 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
25 );
26
27 foreach my $parent_id (@parents) {
28 my $node = $nodes->create({ name=>'child' });
29 $node->parent( $parent_id );
30 }
0179986e 31
82958127 32 ok( ($nodes->count()==81), 'correct number of nodes in random tree' );
33 ok( ($nodes->find(3)->children->count()==7), 'node 3 has correct number of children' );
34 ok( ($nodes->find(22)->children->count()==3), 'node 22 has correct number of children' );
35
36 my $child = ($nodes->find(22)->children->all())[0];
37 $child->parent( $nodes->find(3) );
38 ok( ($nodes->find(3)->children->count()==8), 'node 3 has correct number of children' );
39 ok( ($nodes->find(3)->siblings->count()==3), 'node 3 has correct number of siblings' );
40 ok( ($nodes->find(22)->children->count()==2), 'node 22 has correct number of children' );
41 ok( ($nodes->find(22)->siblings->count()==3), 'node 22 has correct number of siblings' );
42
eccca921 43 $nodes->find(22)->attach_child( $nodes->find(3) );
82958127 44 ok( ($nodes->find(22)->children->count()==3), 'node 22 has correct number of children' );
45 ok( ($nodes->find(22)->siblings->count()==2), 'node 22 has correct number of siblings' );
46
eccca921 47 $nodes->find(22)->attach_sibling( $nodes->find(3) );
82958127 48 ok( ($nodes->find(22)->children->count()==2), 'node 22 has correct number of children' );
49 ok( ($nodes->find(22)->siblings->count()==3), 'node 22 has correct number of siblings' );
c11aa533 50
bb17efa0 51 ok( ($nodes->find(22)->parents->count()==1), 'node 22 has correct number of parents' );
52 ok( (($nodes->find(22)->parents->all())[0]->id()==$nodes->find(22)->parent->id()), 'node 22 parent matches parents' );
53
3c94ae56 54 my @ancestors = $nodes->find(44)->ancestors();
55 ok( scalar(@ancestors)==8, 'node 44 has correct number of ancestors' );
56 ok( $ancestors[0]->id == $nodes->find(44)->parent_id, 'node 44\'s first ancestor is its parent' );
57 ok( $ancestors[-1]->name eq 'root', 'node 44\'s last ancestor is root' );
58
c11aa533 59 if( TreeTest::Schema::Node->can('position_column') ){
60 ok( check_positions(scalar $root->children()), 'positions are correct' );
61 }
1dfd20a4 62
63 lives_and ( sub {
64 is( $nodes->find(3)->copy({name => 'special'})->name,'special','copy test');
65 }, 'copy does not throw');
c11aa533 66}
67
68sub check_positions {
69 my $nodes = shift;
70 my $position = 0;
71 while (my $node = $nodes->next()) {
72 $position ++;
73 return 0 if ($node->position() != $position);
74 return 0 if ( !check_positions(scalar $node->children()) );
75 }
76 return 1;
82958127 77}
78
791;