switched to just registering extra source rather than removing and reregistering
[dbsrgits/DBIx-Class.git] / t / 87ordered.t
CommitLineData
93cec8c3 1# vim: filetype=perl
70350518 2use strict;
3use warnings;
93cec8c3 4
70350518 5use Test::More;
6use lib qw(t/lib);
7use DBICTest;
93cec8c3 8
1d941d67 9use POSIX qw(ceil);
10
a47e1233 11my $schema = DBICTest->init_schema();
93cec8c3 12
1d941d67 13plan tests => 879;
93cec8c3 14
70350518 15my $employees = $schema->resultset('Employee');
16$employees->delete();
169bb185 17
70350518 18foreach (1..5) {
19 $employees->create({ name=>'temp' });
20}
21$employees = $employees->search(undef,{order_by=>'position'});
22ok( check_rs($employees), "intial positions" );
b1c66eea 23
70350518 24hammer_rs( $employees );
169bb185 25
70350518 26DBICTest::Employee->grouping_column('group_id');
27$employees->delete();
79dc353a 28foreach my $group_id (1..4) {
70350518 29 foreach (1..6) {
30 $employees->create({ name=>'temp', group_id=>$group_id });
169bb185 31 }
70350518 32}
33$employees = $employees->search(undef,{order_by=>'group_id,position'});
169bb185 34
79dc353a 35foreach my $group_id (1..4) {
70350518 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 );
169bb185 40}
41
79dc353a 42my $group_3 = $employees->search({group_id=>3});
43my $to_group = 1;
44my $to_pos = undef;
45while (my $employee = $group_3->next) {
46 $employee->move_to_group($to_group, $to_pos);
47 $to_pos++;
48 $to_group = $to_group==1 ? 2 : 1;
49}
50foreach my $group_id (1..4) {
51 my $group_employees = $employees->search({group_id=>$group_id});
52 $group_employees->all();
53 ok( check_rs($group_employees), "group positions after move_to_group" );
54}
55
56my $employee = $employees->search({group_id=>4})->first;
57$employee->position(2);
58$employee->update;
59ok( check_rs($employees->search_rs({group_id=>4})), "overloaded update 1" );
60$employee = $employees->search({group_id=>4})->first;
61$employee->update({position=>3});
62ok( check_rs($employees->search_rs({group_id=>4})), "overloaded update 2" );
63$employee = $employees->search({group_id=>4})->first;
64$employee->group_id(1);
65$employee->update;
66ok(
67 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
68 "overloaded update 3"
69);
70$employee = $employees->search({group_id=>4})->first;
71$employee->update({group_id=>2});
72ok(
73 check_rs($employees->search_rs({group_id=>2})) && check_rs($employees->search_rs({group_id=>4})),
74 "overloaded update 4"
75);
76$employee = $employees->search({group_id=>4})->first;
77$employee->group_id(1);
78$employee->position(3);
79$employee->update;
80ok(
81 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
82 "overloaded update 5"
83);
84$employee = $employees->search({group_id=>4})->first;
85$employee->group_id(2);
86$employee->position(undef);
87$employee->update;
88ok(
89 check_rs($employees->search_rs({group_id=>2})) && check_rs($employees->search_rs({group_id=>4})),
90 "overloaded update 6"
91);
92$employee = $employees->search({group_id=>4})->first;
93$employee->update({group_id=>1,position=>undef});
94ok(
95 check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})),
96 "overloaded update 7"
97);
98
1d941d67 99# multicol tests begin here
100DBICTest::Employee->grouping_column(['group_id', 'group_id_2']);
101$employees->delete();
102foreach my $group_id (1..4) {
103 foreach my $group_id_2 (1..4) {
104 foreach (1..4) {
105 $employees->create({ name=>'temp', group_id=>$group_id, group_id_2=>$group_id_2 });
106 }
107 }
108}
109$employees = $employees->search(undef,{order_by=>'group_id,group_id_2,position'});
110
111foreach my $group_id (1..3) {
112 foreach my $group_id_2 (1..3) {
113 my $group_employees = $employees->search({group_id=>$group_id, group_id_2=>$group_id_2});
114 $group_employees->all();
115 ok( check_rs($group_employees), "group intial positions" );
116 hammer_rs( $group_employees );
117 }
118}
119
120# move_to_group, specifying group by hash
121my $group_4 = $employees->search({group_id=>4});
122$to_group = 1;
123my $to_group_2_base = 7;
124my $to_group_2 = 1;
125$to_pos = undef;
126while (my $employee = $group_4->next) {
127 $employee->move_to_group({group_id=>$to_group, group_id_2=>$to_group_2}, $to_pos);
128 $to_pos++;
129 $to_group = ($to_group % 3) + 1;
130 $to_group_2_base++;
131 $to_group_2 = (ceil($to_group_2_base/3.0) %3) +1
132}
133foreach my $group_id (1..4) {
134 foreach my $group_id_2 (1..4) {
135 my $group_employees = $employees->search({group_id=>$group_id,group_id_2=>$group_id_2});
136 $group_employees->all();
137 ok( check_rs($group_employees), "group positions after move_to_group" );
138 }
139}
140
141$employees->delete();
142foreach my $group_id (1..4) {
143 foreach my $group_id_2 (1..4) {
144 foreach (1..4) {
145 $employees->create({ name=>'temp', group_id=>$group_id, group_id_2=>$group_id_2 });
146 }
147 }
148}
149$employees = $employees->search(undef,{order_by=>'group_id,group_id_2,position'});
150
151$employee = $employees->search({group_id=>4, group_id_2=>1})->first;
152$employee->group_id(1);
153$employee->update;
154ok(
155 check_rs($employees->search_rs({group_id=>4, group_id_2=>1}))
156 && check_rs($employees->search_rs({group_id=>1, group_id_2=>1})),
157 "overloaded multicol update 1"
158);
159
160$employee = $employees->search({group_id=>4, group_id_2=>1})->first;
161$employee->update({group_id=>2});
162ok( check_rs($employees->search_rs({group_id=>4, group_id_2=>1}))
163 && check_rs($employees->search_rs({group_id=>2, group_id_2=>1})),
164 "overloaded multicol update 2"
165);
166
167$employee = $employees->search({group_id=>3, group_id_2=>1})->first;
168$employee->group_id(1);
169$employee->group_id_2(3);
170$employee->update();
171ok( check_rs($employees->search_rs({group_id=>3, group_id_2=>1}))
172 && check_rs($employees->search_rs({group_id=>1, group_id_2=>3})),
173 "overloaded multicol update 3"
174);
175
176$employee = $employees->search({group_id=>3, group_id_2=>1})->first;
177$employee->update({group_id=>2, group_id_2=>3});
178ok( check_rs($employees->search_rs({group_id=>3, group_id_2=>1}))
179 && check_rs($employees->search_rs({group_id=>2, group_id_2=>3})),
180 "overloaded multicol update 4"
181);
182
183$employee = $employees->search({group_id=>3, group_id_2=>2})->first;
184$employee->update({group_id=>2, group_id_2=>4, position=>2});
185ok( check_rs($employees->search_rs({group_id=>3, group_id_2=>2}))
186 && check_rs($employees->search_rs({group_id=>2, group_id_2=>4})),
187 "overloaded multicol update 5"
188);
189
169bb185 190sub hammer_rs {
191 my $rs = shift;
707cbb2d 192 my $employee;
169bb185 193 my $count = $rs->count();
194 my $position_column = $rs->result_class->position_column();
58d387fe 195 my $row;
80010e2b 196
169bb185 197 foreach my $position (1..$count) {
80010e2b 198
dc66dea1 199 ($row) = $rs->search({ $position_column=>$position })->all();
169bb185 200 $row->move_previous();
201 ok( check_rs($rs), "move_previous( $position )" );
80010e2b 202
dc66dea1 203 ($row) = $rs->search({ $position_column=>$position })->all();
169bb185 204 $row->move_next();
205 ok( check_rs($rs), "move_next( $position )" );
80010e2b 206
dc66dea1 207 ($row) = $rs->search({ $position_column=>$position })->all();
169bb185 208 $row->move_first();
209 ok( check_rs($rs), "move_first( $position )" );
80010e2b 210
dc66dea1 211 ($row) = $rs->search({ $position_column=>$position })->all();
169bb185 212 $row->move_last();
213 ok( check_rs($rs), "move_last( $position )" );
80010e2b 214
169bb185 215 foreach my $to_position (1..$count) {
dc66dea1 216 ($row) = $rs->search({ $position_column=>$position })->all();
169bb185 217 $row->move_to($to_position);
218 ok( check_rs($rs), "move_to( $position => $to_position )" );
707cbb2d 219 }
80021def 220
dc66dea1 221 ($row) = $rs->search({ position=>$position })->all();
707cbb2d 222 if ($position==1) {
169bb185 223 ok( !$row->previous_sibling(), 'no previous sibling' );
224 ok( !$row->first_sibling(), 'no first sibling' );
225 }
226 else {
227 ok( $row->previous_sibling(), 'previous sibling' );
228 ok( $row->first_sibling(), 'first sibling' );
707cbb2d 229 }
169bb185 230 if ($position==$count) {
231 ok( !$row->next_sibling(), 'no next sibling' );
232 ok( !$row->last_sibling(), 'no last sibling' );
80021def 233 }
707cbb2d 234 else {
169bb185 235 ok( $row->next_sibling(), 'next sibling' );
236 ok( $row->last_sibling(), 'last sibling' );
707cbb2d 237 }
238
80010e2b 239 }
80010e2b 240}
241
169bb185 242sub check_rs {
243 my( $rs ) = @_;
244 $rs->reset();
245 my $position_column = $rs->result_class->position_column();
93cec8c3 246 my $expected_position = 0;
169bb185 247 while (my $row = $rs->next()) {
93cec8c3 248 $expected_position ++;
169bb185 249 if ($row->get_column($position_column)!=$expected_position) {
133dd22a 250 return 0;
80021def 251 }
93cec8c3 252 }
133dd22a 253 return 1;
93cec8c3 254}
255