From e03c7390aedc87e9d6ea06d8e7d8d0f7e3f8ce20 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Fri, 25 Jun 2021 12:25:21 -0300
Subject: [PATCH] Bug 28632: Make patrons.t robust in terms of elapsed time
 between tests

This patch changes the tests so they don't try to compare updated_on
from many tests ago, so it is less fragile.

I added a warn on the unmatched fields for convenience.

To test:
1. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/patrons.t
=> SUCCESS: Tests pass!
2. Make your computer very slow
3. Repeat 1
=> FAIL: You get things like:
t/db_dependent/api/v1/patrons.t .. 3/7
        #   Failed test 'Returned patron from update matches expected'
        #   at t/db_dependent/api/v1/patrons.t line 513.
        #     Structures begin differing at:
        #          $got->{updated_on} = '2021-06-25T14:26:20+00:00'
        #     $expected->{updated_on} = '2021-06-25T14:26:19+00:00'
        # Looks like you failed 1 test of 42.
4. Apply this patch
5. Repeat 1
=> SUCCESS: Tests pass again!
6. Sign off :-D
---
 t/db_dependent/api/v1/patrons.t | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/t/db_dependent/api/v1/patrons.t b/t/db_dependent/api/v1/patrons.t
index 441120f23a..40b1506d44 100755
--- a/t/db_dependent/api/v1/patrons.t
+++ b/t/db_dependent/api/v1/patrons.t
@@ -462,6 +462,8 @@ subtest 'update() tests' => sub {
         delete $newpatron->{patron_id};
         delete $newpatron->{restricted};
         delete $newpatron->{anonymized};
+        # delete field set by a trigger because it is fragile for comparisons
+        delete $newpatron->{updated_on};
 
         $t->put_ok("//$userid:$password@/api/v1/patrons/-1" => json => $newpatron)
           ->status_is(404)
@@ -528,13 +530,23 @@ subtest 'update() tests' => sub {
         $newpatron->{ surname }    = "user" . $patron_1->id.$patron_2->id;
 
         my $result = $t->put_ok( "//$userid:$password@/api/v1/patrons/" . $patron_2->borrowernumber => json => $newpatron )
-          ->status_is(200, 'Patron updated successfully');
-
-        # Put back the RO attributes
-        $newpatron->{patron_id} = $unauthorized_patron->to_api->{patron_id};
-        $newpatron->{restricted} = $unauthorized_patron->to_api->{restricted};
-        $newpatron->{anonymized} = $unauthorized_patron->to_api->{anonymized};
-        is_deeply($result->tx->res->json, $newpatron, 'Returned patron from update matches expected');
+          ->status_is(200, 'Patron updated successfully')->tx->res->json;
+
+        # Test the updated attributes are returned correctly
+        my $matches    = 0;
+        my @attributes = keys %{$newpatron};
+        foreach my $attribute (@attributes) {
+            if ( exists $result->{$attribute}
+              and $result->{$attribute} eq $newpatron->{$attribute} ) {
+                  $matches++;
+              }
+              else {
+                  warn $result->{$attribute} . " vs " . $newpatron->{$attribute} . " ($attribute)";
+              }
+
+        }
+
+        is( $matches, scalar @attributes, 'Returned patron from update matches expected' );
 
         is(Koha::Patrons->find( $patron_2->id )->cardnumber,
            $newpatron->{ cardnumber }, 'Patron is really updated!');
-- 
2.32.0