Bug 20311: Prevent get_age tests to fail on Feb 28th
[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 => 3;
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 'Call with undefined values' => sub {
46         plan tests => 4;
47
48         my $rule;
49         Koha::IssuingRules->delete;
50
51         is(Koha::IssuingRules->search->count, 0, 'There are no issuing rules.');
52         $rule = Koha::IssuingRules->get_effective_issuing_rule({
53             branchcode   => undef,
54             categorycode => undef,
55             itemtype     => undef,
56         });
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({
60             branchcode => '*',
61             categorycode => '*',
62             itemtype => '*',
63         })->store, 'Given I added an issuing rule branchcode => *,'
64            .' categorycode => *, itemtype => *,');
65         $rule = Koha::IssuingRules->get_effective_issuing_rule({
66             branchcode   => undef,
67             categorycode => undef,
68             itemtype     => undef,
69         });
70         ok(_row_match($rule, '*', '*', '*'), 'When I attempt to get effective'
71            .' issuing rule by providing undefined values, then the above one is'
72            .' returned.');
73     };
74
75     subtest 'Get effective issuing rule in correct order' => sub {
76         plan tests => 18;
77
78         my $rule;
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,
85         });
86         is($rule, undef, 'When I attempt to get effective issuing rule, then undef'
87                         .' is returned.');
88
89         ok(Koha::IssuingRule->new({
90             branchcode => '*',
91             categorycode => '*',
92             itemtype => '*',
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,
98         });
99         ok(_row_match($rule, '*', '*', '*'), 'When I attempt to get effective issuing rule,'
100            .' then the above one is returned.');
101
102         ok(Koha::IssuingRule->new({
103             branchcode => '*',
104             categorycode => '*',
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,
111         });
112         ok(_row_match($rule, '*', '*', $itemtype), 'When I attempt to get effective issuing rule,'
113            .' then the above one is returned.');
114
115         ok(Koha::IssuingRule->new({
116             branchcode => '*',
117             categorycode => $categorycode,
118             itemtype => '*',
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,
124         });
125         ok(_row_match($rule, '*', $categorycode, '*'), 'When I attempt to get effective issuing rule,'
126            .' then the above one is returned.');
127
128         ok(Koha::IssuingRule->new({
129             branchcode => '*',
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,
137         });
138         ok(_row_match($rule, '*', $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
139            .' then the above one is returned.');
140
141         ok(Koha::IssuingRule->new({
142             branchcode => $branchcode,
143             categorycode => '*',
144             itemtype => '*',
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,
150         });
151         ok(_row_match($rule, $branchcode, '*', '*'), 'When I attempt to get effective issuing rule,'
152            .' then the above one is returned.');
153
154         ok(Koha::IssuingRule->new({
155             branchcode => $branchcode,
156             categorycode => '*',
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,
163         });
164         ok(_row_match($rule, $branchcode, '*', $itemtype), 'When I attempt to get effective issuing rule,'
165            .' then the above one is returned.');
166
167         ok(Koha::IssuingRule->new({
168             branchcode => $branchcode,
169             categorycode => $categorycode,
170             itemtype => '*',
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,
176         });
177         ok(_row_match($rule, $branchcode, $categorycode, '*'), 'When I attempt to get effective issuing rule,'
178            .' then the above one is returned.');
179
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,
189         });
190         ok(_row_match($rule, $branchcode, $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
191            .' then the above one is returned.');
192     };
193
194     subtest 'Performance' => sub {
195         plan tests => 4;
196
197         my $worst_case = timethis(500,
198                     sub { Koha::IssuingRules->get_effective_issuing_rule({
199                             branchcode   => 'nonexistent',
200                             categorycode => 'nonexistent',
201                             itemtype     => 'nonexistent',
202                         });
203                     }
204                 );
205         my $mid_case = timethis(500,
206                     sub { Koha::IssuingRules->get_effective_issuing_rule({
207                             branchcode   => $branchcode,
208                             categorycode => 'nonexistent',
209                             itemtype     => 'nonexistent',
210                         });
211                     }
212                 );
213         my $sec_best_case = timethis(500,
214                     sub { Koha::IssuingRules->get_effective_issuing_rule({
215                             branchcode   => $branchcode,
216                             categorycode => $categorycode,
217                             itemtype     => 'nonexistent',
218                         });
219                     }
220                 );
221         my $best_case = timethis(500,
222                     sub { Koha::IssuingRules->get_effective_issuing_rule({
223                             branchcode   => $branchcode,
224                             categorycode => $categorycode,
225                             itemtype     => $itemtype,
226                         });
227                     }
228                 );
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.');
241     };
242 };
243
244 sub _row_match {
245     my ($rule, $branchcode, $categorycode, $itemtype) = @_;
246
247     return $rule->branchcode eq $branchcode && $rule->categorycode eq $categorycode
248             && $rule->itemtype eq $itemtype;
249 }
250
251 $schema->storage->txn_rollback;
252