Merge 'trunk' into 'DBIx-Class-current'
[dbsrgits/DBIx-Class.git] / t / run / 26positioned.tl
CommitLineData
93cec8c3 1# vim: filetype=perl
2
3sub run_tests {
4
707cbb2d 5 plan tests => 56;
93cec8c3 6 my $schema = shift;
93cec8c3 7
133dd22a 8 my $employees = $schema->resultset('Employee::Positioned');
707cbb2d 9 $employees->delete();
93cec8c3 10
707cbb2d 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" );
80010e2b 16
707cbb2d 17 my $employee;
80010e2b 18
707cbb2d 19 foreach my $position (1..$employees->count()) {
80010e2b 20
707cbb2d 21 $employee = $employees->find({ position=>$position });
22 $employee->move_previous();
23 ok( check_positions($employees), "$class: move_previous( $position )" );
80010e2b 24
707cbb2d 25 $employee = $employees->find({ position=>$position });
26 $employee->move_next();
27 ok( check_positions($employees), "$class: move_next( $position )" );
80010e2b 28
707cbb2d 29 $employee = $employees->find({ position=>$position });
30 $employee->move_first();
31 ok( check_positions($employees), "$class: move_first( $position )" );
80010e2b 32
707cbb2d 33 $employee = $employees->find({ position=>$position });
34 $employee->move_last();
35 ok( check_positions($employees), "$class: move_last( $position )" );
80010e2b 36
707cbb2d 37 foreach my $to_position (1..$employees->count()) {
133dd22a 38 $employee = $employees->find({ position=>$position });
707cbb2d 39 $employee->move_to($to_position);
40 ok( check_positions($employees), "$class: move_to( $position => $to_position )" );
41 }
80021def 42
707cbb2d 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' );
80021def 51 }
707cbb2d 52 else {
53 ok( $employee->previous_sibling(), 'previous sibling' );
54 ok( $employee->next_sibling(), 'next sibling' );
55 }
56
80010e2b 57 }
80010e2b 58}
59
60sub check_positions {
80021def 61 my( $employees ) = @_;
62 $employees->reset();
93cec8c3 63 my $expected_position = 0;
80021def 64 while (my $employee = $employees->next()) {
93cec8c3 65 $expected_position ++;
80021def 66 if ($employee->position()!=$expected_position) {
133dd22a 67 return 0;
80021def 68 }
93cec8c3 69 }
133dd22a 70 return 1;
93cec8c3 71}
72
731;