11 my $schema = DBICTest->init_schema();
13 my $employees = $schema->resultset('Employee');
17 $employees->create({ name=>'temp' });
19 $employees = $employees->search(undef,{order_by=>'position'});
20 ok( check_rs($employees), "intial positions" );
22 hammer_rs( $employees );
24 DBICTest::Employee->grouping_column('group_id');
26 foreach my $group_id (1..4) {
28 $employees->create({ name=>'temp', group_id=>$group_id });
31 $employees = $employees->search(undef,{order_by=>'group_id,position'});
33 foreach my $group_id (1..4) {
34 my $group_employees = $employees->search({group_id=>$group_id});
35 $group_employees->all();
36 ok( check_rs($group_employees), "group intial positions" );
37 hammer_rs( $group_employees );
40 my $group_3 = $employees->search({group_id=>3});
44 my @empl = $group_3->all;
45 while (my $employee = shift @empl) {
46 $employee->move_to_group($to_group, $to_pos);
48 $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 ok( check_rs($group_employees), "group positions after move_to_group" );
56 my $employee = $employees->search({group_id=>4})->first;
57 $employee->position(2);
59 ok( check_rs($employees->search_rs({group_id=>4})), "overloaded update 1" );
60 $employee = $employees->search({group_id=>4})->first;
61 $employee->update({position=>3});
62 ok( check_rs($employees->search_rs({group_id=>4})), "overloaded update 2" );
63 $employee = $employees->search({group_id=>4})->first;
64 $employee->group_id(1);
67 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
70 $employee = $employees->search({group_id=>4})->first;
71 $employee->update({group_id=>2});
73 check_rs($employees->search_rs({group_id=>2})) && check_rs($employees->search_rs({group_id=>4})),
76 $employee = $employees->search({group_id=>4})->first;
77 $employee->group_id(1);
78 $employee->position(3);
81 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
84 $employee = $employees->search({group_id=>4})->first;
85 $employee->group_id(2);
86 $employee->position(undef);
89 check_rs($employees->search_rs({group_id=>2})) && check_rs($employees->search_rs({group_id=>4})),
92 $employee = $employees->search({group_id=>4})->first;
93 $employee->update({group_id=>1,position=>undef});
95 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
99 $employee->group_id(2);
100 $employee->name('E of the month');
101 $employee->update({ employee_id => 666, position => 2 });
103 { $employee->get_columns },
110 name => "E of the month",
113 'combined update() worked correctly'
116 { $employee->get_columns },
117 { $employee->get_from_storage->get_columns },
118 'object matches database state',
122 # multicol tests begin here
125 DBICTest::Employee->grouping_column(['group_id_2', 'group_id_3']);
126 $employees->delete();
127 foreach my $group_id_2 (1..4) {
128 foreach my $group_id_3 (1..4) {
130 $employees->create({ name=>'temp', group_id_2=>$group_id_2, group_id_3=>$group_id_3 });
134 $employees = $employees->search(undef,{order_by=>[qw/group_id_2 group_id_3 position/]});
136 foreach my $group_id_2 (1..3) {
137 foreach my $group_id_3 (1..3) {
138 my $group_employees = $employees->search({group_id_2=>$group_id_2, group_id_3=>$group_id_3});
139 $group_employees->all();
140 ok( check_rs($group_employees), "group intial positions" );
141 hammer_rs( $group_employees );
145 # move_to_group, specifying group by hash
146 my $group_4 = $employees->search({group_id_2=>4});
148 my $to_group_2_base = 7;
153 my @empl = $group_3->all;
154 while (my $employee = shift @empl) {
155 $employee->move_to_group({group_id_2=>$to_group, group_id_3=>$to_group_2}, $to_pos);
157 $to_group = ($to_group % 3) + 1;
159 $to_group_2 = (ceil($to_group_2_base/3.0) %3) +1
162 foreach my $group_id_2 (1..4) {
163 foreach my $group_id_3 (1..4) {
164 my $group_employees = $employees->search({group_id_2=>$group_id_2,group_id_3=>$group_id_3});
165 ok( check_rs($group_employees), "group positions after move_to_group" );
169 $employees->delete();
170 foreach my $group_id_2 (1..4) {
171 foreach my $group_id_3 (1..4) {
173 $employees->create({ name=>'temp', group_id_2=>$group_id_2, group_id_3=>$group_id_3 });
177 $employees = $employees->search(undef,{order_by=>[qw/group_id_2 group_id_3 position/]});
179 $employee = $employees->search({group_id_2=>4, group_id_3=>1})->first;
180 $employee->group_id_2(1);
183 check_rs($employees->search_rs({group_id_2=>4, group_id_3=>1}))
184 && check_rs($employees->search_rs({group_id_2=>1, group_id_3=>1})),
185 "overloaded multicol update 1"
188 $employee = $employees->search({group_id_2=>4, group_id_3=>1})->first;
189 $employee->update({group_id_2=>2});
190 ok( check_rs($employees->search_rs({group_id_2=>4, group_id_3=>1}))
191 && check_rs($employees->search_rs({group_id_2=>2, group_id_3=>1})),
192 "overloaded multicol update 2"
195 $employee = $employees->search({group_id_2=>3, group_id_3=>1})->first;
196 $employee->group_id_2(1);
197 $employee->group_id_3(3);
199 ok( check_rs($employees->search_rs({group_id_2=>3, group_id_3=>1}))
200 && check_rs($employees->search_rs({group_id_2=>1, group_id_3=>3})),
201 "overloaded multicol update 3"
204 $employee = $employees->search({group_id_2=>3, group_id_3=>1})->first;
205 $employee->update({group_id_2=>2, group_id_3=>3});
206 ok( check_rs($employees->search_rs({group_id_2=>3, group_id_3=>1}))
207 && check_rs($employees->search_rs({group_id_2=>2, group_id_3=>3})),
208 "overloaded multicol update 4"
211 $employee = $employees->search({group_id_2=>3, group_id_3=>2})->first;
212 $employee->update({group_id_2=>2, group_id_3=>4, position=>2});
213 ok( check_rs($employees->search_rs({group_id_2=>3, group_id_3=>2}))
214 && check_rs($employees->search_rs({group_id_2=>2, group_id_3=>4})),
215 "overloaded multicol update 5"
221 my $count = $rs->count();
222 my $position_column = $rs->result_class->position_column();
225 foreach my $position (1..$count) {
227 ($row) = $rs->search({ $position_column=>$position })->all();
228 $row->move_previous();
229 ok( check_rs($rs), "move_previous( $position )" );
231 ($row) = $rs->search({ $position_column=>$position })->all();
233 ok( check_rs($rs), "move_next( $position )" );
235 ($row) = $rs->search({ $position_column=>$position })->all();
237 ok( check_rs($rs), "move_first( $position )" );
239 ($row) = $rs->search({ $position_column=>$position })->all();
241 ok( check_rs($rs), "move_last( $position )" );
243 foreach my $to_position (1..$count) {
244 ($row) = $rs->search({ $position_column=>$position })->all();
245 $row->move_to($to_position);
246 ok( check_rs($rs), "move_to( $position => $to_position )" );
249 $row = $rs->find({ position => $position });
251 ok( !$row->previous_sibling(), 'no previous sibling' );
252 ok( !$row->first_sibling(), 'no first sibling' );
253 ok( $row->next_sibling->position > $position, 'next sibling position > than us');
254 is( $row->next_sibling->previous_sibling->position, $position, 'next-prev sibling is us');
255 ok( $row->last_sibling->position > $position, 'last sibling position > than us');
258 ok( $row->previous_sibling(), 'previous sibling' );
259 ok( $row->first_sibling(), 'first sibling' );
260 ok( $row->previous_sibling->position < $position, 'prev sibling position < than us');
261 is( $row->previous_sibling->next_sibling->position, $position, 'prev-next sibling is us');
262 ok( $row->first_sibling->position < $position, 'first sibling position < than us');
264 if ($position==$count) {
265 ok( !$row->next_sibling(), 'no next sibling' );
266 ok( !$row->last_sibling(), 'no last sibling' );
267 ok( $row->previous_sibling->position < $position, 'prev sibling position < than us');
268 is( $row->previous_sibling->next_sibling->position, $position, 'prev-next sibling is us');
269 ok( $row->first_sibling->position < $position, 'first sibling position < than us');
272 ok( $row->next_sibling(), 'next sibling' );
273 ok( $row->last_sibling(), 'last sibling' );
274 ok( $row->next_sibling->position > $row->position, 'next sibling position > than us');
275 is( $row->next_sibling->previous_sibling->position, $position, 'next-prev sibling is us');
276 ok( $row->last_sibling->position > $row->position, 'last sibling position > than us');
285 my $position_column = $rs->result_class->position_column();
286 my $expected_position = 0;
287 while (my $row = $rs->next()) {
288 $expected_position ++;
289 if ($row->get_column($position_column)!=$expected_position) {