Bug 24817: Replace sleep calls with date setting in test data
authorMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 6 Mar 2020 14:47:00 +0000 (14:47 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Mon, 9 Mar 2020 10:38:35 +0000 (10:38 +0000)
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
t/db_dependent/Koha/Cash/Register.t

index e24fb06..dfd1881 100644 (file)
@@ -203,31 +203,24 @@ subtest 'cashup' => sub {
     subtest 'outstanding_accountlines' => sub {
         plan tests => 4;
 
     subtest 'outstanding_accountlines' => sub {
         plan tests => 4;
 
-        # add_cashup should not happen simultaneously with any other action
-        # that results in an accountline attached to the same cash register.
-        # In testing, we need to sleep for a second after each action that
-        # adds to the database. (We cannot use Time::Fake as timestamps are
-        # being added at the DB level, not in perl.
         my $accountline1 = $builder->build_object(
             {
                 class => 'Koha::Account::Lines',
         my $accountline1 = $builder->build_object(
             {
                 class => 'Koha::Account::Lines',
-                value => { register_id => $register->id },
+                value => { register_id => $register->id, timestamp => \'NOW() - INTERVAL 5 MINUTE' },
             }
         );
         my $accountline2 = $builder->build_object(
             {
                 class => 'Koha::Account::Lines',
             }
         );
         my $accountline2 = $builder->build_object(
             {
                 class => 'Koha::Account::Lines',
-                value => { register_id => $register->id },
+                value => { register_id => $register->id, timestamp => \'NOW() - INTERVAL 5 MINUTE'},
             }
         );
             }
         );
-        sleep 1;
 
         my $accountlines = $register->outstanding_accountlines;
         is( $accountlines->count, 2, 'No cashup, all accountlines returned' );
 
         my $cashup3 =
           $register->add_cashup( { manager_id => $patron->id, amount => '2.50' } );
 
         my $accountlines = $register->outstanding_accountlines;
         is( $accountlines->count, 2, 'No cashup, all accountlines returned' );
 
         my $cashup3 =
           $register->add_cashup( { manager_id => $patron->id, amount => '2.50' } );
-        sleep 1;
 
         $accountlines = $register->outstanding_accountlines;
         is( $accountlines->count, 0, 'Cashup added, no accountlines returned' );
 
         $accountlines = $register->outstanding_accountlines;
         is( $accountlines->count, 0, 'Cashup added, no accountlines returned' );
@@ -238,7 +231,10 @@ subtest 'cashup' => sub {
                 value => { register_id => $register->id },
             }
         );
                 value => { register_id => $register->id },
             }
         );
-        sleep 1;
+
+        # Fake the cashup timestamp to make sure it's before the accountline we just added,
+        # we can't trust that these two actions are more than a second apart in a test
+        $cashup3->timestamp(\'NOW() - INTERVAL 2 MINUTE')->store;
 
         $accountlines = $register->outstanding_accountlines;
         is( $accountlines->count, 1,
 
         $accountlines = $register->outstanding_accountlines;
         is( $accountlines->count, 1,