add basic cache tests/documentation
[dbsrgits/DBIx-Class.git] / t / run / 26positioned.tl
1 # vim: filetype=perl
2
3 sub run_tests {
4
5     plan tests => 96;
6     my $schema = shift;
7
8     foreach my $class ( 'Positioned', 'PositionedAdjacencyList', 'AdjacencyList' ) {
9
10         my $employees = $schema->resultset('Employee::'.$class);
11
12         if ($employees->result_class->can('position_column')) {
13
14             $employees->delete();
15             foreach (1..5) {
16                 $employees->create({ name=>'temp' });
17             }
18
19             $employees = $employees->search(undef,{order_by=>'position'});
20             ok( check_positions($employees), "$class: intial positions" );
21
22             my $employee;
23
24             foreach my $position (1..$employees->count()) {
25
26                 $employee = $employees->find({ position=>$position });
27                 $employee->move_previous();
28                 ok( check_positions($employees), "$class: move_previous( $position )" );
29
30                 $employee = $employees->find({ position=>$position });
31                 $employee->move_next();
32                 ok( check_positions($employees), "$class: move_next( $position )" );
33
34                 $employee = $employees->find({ position=>$position });
35                 $employee->move_first();
36                 ok( check_positions($employees), "$class: move_first( $position )" );
37
38                 $employee = $employees->find({ position=>$position });
39                 $employee->move_last();
40                 ok( check_positions($employees), "$class: move_last( $position )" );
41
42                 foreach my $to_position (1..$employees->count()) {
43                     $employee = $employees->find({ position=>$position });
44                     $employee->move_to($to_position);
45                     ok( check_positions($employees), "$class: move_to( $position => $to_position )" );
46                 }
47
48             }
49         }
50         if ($employees->result_class->can('parent_column')) {
51
52             $employees->delete();
53             my $mom = $employees->create({ name=>'temp' });
54             foreach (1..14) {
55                 $employees->create({ name=>'temp', parent_id=>$mom->id() });
56             }
57
58             my $children = $mom->children();
59             ok( ($children->count()==14), 'correct number of children' );
60
61             my $grandma = $mom;
62             my @children = $children->all();
63             $mom = pop(@children);
64             foreach my $child (@children) {
65                 $child->parent( $mom );
66             }
67
68             ok( ($mom->children->count() == 13), 'correct number of grandchildren' );
69
70             if ($employees->result_class->can('position_column')) {
71                 # TODO: Test positioning within a tree.
72             }
73         }
74     }
75
76 }
77
78 sub check_positions {
79     my( $employees ) = @_;
80     $employees->reset();
81     my $expected_position = 0;
82     my $is_ok = 1;
83     while (my $employee = $employees->next()) {
84         $expected_position ++;
85         if ($employee->position()!=$expected_position) {
86             $is_ok = 0;
87             last;
88         }
89     }
90     return $is_ok;
91 }
92
93 1;