View | Details | Raw Unified | Return to bug 14256
Collapse All | Expand All

(-)a/t/lib/TestBuilder.pm (-8 / +57 lines)
Lines 155-167 sub _buildColumnValues { Link Here
155
155
156
    my $col_values;
156
    my $col_values;
157
    my @columns = $self->schema->source($source)->columns;
157
    my @columns = $self->schema->source($source)->columns;
158
    for my $col_name( @columns ) {
158
    my @primary_columns = $self->schema->source($source)->primary_columns();
159
        my $col_value = $self->_buildColumnValue({
159
    my %unique_constraints = $self->schema->source($source)->unique_constraints();
160
            source      => $source,
160
161
            column_name => $col_name,
161
    my $values_ok = 0;
162
            value       => $value,
162
    my $at_least_one_constraint_failed;
163
        });
163
164
        $col_values->{$col_name} = $col_value if( defined( $col_value ) );
164
    while ( not $values_ok ) {
165
166
        $at_least_one_constraint_failed = 0;
167
        # generate random values
168
        for my $col_name( @columns ) {
169
            my $col_value = $self->_buildColumnValue({
170
                source      => $source,
171
                column_name => $col_name,
172
                value       => $value,
173
            });
174
            $col_values->{$col_name} = $col_value if( defined( $col_value ) );
175
        }
176
177
        # If default values are set, maybe the data exist in the DB
178
        # But no need to wait for another value
179
        last if exists( $default_value->{$source} );
180
181
        if ( scalar keys %unique_constraints > 0 ) {
182
183
            # verify the data would respect each unique constraint
184
            foreach my $constraint (keys %unique_constraints) {
185
186
                my $condition;
187
                my @constraint_columns = $unique_constraints{$constraint};
188
                # loop through all constraint columns and build the condition
189
                foreach my $constraint_column ( @constraint_columns ) {
190
                    # build the filter
191
                    $condition->{ $constraint_column } =
192
                            $col_values->{ $constraint_column };
193
                }
194
195
                my $count = $self->schema
196
                                 ->resultset( $source )
197
                                 ->search( $condition )
198
                                 ->count();
199
                if ( $count > 0 ) {
200
                    $at_least_one_constraint_failed = 1;
201
                    # no point checking more stuff, exit the loop
202
                    last;
203
                }
204
            }
205
206
            if ( $at_least_one_constraint_failed ) {
207
                $values_ok = 0;
208
            } else {
209
                $values_ok = 1;
210
            }
211
212
        } else {
213
            $values_ok = 1;
214
        }
165
    }
215
    }
166
    return $col_values;
216
    return $col_values;
167
}
217
}
168
- 

Return to bug 14256