Fixes and tests for next/previous _sibling.
[dbsrgits/DBIx-Class.git] / t / run / 26positioned.tl
1 # vim: filetype=perl
2
3 sub run_tests {
4
5     plan tests => 56;
6     my $schema = shift;
7
8     my $employees = $schema->resultset('Employee::Positioned');
9     $employees->delete();
10
11     foreach (1..5) {
12         $employees->create({ name=>'temp' });
13     }
14     $employees = $employees->search(undef,{order_by=>'position'});
15     ok( check_positions($employees), "$class: intial positions" );
16
17     my $employee;
18
19     foreach my $position (1..$employees->count()) {
20
21         $employee = $employees->find({ position=>$position });
22         $employee->move_previous();
23         ok( check_positions($employees), "$class: move_previous( $position )" );
24
25         $employee = $employees->find({ position=>$position });
26         $employee->move_next();
27         ok( check_positions($employees), "$class: move_next( $position )" );
28
29         $employee = $employees->find({ position=>$position });
30         $employee->move_first();
31         ok( check_positions($employees), "$class: move_first( $position )" );
32
33         $employee = $employees->find({ position=>$position });
34         $employee->move_last();
35         ok( check_positions($employees), "$class: move_last( $position )" );
36
37         foreach my $to_position (1..$employees->count()) {
38             $employee = $employees->find({ position=>$position });
39             $employee->move_to($to_position);
40             ok( check_positions($employees), "$class: move_to( $position => $to_position )" );
41         }
42
43         $employee = $employees->find({ position=>$position });
44         if ($position==1) {
45             ok( !$employee->previous_sibling(), 'no previous sibling' );
46             ok( $employee->next_sibling(), 'next sibling' );
47         }
48         elsif ($position==$employees->count()) {
49             ok( $employee->previous_sibling(), 'previous sibling' );
50             ok( !$employee->next_sibling(), 'no next sibling' );
51         }
52         else {
53             ok( $employee->previous_sibling(), 'previous sibling' );
54             ok( $employee->next_sibling(), 'next sibling' );
55         }
56
57     }
58 }
59
60 sub check_positions {
61     my( $employees ) = @_;
62     $employees->reset();
63     my $expected_position = 0;
64     while (my $employee = $employees->next()) {
65         $expected_position ++;
66         if ($employee->position()!=$expected_position) {
67             return 0;
68         }
69     }
70     return 1;
71 }
72
73 1;