From 0f2eca1c96931c14b995b41f3ccb5f39abba458e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 8 Jan 2014 18:31:09 +0100 Subject: [PATCH] Bug 8007: Discharge - Add unit tests --- t/db_dependent/Discharge.t | 59 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 t/db_dependent/Discharge.t diff --git a/t/db_dependent/Discharge.t b/t/db_dependent/Discharge.t new file mode 100644 index 0000000..d7eb8ae --- /dev/null +++ b/t/db_dependent/Discharge.t @@ -0,0 +1,59 @@ +#!/usr/bin/perl; + +use Modern::Perl; +use Test::More; +use MARC::Record; + +use C4::Biblio qw( AddBiblio ); +use C4::Circulation qw( AddIssue AddReturn ); +use C4::Context; +use C4::Items qw( AddItem ); +use C4::Members qw( AddMember GetMember ); + +use Koha::Service::Borrower::Discharge; + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +$dbh->do(q|DELETE FROM discharges|); + +C4::Context->_new_userenv('xxx'); +C4::Context::set_userenv(0, 0, 0, 'firstname', 'surname', 'CPL', 'CPL', '', '', ''); + +my $borrowernumber = AddMember( + cardnumber => 'UTCARD1', + firstname => 'my firstname', + surname => 'my surname', + categorycode => 'S', + branchcode => 'CPL', +); +my $borrower = GetMember( borrowernumber => $borrowernumber ); + +# Discharge not possible with issues +my ( $biblionumber ) = AddBiblio( MARC::Record->new, ''); +my $barcode = 'BARCODE42'; +my ( undef, undef, $itemnumber ) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL', barcode => $barcode }, $biblionumber); +AddIssue( $borrower, $barcode ); +is_deeply( [ Koha::Service::Borrower::Discharge::can_be_discharged({ borrowernumber => $borrowernumber }) ], [ 'has_issues', 0 ], 'A patron with issues cannot be discharged' ); + +is( Koha::Service::Borrower::Discharge::request({ borrowernumber => $borrowernumber }), undef, 'No request done if patron has issues' ); +is( Koha::Service::Borrower::Discharge::discharge({ borrowernumber => $borrowernumber }), undef, 'No discharge done if patron has issues' ); +is_deeply( Koha::Service::Borrower::Discharge::get_pendings(), [], 'There is no pending discharge request' ); + +AddReturn( $barcode ); + +# Discharge possible without issue +is( Koha::Service::Borrower::Discharge::can_be_discharged({ borrowernumber => $borrowernumber }), 1, 'A patron without issues can be discharged' ); + +# FIXME +# At this point, there is a problem with the AutoCommit off +# The transaction is bloked into DBIx::Class::Storage::DBI::_dbh_execute +# line my $rv = $sth->execute(); +# We are using 2 connections and the one used by Koha::Schema has the AutoCommit set to 1 +# Even if we switch off this flag, the connection will be blocked. +# The error is: +# DBIx::Class::ResultSet::create(): DBI Exception: DBD::mysql::st execute failed: Lock wait timeout exceeded; try restarting transaction [for Statement "INSERT INTO discharges ( borrower, needed, validated) VALUES ( ?, ?, ? )" with ParamValues: 0='121', 1='2014-01-08T16:38:29', 2=undef] at /home/koha/src/Koha/DataObject/Discharge.pm line 33 +#is( Koha::Service::Borrower::Discharge::request({ borrowernumber => $borrowernumber }), 1, 'Discharge request sent' ); + +done_testing; -- 1.7.10.4