3 # Copyright 2016 Koha-Suomi Oy
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test
::More tests
=> 1;
26 use Koha
::IssuingRules
;
28 use t
::lib
::TestBuilder
;
30 my $schema = Koha
::Database
->new->schema;
31 $schema->storage->txn_begin;
33 my $builder = t
::lib
::TestBuilder
->new;
35 subtest
'get_effective_issuing_rule' => sub {
38 my $patron = $builder->build({ source
=> 'Borrower' });
39 my $item = $builder->build({ source
=> 'Item' });
41 my $categorycode = $patron->{'categorycode'};
42 my $itemtype = $item->{'itype'};
43 my $branchcode = $item->{'homebranch'};
45 subtest
'Call with undefined values' => sub {
49 Koha
::IssuingRules
->delete;
51 is
(Koha
::IssuingRules
->search->count, 0, 'There are no issuing rules.');
52 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
54 categorycode
=> undef,
57 is
($rule, undef, 'When I attempt to get effective issuing rule by'
58 .' providing undefined values, then undef is returned.');
59 ok
(Koha
::IssuingRule
->new({
63 })->store, 'Given I added an issuing rule branchcode => *,'
64 .' categorycode => *, itemtype => *,');
65 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
67 categorycode
=> undef,
70 ok
(_row_match
($rule, '*', '*', '*'), 'When I attempt to get effective'
71 .' issuing rule by providing undefined values, then the above one is'
75 subtest
'Get effective issuing rule in correct order' => sub {
79 Koha
::IssuingRules
->delete;
80 is
(Koha
::IssuingRules
->search->count, 0, 'There are no issuing rules.');
81 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
82 branchcode
=> $branchcode,
83 categorycode
=> $categorycode,
84 itemtype
=> $itemtype,
86 is
($rule, undef, 'When I attempt to get effective issuing rule, then undef'
89 ok
(Koha
::IssuingRule
->new({
93 })->store, 'Given I added an issuing rule branchcode => *, categorycode => *, itemtype => *,');
94 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
95 branchcode
=> $branchcode,
96 categorycode
=> $categorycode,
97 itemtype
=> $itemtype,
99 ok
(_row_match
($rule, '*', '*', '*'), 'When I attempt to get effective issuing rule,'
100 .' then the above one is returned.');
102 ok
(Koha
::IssuingRule
->new({
105 itemtype
=> $itemtype,
106 })->store, "Given I added an issuing rule branchcode => *, categorycode => *, itemtype => $itemtype,");
107 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
108 branchcode
=> $branchcode,
109 categorycode
=> $categorycode,
110 itemtype
=> $itemtype,
112 ok
(_row_match
($rule, '*', '*', $itemtype), 'When I attempt to get effective issuing rule,'
113 .' then the above one is returned.');
115 ok
(Koha
::IssuingRule
->new({
117 categorycode
=> $categorycode,
119 })->store, "Given I added an issuing rule branchcode => *, categorycode => $categorycode, itemtype => *,");
120 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
121 branchcode
=> $branchcode,
122 categorycode
=> $categorycode,
123 itemtype
=> $itemtype,
125 ok
(_row_match
($rule, '*', $categorycode, '*'), 'When I attempt to get effective issuing rule,'
126 .' then the above one is returned.');
128 ok
(Koha
::IssuingRule
->new({
130 categorycode
=> $categorycode,
131 itemtype
=> $itemtype,
132 })->store, "Given I added an issuing rule branchcode => *, categorycode => $categorycode, itemtype => $itemtype,");
133 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
134 branchcode
=> $branchcode,
135 categorycode
=> $categorycode,
136 itemtype
=> $itemtype,
138 ok
(_row_match
($rule, '*', $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
139 .' then the above one is returned.');
141 ok
(Koha
::IssuingRule
->new({
142 branchcode
=> $branchcode,
145 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => '*', itemtype => '*',");
146 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
147 branchcode
=> $branchcode,
148 categorycode
=> $categorycode,
149 itemtype
=> $itemtype,
151 ok
(_row_match
($rule, $branchcode, '*', '*'), 'When I attempt to get effective issuing rule,'
152 .' then the above one is returned.');
154 ok
(Koha
::IssuingRule
->new({
155 branchcode
=> $branchcode,
157 itemtype
=> $itemtype,
158 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => '*', itemtype => $itemtype,");
159 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
160 branchcode
=> $branchcode,
161 categorycode
=> $categorycode,
162 itemtype
=> $itemtype,
164 ok
(_row_match
($rule, $branchcode, '*', $itemtype), 'When I attempt to get effective issuing rule,'
165 .' then the above one is returned.');
167 ok
(Koha
::IssuingRule
->new({
168 branchcode
=> $branchcode,
169 categorycode
=> $categorycode,
171 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => '*',");
172 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
173 branchcode
=> $branchcode,
174 categorycode
=> $categorycode,
175 itemtype
=> $itemtype,
177 ok
(_row_match
($rule, $branchcode, $categorycode, '*'), 'When I attempt to get effective issuing rule,'
178 .' then the above one is returned.');
180 ok
(Koha
::IssuingRule
->new({
181 branchcode
=> $branchcode,
182 categorycode
=> $categorycode,
183 itemtype
=> $itemtype,
184 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => $itemtype,");
185 $rule = Koha
::IssuingRules
->get_effective_issuing_rule({
186 branchcode
=> $branchcode,
187 categorycode
=> $categorycode,
188 itemtype
=> $itemtype,
190 ok
(_row_match
($rule, $branchcode, $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
191 .' then the above one is returned.');
194 subtest
'Performance' => sub {
197 my $worst_case = timethis
(500,
198 sub { Koha
::IssuingRules
->get_effective_issuing_rule({
199 branchcode
=> 'nonexistent',
200 categorycode
=> 'nonexistent',
201 itemtype
=> 'nonexistent',
205 my $mid_case = timethis
(500,
206 sub { Koha
::IssuingRules
->get_effective_issuing_rule({
207 branchcode
=> $branchcode,
208 categorycode
=> 'nonexistent',
209 itemtype
=> 'nonexistent',
213 my $sec_best_case = timethis
(500,
214 sub { Koha
::IssuingRules
->get_effective_issuing_rule({
215 branchcode
=> $branchcode,
216 categorycode
=> $categorycode,
217 itemtype
=> 'nonexistent',
221 my $best_case = timethis
(500,
222 sub { Koha
::IssuingRules
->get_effective_issuing_rule({
223 branchcode
=> $branchcode,
224 categorycode
=> $categorycode,
225 itemtype
=> $itemtype,
229 ok
($worst_case, 'In worst case, get_effective_issuing_rule finds matching'
230 .' rule '.sprintf('%.2f', $worst_case->iters/$worst_case->cpu_a)
231 .' times per second.');
232 ok
($mid_case, 'In mid case, get_effective_issuing_rule finds matching'
233 .' rule '.sprintf('%.2f', $mid_case->iters/$mid_case->cpu_a)
234 .' times per second.');
235 ok
($sec_best_case, 'In second best case, get_effective_issuing_rule finds matching'
236 .' rule '.sprintf('%.2f', $sec_best_case->iters/$sec_best_case->cpu_a)
237 .' times per second.');
238 ok
($best_case, 'In best case, get_effective_issuing_rule finds matching'
239 .' rule '.sprintf('%.2f', $best_case->iters/$best_case->cpu_a)
240 .' times per second.');
245 my ($rule, $branchcode, $categorycode, $itemtype) = @_;
247 return $rule->branchcode eq $branchcode && $rule->categorycode eq $categorycode
248 && $rule->itemtype eq $itemtype;
251 $schema->storage->txn_rollback;