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 # now that we have transactions we need to work around stupid sqlite
47 my @empl = $group_3->all;
48 while (my $employee = shift @empl) {
49 $employee->discard_changes; # since we are effective shift()ing the $rs while doing this
50 $employee->move_to_group($to_group, $to_pos);
52 $to_group = $to_group==1 ? 2 : 1;
55 foreach my $group_id (1..4) {
56 my $group_employees = $employees->search({group_id=>$group_id});
57 $group_employees->all();
58 ok( check_rs($group_employees), "group positions after move_to_group" );
61 my $employee = $employees->search({group_id=>4})->first;
62 $employee->position(2);
64 ok( check_rs($employees->search_rs({group_id=>4})), "overloaded update 1" );
65 $employee = $employees->search({group_id=>4})->first;
66 $employee->update({position=>3});
67 ok( check_rs($employees->search_rs({group_id=>4})), "overloaded update 2" );
68 $employee = $employees->search({group_id=>4})->first;
69 $employee->group_id(1);
72 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
75 $employee = $employees->search({group_id=>4})->first;
76 $employee->update({group_id=>2});
78 check_rs($employees->search_rs({group_id=>2})) && check_rs($employees->search_rs({group_id=>4})),
81 $employee = $employees->search({group_id=>4})->first;
82 $employee->group_id(1);
83 $employee->position(3);
86 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
89 $employee = $employees->search({group_id=>4})->first;
90 $employee->group_id(2);
91 $employee->position(undef);
94 check_rs($employees->search_rs({group_id=>2})) && check_rs($employees->search_rs({group_id=>4})),
97 $employee = $employees->search({group_id=>4})->first;
98 $employee->update({group_id=>1,position=>undef});
100 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
101 "overloaded update 7"
104 # multicol tests begin here
105 DBICTest::Employee->grouping_column(['group_id_2', 'group_id_3']);
106 $employees->delete();
107 foreach my $group_id_2 (1..4) {
108 foreach my $group_id_3 (1..4) {
110 $employees->create({ name=>'temp', group_id_2=>$group_id_2, group_id_3=>$group_id_3 });
114 $employees = $employees->search(undef,{order_by=>[qw/group_id_2 group_id_3 position/]});
116 foreach my $group_id_2 (1..3) {
117 foreach my $group_id_3 (1..3) {
118 my $group_employees = $employees->search({group_id_2=>$group_id_2, group_id_3=>$group_id_3});
119 $group_employees->all();
120 ok( check_rs($group_employees), "group intial positions" );
121 hammer_rs( $group_employees );
125 # move_to_group, specifying group by hash
126 my $group_4 = $employees->search({group_id_2=>4});
128 my $to_group_2_base = 7;
132 # now that we have transactions we need to work around stupid sqlite
134 my @empl = $group_3->all;
135 while (my $employee = shift @empl) {
136 $employee->move_to_group({group_id_2=>$to_group, group_id_3=>$to_group_2}, $to_pos);
138 $to_group = ($to_group % 3) + 1;
140 $to_group_2 = (ceil($to_group_2_base/3.0) %3) +1
143 foreach my $group_id_2 (1..4) {
144 foreach my $group_id_3 (1..4) {
145 my $group_employees = $employees->search({group_id_2=>$group_id_2,group_id_3=>$group_id_3});
146 $group_employees->all();
147 ok( check_rs($group_employees), "group positions after move_to_group" );
151 $employees->delete();
152 foreach my $group_id_2 (1..4) {
153 foreach my $group_id_3 (1..4) {
155 $employees->create({ name=>'temp', group_id_2=>$group_id_2, group_id_3=>$group_id_3 });
159 $employees = $employees->search(undef,{order_by=>[qw/group_id_2 group_id_3 position/]});
161 $employee = $employees->search({group_id_2=>4, group_id_3=>1})->first;
162 $employee->group_id_2(1);
165 check_rs($employees->search_rs({group_id_2=>4, group_id_3=>1}))
166 && check_rs($employees->search_rs({group_id_2=>1, group_id_3=>1})),
167 "overloaded multicol update 1"
170 $employee = $employees->search({group_id_2=>4, group_id_3=>1})->first;
171 $employee->update({group_id_2=>2});
172 ok( check_rs($employees->search_rs({group_id_2=>4, group_id_3=>1}))
173 && check_rs($employees->search_rs({group_id_2=>2, group_id_3=>1})),
174 "overloaded multicol update 2"
177 $employee = $employees->search({group_id_2=>3, group_id_3=>1})->first;
178 $employee->group_id_2(1);
179 $employee->group_id_3(3);
181 ok( check_rs($employees->search_rs({group_id_2=>3, group_id_3=>1}))
182 && check_rs($employees->search_rs({group_id_2=>1, group_id_3=>3})),
183 "overloaded multicol update 3"
186 $employee = $employees->search({group_id_2=>3, group_id_3=>1})->first;
187 $employee->update({group_id_2=>2, group_id_3=>3});
188 ok( check_rs($employees->search_rs({group_id_2=>3, group_id_3=>1}))
189 && check_rs($employees->search_rs({group_id_2=>2, group_id_3=>3})),
190 "overloaded multicol update 4"
193 $employee = $employees->search({group_id_2=>3, group_id_3=>2})->first;
194 $employee->update({group_id_2=>2, group_id_3=>4, position=>2});
195 ok( check_rs($employees->search_rs({group_id_2=>3, group_id_3=>2}))
196 && check_rs($employees->search_rs({group_id_2=>2, group_id_3=>4})),
197 "overloaded multicol update 5"
203 my $count = $rs->count();
204 my $position_column = $rs->result_class->position_column();
207 foreach my $position (1..$count) {
209 ($row) = $rs->search({ $position_column=>$position })->all();
210 $row->move_previous();
211 ok( check_rs($rs), "move_previous( $position )" );
213 ($row) = $rs->search({ $position_column=>$position })->all();
215 ok( check_rs($rs), "move_next( $position )" );
217 ($row) = $rs->search({ $position_column=>$position })->all();
219 ok( check_rs($rs), "move_first( $position )" );
221 ($row) = $rs->search({ $position_column=>$position })->all();
223 ok( check_rs($rs), "move_last( $position )" );
225 foreach my $to_position (1..$count) {
226 ($row) = $rs->search({ $position_column=>$position })->all();
227 $row->move_to($to_position);
228 ok( check_rs($rs), "move_to( $position => $to_position )" );
231 $row = $rs->find({ position => $position });
233 ok( !$row->previous_sibling(), 'no previous sibling' );
234 ok( !$row->first_sibling(), 'no first sibling' );
235 ok( $row->next_sibling->position > $position, 'next sibling position > than us');
236 is( $row->next_sibling->previous_sibling->position, $position, 'next-prev sibling is us');
237 ok( $row->last_sibling->position > $position, 'last sibling position > than us');
240 ok( $row->previous_sibling(), 'previous sibling' );
241 ok( $row->first_sibling(), 'first sibling' );
242 ok( $row->previous_sibling->position < $position, 'prev sibling position < than us');
243 is( $row->previous_sibling->next_sibling->position, $position, 'prev-next sibling is us');
244 ok( $row->first_sibling->position < $position, 'first sibling position < than us');
246 if ($position==$count) {
247 ok( !$row->next_sibling(), 'no next sibling' );
248 ok( !$row->last_sibling(), 'no last sibling' );
249 ok( $row->previous_sibling->position < $position, 'prev sibling position < than us');
250 is( $row->previous_sibling->next_sibling->position, $position, 'prev-next sibling is us');
251 ok( $row->first_sibling->position < $position, 'first sibling position < than us');
254 ok( $row->next_sibling(), 'next sibling' );
255 ok( $row->last_sibling(), 'last sibling' );
256 ok( $row->next_sibling->position > $row->position, 'next sibling position > than us');
257 is( $row->next_sibling->previous_sibling->position, $position, 'next-prev sibling is us');
258 ok( $row->last_sibling->position > $row->position, 'last sibling position > than us');
267 my $position_column = $rs->result_class->position_column();
268 my $expected_position = 0;
269 while (my $row = $rs->next()) {
270 $expected_position ++;
271 if ($row->get_column($position_column)!=$expected_position) {