From f1ec228ab590ee0413815c068852777f8b7e2546 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 11 May 2017 10:47:22 +0200 Subject: [PATCH] Bug 18356: Extend GetNextDate.t, add GetFictiveIssueNumber.t (unit=year) Content-Type: text/plain; charset=utf-8 This patch deals with tests for yearly frequencies. Adjust/extend GetNextDate.t: [1] Adjust mixup of units/issues in a description. [2] Add testing +2 years on 29-2 of leap year for freq 1 issue/2 years. [3] Add tests for freq 9 issues/year. We also include testing the operation of rounding dates to first acquisition day and month when close enough. Add GetFictiveIssueNumber.t: [1] Two subtests are provided for irregular frequencies (very trivial) and for year frequencies (with three specific test cases). Test plan: [1] Run t/db_dependent/Serials/GetNextDate.t [2] Run t/db_dependent/Serials/GetFictiveIssueNumber.t Note: Without the second patch both tests should fail. This shows the need of the adjustments in the second patch. Signed-off-by: Marcel de Rooy --- t/db_dependent/Serials/GetFictiveIssueNumber.t | 91 ++++++++++++++++++++++++++ t/db_dependent/Serials/GetNextDate.t | 51 ++++++++++++--- 2 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 t/db_dependent/Serials/GetFictiveIssueNumber.t diff --git a/t/db_dependent/Serials/GetFictiveIssueNumber.t b/t/db_dependent/Serials/GetFictiveIssueNumber.t new file mode 100644 index 0000000..dfbc34f --- /dev/null +++ b/t/db_dependent/Serials/GetFictiveIssueNumber.t @@ -0,0 +1,91 @@ +#!/usr/bin/perl + +# This test deals with GetFictiveIssueNumber (from C4::Serials) + +use Modern::Perl; +use Test::More tests => 2; + +use Koha::Database; +use C4::Serials; +use C4::Serials::Frequency; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; +my $dbh = C4::Context->dbh; + +subtest 'Tests for irregular frequency' => sub { + plan tests => 2; + + # Add a frequency + my $freq_irr = AddSubscriptionFrequency({ + description => "Irregular", + unit => undef, + }); + + # Test it + my $subscription = { + periodicity => $freq_irr, + firstacquidate => '1972-02-07', + }; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-12-31'), 0, 'Irregular: should be zero' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-12-31'), 0, 'Irregular: still zero' ); +}; + +subtest 'Tests for yearly frequencies' => sub { + plan tests => 8; + + # First add a few frequencies + my $freq_1i_1y = AddSubscriptionFrequency({ + description => "1 issue per year", + unit => 'year', + issuesperunit => 1, + unitsperissue => 1, + }); + my $freq_1i_3y = AddSubscriptionFrequency({ + description => "1 issue per 3 years", + unit => 'year', + issuesperunit => 1, + unitsperissue => 3, + }); + my $freq_5i_1y = AddSubscriptionFrequency({ + description => "5 issue per year", + unit => 'year', + issuesperunit => 5, + unitsperissue => 1, + }); + + # TEST CASE - 1 issue per year + # Here we should only test the subtraction of both years + my $subscription = { + periodicity => $freq_1i_1y, + firstacquidate => '1972-02-10', + countissuesperunit => 1, + }; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-12-31'), 1, 'Dec 31 still 1' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-01-01'), 2, 'Jan 1 goes to 2' ); + + # TEST CASE - 1 issue per 3 years + # Here we should test the subtraction of both years divided by 3 + $subscription->{periodicity} = $freq_1i_3y; + $subscription->{firstacquidate} = '1972-02-20'; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-01-01'), 1, '1973 still 1' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1974-01-01'), 1, '1974 still 1' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-01-01'), 2, '1975 still 2' ); + + # TEST CASE - 5 issues per year + # Here we look at the subtraction AND the internal countissuesperunit + # We do not really care about the accuracy of the internal count here + $subscription->{periodicity} = $freq_5i_1y; + $subscription->{firstacquidate} = '1972-02-29'; #leap year + $subscription->{countissuesperunit} = 3; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-06-01'), 3, 'Should be 3 due to internal count' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28'), 3, 'Should still be 3 due to internal count' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-03-01'), 8, 'Should jump to 8 due to internal count' ); + +}; + +# TODO: subtest 'Tests for monthly frequencies' => sub { +# TODO: subtest 'Tests for weekly frequencies' => sub { +# TODO: subtest 'Tests for dayly frequencies' => sub { + +$schema->storage->txn_rollback; diff --git a/t/db_dependent/Serials/GetNextDate.t b/t/db_dependent/Serials/GetNextDate.t index fe6059f..cc48e57 100644 --- a/t/db_dependent/Serials/GetNextDate.t +++ b/t/db_dependent/Serials/GetNextDate.t @@ -1,15 +1,16 @@ #!/usr/bin/perl -use C4::Context; -use Test::More tests => 96; use Modern::Perl; +use Test::More tests => 102; +use Koha::Database; +use C4::Serials; +use C4::Serials::Frequency; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; my $dbh = C4::Context->dbh; -$dbh->{RaiseError} = 1; -$dbh->{AutoCommit} = 0; -use C4::Serials::Frequency; -use C4::Serials; # TEST CASE - 1 issue per day, no irregularities my $frequency = { @@ -469,10 +470,14 @@ $publisheddate = GetNextDate($subscription, $publisheddate); is($publisheddate, '1978-01-01'); $publisheddate = GetNextDate($subscription, $publisheddate); is($publisheddate, '1980-01-01'); +# Move publisheddate to Feb 29 (leap year 1980) +$publisheddate = '1980-02-29'; +$publisheddate = GetNextDate( $subscription, $publisheddate ); +is( $publisheddate, '1982-02-28', 'Test +2 year from Feb 29' ); # TEST CASE - 2 issues per year, no irregularity $id = AddSubscriptionFrequency({ - description => "1 issue every 2 years", + description => "2 issues per year", unit => 'year', issuesperunit => 2, unitsperissue => 1, @@ -512,6 +517,36 @@ is($publisheddate, '1973-07-02'); $publisheddate = GetNextDate($subscription, $publisheddate); is($publisheddate, '1974-01-01'); +# TEST CASE - 9 issues per year, dates spread throughout month +$id = AddSubscriptionFrequency({ + description => "9 issues per year", + unit => 'year', + issuesperunit => 9, + unitsperissue => 1, +}); +$subscription = { + periodicity => $id, + firstacquidate => '1970-08-10', + irregularity => '', + countissuesperunit => 1, +}; +my @dates = ( $subscription->{firstacquidate} ); +foreach(1..27) { + push @dates, GetNextDate( $subscription, $dates[-1] ); +} +is( $dates[9], '1971-08-10', 'Freq 9/yr, 1 year passed' ); +is( $dates[18], '1972-08-10', 'Freq 9/yr, 2 years passed (leap year)' ); +is( $dates[27], '1973-08-10', 'Freq 9/yr, 3 years passed' ); +# Now move one back in the cycle and push back 49 days; we expect correction +# because 40 days are added and the difference (9 days) is <= issues per unit. +$subscription->{countissuesperunit} = 9; +$publisheddate = GetNextDate( $subscription, '1973-06-22' ); +is( $publisheddate, '1973-08-10', 'Freq 9/yr, 3 years passed, corrected' ); +# Set back again, one day more. Now we do not expect correction. +$subscription->{countissuesperunit} = 9; +$publisheddate = GetNextDate( $subscription, '1973-06-21' ); +is( $publisheddate, '1973-07-31', 'Freq 9/yr, 3 years passed, not corrected' ); + # TEST CASE - Irregular $id = AddSubscriptionFrequency({ description => "Irregular", @@ -538,4 +573,4 @@ is($publisheddate, undef); $publisheddate = GetNextDate(undef, undef); is($publisheddate, undef); -$dbh->rollback; +$schema->storage->txn_rollback; -- 2.1.4