Bug 17783: Add Koha::Objects->single
[koha.git] / t / db_dependent / Koha / IssuingRules.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha-Suomi Oy
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 1;
23
24 use Benchmark;
25
26 use Koha::IssuingRules;
27
28 use t::lib::TestBuilder;
29
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 my $builder      = t::lib::TestBuilder->new;
34
35 subtest 'get_effective_issuing_rule' => sub {
36     plan tests => 2;
37
38     my $patron       = $builder->build({ source => 'Borrower' });
39     my $item     = $builder->build({ source => 'Item' });
40
41     my $categorycode = $patron->{'categorycode'};
42     my $itemtype     = $item->{'itype'};
43     my $branchcode   = $item->{'homebranch'};
44
45     subtest 'Get effective issuing rule in correct order' => sub {
46         plan tests => 18;
47
48         my $rule;
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,
55         });
56         is($rule, undef, 'When I attempt to get effective issuing rule, then undef'
57                         .' is returned.');
58
59         ok(Koha::IssuingRule->new({
60             branchcode => '*',
61             categorycode => '*',
62             itemtype => '*',
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,
68         });
69         ok(_row_match($rule, '*', '*', '*'), 'When I attempt to get effective issuing rule,'
70            .' then the above one is returned.');
71
72         ok(Koha::IssuingRule->new({
73             branchcode => '*',
74             categorycode => '*',
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,
81         });
82         ok(_row_match($rule, '*', '*', $itemtype), 'When I attempt to get effective issuing rule,'
83            .' then the above one is returned.');
84
85         ok(Koha::IssuingRule->new({
86             branchcode => '*',
87             categorycode => $categorycode,
88             itemtype => '*',
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,
94         });
95         ok(_row_match($rule, '*', $categorycode, '*'), 'When I attempt to get effective issuing rule,'
96            .' then the above one is returned.');
97
98         ok(Koha::IssuingRule->new({
99             branchcode => '*',
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,
107         });
108         ok(_row_match($rule, '*', $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
109            .' then the above one is returned.');
110
111         ok(Koha::IssuingRule->new({
112             branchcode => $branchcode,
113             categorycode => '*',
114             itemtype => '*',
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,
120         });
121         ok(_row_match($rule, $branchcode, '*', '*'), 'When I attempt to get effective issuing rule,'
122            .' then the above one is returned.');
123
124         ok(Koha::IssuingRule->new({
125             branchcode => $branchcode,
126             categorycode => '*',
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,
133         });
134         ok(_row_match($rule, $branchcode, '*', $itemtype), 'When I attempt to get effective issuing rule,'
135            .' then the above one is returned.');
136
137         ok(Koha::IssuingRule->new({
138             branchcode => $branchcode,
139             categorycode => $categorycode,
140             itemtype => '*',
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,
146         });
147         ok(_row_match($rule, $branchcode, $categorycode, '*'), 'When I attempt to get effective issuing rule,'
148            .' then the above one is returned.');
149
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,
159         });
160         ok(_row_match($rule, $branchcode, $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
161            .' then the above one is returned.');
162     };
163
164     subtest 'Performance' => sub {
165         plan tests => 4;
166
167         my $worst_case = timethis(500,
168                     sub { Koha::IssuingRules->get_effective_issuing_rule({
169                             branchcode   => 'nonexistent',
170                             categorycode => 'nonexistent',
171                             itemtype     => 'nonexistent',
172                         });
173                     }
174                 );
175         my $mid_case = timethis(500,
176                     sub { Koha::IssuingRules->get_effective_issuing_rule({
177                             branchcode   => $branchcode,
178                             categorycode => 'nonexistent',
179                             itemtype     => 'nonexistent',
180                         });
181                     }
182                 );
183         my $sec_best_case = timethis(500,
184                     sub { Koha::IssuingRules->get_effective_issuing_rule({
185                             branchcode   => $branchcode,
186                             categorycode => $categorycode,
187                             itemtype     => 'nonexistent',
188                         });
189                     }
190                 );
191         my $best_case = timethis(500,
192                     sub { Koha::IssuingRules->get_effective_issuing_rule({
193                             branchcode   => $branchcode,
194                             categorycode => $categorycode,
195                             itemtype     => $itemtype,
196                         });
197                     }
198                 );
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.');
211     };
212 };
213
214 sub _row_match {
215     my ($rule, $branchcode, $categorycode, $itemtype) = @_;
216
217     return $rule->branchcode eq $branchcode && $rule->categorycode eq $categorycode
218             && $rule->itemtype eq $itemtype;
219 }
220
221 $schema->storage->txn_rollback;
222
223 1;