11 my $schema = DBICTest->init_schema();
15 my $employees = $schema->resultset('Employee');
19 $employees->create({ name=>'temp' });
21 $employees = $employees->search(undef,{order_by=>'position'});
22 ok( check_rs($employees), "intial positions" );
24 hammer_rs( $employees );
26 DBICTest::Employee->grouping_column('group_id');
28 foreach my $group_id (1..4) {
30 $employees->create({ name=>'temp', group_id=>$group_id });
33 $employees = $employees->search(undef,{order_by=>'group_id,position'});
35 foreach my $group_id (1..4) {
36 my $group_employees = $employees->search({group_id=>$group_id});
37 $group_employees->all();
38 ok( check_rs($group_employees), "group intial positions" );
39 hammer_rs( $group_employees );
42 my $group_3 = $employees->search({group_id=>3});
45 while (my $employee = $group_3->next) {
46 $employee->discard_changes; # since we are effective shift()ing the $rs
47 $employee->move_to_group($to_group, $to_pos);
49 $to_group = $to_group==1 ? 2 : 1;
51 foreach my $group_id (1..4) {
52 my $group_employees = $employees->search({group_id=>$group_id});
53 $group_employees->all();
54 ok( check_rs($group_employees), "group positions after move_to_group" );
57 my $employee = $employees->search({group_id=>4})->first;
58 $employee->position(2);
60 ok( check_rs($employees->search_rs({group_id=>4})), "overloaded update 1" );
61 $employee = $employees->search({group_id=>4})->first;
62 $employee->update({position=>3});
63 ok( check_rs($employees->search_rs({group_id=>4})), "overloaded update 2" );
64 $employee = $employees->search({group_id=>4})->first;
65 $employee->group_id(1);
68 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
71 $employee = $employees->search({group_id=>4})->first;
72 $employee->update({group_id=>2});
74 check_rs($employees->search_rs({group_id=>2})) && check_rs($employees->search_rs({group_id=>4})),
77 $employee = $employees->search({group_id=>4})->first;
78 $employee->group_id(1);
79 $employee->position(3);
82 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
85 $employee = $employees->search({group_id=>4})->first;
86 $employee->group_id(2);
87 $employee->position(undef);
90 check_rs($employees->search_rs({group_id=>2})) && check_rs($employees->search_rs({group_id=>4})),
93 $employee = $employees->search({group_id=>4})->first;
94 $employee->update({group_id=>1,position=>undef});
96 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
100 # multicol tests begin here
101 DBICTest::Employee->grouping_column(['group_id_2', 'group_id_3']);
102 $employees->delete();
103 foreach my $group_id_2 (1..4) {
104 foreach my $group_id_3 (1..4) {
106 $employees->create({ name=>'temp', group_id_2=>$group_id_2, group_id_3=>$group_id_3 });
110 $employees = $employees->search(undef,{order_by=>[qw/group_id_2 group_id_3 position/]});
112 foreach my $group_id_2 (1..3) {
113 foreach my $group_id_3 (1..3) {
114 my $group_employees = $employees->search({group_id_2=>$group_id_2, group_id_3=>$group_id_3});
115 $group_employees->all();
116 ok( check_rs($group_employees), "group intial positions" );
117 hammer_rs( $group_employees );
121 # move_to_group, specifying group by hash
122 my $group_4 = $employees->search({group_id_2=>4});
124 my $to_group_2_base = 7;
127 while (my $employee = $group_4->next) {
128 $employee->move_to_group({group_id_2=>$to_group, group_id_3=>$to_group_2}, $to_pos);
130 $to_group = ($to_group % 3) + 1;
132 $to_group_2 = (ceil($to_group_2_base/3.0) %3) +1
134 foreach my $group_id_2 (1..4) {
135 foreach my $group_id_3 (1..4) {
136 my $group_employees = $employees->search({group_id_2=>$group_id_2,group_id_3=>$group_id_3});
137 $group_employees->all();
138 ok( check_rs($group_employees), "group positions after move_to_group" );
142 $employees->delete();
143 foreach my $group_id_2 (1..4) {
144 foreach my $group_id_3 (1..4) {
146 $employees->create({ name=>'temp', group_id_2=>$group_id_2, group_id_3=>$group_id_3 });
150 $employees = $employees->search(undef,{order_by=>[qw/group_id_2 group_id_3 position/]});
152 $employee = $employees->search({group_id_2=>4, group_id_3=>1})->first;
153 $employee->group_id_2(1);
156 check_rs($employees->search_rs({group_id_2=>4, group_id_3=>1}))
157 && check_rs($employees->search_rs({group_id_2=>1, group_id_3=>1})),
158 "overloaded multicol update 1"
161 $employee = $employees->search({group_id_2=>4, group_id_3=>1})->first;
162 $employee->update({group_id_2=>2});
163 ok( check_rs($employees->search_rs({group_id_2=>4, group_id_3=>1}))
164 && check_rs($employees->search_rs({group_id_2=>2, group_id_3=>1})),
165 "overloaded multicol update 2"
168 $employee = $employees->search({group_id_2=>3, group_id_3=>1})->first;
169 $employee->group_id_2(1);
170 $employee->group_id_3(3);
172 ok( check_rs($employees->search_rs({group_id_2=>3, group_id_3=>1}))
173 && check_rs($employees->search_rs({group_id_2=>1, group_id_3=>3})),
174 "overloaded multicol update 3"
177 $employee = $employees->search({group_id_2=>3, group_id_3=>1})->first;
178 $employee->update({group_id_2=>2, group_id_3=>3});
179 ok( check_rs($employees->search_rs({group_id_2=>3, group_id_3=>1}))
180 && check_rs($employees->search_rs({group_id_2=>2, group_id_3=>3})),
181 "overloaded multicol update 4"
184 $employee = $employees->search({group_id_2=>3, group_id_3=>2})->first;
185 $employee->update({group_id_2=>2, group_id_3=>4, position=>2});
186 ok( check_rs($employees->search_rs({group_id_2=>3, group_id_3=>2}))
187 && check_rs($employees->search_rs({group_id_2=>2, group_id_3=>4})),
188 "overloaded multicol update 5"
194 my $count = $rs->count();
195 my $position_column = $rs->result_class->position_column();
198 foreach my $position (1..$count) {
200 ($row) = $rs->search({ $position_column=>$position })->all();
201 $row->move_previous();
202 ok( check_rs($rs), "move_previous( $position )" );
204 ($row) = $rs->search({ $position_column=>$position })->all();
206 ok( check_rs($rs), "move_next( $position )" );
208 ($row) = $rs->search({ $position_column=>$position })->all();
210 ok( check_rs($rs), "move_first( $position )" );
212 ($row) = $rs->search({ $position_column=>$position })->all();
214 ok( check_rs($rs), "move_last( $position )" );
216 foreach my $to_position (1..$count) {
217 ($row) = $rs->search({ $position_column=>$position })->all();
218 $row->move_to($to_position);
219 ok( check_rs($rs), "move_to( $position => $to_position )" );
222 $row = $rs->find({ position => $position });
224 ok( !$row->previous_sibling(), 'no previous sibling' );
225 ok( !$row->first_sibling(), 'no first sibling' );
226 ok( $row->next_sibling->position > $position, 'next sibling position > than us');
227 is( $row->next_sibling->previous_sibling->position, $position, 'next-prev sibling is us');
228 ok( $row->last_sibling->position > $position, 'last sibling position > than us');
231 ok( $row->previous_sibling(), 'previous sibling' );
232 ok( $row->first_sibling(), 'first sibling' );
233 ok( $row->previous_sibling->position < $position, 'prev sibling position < than us');
234 is( $row->previous_sibling->next_sibling->position, $position, 'prev-next sibling is us');
235 ok( $row->first_sibling->position < $position, 'first sibling position < than us');
237 if ($position==$count) {
238 ok( !$row->next_sibling(), 'no next sibling' );
239 ok( !$row->last_sibling(), 'no last sibling' );
240 ok( $row->previous_sibling->position < $position, 'prev sibling position < than us');
241 is( $row->previous_sibling->next_sibling->position, $position, 'prev-next sibling is us');
242 ok( $row->first_sibling->position < $position, 'first sibling position < than us');
245 ok( $row->next_sibling(), 'next sibling' );
246 ok( $row->last_sibling(), 'last sibling' );
247 ok( $row->next_sibling->position > $row->position, 'next sibling position > than us');
248 is( $row->next_sibling->previous_sibling->position, $position, 'next-prev sibling is us');
249 ok( $row->last_sibling->position > $row->position, 'last sibling position > than us');
258 my $position_column = $rs->result_class->position_column();
259 my $expected_position = 0;
260 while (my $row = $rs->next()) {
261 $expected_position ++;
262 if ($row->get_column($position_column)!=$expected_position) {