Bug 41920: Add unit tests

Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Lucas Gass 2026-09-10 15:32:13 +00:00 committed by Pedro Amorim
parent fd82dca909
commit 8d9e7e641a
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
1 changed files with 48 additions and 1 deletions

View File

@ -20,7 +20,7 @@ use Modern::Perl;
use Test::Exception;
use Test::MockModule;
use Test::NoWarnings;
use Test::More tests => 16;
use Test::More tests => 17;
use Koha::Report;
use Koha::Reports;
@ -62,6 +62,8 @@ is( Koha::Reports->search->count, $nb_of_reports + 1, 'Delete should have delete
subtest 'prep_report' => sub {
plan tests => 4;
t::lib::Mocks::mock_config( 'total_running_reports_per_instance_limit', 0 );
my $report = Koha::Report->new(
{
report_name => 'report_name_for_test_1',
@ -178,6 +180,51 @@ subtest 'prep_report throws when total-running limit is exceeded' => sub {
is( $exception->limit, 1, 'exception carries the configured total limit' );
};
subtest 'prep_report() total_running_reports_per_instance_limit' => sub {
plan tests => 3;
my $report = Koha::Report->new( { report_name => 'total_running_test', savedsql => 'SELECT 1' } )->store;
# the 2 other reports
my $other_1 = $builder->build_object( { class => 'Koha::Reports' } );
my $other_2 = $builder->build_object( { class => 'Koha::Reports' } );
# Pretend two reports belonging to other users are in flight whenever
# running() is called without any filter
my $reports_mock = Test::MockModule->new('Koha::Reports');
$reports_mock->mock(
'running',
sub {
my ( $class, $params ) = @_;
return $class->search( { id => undef } )
if $params && ( $params->{report_id} || $params->{user_id} );
return $class->search( { id => [ $other_1->id, $other_2->id ] } );
}
);
# Turn the other 2 off
t::lib::Mocks::mock_config( 'duplicate_running_reports_per_user_limit', 0 );
t::lib::Mocks::mock_config( 'total_running_reports_per_user_limit', 0 );
# Test no limit
t::lib::Mocks::mock_config( 'total_running_reports_per_instance_limit', 0 );
lives_ok { $report->prep_report( [], [] ) }
'no limit enforced when total_running_reports_per_instance_limit is disabled, AKA 0';
# Test running while staying under the limit
t::lib::Mocks::mock_config( 'total_running_reports_per_instance_limit', 5 );
lives_ok { $report->prep_report( [], [] ) }
'report runs while under the total_running_reports_per_instance_limit';
# Test going over the limit
t::lib::Mocks::mock_config( 'total_running_reports_per_instance_limit', 2 );
throws_ok { $report->prep_report( [], [] ) }
'Koha::Exceptions::Report::InstanceTotalRunning',
'exception thrown if total_running_reports_per_instance_limit is reached';
};
subtest 'is_sql_valid' => sub {
plan tests => 3 + 6 * 2;
my @badwords = ( 'UPDATE', 'DELETE', 'DROP', 'INSERT', 'SHOW', 'CREATE' );