From 375df0cd663b3327dafa0007843d4bda701800b0 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 2 Jun 2015 16:09:47 -0300 Subject: [PATCH] Bug 14256: (followup) do not repeat existing values on primary columns This followup patch makes _buildColumnValue test the generated random value so TestBuilder doesn't try to insert repeated values on primary columns. It also removes an unnecesary use of 'eval' that made it difficult to spot errors. Signed-off-by: Tomas Cohen Arazi --- t/lib/TestBuilder.pm | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/t/lib/TestBuilder.pm b/t/lib/TestBuilder.pm index 826ab1a..5fbb149 100644 --- a/t/lib/TestBuilder.pm +++ b/t/lib/TestBuilder.pm @@ -1,9 +1,10 @@ package t::lib::TestBuilder; use Modern::Perl; + use Koha::Database; use String::Random; - +use List::MoreUtils qw(any); my $gen_type = { tinyint => \&_gen_int, @@ -34,6 +35,7 @@ my $gen_type = { mediumblob => \&_gen_blob, blob => \&_gen_blob, longblob => \&_gen_blob, + }; our $default_value = { @@ -238,17 +240,33 @@ sub _buildColumnValue { $col_value = $default_value->{$source}->{$col_name}; } elsif( not $col_info->{default_value} and not $col_info->{is_auto_increment} and not $col_info->{is_foreign_key} ) { - eval { - my $data_type = $col_info->{data_type}; - $data_type =~ s| |_|; - $col_value = $gen_type->{$data_type}->( $self, { info => $col_info } ); - }; - die "The type $col_info->{data_type} is not defined\n" if ($@); + + my $data_type = $col_info->{data_type}; + $data_type =~ s| |_|; + + if ( any { $data_type eq $_ } (keys $gen_type) ) { + my $value_ok = 'no'; + while ( $value_ok eq 'no' ) { + # generate value + $col_value = $gen_type->{$data_type}->( $self, { info => $col_info } ); + # should value be unique? + my $primary_names = $self->schema->source($source)->primary_columns(); + if ( not ( any { $col_name eq $_ } $primary_names and + $self->schema + ->resultset($source) + ->search({ $col_name => $col_value }) + ->count > 0 ) ) { + + $value_ok = 'yes'; + } + } + } else { + die "The type $col_info->{data_type} is not defined\n"; + } } return $col_value; } - sub _gen_int { my ($self, $params) = @_; my $data_type = $params->{info}->{data_type}; -- 2.4.2