Bug 24062: Make sure TestBuilder will stop generate X or other invalid category's...
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 19 Nov 2019 09:14:31 +0000 (10:14 +0100)
committerVictor Grousset/tuxayo <victor@tuxayo.net>
Thu, 11 Jun 2020 16:06:24 +0000 (18:06 +0200)
To make sure this kind of random failures will not appear in a future we
are going to fix it at TestBuilder level.

Test plan:
  prove t/db_dependent/TestBuilder.t
and confirm it returns green

You could also only apply the tests against master, run them several
times and confirm that they fail most of the time.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

(cherry picked from commit fda209ca505a2a0e0099788aa5f654601f1caf07)
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

t/db_dependent/TestBuilder.t
t/lib/TestBuilder.pm

index 6ad1810..3499238 100644 (file)
@@ -341,12 +341,23 @@ subtest 'Date handling' => sub {
 };
 
 subtest 'Default values' => sub {
-    plan tests => 2;
+    plan tests => 3;
+
     $builder = t::lib::TestBuilder->new;
     my $item = $builder->build( { source => 'Item' } );
     is( $item->{more_subfields_xml}, undef, 'This xml field should be undef' );
     $item = $builder->build( { source => 'Item', value => { more_subfields_xml => 'some xml' } } );
     is( $item->{more_subfields_xml}, 'some xml', 'Default should not overwrite assigned value' );
+
+    subtest 'generated dynamically (coderef)' => sub {
+        plan tests => 2;
+        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+        like( $patron->category->category_type, qr{^(A|C|S|I|P|)$}, );
+
+        my $patron_category_X = $builder->build_object({ class => 'Koha::Patron::Categories', value => { category_type => 'X' } });
+        $patron = $builder->build_object({ class => 'Koha::Patrons', value => {categorycode => $patron_category_X->categorycode} });
+        is( $patron->category->category_type, 'X', );
+    };
 };
 
 subtest 'build_object() tests' => sub {
index 72bf0bd..1a7be46 100644 (file)
@@ -401,7 +401,9 @@ sub _buildColumnValue {
         }
         push @$retvalue, $value->{$col_name};
     } elsif( exists $self->{default_values}{$source}{$col_name} ) {
-        push @$retvalue, $self->{default_values}{$source}{$col_name};
+        my $v = $self->{default_values}{$source}{$col_name};
+        $v = &$v() if ref($v) eq 'CODE';
+        push @$retvalue, $v;
     } else {
         my $data_type = $col_info->{data_type};
         $data_type =~ s| |_|;
@@ -550,6 +552,8 @@ sub _gen_default_values {
         Category => {
             enrolmentfee => 0,
             reservefee   => 0,
+            # Not X, used for statistics
+            category_type => sub { return [ qw( A C S I P ) ]->[int(rand(5))] },
         },
         Itemtype => {
             rentalcharge => 0,