From 20c7973836148d618defef31ca5d43dca98223f9 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 19 Nov 2019 10:14:31 +0100 Subject: [PATCH] Bug 24062: Make sure TestBuilder will stop generate X or other invalid category's types 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 Signed-off-by: Marcel de Rooy Signed-off-by: Martin Renvoize (cherry picked from commit fda209ca505a2a0e0099788aa5f654601f1caf07) Signed-off-by: Victor Grousset/tuxayo --- t/db_dependent/TestBuilder.t | 13 ++++++++++++- t/lib/TestBuilder.pm | 6 +++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/TestBuilder.t b/t/db_dependent/TestBuilder.t index 6ad1810706..3499238fac 100644 --- a/t/db_dependent/TestBuilder.t +++ b/t/db_dependent/TestBuilder.t @@ -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 { diff --git a/t/lib/TestBuilder.pm b/t/lib/TestBuilder.pm index 72bf0bd734..1a7be46ccb 100644 --- a/t/lib/TestBuilder.pm +++ b/t/lib/TestBuilder.pm @@ -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, -- 2.39.5