Bug 26157: Hide expected DBI warnings from tests
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 6 Aug 2020 11:11:50 +0000 (13:11 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 24 Aug 2020 09:19:03 +0000 (11:19 +0200)
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

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

t/db_dependent/Koha/Account.t
t/db_dependent/Koha/ApiKeys.t
t/db_dependent/Koha/Object.t
t/db_dependent/Koha/Patrons.t
t/db_dependent/Koha/Suggestions.t

index 9b37a08..0941fbb 100755 (executable)
@@ -340,17 +340,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(
index 154f69c..94ce452 100755 (executable)
@@ -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' );
 
index 919472e..cae049c 100755 (executable)
@@ -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
index 4f434ad..5704463 100644 (file)
@@ -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 );
index f4c4974..a4941bb 100644 (file)
@@ -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;