Commit | Line | Data |
---|---|---|
93cec8c3 | 1 | # vim: filetype=perl |
2 | ||
3 | sub run_tests { | |
4 | ||
80021def | 5 | plan tests => 96; |
93cec8c3 | 6 | my $schema = shift; |
93cec8c3 | 7 | |
133dd22a | 8 | my $employees = $schema->resultset('Employee::Positioned'); |
93cec8c3 | 9 | |
133dd22a | 10 | if ($employees->result_class->can('position_column')) { |
80010e2b | 11 | |
133dd22a | 12 | $employees->delete(); |
13 | foreach (1..5) { | |
14 | $employees->create({ name=>'temp' }); | |
15 | } | |
80010e2b | 16 | |
133dd22a | 17 | $employees = $employees->search(undef,{order_by=>'position'}); |
18 | ok( check_positions($employees), "$class: intial positions" ); | |
80010e2b | 19 | |
133dd22a | 20 | my $employee; |
80010e2b | 21 | |
133dd22a | 22 | foreach my $position (1..$employees->count()) { |
80010e2b | 23 | |
133dd22a | 24 | $employee = $employees->find({ position=>$position }); |
25 | $employee->move_previous(); | |
26 | ok( check_positions($employees), "$class: move_previous( $position )" ); | |
80010e2b | 27 | |
133dd22a | 28 | $employee = $employees->find({ position=>$position }); |
29 | $employee->move_next(); | |
30 | ok( check_positions($employees), "$class: move_next( $position )" ); | |
80010e2b | 31 | |
133dd22a | 32 | $employee = $employees->find({ position=>$position }); |
33 | $employee->move_first(); | |
34 | ok( check_positions($employees), "$class: move_first( $position )" ); | |
80010e2b | 35 | |
133dd22a | 36 | $employee = $employees->find({ position=>$position }); |
37 | $employee->move_last(); | |
38 | ok( check_positions($employees), "$class: move_last( $position )" ); | |
80010e2b | 39 | |
133dd22a | 40 | foreach my $to_position (1..$employees->count()) { |
80021def | 41 | $employee = $employees->find({ position=>$position }); |
133dd22a | 42 | $employee->move_to($to_position); |
43 | ok( check_positions($employees), "$class: move_to( $position => $to_position )" ); | |
80021def | 44 | } |
45 | ||
80021def | 46 | } |
80010e2b | 47 | } |
80010e2b | 48 | } |
49 | ||
50 | sub check_positions { | |
80021def | 51 | my( $employees ) = @_; |
52 | $employees->reset(); | |
93cec8c3 | 53 | my $expected_position = 0; |
80021def | 54 | while (my $employee = $employees->next()) { |
93cec8c3 | 55 | $expected_position ++; |
80021def | 56 | if ($employee->position()!=$expected_position) { |
133dd22a | 57 | return 0; |
80021def | 58 | } |
93cec8c3 | 59 | } |
133dd22a | 60 | return 1; |
93cec8c3 | 61 | } |
62 | ||
63 | 1; |