Lines 1-126
Link Here
|
1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
2 |
|
2 |
|
3 |
use C4::Context; |
3 |
# This test deals with GetFictiveIssueNumber (from C4::Serials) |
4 |
use Test::More tests => 18; |
4 |
|
5 |
use Modern::Perl; |
5 |
use Modern::Perl; |
|
|
6 |
use Test::More tests => 5; |
7 |
|
8 |
use Koha::Database; |
9 |
use C4::Serials; |
10 |
use C4::Serials::Frequency; |
6 |
|
11 |
|
|
|
12 |
my $schema = Koha::Database->new->schema; |
13 |
$schema->storage->txn_begin; |
7 |
my $dbh = C4::Context->dbh; |
14 |
my $dbh = C4::Context->dbh; |
8 |
$dbh->{RaiseError} = 1; |
|
|
9 |
$dbh->{AutoCommit} = 0; |
10 |
|
15 |
|
11 |
use C4::Serials::Frequency; |
16 |
subtest 'Tests for irregular frequency' => sub { |
12 |
use C4::Serials; |
17 |
plan tests => 2; |
13 |
|
18 |
|
14 |
# TEST CASE - 1 issue per day, no irregularities |
19 |
# Add a frequency |
15 |
my $frequency = { |
20 |
my $freq_irr = AddSubscriptionFrequency({ |
16 |
description => "One issue per day", |
21 |
description => "Irregular", |
17 |
unit => 'day', |
22 |
unit => undef, |
18 |
issuesperunit => 1, |
23 |
}); |
19 |
unitsperissue => 1, |
|
|
20 |
}; |
21 |
|
24 |
|
22 |
my $subscription = { |
25 |
# Test it |
23 |
firstacquidate => '1970-01-01', |
26 |
my $subscription = { |
24 |
irregularity => '', |
27 |
periodicity => $freq_irr, |
25 |
countissuesperunit => 1, |
28 |
firstacquidate => '1972-02-07', |
26 |
}; |
29 |
}; |
27 |
my $issueNumber; |
30 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-12-31'), undef, 'Irregular: should be undef' ); |
28 |
|
31 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-12-31'), undef, 'Irregular: still undef' ); |
29 |
$issueNumber = |
|
|
30 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-01', $frequency ); |
31 |
is( $issueNumber, '1' ); |
32 |
|
33 |
$issueNumber = |
34 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-02', $frequency ); |
35 |
is( $issueNumber, '2' ); |
36 |
$issueNumber = |
37 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-03', $frequency ); |
38 |
is( $issueNumber, '3' ); |
39 |
|
40 |
# TEST CASE - 2 issues per day, no irregularity |
41 |
$frequency = { |
42 |
description => "Two issues per day", |
43 |
unit => 'day', |
44 |
issuesperunit => 2, |
45 |
unitsperissue => 1, |
46 |
}; |
32 |
}; |
47 |
$subscription = { |
33 |
|
48 |
firstacquidate => '1970-01-01', |
34 |
subtest 'Tests for yearly frequencies' => sub { |
49 |
irregularity => '', |
35 |
plan tests => 10; |
50 |
countissuesperunit => 1, |
36 |
|
51 |
}; |
37 |
# First add a few frequencies |
52 |
$issueNumber = |
38 |
my $freq_1i_1y = AddSubscriptionFrequency({ |
53 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-01', $frequency ); |
39 |
description => "1 issue per year", |
54 |
is( $issueNumber, '1' ); |
40 |
unit => 'year', |
55 |
$issueNumber = |
41 |
issuesperunit => 1, |
56 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-02', $frequency ); |
42 |
unitsperissue => 1, |
57 |
is( $issueNumber, '3' ); |
43 |
}); |
58 |
$issueNumber = |
44 |
my $freq_1i_3y = AddSubscriptionFrequency({ |
59 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-03', $frequency ); |
45 |
description => "1 issue per 3 years", |
60 |
is( $issueNumber, '5' ); |
46 |
unit => 'year', |
61 |
|
47 |
issuesperunit => 1, |
62 |
$subscription->{countissuesperunit} = 2; |
48 |
unitsperissue => 3, |
63 |
$issueNumber = |
49 |
}); |
64 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-01', $frequency ); |
50 |
my $freq_5i_1y = AddSubscriptionFrequency({ |
65 |
is( $issueNumber, '2' ); |
51 |
description => "5 issues per year", |
66 |
$issueNumber = |
52 |
unit => 'year', |
67 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-02', $frequency ); |
53 |
issuesperunit => 5, |
68 |
is( $issueNumber, '4' ); |
54 |
unitsperissue => 1, |
69 |
$issueNumber = |
55 |
}); |
70 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-03', $frequency ); |
56 |
my $freq_366i_1y = AddSubscriptionFrequency({ |
71 |
is( $issueNumber, '6' ); |
57 |
description => "366 issue per year", |
72 |
|
58 |
unit => 'year', |
73 |
# TEST CASE - 1 issue every 2 days, no irregularity |
59 |
issuesperunit => 366, |
74 |
$frequency = { |
60 |
unitsperissue => 1, |
75 |
description => "one issue every two days", |
61 |
}); |
76 |
unit => 'day', |
62 |
|
77 |
issuesperunit => 1, |
63 |
# TEST CASE - 1 issue per year |
78 |
unitsperissue => 2, |
64 |
my $subscription = { |
|
|
65 |
periodicity => $freq_1i_1y, |
66 |
firstacquidate => '1972-02-10', |
67 |
countissuesperunit => 1, |
68 |
}; |
69 |
|
70 |
my $frequency = GetSubscriptionFrequency($freq_1i_1y); |
71 |
|
72 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-09', $frequency), 1, 'Feb 9 still 1' ); |
73 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-10', $frequency), 2, 'Feb 10 goes to 2' ); |
74 |
|
75 |
# TEST CASE - 1 issue per 3 years |
76 |
$subscription->{periodicity} = $freq_1i_3y; |
77 |
$subscription->{firstacquidate} = '1972-02-20'; |
78 |
$frequency = GetSubscriptionFrequency($freq_1i_3y); |
79 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-19', $frequency), 1, 'Feb 19, 1975 still 1' ); |
80 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-20', $frequency), 2, 'Feb 20, 1975 goes to 2' ); |
81 |
|
82 |
# TEST CASE - 5 issues per year |
83 |
$subscription->{periodicity} = $freq_5i_1y; |
84 |
$subscription->{firstacquidate} = '1972-02-29'; #leap year |
85 |
$subscription->{countissuesperunit} = 1; |
86 |
$frequency = GetSubscriptionFrequency($freq_5i_1y); |
87 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-11', $frequency), 1, 'May 11 still 1' ); |
88 |
$subscription->{countissuesperunit} = 2; |
89 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-12', $frequency), 2, 'May 12 goes to 2' ); |
90 |
$subscription->{countissuesperunit} = 5; |
91 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27', $frequency), 5, 'Feb 27 should still be 5' ); |
92 |
$subscription->{countissuesperunit} = 1; |
93 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28', $frequency), 6, 'Feb 28 goes to 6' ); |
94 |
|
95 |
# TEST CASE - 366 issues per year (hypothetical example) |
96 |
# Testing prevention of divide by zero |
97 |
$subscription->{periodicity} = $freq_366i_1y; |
98 |
$subscription->{firstacquidate} = '1972-02-29'; #leap year |
99 |
$subscription->{countissuesperunit} = 366; |
100 |
$frequency = GetSubscriptionFrequency($freq_366i_1y); |
101 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27', $frequency), 366, 'Feb 27 still at 366' ); |
102 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28', $frequency), 732, 'Feb 28 goes to 732' ); |
103 |
|
79 |
}; |
104 |
}; |
80 |
$subscription = { |
105 |
|
81 |
firstacquidate => '1970-01-01', |
106 |
subtest 'Tests for monthly frequencies' => sub { |
82 |
irregularity => '', |
107 |
plan tests => 8; |
83 |
countissuesperunit => 1, |
108 |
|
|
|
109 |
# First add a few frequencies |
110 |
my $freq_1i_5m = AddSubscriptionFrequency({ |
111 |
description => "1 issue per 5 months", |
112 |
unit => 'month', |
113 |
issuesperunit => 1, |
114 |
unitsperissue => 5, |
115 |
}); |
116 |
my $freq_4i_1m = AddSubscriptionFrequency({ |
117 |
description => "4 issue per month", |
118 |
unit => 'month', |
119 |
issuesperunit => 4, |
120 |
unitsperissue => 1, |
121 |
}); |
122 |
|
123 |
# TEST CASE - 1 issue per 5 months |
124 |
my $subscription = { |
125 |
periodicity => $freq_1i_5m, |
126 |
firstacquidate => '1972-02-10', |
127 |
countissuesperunit => 1, |
128 |
}; |
129 |
my $frequency = GetSubscriptionFrequency($freq_1i_5m); |
130 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-07-09', $frequency), 1, 'Jul 9 still 1' ); |
131 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-07-10', $frequency), 2, 'Jul 10 goes to 2' ); |
132 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-05-09', $frequency), 3, 'May 9 still 3' ); |
133 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-05-10', $frequency), 4, 'May 10 goes to 4' ); |
134 |
|
135 |
# TEST CASE - 4 issue per 1 months |
136 |
$subscription = { |
137 |
periodicity => $freq_4i_1m, |
138 |
firstacquidate => '1972-02-22', |
139 |
countissuesperunit => 1, |
140 |
}; |
141 |
$frequency = GetSubscriptionFrequency($freq_4i_1m); |
142 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-28', $frequency), 1, 'Feb 28 still 1' ); |
143 |
$subscription->{countissuesperunit} = 2; |
144 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-29', $frequency), 2, 'Feb 29 goes to 2' ); |
145 |
$subscription->{countissuesperunit} = 4; |
146 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-21', $frequency), 4, 'Mar 21 still 4' ); |
147 |
$subscription->{countissuesperunit} = 1; |
148 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-22', $frequency), 5, 'Mar 22 goes to 5' ); |
149 |
|
84 |
}; |
150 |
}; |
85 |
$issueNumber = |
151 |
|
86 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-01', $frequency ); |
152 |
subtest 'Tests for weekly frequencies' => sub { |
87 |
is( $issueNumber, 1 ); |
153 |
plan tests => 4; |
88 |
$issueNumber = |
154 |
|
89 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-02', $frequency ); |
155 |
# First add a few frequencies |
90 |
is( $issueNumber, 1 ); |
156 |
my $freq_1i_7w = AddSubscriptionFrequency({ |
91 |
$issueNumber = |
157 |
description => "1 issue per 7 weeks", |
92 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-03', $frequency ); |
158 |
unit => 'week', |
93 |
is( $issueNumber, 2 ); |
159 |
issuesperunit => 1, |
94 |
$issueNumber = |
160 |
unitsperissue => 7, |
95 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-04', $frequency ); |
161 |
}); |
96 |
is( $issueNumber, 2 ); |
162 |
my $freq_3i_1w = AddSubscriptionFrequency({ |
97 |
$issueNumber = |
163 |
description => "3 issues per week", |
98 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-05', $frequency ); |
164 |
unit => 'week', |
99 |
is( $issueNumber, 3 ); |
165 |
issuesperunit => 3, |
100 |
|
166 |
unitsperissue => 1, |
101 |
# TEST CASE - 1 issue per week, no irregularity |
167 |
}); |
102 |
$frequency = { |
168 |
|
103 |
description => "one issue per week", |
169 |
# TEST CASE - 1 issue per 7 weeks |
104 |
unit => 'week', |
170 |
my $subscription = { |
105 |
issuesperunit => 1, |
171 |
periodicity => $freq_1i_7w, |
106 |
unitsperissue => 1, |
172 |
firstacquidate => '1972-02-10', |
|
|
173 |
countissuesperunit => 1, |
174 |
}; |
175 |
my $frequency = GetSubscriptionFrequency($freq_1i_7w); |
176 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-29', $frequency), 1, 'Mar 29 still 1' ); |
177 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-30', $frequency), 2, 'Mar 30 goes to 2' ); |
178 |
|
179 |
# TEST CASE - 3 issue per 1 week |
180 |
$subscription = { |
181 |
periodicity => $freq_3i_1w, |
182 |
firstacquidate => '1972-02-03', |
183 |
countissuesperunit => 1, |
184 |
}; |
185 |
$subscription->{countissuesperunit} = 3; |
186 |
$frequency = GetSubscriptionFrequency($freq_3i_1w); |
187 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-09', $frequency), 3, 'Feb 9 still 3' ); |
188 |
$subscription->{countissuesperunit} = 1; |
189 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-10', $frequency), 4, 'Feb 10 goes to 4' ); |
107 |
}; |
190 |
}; |
108 |
$subscription = { |
191 |
|
109 |
firstacquidate => '1970-01-01', |
192 |
subtest 'Tests for dayly frequencies' => sub { |
110 |
irregularity => '', |
193 |
plan tests => 4; |
111 |
countissuesperunit => 1, |
194 |
|
|
|
195 |
# First add a few frequencies |
196 |
my $freq_1i_12d = AddSubscriptionFrequency({ |
197 |
description => "1 issue per 12 days", |
198 |
unit => 'day', |
199 |
issuesperunit => 1, |
200 |
unitsperissue => 12, |
201 |
}); |
202 |
my $freq_3i_1d = AddSubscriptionFrequency({ |
203 |
description => "3 issues per day", |
204 |
unit => 'day', |
205 |
issuesperunit => 3, |
206 |
unitsperissue => 1, |
207 |
}); |
208 |
|
209 |
# TEST CASE - 1 issue per 12 days |
210 |
my $subscription = { |
211 |
periodicity => $freq_1i_12d, |
212 |
firstacquidate => '1972-03-16', |
213 |
countissuesperunit => 1, |
214 |
}; |
215 |
my $frequency = GetSubscriptionFrequency($freq_1i_12d); |
216 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-27', $frequency), 1, 'Mar 27 still 1' ); |
217 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-28', $frequency), 2, 'Mar 28 goes to 2' ); |
218 |
|
219 |
# TEST CASE - 3 issue per day |
220 |
$subscription = { |
221 |
periodicity => $freq_3i_1d, |
222 |
firstacquidate => '1972-04-23', |
223 |
countissuesperunit => 1, |
224 |
}; |
225 |
$subscription->{countissuesperunit} = 3; |
226 |
$frequency = GetSubscriptionFrequency($freq_3i_1d); |
227 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-01', $frequency), 27, 'May 1 still 27' ); |
228 |
$subscription->{countissuesperunit} = 1; |
229 |
is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-02', $frequency), 28, 'May 2 goes to 28' ); |
112 |
}; |
230 |
}; |
113 |
$issueNumber = |
231 |
|
114 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-01', $frequency ); |
232 |
$schema->storage->txn_rollback; |
115 |
is( $issueNumber, 1 ); |
|
|
116 |
$issueNumber = |
117 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-02', $frequency ); |
118 |
is( $issueNumber, 1 ); |
119 |
$issueNumber = |
120 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-08', $frequency ); |
121 |
is( $issueNumber, 2 ); |
122 |
$issueNumber = |
123 |
C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-15', $frequency ); |
124 |
is( $issueNumber, 3 ); |
125 |
|
126 |
$dbh->rollback; |
127 |
- |
|
|