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 | |
80021def |
8 | foreach my $class ( 'Positioned', 'PositionedAdjacencyList', 'AdjacencyList' ) { |
93cec8c3 |
9 | |
80021def |
10 | my $employees = $schema->resultset('Employee::'.$class); |
80010e2b |
11 | |
80021def |
12 | if ($employees->result_class->can('position_column')) { |
80010e2b |
13 | |
80021def |
14 | $employees->delete(); |
15 | foreach (1..5) { |
16 | $employees->create({ name=>'temp' }); |
17 | } |
80010e2b |
18 | |
80021def |
19 | $employees = $employees->search(undef,{order_by=>'position'}); |
20 | ok( check_positions($employees), "$class: intial positions" ); |
80010e2b |
21 | |
80021def |
22 | my $employee; |
80010e2b |
23 | |
80021def |
24 | foreach my $position (1..$employees->count()) { |
80010e2b |
25 | |
80021def |
26 | $employee = $employees->find({ position=>$position }); |
27 | $employee->move_previous(); |
28 | ok( check_positions($employees), "$class: move_previous( $position )" ); |
80010e2b |
29 | |
80021def |
30 | $employee = $employees->find({ position=>$position }); |
31 | $employee->move_next(); |
32 | ok( check_positions($employees), "$class: move_next( $position )" ); |
80010e2b |
33 | |
80021def |
34 | $employee = $employees->find({ position=>$position }); |
35 | $employee->move_first(); |
36 | ok( check_positions($employees), "$class: move_first( $position )" ); |
80010e2b |
37 | |
80021def |
38 | $employee = $employees->find({ position=>$position }); |
39 | $employee->move_last(); |
40 | ok( check_positions($employees), "$class: move_last( $position )" ); |
80010e2b |
41 | |
80021def |
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 | } |
80010e2b |
47 | |
80021def |
48 | } |
80010e2b |
49 | } |
80021def |
50 | if ($employees->result_class->can('parent_column')) { |
80010e2b |
51 | |
80021def |
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 | } |
80010e2b |
74 | } |
75 | |
76 | } |
77 | |
78 | sub check_positions { |
80021def |
79 | my( $employees ) = @_; |
80 | $employees->reset(); |
93cec8c3 |
81 | my $expected_position = 0; |
80021def |
82 | my $is_ok = 1; |
83 | while (my $employee = $employees->next()) { |
93cec8c3 |
84 | $expected_position ++; |
80021def |
85 | if ($employee->position()!=$expected_position) { |
86 | $is_ok = 0; |
87 | last; |
88 | } |
93cec8c3 |
89 | } |
80021def |
90 | return $is_ok; |
93cec8c3 |
91 | } |
92 | |
93 | 1; |