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