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;
50 ok(!Koha::IssuingRules->search->count, 'There are no issuing rules.');
51 $rule = Koha::IssuingRules->get_effective_issuing_rule({
53 categorycode => undef,
56 is($rule, undef, 'When I attempt to get effective issuing rule by'
57 .' providing undefined values, then undef is returned.');
58 ok(Koha::IssuingRule->new({
62 })->store, 'Given I added an issuing rule branchcode => *,'
63 .' categorycode => *, itemtype => *,');
64 $rule = Koha::IssuingRules->get_effective_issuing_rule({
66 categorycode => undef,
69 ok(_row_match($rule, '*', '*', '*'), 'When I attempt to get effective'
70 .' issuing rule by providing undefined values, then the above one is'
74 subtest 'Get effective issuing rule in correct order' => sub {
78 Koha::IssuingRules->delete;
79 ok(!Koha::IssuingRules->search->count, 'There are no issuing rules.');
80 $rule = Koha::IssuingRules->get_effective_issuing_rule({
81 branchcode => $branchcode,
82 categorycode => $categorycode,
83 itemtype => $itemtype,
85 is($rule, undef, 'When I attempt to get effective issuing rule, then undef'
88 ok(Koha::IssuingRule->new({
92 })->store, 'Given I added an issuing rule branchcode => *, categorycode => *, itemtype => *,');
93 $rule = Koha::IssuingRules->get_effective_issuing_rule({
94 branchcode => $branchcode,
95 categorycode => $categorycode,
96 itemtype => $itemtype,
98 ok(_row_match($rule, '*', '*', '*'), 'When I attempt to get effective issuing rule,'
99 .' then the above one is returned.');
101 ok(Koha::IssuingRule->new({
104 itemtype => $itemtype,
105 })->store, "Given I added an issuing rule branchcode => *, categorycode => *, itemtype => $itemtype,");
106 $rule = Koha::IssuingRules->get_effective_issuing_rule({
107 branchcode => $branchcode,
108 categorycode => $categorycode,
109 itemtype => $itemtype,
111 ok(_row_match($rule, '*', '*', $itemtype), 'When I attempt to get effective issuing rule,'
112 .' then the above one is returned.');
114 ok(Koha::IssuingRule->new({
116 categorycode => $categorycode,
118 })->store, "Given I added an issuing rule branchcode => *, categorycode => $categorycode, itemtype => *,");
119 $rule = Koha::IssuingRules->get_effective_issuing_rule({
120 branchcode => $branchcode,
121 categorycode => $categorycode,
122 itemtype => $itemtype,
124 ok(_row_match($rule, '*', $categorycode, '*'), 'When I attempt to get effective issuing rule,'
125 .' then the above one is returned.');
127 ok(Koha::IssuingRule->new({
129 categorycode => $categorycode,
130 itemtype => $itemtype,
131 })->store, "Given I added an issuing rule branchcode => *, categorycode => $categorycode, itemtype => $itemtype,");
132 $rule = Koha::IssuingRules->get_effective_issuing_rule({
133 branchcode => $branchcode,
134 categorycode => $categorycode,
135 itemtype => $itemtype,
137 ok(_row_match($rule, '*', $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
138 .' then the above one is returned.');
140 ok(Koha::IssuingRule->new({
141 branchcode => $branchcode,
144 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => '*', itemtype => '*',");
145 $rule = Koha::IssuingRules->get_effective_issuing_rule({
146 branchcode => $branchcode,
147 categorycode => $categorycode,
148 itemtype => $itemtype,
150 ok(_row_match($rule, $branchcode, '*', '*'), 'When I attempt to get effective issuing rule,'
151 .' then the above one is returned.');
153 ok(Koha::IssuingRule->new({
154 branchcode => $branchcode,
156 itemtype => $itemtype,
157 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => '*', itemtype => $itemtype,");
158 $rule = Koha::IssuingRules->get_effective_issuing_rule({
159 branchcode => $branchcode,
160 categorycode => $categorycode,
161 itemtype => $itemtype,
163 ok(_row_match($rule, $branchcode, '*', $itemtype), 'When I attempt to get effective issuing rule,'
164 .' then the above one is returned.');
166 ok(Koha::IssuingRule->new({
167 branchcode => $branchcode,
168 categorycode => $categorycode,
170 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => '*',");
171 $rule = Koha::IssuingRules->get_effective_issuing_rule({
172 branchcode => $branchcode,
173 categorycode => $categorycode,
174 itemtype => $itemtype,
176 ok(_row_match($rule, $branchcode, $categorycode, '*'), 'When I attempt to get effective issuing rule,'
177 .' then the above one is returned.');
179 ok(Koha::IssuingRule->new({
180 branchcode => $branchcode,
181 categorycode => $categorycode,
182 itemtype => $itemtype,
183 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => $itemtype,");
184 $rule = Koha::IssuingRules->get_effective_issuing_rule({
185 branchcode => $branchcode,
186 categorycode => $categorycode,
187 itemtype => $itemtype,
189 ok(_row_match($rule, $branchcode, $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
190 .' then the above one is returned.');
193 subtest 'Performance' => sub {
196 my $worst_case = timethis(500,
197 sub { Koha::IssuingRules->get_effective_issuing_rule({
198 branchcode => 'nonexistent',
199 categorycode => 'nonexistent',
200 itemtype => 'nonexistent',
204 my $mid_case = timethis(500,
205 sub { Koha::IssuingRules->get_effective_issuing_rule({
206 branchcode => $branchcode,
207 categorycode => 'nonexistent',
208 itemtype => 'nonexistent',
212 my $sec_best_case = timethis(500,
213 sub { Koha::IssuingRules->get_effective_issuing_rule({
214 branchcode => $branchcode,
215 categorycode => $categorycode,
216 itemtype => 'nonexistent',
220 my $best_case = timethis(500,
221 sub { Koha::IssuingRules->get_effective_issuing_rule({
222 branchcode => $branchcode,
223 categorycode => $categorycode,
224 itemtype => $itemtype,
228 ok($worst_case, 'In worst case, get_effective_issuing_rule finds matching'
229 .' rule '.sprintf('%.2f', $worst_case->iters/$worst_case->cpu_a)
230 .' times per second.');
231 ok($mid_case, 'In mid case, get_effective_issuing_rule finds matching'
232 .' rule '.sprintf('%.2f', $mid_case->iters/$mid_case->cpu_a)
233 .' times per second.');
234 ok($sec_best_case, 'In second best case, get_effective_issuing_rule finds matching'
235 .' rule '.sprintf('%.2f', $sec_best_case->iters/$sec_best_case->cpu_a)
236 .' times per second.');
237 ok($best_case, 'In best case, get_effective_issuing_rule finds matching'
238 .' rule '.sprintf('%.2f', $best_case->iters/$best_case->cpu_a)
239 .' times per second.');
244 my ($rule, $branchcode, $categorycode, $itemtype) = @_;
246 return $rule->branchcode eq $branchcode && $rule->categorycode eq $categorycode
247 && $rule->itemtype eq $itemtype;
250 $schema->storage->txn_rollback;