Fixes to Tree::AdjacencyList, and working tests.
[dbsrgits/DBIx-Class.git] / t / run / 27adjacency_list.tl
1 # vim: filetype=perl
2
3 sub run_tests {
4
5     plan tests => 5;
6     my $schema = shift;
7
8     my $employees = $schema->resultset('Employee::AdjacencyList');
9     $employees->delete();
10
11     my $grandma = $employees->create({ name=>'grandma', parent_id=>0 });
12     foreach (1..15) {
13         $employees->create({ name=>'temp', parent_id=>$grandma->id() });
14     }
15     ok( ($grandma->children->count()==15), 'grandma children' );
16
17     my $mom = ($grandma->children->search(undef,{rows=>1})->all())[0];
18     foreach (1..5) {
19         ($grandma->children->search(undef,{rows=>1})->all())[0]->parent( $mom );
20     }
21     ok( ($mom->children->count()==5), 'mom children' );
22     ok( ($grandma->children->count()==10), 'grandma children' );
23
24     $mom = ($grandma->children->search(undef,{rows=>2})->all())[0];
25     foreach (1..4) {
26         ($grandma->children->search(undef,{rows=>1})->all())[0]->parent( $mom );
27     }
28     ok( ($mom->children->count()==4), 'mom children' );
29     ok( ($grandma->children->count()==6), 'grandma children' );
30
31     ok( check_rs( scalar $grandma->children() ), 'correct positions' );
32 }
33
34 sub check_rs {
35     my( $rs ) = @_;
36     $rs->reset();
37     my $position_column = $rs->result_class->position_column();
38     my $expected_position = 0;
39     while (my $row = $rs->next()) {
40         $expected_position ++;
41         if ($row->get_column($position_column)!=$expected_position) {
42             return 0;
43         }
44         my $children = $row->children();
45         while (my $child = $children->next()) {
46             return 0 if (!check_rs( scalar $child->children() ));
47         }
48     }
49     return 1;
50 }
51
52 1;