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 'Get effective issuing rule in correct order' => sub {
49 Koha::IssuingRules->delete;
50 ok(!Koha::IssuingRules->search->count, 'There are no issuing rules.');
51 $rule = Koha::IssuingRules->get_effective_issuing_rule({
52 branchcode => $branchcode,
53 categorycode => $categorycode,
54 itemtype => $itemtype,
56 is($rule, undef, 'When I attempt to get effective issuing rule, then undef'
59 ok(Koha::IssuingRule->new({
63 })->store, 'Given I added an issuing rule branchcode => *, categorycode => *, itemtype => *,');
64 $rule = Koha::IssuingRules->get_effective_issuing_rule({
65 branchcode => $branchcode,
66 categorycode => $categorycode,
67 itemtype => $itemtype,
69 ok(_row_match($rule, '*', '*', '*'), 'When I attempt to get effective issuing rule,'
70 .' then the above one is returned.');
72 ok(Koha::IssuingRule->new({
75 itemtype => $itemtype,
76 })->store, "Given I added an issuing rule branchcode => *, categorycode => *, itemtype => $itemtype,");
77 $rule = Koha::IssuingRules->get_effective_issuing_rule({
78 branchcode => $branchcode,
79 categorycode => $categorycode,
80 itemtype => $itemtype,
82 ok(_row_match($rule, '*', '*', $itemtype), 'When I attempt to get effective issuing rule,'
83 .' then the above one is returned.');
85 ok(Koha::IssuingRule->new({
87 categorycode => $categorycode,
89 })->store, "Given I added an issuing rule branchcode => *, categorycode => $categorycode, itemtype => *,");
90 $rule = Koha::IssuingRules->get_effective_issuing_rule({
91 branchcode => $branchcode,
92 categorycode => $categorycode,
93 itemtype => $itemtype,
95 ok(_row_match($rule, '*', $categorycode, '*'), 'When I attempt to get effective issuing rule,'
96 .' then the above one is returned.');
98 ok(Koha::IssuingRule->new({
100 categorycode => $categorycode,
101 itemtype => $itemtype,
102 })->store, "Given I added an issuing rule branchcode => *, categorycode => $categorycode, itemtype => $itemtype,");
103 $rule = Koha::IssuingRules->get_effective_issuing_rule({
104 branchcode => $branchcode,
105 categorycode => $categorycode,
106 itemtype => $itemtype,
108 ok(_row_match($rule, '*', $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
109 .' then the above one is returned.');
111 ok(Koha::IssuingRule->new({
112 branchcode => $branchcode,
115 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => '*', itemtype => '*',");
116 $rule = Koha::IssuingRules->get_effective_issuing_rule({
117 branchcode => $branchcode,
118 categorycode => $categorycode,
119 itemtype => $itemtype,
121 ok(_row_match($rule, $branchcode, '*', '*'), 'When I attempt to get effective issuing rule,'
122 .' then the above one is returned.');
124 ok(Koha::IssuingRule->new({
125 branchcode => $branchcode,
127 itemtype => $itemtype,
128 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => '*', itemtype => $itemtype,");
129 $rule = Koha::IssuingRules->get_effective_issuing_rule({
130 branchcode => $branchcode,
131 categorycode => $categorycode,
132 itemtype => $itemtype,
134 ok(_row_match($rule, $branchcode, '*', $itemtype), 'When I attempt to get effective issuing rule,'
135 .' then the above one is returned.');
137 ok(Koha::IssuingRule->new({
138 branchcode => $branchcode,
139 categorycode => $categorycode,
141 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => '*',");
142 $rule = Koha::IssuingRules->get_effective_issuing_rule({
143 branchcode => $branchcode,
144 categorycode => $categorycode,
145 itemtype => $itemtype,
147 ok(_row_match($rule, $branchcode, $categorycode, '*'), 'When I attempt to get effective issuing rule,'
148 .' then the above one is returned.');
150 ok(Koha::IssuingRule->new({
151 branchcode => $branchcode,
152 categorycode => $categorycode,
153 itemtype => $itemtype,
154 })->store, "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => $itemtype,");
155 $rule = Koha::IssuingRules->get_effective_issuing_rule({
156 branchcode => $branchcode,
157 categorycode => $categorycode,
158 itemtype => $itemtype,
160 ok(_row_match($rule, $branchcode, $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
161 .' then the above one is returned.');
164 subtest 'Performance' => sub {
167 my $worst_case = timethis(500,
168 sub { Koha::IssuingRules->get_effective_issuing_rule({
169 branchcode => 'nonexistent',
170 categorycode => 'nonexistent',
171 itemtype => 'nonexistent',
175 my $mid_case = timethis(500,
176 sub { Koha::IssuingRules->get_effective_issuing_rule({
177 branchcode => $branchcode,
178 categorycode => 'nonexistent',
179 itemtype => 'nonexistent',
183 my $sec_best_case = timethis(500,
184 sub { Koha::IssuingRules->get_effective_issuing_rule({
185 branchcode => $branchcode,
186 categorycode => $categorycode,
187 itemtype => 'nonexistent',
191 my $best_case = timethis(500,
192 sub { Koha::IssuingRules->get_effective_issuing_rule({
193 branchcode => $branchcode,
194 categorycode => $categorycode,
195 itemtype => $itemtype,
199 ok($worst_case, 'In worst case, get_effective_issuing_rule finds matching'
200 .' rule '.sprintf('%.2f', $worst_case->iters/$worst_case->cpu_a)
201 .' times per second.');
202 ok($mid_case, 'In mid case, get_effective_issuing_rule finds matching'
203 .' rule '.sprintf('%.2f', $mid_case->iters/$mid_case->cpu_a)
204 .' times per second.');
205 ok($sec_best_case, 'In second best case, get_effective_issuing_rule finds matching'
206 .' rule '.sprintf('%.2f', $sec_best_case->iters/$sec_best_case->cpu_a)
207 .' times per second.');
208 ok($best_case, 'In best case, get_effective_issuing_rule finds matching'
209 .' rule '.sprintf('%.2f', $best_case->iters/$best_case->cpu_a)
210 .' times per second.');
215 my ($rule, $branchcode, $categorycode, $itemtype) = @_;
217 return $rule->branchcode eq $branchcode && $rule->categorycode eq $categorycode
218 && $rule->itemtype eq $itemtype;
221 $schema->storage->txn_rollback;