View | Details | Raw Unified | Return to bug 18356
Collapse All | Expand All

(-)a/t/db_dependent/Serials/GetFictiveIssueNumber.t (+104 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This test deals with GetFictiveIssueNumber (from C4::Serials)
4
5
use Modern::Perl;
6
use Test::More tests => 2;
7
8
use Koha::Database;
9
use C4::Serials;
10
use C4::Serials::Frequency;
11
12
my $schema  = Koha::Database->new->schema;
13
$schema->storage->txn_begin;
14
my $dbh = C4::Context->dbh;
15
16
subtest 'Tests for irregular frequency' => sub {
17
    plan tests => 2;
18
19
    # Add a frequency
20
    my $freq_irr = AddSubscriptionFrequency({
21
        description => "Irregular",
22
        unit => undef,
23
    });
24
25
    # Test it
26
    my $subscription = {
27
        periodicity => $freq_irr,
28
        firstacquidate => '1972-02-07',
29
    };
30
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-12-31'), 0, 'Irregular: should be zero' );
31
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-12-31'), 0, 'Irregular: still zero' );
32
};
33
34
subtest 'Tests for yearly frequencies' => sub {
35
    plan tests => 10;
36
37
    # First add a few frequencies
38
    my $freq_1i_1y = AddSubscriptionFrequency({
39
        description => "1 issue per year",
40
        unit => 'year',
41
        issuesperunit => 1,
42
        unitsperissue => 1,
43
    });
44
    my $freq_1i_3y = AddSubscriptionFrequency({
45
        description => "1 issue per 3 years",
46
        unit => 'year',
47
        issuesperunit => 1,
48
        unitsperissue => 3,
49
    });
50
    my $freq_5i_1y = AddSubscriptionFrequency({
51
        description => "5 issues per year",
52
        unit => 'year',
53
        issuesperunit => 5,
54
        unitsperissue => 1,
55
    });
56
    my $freq_366i_1y = AddSubscriptionFrequency({
57
        description => "366 issue per year",
58
        unit => 'year',
59
        issuesperunit => 366,
60
        unitsperissue => 1,
61
    });
62
63
    # TEST CASE - 1 issue per year
64
    my $subscription = {
65
        periodicity => $freq_1i_1y,
66
        firstacquidate => '1972-02-10',
67
        countissuesperunit => 1,
68
    };
69
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-09'), 1, 'Feb 9 still 1' );
70
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-10'), 2, 'Feb 10 goes to 2' );
71
72
    # TEST CASE - 1 issue per 3 years
73
    $subscription->{periodicity} = $freq_1i_3y;
74
    $subscription->{firstacquidate} = '1972-02-20';
75
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-19'), 1, 'Feb 19, 1975 still 1' );
76
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-20'), 2, 'Feb 20, 1975 goes to 2' );
77
78
    # TEST CASE - 5 issues per year
79
    $subscription->{periodicity} = $freq_5i_1y;
80
    $subscription->{firstacquidate} = '1972-02-29'; #leap year
81
    $subscription->{countissuesperunit} = 1;
82
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-11'), 1, 'May 11 still 1' );
83
    $subscription->{countissuesperunit} = 2;
84
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-12'), 2, 'May 12 goes to 2' );
85
    $subscription->{countissuesperunit} = 5;
86
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27'), 5, 'Feb 27 should still be 5' );
87
    $subscription->{countissuesperunit} = 1;
88
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28'), 6, 'Feb 28 goes to 6' );
89
90
    # TEST CASE - 366 issues per year (hypothetical example)
91
    # Testing prevention of divide by zero
92
    $subscription->{periodicity} = $freq_366i_1y;
93
    $subscription->{firstacquidate} = '1972-02-29'; #leap year
94
    $subscription->{countissuesperunit} = 366;
95
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27'), 366, 'Feb 27 still at 366' );
96
    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28'), 732, 'Feb 28 goes to 732' );
97
98
};
99
100
# TODO: subtest 'Tests for monthly frequencies' => sub {
101
# TODO: subtest 'Tests for weekly frequencies' => sub {
102
# TODO: subtest 'Tests for dayly frequencies' => sub {
103
104
$schema->storage->txn_rollback;
(-)a/t/db_dependent/Serials/GetNextDate.t (-9 / +39 lines)
Lines 1-15 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
use C4::Context;
4
use Test::More tests => 96;
5
use Modern::Perl;
3
use Modern::Perl;
4
use Test::More tests => 102;
6
5
6
use Koha::Database;
7
use C4::Serials;
8
use C4::Serials::Frequency;
9
10
my $schema  = Koha::Database->new->schema;
11
$schema->storage->txn_begin;
7
my $dbh = C4::Context->dbh;
12
my $dbh = C4::Context->dbh;
8
$dbh->{RaiseError} = 1;
9
$dbh->{AutoCommit} = 0;
10
13
11
use C4::Serials::Frequency;
12
use C4::Serials;
13
14
14
# TEST CASE - 1 issue per day, no irregularities
15
# TEST CASE - 1 issue per day, no irregularities
15
my $frequency = {
16
my $frequency = {
Lines 469-478 $publisheddate = GetNextDate($subscription, $publisheddate); Link Here
469
is($publisheddate, '1978-01-01');
470
is($publisheddate, '1978-01-01');
470
$publisheddate = GetNextDate($subscription, $publisheddate);
471
$publisheddate = GetNextDate($subscription, $publisheddate);
471
is($publisheddate, '1980-01-01');
472
is($publisheddate, '1980-01-01');
473
# Move publisheddate to Feb 29 (leap year 1980)
474
$publisheddate = '1980-02-29';
475
$publisheddate = GetNextDate( $subscription, $publisheddate );
476
is( $publisheddate, '1982-02-28', 'Test +2 year from Feb 29' );
472
477
473
# TEST CASE - 2 issues per year, no irregularity
478
# TEST CASE - 2 issues per year, no irregularity
474
$id = AddSubscriptionFrequency({
479
$id = AddSubscriptionFrequency({
475
    description => "1 issue every 2 years",
480
    description => "2 issues per year",
476
    unit => 'year',
481
    unit => 'year',
477
    issuesperunit => 2,
482
    issuesperunit => 2,
478
    unitsperissue => 1,
483
    unitsperissue => 1,
Lines 512-517 is($publisheddate, '1973-07-02'); Link Here
512
$publisheddate = GetNextDate($subscription, $publisheddate);
517
$publisheddate = GetNextDate($subscription, $publisheddate);
513
is($publisheddate, '1974-01-01');
518
is($publisheddate, '1974-01-01');
514
519
520
# TEST CASE - 9 issues per year, dates spread throughout month
521
$id = AddSubscriptionFrequency({
522
    description => "9 issues per year",
523
    unit => 'year',
524
    issuesperunit => 9,
525
    unitsperissue => 1,
526
});
527
$subscription = {
528
    periodicity => $id,
529
    firstacquidate => '1970-08-10',
530
    irregularity => '',
531
    countissuesperunit => 1,
532
};
533
my @dates = ( $subscription->{firstacquidate} );
534
foreach(1..27) {
535
    push @dates, GetNextDate( $subscription, $dates[-1] );
536
}
537
is( $dates[9],  '1971-08-10', 'Freq 9/yr, 1 year passed' );
538
is( $dates[18], '1972-08-10', 'Freq 9/yr, 2 years passed (leap year)' );
539
is( $dates[27], '1973-08-10', 'Freq 9/yr, 3 years passed' );
540
# Keep (first) position in cycle, but shift back 9 days
541
is( GetNextDate( $subscription, '1973-08-01' ), '1973-09-10', 'Back 9 days, without annual correction' );
542
# Set position to last in cycle, and shift back 9 days; annual correction
543
$subscription->{countissuesperunit} = 9;
544
is( GetNextDate( $subscription, '1973-08-01' ), '1973-09-15', 'Last in cycle, back 9 days, expect annual correction' );
545
515
# TEST CASE - Irregular
546
# TEST CASE - Irregular
516
$id = AddSubscriptionFrequency({
547
$id = AddSubscriptionFrequency({
517
    description => "Irregular",
548
    description => "Irregular",
Lines 538-541 is($publisheddate, undef); Link Here
538
$publisheddate = GetNextDate(undef, undef);
569
$publisheddate = GetNextDate(undef, undef);
539
is($publisheddate, undef);
570
is($publisheddate, undef);
540
571
541
$dbh->rollback;
572
$schema->storage->txn_rollback;
542
- 

Return to bug 18356