From 3139f6c24da19a381f5c5a0be9541ab719680ba6 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 6 Aug 2020 13:11:50 +0200 Subject: [PATCH] Bug 26157: Hide expected DBI warnings from tests We must redirect them to /dev/null if we know they will appear. Test plan: prove the different test files and confirm that the output is clean when this patch is applied --- t/db_dependent/Koha/Account.t | 28 ++++++++++------- t/db_dependent/Koha/ApiKeys.t | 14 +++++++-- t/db_dependent/Koha/Object.t | 11 ++++--- t/db_dependent/Koha/Patrons.t | 10 ++++-- t/db_dependent/Koha/Suggestions.t | 51 +++++++++++++++++++++++-------- 5 files changed, 81 insertions(+), 33 deletions(-) diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t index ebab6b0ae5..9861440167 100755 --- a/t/db_dependent/Koha/Account.t +++ b/t/db_dependent/Koha/Account.t @@ -339,17 +339,23 @@ subtest 'add_debit() tests' => sub { ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)'; throws_ok { - $account->add_debit( - { - amount => 5, - description => 'type validation failure', - library_id => $patron->branchcode, - note => 'this should fail anyway', - type => 'failure', - user_id => $patron->id, - interface => 'commandline' - } - ); } 'Koha::Exceptions::Account::UnrecognisedType', 'Expected validation exception thrown (type)'; + local *STDERR; + open STDERR, '>', '/dev/null'; + $account->add_debit( + { + amount => 5, + description => 'type validation failure', + library_id => $patron->branchcode, + note => 'this should fail anyway', + type => 'failure', + user_id => $patron->id, + interface => 'commandline' + } + ); + close STDERR; + } + 'Koha::Exceptions::Account::UnrecognisedType', + 'Expected validation exception thrown (type)'; throws_ok { $account->add_debit( diff --git a/t/db_dependent/Koha/ApiKeys.t b/t/db_dependent/Koha/ApiKeys.t index 154f69c018..94ce452c8b 100755 --- a/t/db_dependent/Koha/ApiKeys.t +++ b/t/db_dependent/Koha/ApiKeys.t @@ -79,10 +79,18 @@ subtest 'store() tests' => sub { my $deleted_id = $patron_to_delete->id; $patron_to_delete->delete; - throws_ok - { Koha::ApiKey->new({ patron_id => $deleted_id, description => 'a description' })->store } + { # hide useless warnings + local *STDERR; + open STDERR, '>', '/dev/null'; + throws_ok { + Koha::ApiKey->new( + { patron_id => $deleted_id, description => 'a description' } ) + ->store + } 'Koha::Exceptions::Object::FKConstraint', - 'Invalid patron ID raises exception'; + 'Invalid patron ID raises exception'; + close STDERR; + } is( $@->message, 'Broken FK constraint', 'Exception message is correct' ); is( $@->broken_fk, 'patron_id', 'Exception field is correct' ); diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index 919472e874..cae049cbd2 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -620,8 +620,8 @@ subtest 'store() tests' => sub { my $dbh = $schema->storage->dbh; { - local $dbh->{PrintError} = 0; - local $dbh->{RaiseError} = 0; + local *STDERR; + open STDERR, '>', '/dev/null'; throws_ok { $api_key->store } 'Koha::Exceptions::Object::FKConstraint', @@ -662,6 +662,7 @@ subtest 'store() tests' => sub { qr/(api_keys\.)?secret/, 'Exception field is correct (note that MySQL 8 is displaying the tablename)' ); + close STDERR; } # Successful test @@ -714,10 +715,12 @@ subtest 'store() tests' => sub { my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + try { - local $schema->storage->dbh->{RaiseError} = 0; - local $schema->storage->dbh->{PrintError} = 0; + local *STDERR; + open STDERR, '>', '/dev/null'; $patron->lastseen('wrong_value')->store; + close STDERR; } catch { ok( $_->isa('Koha::Exceptions::Object::BadValue'), 'Exception thrown correctly' ); like( $_->property, qr/(borrowers\.)?lastseen/, 'Column should be the expected one' ); # The table name is not always displayed, it depends on the DBMS version diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 4f434adc3c..5704463390 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -1738,10 +1738,14 @@ subtest '->store' => sub { my $patron_1 = $builder->build_object({class=> 'Koha::Patrons'}); my $patron_2 = $builder->build_object({class=> 'Koha::Patrons'}); - throws_ok - { $patron_2->userid($patron_1->userid)->store; } + { + local *STDERR; + open STDERR, '>', '/dev/null'; + throws_ok { $patron_2->userid( $patron_1->userid )->store; } 'Koha::Exceptions::Object::DuplicateID', - 'Koha::Patron->store raises an exception on duplicate ID'; + 'Koha::Patron->store raises an exception on duplicate ID'; + close STDERR; + } # Test password t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); diff --git a/t/db_dependent/Koha/Suggestions.t b/t/db_dependent/Koha/Suggestions.t index f4c4974548..a4941bbc59 100644 --- a/t/db_dependent/Koha/Suggestions.t +++ b/t/db_dependent/Koha/Suggestions.t @@ -125,9 +125,16 @@ subtest 'constraints' => sub { "The suggestion is not deleted when the related branch is deleted" ); # managerid - throws_ok { $suggestion->managedby($nonexistent_borrowernumber)->store; } - 'Koha::Exceptions::Object::FKConstraint', - 'store raises an exception on invalid managerid'; + { # hide useless warnings + local *STDERR; + open STDERR, '>', '/dev/null'; + throws_ok { + $suggestion->managedby($nonexistent_borrowernumber)->store; + } + 'Koha::Exceptions::Object::FKConstraint', + 'store raises an exception on invalid managerid'; + close STDERR; + } my $manager = $builder->build_object( { class => "Koha::Patrons" } ); $suggestion->managedby( $manager->borrowernumber )->store; $manager->delete; @@ -136,9 +143,16 @@ subtest 'constraints' => sub { "The suggestion is not deleted when the related manager is deleted" ); # acceptedby - throws_ok { $suggestion->acceptedby($nonexistent_borrowernumber)->store; } - 'Koha::Exceptions::Object::FKConstraint', - 'store raises an exception on invalid acceptedby id'; + { # hide useless warnings + local *STDERR; + open STDERR, '>', '/dev/null'; + throws_ok { + $suggestion->acceptedby($nonexistent_borrowernumber)->store; + } + 'Koha::Exceptions::Object::FKConstraint', + 'store raises an exception on invalid acceptedby id'; + close STDERR; + } my $acceptor = $builder->build_object( { class => "Koha::Patrons" } ); $suggestion->acceptedby( $acceptor->borrowernumber )->store; $acceptor->delete; @@ -147,9 +161,16 @@ subtest 'constraints' => sub { "The suggestion is not deleted when the related acceptor is deleted" ); # rejectedby - throws_ok { $suggestion->rejectedby($nonexistent_borrowernumber)->store; } - 'Koha::Exceptions::Object::FKConstraint', - 'store raises an exception on invalid rejectedby id'; + { # hide useless warnings + local *STDERR; + open STDERR, '>', '/dev/null'; + throws_ok { + $suggestion->rejectedby($nonexistent_borrowernumber)->store; + } + 'Koha::Exceptions::Object::FKConstraint', + 'store raises an exception on invalid rejectedby id'; + close STDERR; + } my $rejecter = $builder->build_object( { class => "Koha::Patrons" } ); $suggestion->rejectedby( $rejecter->borrowernumber )->store; $rejecter->delete; @@ -158,9 +179,15 @@ subtest 'constraints' => sub { "The suggestion is not deleted when the related rejecter is deleted" ); # budgetid - throws_ok { $suggestion->budgetid($nonexistent_borrowernumber)->store; } - 'Koha::Exceptions::Object::FKConstraint', - 'store raises an exception on invalid budgetid'; + { # hide useless warnings + local *STDERR; + open STDERR, '>', '/dev/null'; + + throws_ok { $suggestion->budgetid($nonexistent_borrowernumber)->store; } + 'Koha::Exceptions::Object::FKConstraint', + 'store raises an exception on invalid budgetid'; + close STDERR; + } my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } ); $suggestion->budgetid( $fund->id )->store; $fund->delete; -- 2.20.1