Commit | Line | Data |
93cec8c3 |
1 | # vim: filetype=perl |
70350518 |
2 | use strict; |
3 | use warnings; |
93cec8c3 |
4 | |
70350518 |
5 | use Test::More; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
93cec8c3 |
8 | |
1d941d67 |
9 | use POSIX qw(ceil); |
10 | |
a47e1233 |
11 | my $schema = DBICTest->init_schema(); |
93cec8c3 |
12 | |
97bfce50 |
13 | plan tests => 1269; |
93cec8c3 |
14 | |
70350518 |
15 | my $employees = $schema->resultset('Employee'); |
16 | $employees->delete(); |
169bb185 |
17 | |
70350518 |
18 | foreach (1..5) { |
19 | $employees->create({ name=>'temp' }); |
20 | } |
21 | $employees = $employees->search(undef,{order_by=>'position'}); |
22 | ok( check_rs($employees), "intial positions" ); |
b1c66eea |
23 | |
70350518 |
24 | hammer_rs( $employees ); |
169bb185 |
25 | |
70350518 |
26 | DBICTest::Employee->grouping_column('group_id'); |
27 | $employees->delete(); |
79dc353a |
28 | foreach 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 |
35 | foreach 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 |
42 | my $group_3 = $employees->search({group_id=>3}); |
43 | my $to_group = 1; |
44 | my $to_pos = undef; |
45 | while (my $employee = $group_3->next) { |
9beded8a |
46 | $employee->discard_changes; # since we are effective shift()ing the $rs |
79dc353a |
47 | $employee->move_to_group($to_group, $to_pos); |
48 | $to_pos++; |
49 | $to_group = $to_group==1 ? 2 : 1; |
50 | } |
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" ); |
55 | } |
56 | |
57 | my $employee = $employees->search({group_id=>4})->first; |
58 | $employee->position(2); |
59 | $employee->update; |
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); |
66 | $employee->update; |
67 | ok( |
68 | check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})), |
69 | "overloaded update 3" |
70 | ); |
71 | $employee = $employees->search({group_id=>4})->first; |
72 | $employee->update({group_id=>2}); |
73 | ok( |
74 | check_rs($employees->search_rs({group_id=>2})) && check_rs($employees->search_rs({group_id=>4})), |
75 | "overloaded update 4" |
76 | ); |
77 | $employee = $employees->search({group_id=>4})->first; |
78 | $employee->group_id(1); |
79 | $employee->position(3); |
80 | $employee->update; |
81 | ok( |
82 | check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})), |
83 | "overloaded update 5" |
84 | ); |
85 | $employee = $employees->search({group_id=>4})->first; |
86 | $employee->group_id(2); |
87 | $employee->position(undef); |
88 | $employee->update; |
89 | ok( |
90 | check_rs($employees->search_rs({group_id=>2})) && check_rs($employees->search_rs({group_id=>4})), |
91 | "overloaded update 6" |
92 | ); |
93 | $employee = $employees->search({group_id=>4})->first; |
94 | $employee->update({group_id=>1,position=>undef}); |
95 | ok( |
96 | check_rs($employees->search_rs({group_id=>1})) && check_rs($employees->search_rs({group_id=>4})), |
97 | "overloaded update 7" |
98 | ); |
99 | |
1d941d67 |
100 | # multicol tests begin here |
9beded8a |
101 | DBICTest::Employee->grouping_column(['group_id_2', 'group_id_3']); |
1d941d67 |
102 | $employees->delete(); |
9beded8a |
103 | foreach my $group_id_2 (1..4) { |
104 | foreach my $group_id_3 (1..4) { |
1d941d67 |
105 | foreach (1..4) { |
9beded8a |
106 | $employees->create({ name=>'temp', group_id_2=>$group_id_2, group_id_3=>$group_id_3 }); |
1d941d67 |
107 | } |
108 | } |
109 | } |
9beded8a |
110 | $employees = $employees->search(undef,{order_by=>[qw/group_id_2 group_id_3 position/]}); |
1d941d67 |
111 | |
9beded8a |
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}); |
1d941d67 |
115 | $group_employees->all(); |
116 | ok( check_rs($group_employees), "group intial positions" ); |
117 | hammer_rs( $group_employees ); |
118 | } |
119 | } |
120 | |
121 | # move_to_group, specifying group by hash |
9beded8a |
122 | my $group_4 = $employees->search({group_id_2=>4}); |
1d941d67 |
123 | $to_group = 1; |
124 | my $to_group_2_base = 7; |
125 | my $to_group_2 = 1; |
126 | $to_pos = undef; |
127 | while (my $employee = $group_4->next) { |
9beded8a |
128 | $employee->move_to_group({group_id_2=>$to_group, group_id_3=>$to_group_2}, $to_pos); |
1d941d67 |
129 | $to_pos++; |
130 | $to_group = ($to_group % 3) + 1; |
131 | $to_group_2_base++; |
132 | $to_group_2 = (ceil($to_group_2_base/3.0) %3) +1 |
133 | } |
9beded8a |
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}); |
1d941d67 |
137 | $group_employees->all(); |
138 | ok( check_rs($group_employees), "group positions after move_to_group" ); |
139 | } |
140 | } |
141 | |
142 | $employees->delete(); |
9beded8a |
143 | foreach my $group_id_2 (1..4) { |
144 | foreach my $group_id_3 (1..4) { |
1d941d67 |
145 | foreach (1..4) { |
9beded8a |
146 | $employees->create({ name=>'temp', group_id_2=>$group_id_2, group_id_3=>$group_id_3 }); |
1d941d67 |
147 | } |
148 | } |
149 | } |
9beded8a |
150 | $employees = $employees->search(undef,{order_by=>[qw/group_id_2 group_id_3 position/]}); |
1d941d67 |
151 | |
9beded8a |
152 | $employee = $employees->search({group_id_2=>4, group_id_3=>1})->first; |
153 | $employee->group_id_2(1); |
1d941d67 |
154 | $employee->update; |
155 | ok( |
9beded8a |
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})), |
1d941d67 |
158 | "overloaded multicol update 1" |
159 | ); |
160 | |
9beded8a |
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" |
1d941d67 |
166 | ); |
167 | |
9beded8a |
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); |
1d941d67 |
171 | $employee->update(); |
9beded8a |
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})), |
1d941d67 |
174 | "overloaded multicol update 3" |
175 | ); |
176 | |
9beded8a |
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})), |
1d941d67 |
181 | "overloaded multicol update 4" |
182 | ); |
183 | |
9beded8a |
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})), |
1d941d67 |
188 | "overloaded multicol update 5" |
189 | ); |
190 | |
169bb185 |
191 | sub hammer_rs { |
192 | my $rs = shift; |
707cbb2d |
193 | my $employee; |
169bb185 |
194 | my $count = $rs->count(); |
195 | my $position_column = $rs->result_class->position_column(); |
58d387fe |
196 | my $row; |
80010e2b |
197 | |
169bb185 |
198 | foreach my $position (1..$count) { |
80010e2b |
199 | |
dc66dea1 |
200 | ($row) = $rs->search({ $position_column=>$position })->all(); |
169bb185 |
201 | $row->move_previous(); |
202 | ok( check_rs($rs), "move_previous( $position )" ); |
80010e2b |
203 | |
dc66dea1 |
204 | ($row) = $rs->search({ $position_column=>$position })->all(); |
169bb185 |
205 | $row->move_next(); |
206 | ok( check_rs($rs), "move_next( $position )" ); |
80010e2b |
207 | |
dc66dea1 |
208 | ($row) = $rs->search({ $position_column=>$position })->all(); |
169bb185 |
209 | $row->move_first(); |
210 | ok( check_rs($rs), "move_first( $position )" ); |
80010e2b |
211 | |
dc66dea1 |
212 | ($row) = $rs->search({ $position_column=>$position })->all(); |
169bb185 |
213 | $row->move_last(); |
214 | ok( check_rs($rs), "move_last( $position )" ); |
80010e2b |
215 | |
169bb185 |
216 | foreach my $to_position (1..$count) { |
dc66dea1 |
217 | ($row) = $rs->search({ $position_column=>$position })->all(); |
169bb185 |
218 | $row->move_to($to_position); |
219 | ok( check_rs($rs), "move_to( $position => $to_position )" ); |
707cbb2d |
220 | } |
80021def |
221 | |
97bfce50 |
222 | $row = $rs->find({ position => $position }); |
707cbb2d |
223 | if ($position==1) { |
169bb185 |
224 | ok( !$row->previous_sibling(), 'no previous sibling' ); |
225 | ok( !$row->first_sibling(), 'no first sibling' ); |
97bfce50 |
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'); |
169bb185 |
229 | } |
230 | else { |
231 | ok( $row->previous_sibling(), 'previous sibling' ); |
232 | ok( $row->first_sibling(), 'first sibling' ); |
97bfce50 |
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'); |
707cbb2d |
236 | } |
169bb185 |
237 | if ($position==$count) { |
238 | ok( !$row->next_sibling(), 'no next sibling' ); |
239 | ok( !$row->last_sibling(), 'no last sibling' ); |
97bfce50 |
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'); |
80021def |
243 | } |
707cbb2d |
244 | else { |
169bb185 |
245 | ok( $row->next_sibling(), 'next sibling' ); |
246 | ok( $row->last_sibling(), 'last sibling' ); |
97bfce50 |
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'); |
707cbb2d |
250 | } |
251 | |
80010e2b |
252 | } |
80010e2b |
253 | } |
254 | |
169bb185 |
255 | sub check_rs { |
256 | my( $rs ) = @_; |
257 | $rs->reset(); |
258 | my $position_column = $rs->result_class->position_column(); |
93cec8c3 |
259 | my $expected_position = 0; |
169bb185 |
260 | while (my $row = $rs->next()) { |
93cec8c3 |
261 | $expected_position ++; |
169bb185 |
262 | if ($row->get_column($position_column)!=$expected_position) { |
133dd22a |
263 | return 0; |
80021def |
264 | } |
93cec8c3 |
265 | } |
133dd22a |
266 | return 1; |
93cec8c3 |
267 | } |
268 | |