From 5058dc05d7558025d68b194e510d72029f0a005a Mon Sep 17 00:00:00 2001 From: Yohann Dufour Date: Tue, 24 Jun 2014 14:27:21 +0200 Subject: [PATCH] Bug 12470: adding unit tests for the routines CheckValidBarCode and CheckIfIssuedToPatron of the module C4::Circulation.pm The routines CheckValidBarcode and CheckIfIssuedToPatron were not tested Test plan: 1/ Apply the patch 2/ Execute : prove t/db_dependent/Circulation/CheckValidBarcode.t t/db_dependent/Circulation/CheckIfIssuedToPatron.t 3/ The result of the command has to be a success without error or warning : t/db_dependent/Circulation/CheckValidBarcode.t ...... ok t/db_dependent/Circulation/CheckIfIssuedToPatron.t .. ok All tests successful. Files=2, Tests=32, 3 wallclock secs ( 0.04 usr 0.01 sys + 2.88 cusr 0.17 csys = 3.10 CPU) Result: PASS Signed-off-by: Bernardo Gonzalez Kriegel Patches squashed Tests pass, no koha-qa errors Signed-off-by: Kyle M Hall Tests pass using sample data provided with Koha Signed-off-by: Tomas Cohen Arazi Signed-off-by: Galen Charlton --- .../Circulation/CheckIfIssuedToPatron.t | 107 ++++++++++++++++++ .../Circulation/CheckValidBarcode.t | 70 ++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 t/db_dependent/Circulation/CheckIfIssuedToPatron.t create mode 100644 t/db_dependent/Circulation/CheckValidBarcode.t diff --git a/t/db_dependent/Circulation/CheckIfIssuedToPatron.t b/t/db_dependent/Circulation/CheckIfIssuedToPatron.t new file mode 100644 index 0000000000..3ae7d0da98 --- /dev/null +++ b/t/db_dependent/Circulation/CheckIfIssuedToPatron.t @@ -0,0 +1,107 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Test::More tests => 22; +use Test::MockModule; + +use C4::Biblio; +use C4::Items; +use C4::Members; +use C4::Branch; +use C4::Category; +use MARC::Record; + +BEGIN { + use_ok('C4::Circulation'); +} + + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +$dbh->do(q|DELETE FROM issues|); +$dbh->do(q|DELETE FROM items|); +$dbh->do(q|DELETE FROM borrowers|); +$dbh->do(q|DELETE FROM branches|); +$dbh->do(q|DELETE FROM biblio|); +$dbh->do(q|DELETE FROM items|); +$dbh->do(q|DELETE FROM categories|); + + +my $branchcode = 'B'; +ModBranch({ add => 1, branchcode => $branchcode, branchname => 'Branch' }); + +my $categorycode = 'C'; +$dbh->do("INSERT INTO categories(categorycode) VALUES(?)", undef, $categorycode); + +my %item_branch_infos = ( + homebranch => $branchcode, + holdingbranch => $branchcode, +); + +my ($biblionumber1) = AddBiblio(MARC::Record->new, ''); +my $barcode1 = '0101'; +AddItem({ barcode => $barcode1, %item_branch_infos }, $biblionumber1); +my ($biblionumber2) = AddBiblio(MARC::Record->new, ''); +my $barcode2 = '0202'; +AddItem({ barcode => $barcode2, %item_branch_infos }, $biblionumber2); + +my $borrowernumber1 = AddMember(categorycode => $categorycode, branchcode => $branchcode); +my $borrowernumber2 = AddMember(categorycode => $categorycode, branchcode => $branchcode); +my $borrower1 = GetMember(borrowernumber => $borrowernumber1); +my $borrower2 = GetMember(borrowernumber => $borrowernumber2); + +my $module = new Test::MockModule('C4::Context'); +$module->mock('userenv', sub { { branch => $branchcode } }); + + +my $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns unef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' ); + +AddIssue($borrower1, '0101'); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron(); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1); +is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' ); + +AddIssue($borrower2, '0202'); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron(); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1); +is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1); +is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' ); +$check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2); +is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' ); + +$dbh->rollback(); diff --git a/t/db_dependent/Circulation/CheckValidBarcode.t b/t/db_dependent/Circulation/CheckValidBarcode.t new file mode 100644 index 0000000000..037e404a5b --- /dev/null +++ b/t/db_dependent/Circulation/CheckValidBarcode.t @@ -0,0 +1,70 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Test::More tests => 10; + +use C4::Circulation; +use C4::Biblio; +use C4::Items; +use C4::Branch; + + +BEGIN { + use_ok('C4::Circulation'); +} + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +$dbh->do(q|DELETE FROM issues|); +$dbh->do(q|DELETE FROM items|); +$dbh->do(q|DELETE FROM borrowers|); +$dbh->do(q|DELETE FROM branches|); +$dbh->do(q|DELETE FROM biblio|); +$dbh->do(q|DELETE FROM categories|); + + +my $branchcode = 'B'; +ModBranch({ add => 1, branchcode => $branchcode, branchname => 'Branch' }); + +my $categorycode = 'C'; +$dbh->do("INSERT INTO categories(categorycode) VALUES(?)", undef, $categorycode); + +my %item_branch_infos = ( + homebranch => $branchcode, + holdingbranch => $branchcode, +); + +my $barcode1 = '0101'; +my $barcode2 = '0102'; +my $barcode3 = '0203'; + +my $check_valid_barcode = C4::Circulation::CheckValidBarcode(); +is( $check_valid_barcode, 0, 'CheckValidBarcode without barcode returns false' ); +$check_valid_barcode = C4::Circulation::CheckValidBarcode($barcode1); +is( $check_valid_barcode, 0, 'CheckValidBarcode with an invalid barcode returns true' ); +$check_valid_barcode = C4::Circulation::CheckValidBarcode($barcode2); +is( $check_valid_barcode, 0, 'CheckValidBarcode with an invalid barcode returns true' ); +$check_valid_barcode = C4::Circulation::CheckValidBarcode($barcode3); +is( $check_valid_barcode, 0, 'CheckValidBarcode with an invalid barcode returns true' ); + +my ($biblionumber1) = AddBiblio(MARC::Record->new, ''); +AddItem({ barcode => $barcode1, %item_branch_infos }, $biblionumber1); +AddItem({ barcode => $barcode2, %item_branch_infos }, $biblionumber1); +my ($biblionumber2) = AddBiblio(MARC::Record->new, ''); +AddItem({ barcode => $barcode3, %item_branch_infos }, $biblionumber2); + +$check_valid_barcode = C4::Circulation::CheckValidBarcode(); +is( $check_valid_barcode, 0, 'CheckValidBarcode without barcode returns false' ); +$check_valid_barcode = C4::Circulation::CheckValidBarcode($barcode1); +is( $check_valid_barcode, 1, 'CheckValidBarcode returns true' ); +$check_valid_barcode = C4::Circulation::CheckValidBarcode($barcode2); +is( $check_valid_barcode, 1, 'CheckValidBarcode returns true' ); +$check_valid_barcode = C4::Circulation::CheckValidBarcode($barcode3); +is( $check_valid_barcode, 1, 'CheckValidBarcode returns true' ); +$check_valid_barcode = C4::Circulation::CheckValidBarcode('wrong barcode'); +is( $check_valid_barcode, 0, 'CheckValidBarcode with an invalid barcode returns false' ); + +$dbh->rollback(); -- 2.39.5