From 0ede61af6533b2b015dc82c2fd69ca67c31e4317 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 27 Aug 2015 09:16:43 -0400 Subject: [PATCH] Bug 14297 - Unit Tests --- t/db_dependent/HoldsQueue.t | 61 +++++++++++++++++- t/db_dependent/HoldsQueue_Bug_14297.t | 113 +++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 1 deletions(-) create mode 100755 t/db_dependent/HoldsQueue_Bug_14297.t diff --git a/t/db_dependent/HoldsQueue.t b/t/db_dependent/HoldsQueue.t index 6d997d6..97018a7 100755 --- a/t/db_dependent/HoldsQueue.t +++ b/t/db_dependent/HoldsQueue.t @@ -12,7 +12,7 @@ use C4::Context; use Data::Dumper; -use Test::More tests => 21; +use Test::More tests => 22; use C4::Branch; @@ -297,6 +297,65 @@ $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice ok( @$holds_queue == 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" ); #warn "HOLDS QUEUE: " . Data::Dumper::Dumper( $holds_queue ); +# Bug 14297 +$borrowernumber = AddMember(%data); +$borrower = GetMember( borrowernumber => $borrowernumber ); +$borrower_branchcode = $borrower->{branchcode}; +$itemtype = $item_types[0]->{itemtype}; +my $library_A = 'CPL'; +my $library_B = 'FFL'; +my $library_C = 'MPL'; # Same as our borrower's home library +$dbh->do("DELETE FROM reserves"); +$dbh->do("DELETE FROM issues"); +$dbh->do("DELETE FROM items"); +$dbh->do("DELETE FROM biblio"); +$dbh->do("DELETE FROM biblioitems"); +$dbh->do("DELETE FROM transport_cost"); +$dbh->do("DELETE FROM tmp_holdsqueue"); +$dbh->do("DELETE FROM hold_fill_targets"); +$dbh->do("DELETE FROM default_branch_circ_rules"); +$dbh->do("DELETE FROM default_branch_item_rules"); +$dbh->do("DELETE FROM default_circ_rules"); +$dbh->do("DELETE FROM branch_item_rules"); + +$dbh->do(" + INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01') +"); + +$biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'") + or BAIL_OUT("Cannot find newly created biblio record"); + +$dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')"); + +$biblioitemnumber = + $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber") + or BAIL_OUT("Cannot find newly created biblioitems record"); + +$dbh->do(" + INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype) + VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype') +"); + +$dbh->do(" + INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype) + VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype') +"); + +$dbh->do(" + INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES + ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' ); +"); + +$dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", + undef, join( ',', $library_B, $library_A, $library_C ) ); +$dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" ); + +my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 ); +C4::HoldsQueue::CreateQueue(); +$holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} }); +is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from least cost branch" ); +# End Bug 14297 + # Cleanup $dbh->rollback; diff --git a/t/db_dependent/HoldsQueue_Bug_14297.t b/t/db_dependent/HoldsQueue_Bug_14297.t new file mode 100755 index 0000000..eb185e0 --- /dev/null +++ b/t/db_dependent/HoldsQueue_Bug_14297.t @@ -0,0 +1,113 @@ +#!/usr/bin/perl + +# Test C4::HoldsQueue::CreateQueue() for both transport cost matrix +# and StaticHoldsQueueWeight array (no RandomizeHoldsQueueWeight, no point) +# Wraps tests in transaction that's rolled back, so no data is destroyed +# MySQL WARNING: This makes sense only if your tables are InnoDB, otherwise +# transactions are not supported and mess is left behind + +use Modern::Perl; + +use C4::Context; + +use Data::Dumper; + +use Test::More tests => 3; + +use C4::Branch; +use C4::ItemType; +use C4::Members; + +BEGIN { + use FindBin; + use lib $FindBin::Bin; + use_ok('C4::Reserves'); + use_ok('C4::HoldsQueue'); +} + +# Start transaction +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +my $TITLE = "Test Holds Queue XXX"; + +my %data = ( + cardnumber => 'CARDNUMBER42', + firstname => 'my firstname', + surname => 'my surname', + categorycode => 'S', + branchcode => 'MPL', +); + +my $borrowernumber = AddMember(%data); +my $borrower = GetMember( borrowernumber => $borrowernumber ); +# Set special (for this test) branches +my $borrower_branchcode = $borrower->{branchcode}; +my $branches = C4::Branch::GetBranches; +my @other_branches = grep { $_ ne $borrower_branchcode } keys %$branches; +my $least_cost_branch_code = pop @other_branches + or BAIL_OUT("No point testing only one branch..."); +my @item_types = C4::ItemType->all; +my $itemtype = grep { $_->{notforloan} == 1 } @item_types + or BAIL_OUT("No adequate itemtype"); + +# Bug 14297 +$itemtype = $item_types[0]->{itemtype}; +my $library_A = 'CPL'; +my $library_B = 'FFL'; +my $library_C = 'MPL'; # Same as our borrower's home library +$dbh->do("DELETE FROM reserves"); +$dbh->do("DELETE FROM issues"); +$dbh->do("DELETE FROM items"); +$dbh->do("DELETE FROM biblio"); +$dbh->do("DELETE FROM biblioitems"); +$dbh->do("DELETE FROM transport_cost"); +$dbh->do("DELETE FROM tmp_holdsqueue"); +$dbh->do("DELETE FROM hold_fill_targets"); +$dbh->do("DELETE FROM default_branch_circ_rules"); +$dbh->do("DELETE FROM default_branch_item_rules"); +$dbh->do("DELETE FROM default_circ_rules"); +$dbh->do("DELETE FROM branch_item_rules"); + +$dbh->do(" + INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01') +"); + +my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'") + or BAIL_OUT("Cannot find newly created biblio record"); + +$dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')"); + +my $biblioitemnumber = + $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber") + or BAIL_OUT("Cannot find newly created biblioitems record"); + +$dbh->do(" + INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype) + VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype') +"); + +$dbh->do(" + INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype) + VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype') +"); + +$dbh->do(" + INSERT INTO branch_item_rules ( branchcode, itemtype, holdallowed, returnbranch ) VALUES + ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' ); +"); + +$dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", + undef, join( ',', $library_B, $library_A, $library_C ) ); +$dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" ); + +my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 ); +C4::HoldsQueue::CreateQueue(); +my $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} }); +is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from least cost branch" ); +# End Bug 14297 + +# Cleanup +$dbh->rollback; + -- 1.7.2.5