Commit | Line | Data |
93cec8c3 |
1 | # vim: filetype=perl |
2 | |
3 | sub run_tests { |
4 | |
169bb185 |
5 | plan tests => 321; |
93cec8c3 |
6 | my $schema = shift; |
93cec8c3 |
7 | |
02f1c2b6 |
8 | my $employees = $schema->resultset('Employee'); |
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'}); |
169bb185 |
15 | ok( check_rs($employees), "intial positions" ); |
80010e2b |
16 | |
169bb185 |
17 | hammer_rs( $employees ); |
18 | |
a733c37f |
19 | DBICTest::Employee->grouping_column('group_id'); |
169bb185 |
20 | $employees->delete(); |
21 | foreach my $group_id (1..3) { |
22 | foreach (1..6) { |
23 | $employees->create({ name=>'temp', group_id=>$group_id }); |
24 | } |
25 | } |
26 | $employees = $employees->search(undef,{order_by=>'group_id,position'}); |
27 | |
28 | foreach my $group_id (1..3) { |
29 | my $group_employees = $employees->search({group_id=>$group_id}); |
30 | $group_employees->all(); |
31 | ok( check_rs($group_employees), "group intial positions" ); |
32 | hammer_rs( $group_employees ); |
33 | } |
34 | |
35 | } |
36 | |
37 | sub hammer_rs { |
38 | my $rs = shift; |
707cbb2d |
39 | my $employee; |
169bb185 |
40 | my $count = $rs->count(); |
41 | my $position_column = $rs->result_class->position_column(); |
80010e2b |
42 | |
169bb185 |
43 | foreach my $position (1..$count) { |
80010e2b |
44 | |
169bb185 |
45 | $row = $rs->find({ $position_column=>$position }); |
46 | $row->move_previous(); |
47 | ok( check_rs($rs), "move_previous( $position )" ); |
80010e2b |
48 | |
169bb185 |
49 | $row = $rs->find({ $position_column=>$position }); |
50 | $row->move_next(); |
51 | ok( check_rs($rs), "move_next( $position )" ); |
80010e2b |
52 | |
169bb185 |
53 | $row = $rs->find({ $position_column=>$position }); |
54 | $row->move_first(); |
55 | ok( check_rs($rs), "move_first( $position )" ); |
80010e2b |
56 | |
169bb185 |
57 | $row = $rs->find({ $position_column=>$position }); |
58 | $row->move_last(); |
59 | ok( check_rs($rs), "move_last( $position )" ); |
80010e2b |
60 | |
169bb185 |
61 | foreach my $to_position (1..$count) { |
62 | $row = $rs->find({ $position_column=>$position }); |
63 | $row->move_to($to_position); |
64 | ok( check_rs($rs), "move_to( $position => $to_position )" ); |
707cbb2d |
65 | } |
80021def |
66 | |
169bb185 |
67 | $row = $rs->find({ position=>$position }); |
707cbb2d |
68 | if ($position==1) { |
169bb185 |
69 | ok( !$row->previous_sibling(), 'no previous sibling' ); |
70 | ok( !$row->first_sibling(), 'no first sibling' ); |
71 | } |
72 | else { |
73 | ok( $row->previous_sibling(), 'previous sibling' ); |
74 | ok( $row->first_sibling(), 'first sibling' ); |
707cbb2d |
75 | } |
169bb185 |
76 | if ($position==$count) { |
77 | ok( !$row->next_sibling(), 'no next sibling' ); |
78 | ok( !$row->last_sibling(), 'no last sibling' ); |
80021def |
79 | } |
707cbb2d |
80 | else { |
169bb185 |
81 | ok( $row->next_sibling(), 'next sibling' ); |
82 | ok( $row->last_sibling(), 'last sibling' ); |
707cbb2d |
83 | } |
84 | |
80010e2b |
85 | } |
80010e2b |
86 | } |
87 | |
169bb185 |
88 | sub check_rs { |
89 | my( $rs ) = @_; |
90 | $rs->reset(); |
91 | my $position_column = $rs->result_class->position_column(); |
93cec8c3 |
92 | my $expected_position = 0; |
169bb185 |
93 | while (my $row = $rs->next()) { |
93cec8c3 |
94 | $expected_position ++; |
169bb185 |
95 | if ($row->get_column($position_column)!=$expected_position) { |
133dd22a |
96 | return 0; |
80021def |
97 | } |
93cec8c3 |
98 | } |
133dd22a |
99 | return 1; |
93cec8c3 |
100 | } |
101 | |
102 | 1; |