Revision 5a469687
Von Sven Schöling vor mehr als 10 Jahren hinzugefügt
t/db_helper/acts_as_list.t | ||
---|---|---|
10 | 10 |
use Support::TestSetup; |
11 | 11 |
|
12 | 12 |
eval { |
13 |
require 'SL::DB::RequirementSpec'; |
|
14 |
require 'SL::DB::RequirementSpecItem'; |
|
15 |
require 'SL::DB::RequirementSpecTextBlock'; |
|
13 |
require SL::DB::RequirementSpec; |
|
14 |
require SL::DB::RequirementSpecItem; |
|
15 |
require SL::DB::RequirementSpecTextBlock; |
|
16 |
require SL::DB::RequirementSpecType; |
|
17 |
require SL::DB::RequirementSpecStatus; |
|
16 | 18 |
1; |
17 | 19 |
} or my $skip = 'RequirementSpec is not available for this test'; |
18 | 20 |
|
... | ... | |
22 | 24 |
plan tests => 48; |
23 | 25 |
} |
24 | 26 |
|
27 |
my ($customer, $status, $type, $r_spec, @items); |
|
28 |
|
|
29 |
sub init { |
|
30 |
$customer = SL::DB::Customer->new(name => 'Test Customer')->save; |
|
31 |
$status = SL::DB::Manager::RequirementSpecStatus->find_by(name => '', description => '') || |
|
32 |
SL::DB::RequirementSpecStatus->new(name => '', description => '', position => 0)->save; |
|
33 |
$type = SL::DB::Manager::RequirementSpecType->find_by(description => '') || |
|
34 |
SL::DB::RequirementSpecType->new(description => '', position => 0)->save; |
|
35 |
} |
|
36 |
|
|
37 |
sub cleanup { |
|
38 |
$customer->delete; |
|
39 |
$status->delete; |
|
40 |
$type->delete; |
|
41 |
} |
|
42 |
|
|
43 |
sub cleanup_req_spec { |
|
44 |
SL::DB::Manager::RequirementSpec->delete_all(where => [ customer_id => $customer->id ], cascade => 1); |
|
45 |
} |
|
46 |
|
|
25 | 47 |
sub reset_state { |
26 |
"SL::DB::Manager::${_}"->delete_all(all => 1) for qw(RequirementSpecTextBlock RequirementSpecItem RequirementSpec); |
|
48 |
cleanup_req_spec(); |
|
49 |
@items = (); |
|
27 | 50 |
|
28 |
SL::DB::RequirementSpec->new(id => 2, type_id => 1, status_id => 1, customer_id => 12395, hourly_rate => 42.24, title => "Azumbo")->save;
|
|
51 |
$r_spec = SL::DB::RequirementSpec->new(type_id => $type->id, status_id => $status->id, customer_id => $customer->id, hourly_rate => 42.24, title => "Azumbo")->save;
|
|
29 | 52 |
|
30 |
SL::DB::RequirementSpecItem->new(requirement_spec_id => 2, parent_id => undef, id => 1, position => 1, fb_number => "A01", title => "Mühköh", description => "The Kuh.")->save;
|
|
31 |
SL::DB::RequirementSpecItem->new(requirement_spec_id => 2, parent_id => undef, id => 2, position => 2, fb_number => "A02", title => "Geheim", description => "Kofferkombination")->save;
|
|
32 |
SL::DB::RequirementSpecItem->new(requirement_spec_id => 2, parent_id => 1, id => 3, position => 1, fb_number => "FB0001", title => "Yäääh", description => "Und so")->save;
|
|
33 |
SL::DB::RequirementSpecItem->new(requirement_spec_id => 2, parent_id => 1, id => 4, position => 2, fb_number => "FB0012", title => "Blubb", description => "blabb")->save;
|
|
34 |
SL::DB::RequirementSpecItem->new(requirement_spec_id => 2, parent_id => 1, id => 5, position => 3, fb_number => "FB0022", title => "Fingo", description => "fungo")->save;
|
|
35 |
SL::DB::RequirementSpecItem->new(requirement_spec_id => 2, parent_id => 4, id => 6, position => 1, fb_number => "UFB002", title => "Suppi", description => "Suppa")->save;
|
|
36 |
SL::DB::RequirementSpecItem->new(requirement_spec_id => 2, parent_id => 4, id => 7, position => 2, fb_number => "UFB000", title => "Suppa", description => "Suppi")->save;
|
|
37 |
SL::DB::RequirementSpecItem->new(requirement_spec_id => 2, parent_id => 2, id => 8, position => 1, fb_number => "FB0018", title => "Neuneins", description => "Eins")->save;
|
|
53 |
push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => undef, position => 1, fb_number => "A01", title => "Mühköh", item_type => 'section', description => "The Kuh.")->save;
|
|
54 |
push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => undef, position => 2, fb_number => "A02", title => "Geheim", item_type => 'section', description => "Kofferkombination")->save;
|
|
55 |
push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(1), position => 1, fb_number => "FB0001", title => "Yäääh", item_type => 'function-block', description => "Und so")->save;
|
|
56 |
push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(1), position => 2, fb_number => "FB0012", title => "Blubb", item_type => 'function-block', description => "blabb")->save;
|
|
57 |
push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(1), position => 3, fb_number => "FB0022", title => "Fingo", item_type => 'function-block', description => "fungo")->save;
|
|
58 |
push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(4), position => 1, fb_number => "UFB002", title => "Suppi", item_type => 'sub-function-block', description => "Suppa")->save;
|
|
59 |
push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(4), position => 2, fb_number => "UFB000", title => "Suppa", item_type => 'sub-function-block', description => "Suppi")->save;
|
|
60 |
push @items, SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, parent_id => get_id(2), position => 1, fb_number => "FB0018", title => "Neuneins", item_type => 'function-block', description => "Eins")->save;
|
|
38 | 61 |
|
39 |
SL::DB::RequirementSpec->new->db->dbh->do(qq|SELECT pg_catalog.setval('| . $_->[0] . qq|', | . $_->[1] . qq|, true)|) for (['requirement_spec_items_id_seq', 8], [ 'requirement_specs_id_seq', 2 ]); |
|
62 |
# SL::DB::RequirementSpec->new->db->dbh->do(qq|SELECT pg_catalog.setval('| . $_->[0] . qq|', | . $_->[1] . qq|, true)|) for (['requirement_spec_items_id_seq', 8], [ 'requirement_specs_id_seq', 2 ]);
|
|
40 | 63 |
} |
41 | 64 |
|
42 | 65 |
sub values_eq { |
... | ... | |
45 | 68 |
&& (!defined($val1) || ($val1 == $val2)); |
46 | 69 |
} |
47 | 70 |
|
71 |
# accepts a list of arefs, with positions: |
|
72 |
# 1 - pseudo id of item |
|
73 |
# 2 - expceted pseudo parent_id |
|
74 |
# 3 - expected position |
|
48 | 75 |
sub test_positions { |
49 | 76 |
my ($message, @positions) = @_; |
50 | 77 |
|
51 | 78 |
my $failures = |
52 | 79 |
join ' ', |
53 | 80 |
map { join ':', map { $_ // 'undef' } @{ $_ } } |
54 |
grep { !values_eq($_->[1], $_->[3]) || !values_eq($_->[2], $_->[4]) } |
|
55 |
map { my $item = SL::DB::RequirementSpecItem->new(id => $_->[0])->load; [ @{ $_ }, $item->parent_id, $item->position ] }
|
|
81 |
grep { !values_eq($_->[1] && get_item($_->[1]) ? get_id($_->[1]) : undef, $_->[3]) || !values_eq($_->[2], $_->[4]) }
|
|
82 |
map { my $item = SL::DB::RequirementSpecItem->new(id => get_id($_->[0]))->load; [ @{ $_ }, $item->parent_id, $item->position ] }
|
|
56 | 83 |
@positions; |
57 | 84 |
|
58 | 85 |
is($failures, '', $message); |
59 | 86 |
} |
60 | 87 |
|
61 | 88 |
sub new_item { |
62 |
return SL::DB::RequirementSpecItem->new(requirement_spec_id => 2, fb_number => 'dummy', title => 'dummy', @_); |
|
89 |
my %params = @_; |
|
90 |
|
|
91 |
my $new_item = SL::DB::RequirementSpecItem->new(requirement_spec_id => $r_spec->id, fb_number => 'dummy', title => 'dummy', item_type => $params{parent_id} ? 'function-block' : 'section', @_); |
|
92 |
push @items, $new_item; |
|
93 |
$new_item; |
|
63 | 94 |
} |
64 | 95 |
|
96 |
# the first version of this used hardcoded ids. |
|
97 |
# to make things a little more portable the objects are now created with the |
|
98 |
# default id sequences, but retain their numbering. whenever you see get_id or |
|
99 |
# get_item used it looks up the old id in the @items array |
|
65 | 100 |
sub get_item { |
66 |
return SL::DB::RequirementSpecItem->new(id => $_[0])->load; |
|
101 |
return SL::DB::RequirementSpecItem->new(id => get_id($_[0]))->load; |
|
102 |
} |
|
103 |
|
|
104 |
sub get_id { |
|
105 |
$items[$_[0]-1]->id |
|
67 | 106 |
} |
68 | 107 |
|
69 | 108 |
Support::TestSetup::login(); |
109 |
|
|
70 | 110 |
my $item; |
71 | 111 |
|
72 | 112 |
# 1 |
... | ... | |
78 | 118 |
# 2 |
79 | 119 |
# `- 8 |
80 | 120 |
|
121 |
init(); |
|
81 | 122 |
reset_state(); |
82 | 123 |
test_positions "reset_state", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ]; |
83 | 124 |
|
84 | 125 |
# Einfügen neuer Objekte: "set_position" |
85 |
new_item(parent_id => 1)->save;
|
|
126 |
new_item(parent_id => get_id(1))->save;
|
|
86 | 127 |
test_positions "set_position via new with parent_id NOT NULL", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ], [ 9, 1, 4 ]; |
87 | 128 |
|
88 | 129 |
reset_state(); |
... | ... | |
138 | 179 |
|
139 | 180 |
# Listen neu anordnen |
140 | 181 |
reset_state(); |
141 |
get_item(8)->reorder_list(4, 5, 3); |
|
142 |
test_positions "reoder_list called as instance method", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 3, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ]; |
|
182 |
get_item(8)->reorder_list(map { get_id($_) } 4, 5, 3);
|
|
183 |
test_positions "reorder_list called as instance method", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 3, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
|
|
143 | 184 |
|
144 | 185 |
reset_state(); |
145 |
SL::DB::RequirementSpecItem->reorder_list(4, 5, 3); |
|
146 |
test_positions "reoder_list called as class method", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 3, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ]; |
|
186 |
SL::DB::RequirementSpecItem->reorder_list(map { get_id($_) } 4, 5, 3);
|
|
187 |
test_positions "reorder_list called as class method", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 4, 1, 1 ], [ 5, 1, 2 ], [ 3, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ];
|
|
147 | 188 |
|
148 | 189 |
# Aus Liste entfernen |
149 | 190 |
reset_state(); |
... | ... | |
168 | 209 |
|
169 | 210 |
# Zu Liste hinzufügen |
170 | 211 |
reset_state(); |
171 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(1); $item->add_to_list(position => 'last');
|
|
212 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'last');
|
|
172 | 213 |
test_positions "add_to_list position 'last'", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 4 ]; |
173 | 214 |
|
174 | 215 |
reset_state(); |
175 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(1); $item->add_to_list(position => 'first');
|
|
216 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'first');
|
|
176 | 217 |
test_positions "add_to_list position 'first'", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 2 ], [ 4, 1, 3 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 1 ]; |
177 | 218 |
|
178 | 219 |
reset_state(); |
179 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(1); $item->add_to_list(position => 'before', reference => 3);
|
|
220 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'before', reference => get_id(3));
|
|
180 | 221 |
test_positions "add_to_list position 'before' first by ID", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 2 ], [ 4, 1, 3 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 1 ]; |
181 | 222 |
|
182 | 223 |
reset_state(); |
183 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(1); $item->add_to_list(position => 'before', reference => get_item(3));
|
|
224 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'before', reference => get_item(3));
|
|
184 | 225 |
test_positions "add_to_list position 'before' first by reference", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 2 ], [ 4, 1, 3 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 1 ]; |
185 | 226 |
|
186 | 227 |
reset_state(); |
187 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(1); $item->add_to_list(position => 'before', reference => 4);
|
|
228 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'before', reference => get_id(4));
|
|
188 | 229 |
test_positions "add_to_list position 'before' middle by ID", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 3 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 2 ]; |
189 | 230 |
|
190 | 231 |
reset_state(); |
191 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(1); $item->add_to_list(position => 'before', reference => get_item(4));
|
|
232 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'before', reference => get_item(4));
|
|
192 | 233 |
test_positions "add_to_list position 'before' middle by reference", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 3 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 2 ]; |
193 | 234 |
|
194 | 235 |
reset_state(); |
195 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(1); $item->add_to_list(position => 'after', reference => 5);
|
|
236 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'after', reference => get_id(5));
|
|
196 | 237 |
test_positions "add_to_list position 'after' last by ID", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 4 ]; |
197 | 238 |
|
198 | 239 |
reset_state(); |
199 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(1); $item->add_to_list(position => 'after', reference => get_item(5));
|
|
240 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'after', reference => get_item(5));
|
|
200 | 241 |
test_positions "add_to_list position 'after' last by reference", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 4 ]; |
201 | 242 |
|
202 | 243 |
reset_state(); |
203 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(1); $item->add_to_list(position => 'after', reference => 4);
|
|
244 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'after', reference => get_id(4));
|
|
204 | 245 |
test_positions "add_to_list position 'after' middle by ID", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 3 ]; |
205 | 246 |
|
206 | 247 |
reset_state(); |
207 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(1); $item->add_to_list(position => 'after', reference => get_item(4));
|
|
248 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(1)); $item->add_to_list(position => 'after', reference => get_item(4));
|
|
208 | 249 |
test_positions "add_to_list position 'after' middle by reference", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 4 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 1, 3 ]; |
209 | 250 |
|
210 | 251 |
reset_state(); |
211 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(3); $item->add_to_list(position => 'last');
|
|
252 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(3)); $item->add_to_list(position => 'last');
|
|
212 | 253 |
test_positions "add_to_list position 'last' in empty", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 3, 1 ]; |
213 | 254 |
|
214 | 255 |
reset_state(); |
215 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(3); $item->add_to_list(position => 'first');
|
|
256 |
$item = get_item(8); $item->remove_from_list; $item->parent_id(get_id(3)); $item->add_to_list(position => 'first');
|
|
216 | 257 |
test_positions "add_to_list position 'first' in empty", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 4, 1, 2 ], [ 5, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 3, 1 ]; |
217 | 258 |
|
218 | 259 |
reset_state(); |
219 |
$item = get_item(5); $item->add_to_list(position => 'after', reference => 3);
|
|
260 |
$item = get_item(5); $item->add_to_list(position => 'after', reference => get_id(3));
|
|
220 | 261 |
test_positions "add_to_list without prior remove_from_list", [ 1, undef, 1 ], [ 2, undef, 2 ], [ 3, 1, 1 ], [ 5, 1, 2 ], [ 4, 1, 3 ], [ 6, 4, 1 ], [ 7, 4, 2 ], [ 8, 2, 1 ]; |
221 | 262 |
|
222 | 263 |
reset_state(); |
223 | 264 |
$item = get_item(4); |
224 |
is($item->get_next_in_list->id, 5, 'Next of 4 is 5');
|
|
225 |
is($item->get_previous_in_list->id, 3, 'Previous of 4 is 5');
|
|
226 |
is($item->get_next_in_list->get_previous_in_list->id, 4, 'Previous of Next of 4 is 4');
|
|
227 |
is($item->get_previous_in_list->get_next_in_list->id, 4, 'Next of Previous of 4 is 4');
|
|
228 |
is($item->get_next_in_list->get_next_in_list, undef, 'Next of Next of 4 is undef'); |
|
229 |
is($item->get_previous_in_list->get_previous_in_list, undef, 'Previous of Previous of 4 is undef'); |
|
265 |
is($item->get_next_in_list->id, get_id(5), 'Next of 4 is 5');
|
|
266 |
is($item->get_previous_in_list->id, get_id(3), 'Previous of 4 is 5');
|
|
267 |
is($item->get_next_in_list->get_previous_in_list->id, get_id(4), 'Previous of Next of 4 is 4');
|
|
268 |
is($item->get_previous_in_list->get_next_in_list->id, get_id(4), 'Next of Previous of 4 is 4');
|
|
269 |
is($item->get_next_in_list->get_next_in_list, undef, 'Next of Next of 4 is undef');
|
|
270 |
is($item->get_previous_in_list->get_previous_in_list, undef, 'Previous of Previous of 4 is undef');
|
|
230 | 271 |
|
231 | 272 |
# Parametervalidierung |
232 | 273 |
throws_ok { new_item()->move_position_up } qr/not.*been.*saved/i, 'move up not saved yet'; |
... | ... | |
240 | 281 |
throws_ok { get_item(8)->add_to_list(position => 'before') } qr/missing.*parameter.*reference/i, 'missing reference for position "before"'; |
241 | 282 |
throws_ok { get_item(8)->add_to_list(position => 'after') } qr/missing.*parameter.*reference/i, 'missing reference for position "after"'; |
242 | 283 |
|
243 |
done_testing(); |
|
284 |
END { |
|
285 |
# safely try to clean up |
|
286 |
eval { |
|
287 |
cleanup_req_spec(); |
|
288 |
cleanup(); |
|
289 |
} |
|
290 |
} |
Auch abrufbar als: Unified diff
Diverse Fixes, damit der Testcase mit den aktuellen requirement_specs durchläuft.
- braucht jetzt keine leere Datenbank mehr, erstellt Ficture und löscht sie hinterher wieder.
- Update auf item_type
- Unmöglicher Check im Header korrigiert
- Zahl der Testcases jetzt korrekt hart kodiert