Bug 20434: Update UNIMARC framework - authorised values
[koha.git] / t / db_dependent / Serials / GetNextDate.t
blobbcbb76c1ade8ce6c698b720aeb982e84cc477ba1
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 102;
6 use Koha::Database;
7 use C4::Serials;
8 use C4::Serials::Frequency;
10 my $schema = Koha::Database->new->schema;
11 $schema->storage->txn_begin;
12 my $dbh = C4::Context->dbh;
15 # TEST CASE - 1 issue per day, no irregularities
16 my $frequency = {
17 description => "One issue per day",
18 unit => 'day',
19 issuesperunit => 1,
20 unitsperissue => 1,
23 my $subscription = {
24 firstacquidate => '1970-01-01',
25 irregularity => '',
26 countissuesperunit => 1,
28 my $publisheddate = $subscription->{firstacquidate};
30 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
31 is($publisheddate, '1970-01-02');
32 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
33 is($publisheddate, '1970-01-03');
34 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
35 is($publisheddate, '1970-01-04');
37 # TEST CASE - 1 issue per day, irregularities
38 $subscription = {
39 firstacquidate => '1970-01-01',
40 irregularity => '2;4', # Skip the second and fourth issues
41 countissuesperunit => 1,
43 $publisheddate = $subscription->{firstacquidate};
44 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
45 is($publisheddate, '1970-01-03');
46 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
47 is($publisheddate, '1970-01-05');
48 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
49 is($publisheddate, '1970-01-06');
51 # TEST CASE - 2 issues per day, no irregularity
52 $frequency = {
53 description => "Two issues per day",
54 unit => 'day',
55 issuesperunit => 2,
56 unitsperissue => 1,
58 $subscription = {
59 firstacquidate => '1970-01-01',
60 irregularity => '',
61 countissuesperunit => 1,
63 $publisheddate = $subscription->{firstacquidate};
64 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
65 is($publisheddate, '1970-01-01');
66 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
67 is($publisheddate, '1970-01-02');
68 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
69 is($publisheddate, '1970-01-02');
70 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
71 is($publisheddate, '1970-01-03');
73 # TEST CASE - 2 issues per day, irregularities
74 $subscription = {
75 firstacquidate => '1970-01-01',
76 irregularity => '3;5;6',
77 countissuesperunit => 1,
79 $publisheddate = $subscription->{firstacquidate};
80 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
81 is($publisheddate, '1970-01-01');
82 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
83 is($publisheddate, '1970-01-02');
84 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
85 is($publisheddate, '1970-01-04');
86 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
87 is($publisheddate, '1970-01-04');
88 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
89 is($publisheddate, '1970-01-05');
91 # TEST CASE - 1 issue every 2 days, no irregularity
92 $frequency = {
93 description => "one issue every two days",
94 unit => 'day',
95 issuesperunit => 1,
96 unitsperissue => 2,
98 $subscription = {
99 firstacquidate => '1970-01-01',
100 irregularity => '',
101 countissuesperunit => 1,
103 $publisheddate = $subscription->{firstacquidate};
104 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
105 is($publisheddate, '1970-01-03');
106 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
107 is($publisheddate, '1970-01-05');
109 # TEST CASE - 1 issue every 2 days, irregularities
110 $subscription = {
111 firstacquidate => '1970-01-01',
112 irregularity => '3',
113 countissuesperunit => 1,
115 $publisheddate = $subscription->{firstacquidate};
116 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
117 is($publisheddate, '1970-01-03');
118 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
119 is($publisheddate, '1970-01-07');
120 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
121 is($publisheddate, '1970-01-09');
123 # TEST CASE - 1 issue per week, no irregularity
124 $frequency = {
125 description => "one issue per week",
126 unit => 'week',
127 issuesperunit => 1,
128 unitsperissue => 1,
130 $subscription = {
131 firstacquidate => '1970-01-01',
132 irregularity => '',
133 countissuesperunit => 1,
135 $publisheddate = $subscription->{firstacquidate};
136 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
137 is($publisheddate, '1970-01-08');
138 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
139 is($publisheddate, '1970-01-15');
141 # TEST CASE - 1 issue per week, irregularities
142 $subscription = {
143 firstacquidate => '1970-01-01',
144 irregularity => '3',
145 countissuesperunit => 1,
147 $publisheddate = $subscription->{firstacquidate};
148 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
149 is($publisheddate, '1970-01-08');
150 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
151 is($publisheddate, '1970-01-22');
152 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
153 is($publisheddate, '1970-01-29');
155 # TEST CASE - 1 issue every 2 weeks, no irregularity
156 $frequency = {
157 description => "one issue every 2 weeks",
158 unit => 'week',
159 issuesperunit => 1,
160 unitsperissue => 2,
162 $subscription = {
163 firstacquidate => '1970-01-01',
164 irregularity => '',
165 countissuesperunit => 1,
167 $publisheddate = $subscription->{firstacquidate};
168 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
169 is($publisheddate, '1970-01-15');
170 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
171 is($publisheddate, '1970-01-29');
172 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
173 is($publisheddate, '1970-02-12');
175 # TEST CASE - 1 issue every 2 weeks, irregularities
176 $subscription = {
177 firstacquidate => '1970-01-01',
178 irregularity => '3',
179 countissuesperunit => 1,
181 $publisheddate = $subscription->{firstacquidate};
182 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
183 is($publisheddate, '1970-01-15');
184 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
185 is($publisheddate, '1970-02-12');
186 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
187 is($publisheddate, '1970-02-26');
189 # TEST CASE - 2 issues per week, no irregularity
190 $frequency = {
191 description => "two issues per week",
192 unit => 'week',
193 issuesperunit => 2,
194 unitsperissue => 1,
196 $subscription = {
197 firstacquidate => '1970-01-01',
198 irregularity => '',
199 countissuesperunit => 1,
201 $publisheddate = $subscription->{firstacquidate};
202 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
203 is($publisheddate, '1970-01-04');
204 # when more than 1 issue per week, date is automatically set to the same day of
205 # week as firstacquidate
206 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
207 is($publisheddate, '1970-01-08');
208 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
209 is($publisheddate, '1970-01-11');
210 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
211 is($publisheddate, '1970-01-15');
213 # TEST CASE - 2 issues per week, irregularities
214 $subscription = {
215 firstacquidate => '1970-01-01',
216 irregularity => '3;5;6',
217 countissuesperunit => 1,
219 $publisheddate = $subscription->{firstacquidate};
220 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
221 is($publisheddate, '1970-01-04');
222 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
223 is($publisheddate, '1970-01-11');
224 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
225 is($publisheddate, '1970-01-22');
226 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
227 is($publisheddate, '1970-01-25');
228 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
229 is($publisheddate, '1970-01-29');
231 # TEST CASE - 6 issues per week, no irregularity
232 $frequency = {
233 description => "six issues per week",
234 unit => 'week',
235 issuesperunit => 6,
236 unitsperissue => 1,
238 $subscription = {
239 firstacquidate => '1970-01-06',
240 irregularity => '',
241 countissuesperunit => 1,
243 $publisheddate = $subscription->{firstacquidate};
244 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
245 is($publisheddate, '1970-01-07');
246 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
247 is($publisheddate, '1970-01-08');
248 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
249 is($publisheddate, '1970-01-09');
250 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
251 is($publisheddate, '1970-01-10');
252 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
253 is($publisheddate, '1970-01-11');
254 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
255 is($publisheddate, '1970-01-13');
257 # TEST CASE - 6 issues per week, irregularities
258 $subscription = {
259 firstacquidate => '1970-01-06',
260 irregularity => '3;5;6',
261 countissuesperunit => 1,
263 $publisheddate = $subscription->{firstacquidate};
264 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
265 is($publisheddate, '1970-01-07');
266 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
267 is($publisheddate, '1970-01-09');
268 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
269 is($publisheddate, '1970-01-13');
270 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
271 is($publisheddate, '1970-01-14');
273 # TEST CASE - 1 issue per month, no irregularity
274 $frequency = {
275 description => "1 issue per month",
276 unit => 'month',
277 issuesperunit => 1,
278 unitsperissue => 1,
280 $subscription = {
281 firstacquidate => '1970-01-01',
282 irregularity => '',
283 countissuesperunit => 1,
285 $publisheddate = $subscription->{firstacquidate};
286 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
287 is($publisheddate, '1970-02-01');
288 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
289 is($publisheddate, '1970-03-01');
290 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
291 is($publisheddate, '1970-04-01');
293 # TEST CASE - 1 issue per month, irregularities
294 $subscription = {
295 firstacquidate => '1970-01-01',
296 irregularity => '2;4',
297 countissuesperunit => 1,
299 $publisheddate = $subscription->{firstacquidate};
300 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
301 is($publisheddate, '1970-03-01');
302 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
303 is($publisheddate, '1970-05-01');
304 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
305 is($publisheddate, '1970-06-01');
307 # TEST CASE - 1 issue every 2 months, no irregularity
308 $frequency = {
309 description => "1 issue every 2 months",
310 unit => 'month',
311 issuesperunit => 1,
312 unitsperissue => 2,
314 $subscription = {
315 firstacquidate => '1970-01-01',
316 irregularity => '',
317 countissuesperunit => 1,
319 $publisheddate = $subscription->{firstacquidate};
320 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
321 is($publisheddate, '1970-03-01');
322 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
323 is($publisheddate, '1970-05-01');
324 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
325 is($publisheddate, '1970-07-01');
327 # TEST CASE - 1 issue every 2 months, irregularities
328 $subscription = {
329 firstacquidate => '1970-01-01',
330 irregularity => '2;3',
331 countissuesperunit => 1,
333 $publisheddate = $subscription->{firstacquidate};
334 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
335 is($publisheddate, '1970-07-01');
336 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
337 is($publisheddate, '1970-09-01');
338 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
339 is($publisheddate, '1970-11-01');
341 # TEST CASE - 2 issues per month, no irregularity
342 $frequency = {
343 description => "2 issues per month",
344 unit => 'month',
345 issuesperunit => 2,
346 unitsperissue => 1,
348 $subscription = {
349 firstacquidate => '1970-01-01',
350 irregularity => '',
351 countissuesperunit => 1,
353 $publisheddate = $subscription->{firstacquidate};
354 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
355 is($publisheddate, '1970-01-16', 'January has 31 days');
356 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
357 is($publisheddate, '1970-02-01');
358 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
359 is($publisheddate, '1970-02-16', 'February has only 28 days');
360 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
361 is($publisheddate, '1970-03-01');
363 # TEST CASE - 2 issues per month, irregularities
364 $subscription = {
365 firstacquidate => '1970-01-01',
366 irregularity => '3;5;6',
367 countissuesperunit => 1,
369 $publisheddate = $subscription->{firstacquidate};
370 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
371 is($publisheddate, '1970-01-16', 'January has 31 days');
372 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
373 is($publisheddate, '1970-02-16', 'February has only 28 days');
374 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
375 is($publisheddate, '1970-04-01');
376 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
377 is($publisheddate, '1970-04-16', 'April has 30 days');
378 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
379 is($publisheddate, '1970-05-01');
381 # TEST CASE - 1 issue per year, no irregularity
382 $frequency = {
383 description => "1 issue per year",
384 unit => 'year',
385 issuesperunit => 1,
386 unitsperissue => 1,
388 $subscription = {
389 firstacquidate => '1970-01-01',
390 irregularity => '',
391 countissuesperunit => 1,
393 $publisheddate = $subscription->{firstacquidate};
394 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
395 is($publisheddate, '1971-01-01');
396 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
397 is($publisheddate, '1972-01-01');
398 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
399 is($publisheddate, '1973-01-01');
401 # TEST CASE - 1 issue per year, irregularities
402 $subscription = {
403 firstacquidate => '1970-01-01',
404 irregularity => '2;4',
405 countissuesperunit => 1,
407 $publisheddate = $subscription->{firstacquidate};
408 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
409 is($publisheddate, '1972-01-01');
410 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
411 is($publisheddate, '1974-01-01');
412 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
413 is($publisheddate, '1975-01-01');
415 # TEST CASE - 1 issue every 2 years, no irregularity
416 $frequency = {
417 description => "1 issue every 2 years",
418 unit => 'year',
419 issuesperunit => 1,
420 unitsperissue => 2,
422 $subscription = {
423 firstacquidate => '1970-01-01',
424 irregularity => '',
425 countissuesperunit => 1,
427 $publisheddate = $subscription->{firstacquidate};
428 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
429 is($publisheddate, '1972-01-01');
430 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
431 is($publisheddate, '1974-01-01');
432 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
433 is($publisheddate, '1976-01-01');
435 # TEST CASE - 1 issue every 2 years, irregularities
436 $subscription = {
437 firstacquidate => '1970-01-01',
438 irregularity => '2;4',
439 countissuesperunit => 1,
441 $publisheddate = $subscription->{firstacquidate};
442 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
443 is($publisheddate, '1974-01-01');
444 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
445 is($publisheddate, '1978-01-01');
446 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
447 is($publisheddate, '1980-01-01');
448 # Move publisheddate to Feb 29 (leap year 1980)
449 $publisheddate = '1980-02-29';
450 $publisheddate = GetNextDate( $subscription, $publisheddate, $frequency );
451 is( $publisheddate, '1982-02-28', 'Test +2 year from Feb 29' );
453 # TEST CASE - 2 issues per year, no irregularity
454 $frequency = {
455 description => "1 issue every 2 years",
456 unit => 'year',
457 issuesperunit => 2,
458 unitsperissue => 1,
460 $subscription = {
461 firstacquidate => '1970-01-01',
462 irregularity => '',
463 countissuesperunit => 1,
465 $publisheddate = $subscription->{firstacquidate};
466 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
467 is($publisheddate, '1970-07-02');
468 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
469 is($publisheddate, '1971-01-01');
470 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
471 is($publisheddate, '1971-07-02');
472 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
473 is($publisheddate, '1972-01-01');
475 # TEST CASE - 2 issues per year, irregularities
476 $subscription = {
477 firstacquidate => '1970-01-01',
478 irregularity => '3;5;6',
479 countissuesperunit => 1,
481 $publisheddate = $subscription->{firstacquidate};
482 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
483 is($publisheddate, '1970-07-02');
484 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
485 is($publisheddate, '1971-07-02');
486 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
487 is($publisheddate, '1973-01-01');
488 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
489 is($publisheddate, '1973-07-02');
490 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
491 is($publisheddate, '1974-01-01');
493 # TEST CASE - 9 issues per year, dates spread throughout month
494 $frequency = {
495 description => "9 issues per year",
496 unit => 'year',
497 issuesperunit => 9,
498 unitsperissue => 1,
500 $subscription = {
501 firstacquidate => '1970-08-10',
502 irregularity => '',
503 countissuesperunit => 1,
505 my @dates = ( $subscription->{firstacquidate} );
506 foreach(1..27) {
507 push @dates, GetNextDate( $subscription, $dates[-1], $frequency );
509 is( $dates[9], '1971-08-10', 'Freq 9/yr, 1 year passed' );
510 is( $dates[18], '1972-08-10', 'Freq 9/yr, 2 years passed (leap year)' );
511 is( $dates[27], '1973-08-10', 'Freq 9/yr, 3 years passed' );
512 # Keep (first) position in cycle, but shift back 9 days
513 is( GetNextDate( $subscription, '1973-08-01', $frequency ), '1973-09-10', 'Back 9 days, without annual correction' );
514 # Set position to last in cycle, and shift back 9 days; annual correction
515 $subscription->{countissuesperunit} = 9;
516 is( GetNextDate( $subscription, '1973-08-01', $frequency ), '1973-09-15', 'Last in cycle, back 9 days, expect annual correction' );
518 # TEST CASE - Irregular
519 $frequency = {
520 description => "Irregular",
521 unit => undef,
522 issuesperunit => 1,
523 unitsperissue => 1,
525 $subscription = {
526 firstacquidate => '1970-01-01',
527 irregularity => '',
528 countissuesperunit => 1,
530 $publisheddate = $subscription->{firstacquidate};
531 # GetNextDate always return undef if subscription is irregular
532 $publisheddate = GetNextDate($subscription, $publisheddate, $frequency);
533 is($publisheddate, undef);
535 # GetNextDate returns undef if one of two first parameters is undef
536 $publisheddate = GetNextDate($subscription, undef, $frequency);
537 is($publisheddate, undef);
538 $publisheddate = GetNextDate(undef, $subscription->{firstacquidate}, $frequency);
539 is($publisheddate, undef);
540 $publisheddate = GetNextDate(undef, undef, $frequency);
541 is($publisheddate, undef);
543 $schema->storage->txn_rollback;