Lines 19-25
Link Here
|
19 |
|
19 |
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
|
21 |
|
22 |
use Test::More tests => 4; |
22 |
use Benchmark; |
|
|
23 |
use Test::More tests => 7; |
24 |
use Test::Deep qw( cmp_methods ); |
23 |
use Test::Exception; |
25 |
use Test::Exception; |
24 |
|
26 |
|
25 |
use Koha::CirculationRules; |
27 |
use Koha::CirculationRules; |
Lines 27-37
use Koha::Database;
Link Here
|
27 |
|
29 |
|
28 |
use t::lib::Mocks; |
30 |
use t::lib::Mocks; |
29 |
use t::lib::TestBuilder; |
31 |
use t::lib::TestBuilder; |
30 |
use t::lib::Mocks; |
|
|
31 |
|
32 |
|
32 |
my $schema = Koha::Database->new->schema; |
33 |
my $schema = Koha::Database->new->schema; |
33 |
my $builder = t::lib::TestBuilder->new; |
34 |
my $builder = t::lib::TestBuilder->new; |
34 |
|
35 |
|
|
|
36 |
subtest 'get_effective_issuing_rule' => sub { |
37 |
plan tests => 2; |
38 |
|
39 |
$schema->storage->txn_begin; |
40 |
|
41 |
my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'}; |
42 |
my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'}; |
43 |
my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'}; |
44 |
|
45 |
subtest 'Call with undefined values' => sub { |
46 |
plan tests => 5; |
47 |
|
48 |
my $rule; |
49 |
Koha::CirculationRules->delete; |
50 |
|
51 |
is(Koha::CirculationRules->search->count, 0, 'There are no issuing rules.'); |
52 |
# undef, undef, undef => 1 |
53 |
$rule = Koha::CirculationRules->get_effective_rule({ |
54 |
branchcode => undef, |
55 |
categorycode => undef, |
56 |
itemtype => undef, |
57 |
rule_name => 'fine', |
58 |
rule_value => 1, |
59 |
}); |
60 |
is($rule, undef, 'When I attempt to get effective issuing rule by' |
61 |
.' providing undefined values, then undef is returned.'); |
62 |
|
63 |
# undef, undef, undef => 2 |
64 |
ok( |
65 |
Koha::CirculationRule->new( |
66 |
{ |
67 |
branchcode => undef, |
68 |
categorycode => undef, |
69 |
itemtype => undef, |
70 |
rule_name => 'fine', |
71 |
rule_value => 2, |
72 |
} |
73 |
)->store, |
74 |
'Given I added an issuing rule branchcode => undef,' |
75 |
.' categorycode => undef, itemtype => undef,'); |
76 |
$rule = Koha::CirculationRules->get_effective_rule({ |
77 |
branchcode => undef, |
78 |
categorycode => undef, |
79 |
itemtype => undef, |
80 |
rule_name => 'fine', |
81 |
}); |
82 |
_is_row_match( |
83 |
$rule, |
84 |
{ |
85 |
branchcode => undef, |
86 |
categorycode => undef, |
87 |
itemtype => undef, |
88 |
rule_name => 'fine', |
89 |
rule_value => 2, |
90 |
}, |
91 |
'When I attempt to get effective' |
92 |
.' issuing rule by providing undefined values, then the above one is' |
93 |
.' returned.' |
94 |
); |
95 |
}; |
96 |
|
97 |
subtest 'Performance' => sub { |
98 |
plan tests => 4; |
99 |
|
100 |
my $worst_case = timethis(500, |
101 |
sub { Koha::CirculationRules->get_effective_rule({ |
102 |
branchcode => 'nonexistent', |
103 |
categorycode => 'nonexistent', |
104 |
itemtype => 'nonexistent', |
105 |
rule_name => 'nonexistent', |
106 |
}); |
107 |
} |
108 |
); |
109 |
my $mid_case = timethis(500, |
110 |
sub { Koha::CirculationRules->get_effective_rule({ |
111 |
branchcode => $branchcode, |
112 |
categorycode => 'nonexistent', |
113 |
itemtype => 'nonexistent', |
114 |
rule_name => 'nonexistent', |
115 |
}); |
116 |
} |
117 |
); |
118 |
my $sec_best_case = timethis(500, |
119 |
sub { Koha::CirculationRules->get_effective_rule({ |
120 |
branchcode => $branchcode, |
121 |
categorycode => $categorycode, |
122 |
itemtype => 'nonexistent', |
123 |
rule_name => 'nonexistent', |
124 |
}); |
125 |
} |
126 |
); |
127 |
my $best_case = timethis(500, |
128 |
sub { Koha::CirculationRules->get_effective_rule({ |
129 |
branchcode => $branchcode, |
130 |
categorycode => $categorycode, |
131 |
itemtype => $itemtype, |
132 |
rule_name => 'nonexistent', |
133 |
}); |
134 |
} |
135 |
); |
136 |
ok($worst_case, 'In worst case, get_effective_issuing_rule finds matching' |
137 |
.' rule '.sprintf('%.2f', $worst_case->iters/$worst_case->cpu_a) |
138 |
.' times per second.'); |
139 |
ok($mid_case, 'In mid case, get_effective_issuing_rule finds matching' |
140 |
.' rule '.sprintf('%.2f', $mid_case->iters/$mid_case->cpu_a) |
141 |
.' times per second.'); |
142 |
ok($sec_best_case, 'In second best case, get_effective_issuing_rule finds matching' |
143 |
.' rule '.sprintf('%.2f', $sec_best_case->iters/$sec_best_case->cpu_a) |
144 |
.' times per second.'); |
145 |
ok($best_case, 'In best case, get_effective_issuing_rule finds matching' |
146 |
.' rule '.sprintf('%.2f', $best_case->iters/$best_case->cpu_a) |
147 |
.' times per second.'); |
148 |
}; |
149 |
|
150 |
$schema->storage->txn_rollback; |
151 |
|
152 |
}; |
153 |
|
154 |
subtest 'set_rule' => sub { |
155 |
plan tests => 3; |
156 |
|
157 |
$schema->storage->txn_begin; |
158 |
|
159 |
my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'}; |
160 |
my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'}; |
161 |
my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'}; |
162 |
|
163 |
subtest 'Correct call' => sub { |
164 |
plan tests => 4; |
165 |
|
166 |
Koha::CirculationRules->delete; |
167 |
|
168 |
lives_ok( sub { |
169 |
Koha::CirculationRules->set_rule( { |
170 |
branchcode => $branchcode, |
171 |
rule_name => 'lostreturn', |
172 |
rule_value => '', |
173 |
} ); |
174 |
}, 'setting lostreturn with branch' ); |
175 |
|
176 |
lives_ok( sub { |
177 |
Koha::CirculationRules->set_rule( { |
178 |
branchcode => $branchcode, |
179 |
categorycode => $categorycode, |
180 |
rule_name => 'patron_maxissueqty', |
181 |
rule_value => '', |
182 |
} ); |
183 |
}, 'setting patron_maxissueqty with branch/category succeeds' ); |
184 |
|
185 |
lives_ok( sub { |
186 |
Koha::CirculationRules->set_rule( { |
187 |
branchcode => $branchcode, |
188 |
itemtype => $itemtype, |
189 |
rule_name => 'holdallowed', |
190 |
rule_value => '', |
191 |
} ); |
192 |
}, 'setting holdallowed with branch/itemtype succeeds' ); |
193 |
|
194 |
lives_ok( sub { |
195 |
Koha::CirculationRules->set_rule( { |
196 |
branchcode => $branchcode, |
197 |
categorycode => $categorycode, |
198 |
itemtype => $itemtype, |
199 |
rule_name => 'fine', |
200 |
rule_value => '', |
201 |
} ); |
202 |
}, 'setting fine with branch/category/itemtype succeeds' ); |
203 |
}; |
204 |
|
205 |
subtest 'Call with missing params' => sub { |
206 |
plan tests => 4; |
207 |
|
208 |
Koha::CirculationRules->delete; |
209 |
|
210 |
throws_ok( sub { |
211 |
Koha::CirculationRules->set_rule( { |
212 |
rule_name => 'lostreturn', |
213 |
rule_value => '', |
214 |
} ); |
215 |
}, qr/branchcode/, 'setting lostreturn without branch fails' ); |
216 |
|
217 |
throws_ok( sub { |
218 |
Koha::CirculationRules->set_rule( { |
219 |
branchcode => $branchcode, |
220 |
rule_name => 'patron_maxissueqty', |
221 |
rule_value => '', |
222 |
} ); |
223 |
}, qr/categorycode/, 'setting patron_maxissueqty without categorycode fails' ); |
224 |
|
225 |
throws_ok( sub { |
226 |
Koha::CirculationRules->set_rule( { |
227 |
branchcode => $branchcode, |
228 |
rule_name => 'holdallowed', |
229 |
rule_value => '', |
230 |
} ); |
231 |
}, qr/itemtype/, 'setting holdallowed without itemtype fails' ); |
232 |
|
233 |
throws_ok( sub { |
234 |
Koha::CirculationRules->set_rule( { |
235 |
branchcode => $branchcode, |
236 |
categorycode => $categorycode, |
237 |
rule_name => 'fine', |
238 |
rule_value => '', |
239 |
} ); |
240 |
}, qr/itemtype/, 'setting fine without itemtype fails' ); |
241 |
}; |
242 |
|
243 |
subtest 'Call with extra params' => sub { |
244 |
plan tests => 3; |
245 |
|
246 |
Koha::CirculationRules->delete; |
247 |
|
248 |
throws_ok( sub { |
249 |
Koha::CirculationRules->set_rule( { |
250 |
branchcode => $branchcode, |
251 |
categorycode => $categorycode, |
252 |
rule_name => 'lostreturn', |
253 |
rule_value => '', |
254 |
} ); |
255 |
}, qr/categorycode/, 'setting lostreturn with categorycode fails' ); |
256 |
|
257 |
throws_ok( sub { |
258 |
Koha::CirculationRules->set_rule( { |
259 |
branchcode => $branchcode, |
260 |
categorycode => $categorycode, |
261 |
itemtype => $itemtype, |
262 |
rule_name => 'patron_maxissueqty', |
263 |
rule_value => '', |
264 |
} ); |
265 |
}, qr/itemtype/, 'setting patron_maxissueqty with itemtype fails' ); |
266 |
|
267 |
throws_ok( sub { |
268 |
Koha::CirculationRules->set_rule( { |
269 |
branchcode => $branchcode, |
270 |
rule_name => 'holdallowed', |
271 |
categorycode => $categorycode, |
272 |
itemtype => $itemtype, |
273 |
rule_value => '', |
274 |
} ); |
275 |
}, qr/categorycode/, 'setting holdallowed with categorycode fails' ); |
276 |
}; |
277 |
|
278 |
$schema->storage->txn_rollback; |
279 |
}; |
280 |
|
281 |
subtest 'clone' => sub { |
282 |
plan tests => 2; |
283 |
|
284 |
$schema->storage->txn_begin; |
285 |
|
286 |
my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'}; |
287 |
my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'}; |
288 |
my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'}; |
289 |
|
290 |
subtest 'Clone multiple rules' => sub { |
291 |
plan tests => 4; |
292 |
|
293 |
Koha::CirculationRules->delete; |
294 |
|
295 |
Koha::CirculationRule->new({ |
296 |
branchcode => undef, |
297 |
categorycode => $categorycode, |
298 |
itemtype => $itemtype, |
299 |
rule_name => 'fine', |
300 |
rule_value => 5, |
301 |
})->store; |
302 |
|
303 |
Koha::CirculationRule->new({ |
304 |
branchcode => undef, |
305 |
categorycode => $categorycode, |
306 |
itemtype => $itemtype, |
307 |
rule_name => 'lengthunit', |
308 |
rule_value => 'days', |
309 |
})->store; |
310 |
|
311 |
Koha::CirculationRules->search({ branchcode => undef })->clone($branchcode); |
312 |
|
313 |
my $rule_fine = Koha::CirculationRules->get_effective_rule({ |
314 |
branchcode => $branchcode, |
315 |
categorycode => $categorycode, |
316 |
itemtype => $itemtype, |
317 |
rule_name => 'fine', |
318 |
}); |
319 |
my $rule_lengthunit = Koha::CirculationRules->get_effective_rule({ |
320 |
branchcode => $branchcode, |
321 |
categorycode => $categorycode, |
322 |
itemtype => $itemtype, |
323 |
rule_name => 'lengthunit', |
324 |
}); |
325 |
|
326 |
_is_row_match( |
327 |
$rule_fine, |
328 |
{ |
329 |
branchcode => $branchcode, |
330 |
categorycode => $categorycode, |
331 |
itemtype => $itemtype, |
332 |
rule_name => 'fine', |
333 |
rule_value => 5, |
334 |
}, |
335 |
'When I attempt to get cloned fine rule,' |
336 |
.' then the above one is returned.' |
337 |
); |
338 |
_is_row_match( |
339 |
$rule_lengthunit, |
340 |
{ |
341 |
branchcode => $branchcode, |
342 |
categorycode => $categorycode, |
343 |
itemtype => $itemtype, |
344 |
rule_name => 'lengthunit', |
345 |
rule_value => 'days', |
346 |
}, |
347 |
'When I attempt to get cloned lengthunit rule,' |
348 |
.' then the above one is returned.' |
349 |
); |
350 |
|
351 |
}; |
352 |
|
353 |
subtest 'Clone one rule' => sub { |
354 |
plan tests => 2; |
355 |
|
356 |
Koha::CirculationRules->delete; |
357 |
|
358 |
Koha::CirculationRule->new({ |
359 |
branchcode => undef, |
360 |
categorycode => $categorycode, |
361 |
itemtype => $itemtype, |
362 |
rule_name => 'fine', |
363 |
rule_value => 5, |
364 |
})->store; |
365 |
|
366 |
my $rule = Koha::CirculationRules->search({ branchcode => undef })->next; |
367 |
$rule->clone($branchcode); |
368 |
|
369 |
my $cloned_rule = Koha::CirculationRules->get_effective_rule({ |
370 |
branchcode => $branchcode, |
371 |
categorycode => $categorycode, |
372 |
itemtype => $itemtype, |
373 |
rule_name => 'fine', |
374 |
}); |
375 |
|
376 |
_is_row_match( |
377 |
$cloned_rule, |
378 |
{ |
379 |
branchcode => $branchcode, |
380 |
categorycode => $categorycode, |
381 |
itemtype => $itemtype, |
382 |
rule_name => 'fine', |
383 |
rule_value => '5', |
384 |
}, |
385 |
'When I attempt to get cloned fine rule,' |
386 |
.' then the above one is returned.' |
387 |
); |
388 |
|
389 |
}; |
390 |
|
391 |
$schema->storage->txn_rollback; |
392 |
}; |
393 |
|
35 |
subtest 'set_rule + get_effective_rule' => sub { |
394 |
subtest 'set_rule + get_effective_rule' => sub { |
36 |
plan tests => 9; |
395 |
plan tests => 9; |
37 |
|
396 |
|
Lines 128-134
subtest 'set_rule + get_effective_rule' => sub {
Link Here
|
128 |
|
487 |
|
129 |
|
488 |
|
130 |
subtest 'test rule matching with different combinations of rule scopes' => sub { |
489 |
subtest 'test rule matching with different combinations of rule scopes' => sub { |
131 |
my ( $tests, $order ) = prepare_tests_for_rule_scope_combinations( |
490 |
my ( $tests, $order ) = _prepare_tests_for_rule_scope_combinations( |
132 |
{ |
491 |
{ |
133 |
branchcode => $branchcode, |
492 |
branchcode => $branchcode, |
134 |
categorycode => $categorycode, |
493 |
categorycode => $categorycode, |
Lines 442-448
subtest 'get_lostreturn_policy() tests' => sub {
Link Here
|
442 |
$schema->storage->txn_rollback; |
801 |
$schema->storage->txn_rollback; |
443 |
}; |
802 |
}; |
444 |
|
803 |
|
445 |
sub prepare_tests_for_rule_scope_combinations { |
804 |
sub _is_row_match { |
|
|
805 |
my ( $rule, $expected, $message ) = @_; |
806 |
|
807 |
ok( $rule, $message ) ? |
808 |
cmp_methods( $rule, [ %$expected ], $message ) : |
809 |
fail( $message ); |
810 |
} |
811 |
|
812 |
sub _prepare_tests_for_rule_scope_combinations { |
446 |
my ( $scope, $rule_name ) = @_; |
813 |
my ( $scope, $rule_name ) = @_; |
447 |
|
814 |
|
448 |
# Here we create a combinations of 1s and 0s the following way |
815 |
# Here we create a combinations of 1s and 0s the following way |