Bug 19475: Add unit tests
authorEmmi Takkinen <emmi.takkinen@outlook.com>
Thu, 12 Mar 2020 10:35:39 +0000 (12:35 +0200)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 3 Apr 2020 13:26:33 +0000 (14:26 +0100)
To test prove:
t/db_dependent/Holidays.t

Sponsored-by: Koha-Suomi Oy
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

t/db_dependent/Holidays.t

index 70c71d4..11a0ce1 100755 (executable)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 16;
+use Test::More tests => 17;
 
 use DateTime;
 use DateTime::TimeZone;
@@ -203,3 +203,118 @@ sub _add_exception {
 
 $schema->storage->txn_rollback;
 
+subtest 'copy_to_branch' => sub {
+
+    plan tests => 8;
+
+    $schema->storage->txn_begin;
+
+    my $branch1 = $builder->build( { source => 'Branch' } )->{ branchcode };
+    my $calendar1 = C4::Calendar->new( branchcode => $branch1 );
+    my $sunday = dt_from_string("2020-03-15");
+    $calendar1->insert_week_day_holiday(
+        weekday     => 0,
+        title       => '',
+        description => 'Sundays',
+    );
+
+    my $day_month = dt_from_string("2020-03-17");
+    $calendar1->insert_day_month_holiday(
+        day         => $day_month->day(),
+        month       => $day_month->month(),
+        year        => $day_month->year(),
+        title       => '',
+        description => "",
+    );
+
+    my $future_date = dt_from_string("9999-12-31");
+    $calendar1->insert_single_holiday(
+        day         => $future_date->day(),
+        month       => $future_date->month(),
+        year        => $future_date->year(),
+        title       => "",
+        description => "",
+    );
+
+    my $future_exception = dt_from_string("9999-12-30");
+    $calendar1->insert_exception_holiday(
+        day         => $future_exception->day(),
+        month       => $future_exception->month(),
+        year        => $future_exception->year(),
+        title       => "",
+        description => "",
+    );
+
+    my $past_date = dt_from_string("2019-11-20");
+    $calendar1->insert_single_holiday(
+        day         => $past_date->day(),
+        month       => $past_date->month(),
+        year        => $past_date->year(),
+        title       => "",
+        description => "",
+    );
+
+    my $past_exception = dt_from_string("2020-03-09");
+    $calendar1->insert_exception_holiday(
+        day         => $past_exception->day(),
+        month       => $past_exception->month(),
+        year        => $past_exception->year(),
+        title       => "",
+        description => "",
+    );
+
+    my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
+
+    C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
+
+    my $calendar2 = C4::Calendar->new( branchcode => $branch2 );
+    my $exceptions = $calendar2->get_exception_holidays;
+
+    is( $calendar2->isHoliday( $sunday->day, $sunday->month, $sunday->year ), 1, "Weekday holiday copied to branch 2" );
+    is( $calendar2->isHoliday( $day_month->day, $day_month->month, $day_month->year ), 1, "Day/month holiday copied to branch 2" );
+    is( $calendar2->isHoliday( $future_date->day, $future_date->month, $future_date->year ), 1, "Single holiday copied to branch 2" );
+    is( ( grep { $_->{date} eq "9999-12-30"} values %$exceptions ), 1, "Exception holiday copied to branch 2" );
+    is( $calendar2->isHoliday( $past_date->day, $past_date->month, $past_date->year ), 0, "Don't copy past single holidays" );
+    is( ( grep { $_->{date} eq "2020-03-09"} values %$exceptions ), 0, "Don't copy past exception holidays " );
+
+    C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
+
+    #Select all rows with same values from database
+    my $dbh = C4::Context->dbh;
+    my $get_repeatable_holidays = "SELECT a.* FROM repeatable_holidays a
+        JOIN (SELECT branchcode, weekday, day, month, COUNT(*)
+        FROM repeatable_holidays
+        GROUP BY branchcode, weekday, day, month HAVING count(*) > 1) b
+        ON a.branchcode = b.branchcode
+        AND ( a.weekday = b.weekday OR (a.day = b.day AND a.month = b.month))
+        ORDER BY a.branchcode;";
+    my $sth  = $dbh->prepare($get_repeatable_holidays);
+    $sth->execute;
+
+    my @repeatable_holidays;
+    while(my $row = $sth->fetchrow_hashref){
+        push @repeatable_holidays, $row
+    }
+
+    is( scalar(@repeatable_holidays), 0, "None of the repeatable holidays were doubled");
+
+    my $get_special_holidays = "SELECT a.* FROM special_holidays a
+    JOIN (SELECT branchcode, day, month, year, isexception, COUNT(*)
+    FROM special_holidays
+    GROUP BY branchcode, day, month, year, isexception HAVING count(*) > 1) b
+    ON a.branchcode = b.branchcode
+    AND a.day = b.day AND a.month = b.month AND a.year = b.year AND a.isexception = b.isexception
+    ORDER BY a.branchcode;";
+    $sth  = $dbh->prepare($get_special_holidays);
+    $sth->execute;
+
+    my @special_holidays;
+    while(my $row = $sth->fetchrow_hashref){
+        push @special_holidays, $row
+    }
+
+    is( scalar(@special_holidays), 0, "None of the special holidays were doubled");
+
+    $schema->storage->txn_rollback;
+
+}