Bugzilla – Attachment 184959 Details for
Bug 28530
Allow configuration of floating limits by item type
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 28530: Add unit tests
Bug-28530-Add-unit-tests.patch (text/plain), 13.89 KB, created by
Lucas Gass (lukeg)
on 2025-07-31 21:14:00 UTC
(
hide
)
Description:
Bug 28530: Add unit tests
Filename:
MIME Type:
Creator:
Lucas Gass (lukeg)
Created:
2025-07-31 21:14:00 UTC
Size:
13.89 KB
patch
obsolete
>From eb61440475f25e86659a22a4d5b871a1195221f7 Mon Sep 17 00:00:00 2001 >From: Kyle M Hall <kyle@bywatersolutions.com> >Date: Mon, 18 Dec 2023 14:23:25 -0500 >Subject: [PATCH] Bug 28530: Add unit tests > >Signed-off-by: Andrew Fuerste-Henry <andrewfh@dubcolib.org> >Signed-off-by: Kris Becker <kbecker@jcls.org> >--- > t/db_dependent/Circulation/Returns.t | 89 ++++++- > t/db_dependent/Koha/Library/FloatLimits.t | 303 ++++++++++++++++++++++ > 2 files changed, 391 insertions(+), 1 deletion(-) > create mode 100755 t/db_dependent/Koha/Library/FloatLimits.t > >diff --git a/t/db_dependent/Circulation/Returns.t b/t/db_dependent/Circulation/Returns.t >index 2f6d326fdfe..5c4453988b8 100755 >--- a/t/db_dependent/Circulation/Returns.t >+++ b/t/db_dependent/Circulation/Returns.t >@@ -18,7 +18,7 @@ > use Modern::Perl; > > use Test::NoWarnings; >-use Test::More tests => 8; >+use Test::More tests => 9; > use Test::MockModule; > use Test::Warn; > >@@ -420,4 +420,91 @@ subtest 'Backdated returns should reduce fine if needed' => sub { > is( $fine, undef, "Fine was removed correctly with a backdated return" ); > }; > >+subtest 'Test library float limits' => sub { >+ plan tests => 2; >+ >+ my $library1 = $builder->build( { source => 'Branch' } ); >+ my $library2 = $builder->build( { source => 'Branch' } ); >+ my $library3 = $builder->build( { source => 'Branch' } ); >+ my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } ); >+ >+ my $biblio = $builder->build_sample_biblio(); >+ >+ for ( 1 .. 5 ) { >+ $builder->build_sample_item( >+ { >+ biblionumber => $biblio->biblionumber, >+ homebranch => $library1->{branchcode}, >+ holdingbranch => $library1->{branchcode}, >+ itype => $itemtype->itemtype, >+ } >+ ); >+ } >+ >+ for ( 1 .. 10 ) { >+ $builder->build_sample_item( >+ { >+ biblionumber => $biblio->biblionumber, >+ homebranch => $library2->{branchcode}, >+ holdingbranch => $library2->{branchcode}, >+ itype => $itemtype->itemtype, >+ } >+ ); >+ } >+ >+ for ( 1 .. 15 ) { >+ $builder->build_sample_item( >+ { >+ biblionumber => $biblio->biblionumber, >+ homebranch => $library3->{branchcode}, >+ holdingbranch => $library3->{branchcode}, >+ itype => $itemtype->itemtype, >+ } >+ ); >+ } >+ >+ my $item = $builder->build_sample_item( >+ { >+ biblionumber => $biblio->biblionumber, >+ homebranch => $library1->{branchcode}, >+ holdingbranch => $library1->{branchcode}, >+ itype => $itemtype->itemtype, >+ } >+ ); >+ >+ my $limit1 = Koha::Library::FloatLimit->new( >+ { >+ branchcode => $library1->{branchcode}, >+ itemtype => $itemtype->itemtype, >+ float_limit => 1, >+ } >+ )->store(); >+ >+ my $limit2 = Koha::Library::FloatLimit->new( >+ { >+ branchcode => $library2->{branchcode}, >+ itemtype => $itemtype->itemtype, >+ float_limit => 100, >+ } >+ )->store(); >+ >+ my $limit3 = Koha::Library::FloatLimit->new( >+ { >+ branchcode => $library3->{branchcode}, >+ itemtype => $itemtype->itemtype, >+ float_limit => 1000, >+ } >+ )->store(); >+ >+ t::lib::Mocks::mock_preference( 'UseLibraryFloatLimits', '0' ); >+ >+ my ( $doreturn, $messages, $iteminformation, $borrower ) = AddReturn( $item->barcode, $item->holdingbranch ); >+ is( $messages->{TransferTrigger}, undef, "Library float limit not triggered if syspref is not enabled" ); >+ >+ t::lib::Mocks::mock_preference( 'UseLibraryFloatLimits', '1' ); >+ >+ ( $doreturn, $messages, $iteminformation, $borrower ) = AddReturn( $item->barcode, $item->holdingbranch ); >+ is( $messages->{TransferTrigger}, 'LibraryFloatLimit', "Library float limit is triggered if syspref is enabled" ); >+}; >+ > $schema->storage->txn_rollback; >diff --git a/t/db_dependent/Koha/Library/FloatLimits.t b/t/db_dependent/Koha/Library/FloatLimits.t >new file mode 100755 >index 00000000000..7881130b92c >--- /dev/null >+++ b/t/db_dependent/Koha/Library/FloatLimits.t >@@ -0,0 +1,303 @@ >+#!/usr/bin/perl >+ >+# Copyright 2023 ByWater Solutions >+# >+# This file is part of Koha >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use Test::More tests => 6; >+ >+use Koha::Database; >+use C4::Circulation qw(CreateBranchTransferLimit); >+use Koha::DateUtils qw( dt_from_string ); >+ >+use t::lib::Mocks; >+use t::lib::TestBuilder; >+ >+BEGIN { >+ use_ok('Koha::Library::FloatLimit'); >+ use_ok('Koha::Library::FloatLimits'); >+} >+ >+my $schema = Koha::Database->new->schema; >+$schema->storage->txn_begin; >+ >+my $builder = t::lib::TestBuilder->new; >+my $library1 = $builder->build( { source => 'Branch' } ); >+my $library2 = $builder->build( { source => 'Branch' } ); >+my $library3 = $builder->build( { source => 'Branch' } ); >+my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } ); >+ >+my $biblio = $builder->build_sample_biblio(); >+ >+for ( 1 .. 5 ) { >+ $builder->build_sample_item( >+ { >+ biblionumber => $biblio->biblionumber, >+ homebranch => $library1->{branchcode}, >+ holdingbranch => $library1->{branchcode}, >+ itype => $itemtype->itemtype, >+ } >+ ); >+} >+ >+for ( 1 .. 10 ) { >+ $builder->build_sample_item( >+ { >+ biblionumber => $biblio->biblionumber, >+ homebranch => $library2->{branchcode}, >+ holdingbranch => $library2->{branchcode}, >+ itype => $itemtype->itemtype, >+ } >+ ); >+} >+ >+for ( 1 .. 15 ) { >+ $builder->build_sample_item( >+ { >+ biblionumber => $biblio->biblionumber, >+ homebranch => $library3->{branchcode}, >+ holdingbranch => $library3->{branchcode}, >+ itype => $itemtype->itemtype, >+ } >+ ); >+} >+ >+my $item = $builder->build_sample_item( >+ { >+ biblionumber => $biblio->biblionumber, >+ homebranch => $library1->{branchcode}, >+ holdingbranch => $library1->{branchcode}, >+ itype => $itemtype->itemtype, >+ } >+); >+ >+my $limit1 = Koha::Library::FloatLimit->new( >+ { >+ branchcode => $library1->{branchcode}, >+ itemtype => $itemtype->itemtype, >+ float_limit => 1, >+ } >+)->store(); >+ >+my $float_limit2 = Koha::Library::FloatLimit->new( >+ { >+ branchcode => $library2->{branchcode}, >+ itemtype => $itemtype->itemtype, >+ float_limit => 100, >+ } >+)->store(); >+ >+my $float_limit3 = Koha::Library::FloatLimit->new( >+ { >+ branchcode => $library3->{branchcode}, >+ itemtype => $itemtype->itemtype, >+ float_limit => 1000, >+ } >+)->store(); >+ >+is( >+ Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, >+ $library3->{branchcode}, >+ "Correct library selected for float limit transfer" >+); >+ >+t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' ); >+t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' ); >+ >+my $to = $library3->{branchcode}; >+my $from = $item->holdingbranch; >+my $code = $itemtype->itemtype; >+CreateBranchTransferLimit( $to, $from, $code ); >+ >+is( C4::Circulation::IsBranchTransferAllowed( $to, $from, $code ), 0, "Transfer to best library is no longer allowed" ); >+say "C4::Circulation::IsBranchTransferAllowed( $to, $from, $code )"; >+ >+subtest 'onloan items excluded from ratio calculation' => sub { >+ plan tests => 5; >+ >+ # Reset transfer limits for clean test >+ t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' ); >+ >+ # Verify initial item counts >+ my $library2_initial = Koha::Items->search( >+ { >+ holdingbranch => $library2->{branchcode}, >+ itype => $itemtype->itemtype, >+ onloan => undef >+ } >+ )->count; >+ >+ my $library3_initial = Koha::Items->search( >+ { >+ holdingbranch => $library3->{branchcode}, >+ itype => $itemtype->itemtype, >+ onloan => undef >+ } >+ )->count; >+ >+ is( $library2_initial, 10, "Library2 initially has 10 available items" ); >+ is( $library3_initial, 15, "Library3 initially has 15 available items" ); >+ >+ # Initial library selection based on ratios >+ is( >+ Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, >+ $library3->{branchcode}, >+ "Library3 initially selected (lowest ratio: 15/1000 = 0.015)" >+ ); >+ >+ # Check out enough items from library3 to change the selection >+ my @library3_items = Koha::Items->search( >+ { >+ holdingbranch => $library3->{branchcode}, >+ itype => $itemtype->itemtype, >+ onloan => undef >+ } >+ )->as_list; >+ >+ my $checkout_date = dt_from_string(); >+ for my $i ( 0 .. 9 ) { >+ $library3_items[$i]->onloan($checkout_date)->store; >+ } >+ >+ # Verify the count decreased >+ my $library3_after_checkout = Koha::Items->search( >+ { >+ holdingbranch => $library3->{branchcode}, >+ itype => $itemtype->itemtype, >+ onloan => undef >+ } >+ )->count; >+ >+ is( $library3_after_checkout, 5, "Library3 now has 5 available items after 10 checkouts" ); >+ >+ # Library3 should still be selected >+ is( >+ Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, >+ $library3->{branchcode}, >+ "Library3 still selected after checkouts (ratio now 5/1000 = 0.005, still better than library2's 10/100 = 0.1)" >+ ); >+}; >+ >+subtest 'items in transit counted toward destination branch' => sub { >+ plan tests => 6; >+ >+ # Reset transfer limits for clean test >+ t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' ); >+ >+ Koha::Items->search( >+ { >+ holdingbranch => $library3->{branchcode}, >+ itype => $itemtype->itemtype, >+ onloan => { '!=' => undef } >+ } >+ )->update( { onloan => undef } ); >+ >+ # Verify initial counts >+ my $library2_initial = Koha::Items->search( >+ { >+ holdingbranch => $library2->{branchcode}, >+ itype => $itemtype->itemtype, >+ onloan => undef >+ } >+ )->count; >+ >+ my $library3_initial = Koha::Items->search( >+ { >+ holdingbranch => $library3->{branchcode}, >+ itype => $itemtype->itemtype, >+ onloan => undef >+ } >+ )->count; >+ >+ is( $library2_initial, 10, "Library2 initially has 10 available items" ); >+ is( $library3_initial, 15, "Library3 initially has 15 available items" ); >+ >+ # Initial selection should be library3 >+ is( >+ Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, >+ $library3->{branchcode}, >+ "Library3 initially selected (ratio: 15/1000 = 0.015)" >+ ); >+ >+ # Create items in transit TO library2 >+ my @transit_items; >+ for ( 1 .. 2 ) { >+ my $transit_item = $builder->build_sample_item( >+ { >+ biblionumber => $biblio->biblionumber, >+ homebranch => $library1->{branchcode}, >+ holdingbranch => $library1->{branchcode}, >+ itype => $itemtype->itemtype, >+ } >+ ); >+ push @transit_items, $transit_item; >+ } >+ >+ # Create active transfers to library2 >+ my $transfer_date = dt_from_string(); >+ for my $transit_item (@transit_items) { >+ Koha::Item::Transfer->new( >+ { >+ itemnumber => $transit_item->itemnumber, >+ frombranch => $library1->{branchcode}, >+ tobranch => $library2->{branchcode}, >+ daterequested => $transfer_date, >+ datesent => $transfer_date, >+ reason => 'LibraryFloatLimit' >+ } >+ )->store; >+ } >+ >+ # Verify that items in transit are counted toward destination >+ my $in_transit_count = Koha::Items->search( >+ { >+ itype => $itemtype->itemtype, >+ }, >+ { >+ join => 'branchtransfers', >+ where => { >+ 'branchtransfers.tobranch' => $library2->{branchcode}, >+ 'branchtransfers.datearrived' => undef, >+ 'branchtransfers.datecancelled' => undef, >+ }, >+ distinct => 1 >+ } >+ )->count; >+ >+ is( $in_transit_count, 2, "2 items are in transit to library2" ); >+ >+ # Test 5: Library2's physical count should still be 10 >+ my $library2_physical = Koha::Items->search( >+ { >+ holdingbranch => $library2->{branchcode}, >+ itype => $itemtype->itemtype, >+ onloan => undef >+ } >+ )->count; >+ >+ is( $library2_physical, 10, "Library2 still has 10 physical items" ); >+ >+ # Library2's count should now be 10 + 2 = 12 >+ is( >+ Koha::Library::FloatLimits->lowest_ratio_library( $item, $library1->{branchcode} )->branchcode, >+ $library3->{branchcode}, >+ "Library3 still selected after items in transit to library2" >+ ); >+}; >+ >+$schema->storage->txn_rollback; >-- >2.39.5
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 28530
:
160038
|
160039
|
160040
|
160041
|
160042
|
160043
|
160044
|
160045
|
160046
|
160047
|
160048
|
160049
|
160050
|
160053
|
160054
|
160055
|
160056
|
160057
|
160058
|
160061
|
160062
|
160063
|
160064
|
160065
|
160066
|
160067
|
160068
|
160071
|
160072
|
160073
|
160074
|
160075
|
160076
|
160077
|
160078
|
160079
|
160080
|
160081
|
160082
|
160083
|
160084
|
163919
|
168343
|
168344
|
168345
|
168346
|
168347
|
168348
|
168349
|
168350
|
178762
|
178763
|
178764
|
178765
|
178766
|
178767
|
178768
|
178769
|
178783
|
178784
|
178785
|
178934
|
178935
|
178936
|
178937
|
178938
|
178939
|
178940
|
178941
|
178942
|
178943
|
178944
|
178945
|
178946
|
179943
|
179944
|
179945
|
179946
|
179947
|
179948
|
179949
|
179950
|
179951
|
179952
|
179953
|
179954
|
179955
|
180028
|
182931
|
184754
|
184755
|
184917
|
184936
|
184951
|
184952
|
184953
|
184954
|
184955
|
184956
|
184957
|
184958
|
184959
|
184960
|
184961
|
184963
|
184965
|
184966
|
184967
|
185038
|
185836
|
185837
|
185838
|
185839
|
185840
|
185841
|
185842
|
185843