t::lib::TestBuilder.pm - Koha module to create test records


    use t::lib::TestBuilder;
    my $builder = t::lib::TestBuilder->new;

    # The following call creates a patron, linked to branch CPL.
    # Surname is provided, other columns are randomly generated.
    # Branch CPL is created if it does not exist.
    my $patron = $builder->build({
        source => 'Borrower',
        value  => { surname => 'Jansen', branchcode => 'CPL' },


This module automatically creates database records for you. If needed, records for foreign keys are created too. Values will be randomly generated if not passed to TestBuilder. Note that you should wrap these actions in a transaction yourself.



    my $builder = t::lib::TestBuilder->new;

    Constructor - Returns the object TestBuilder


    my $schema = $builder->schema;

    Getter - Returns the schema of DBIx::Class


        source => $source,
        records => $patron, # OR: records => [ $patron, ... ],

    Delete individual records, created by builder.
    Returns the number of delete attempts, or undef.


    $builder->build({ source  => $source_name, value => $value });

    Create a test record in the table, represented by $source_name.
    The name is required and must conform to the DBIx::Class schema.
    Values may be specified by the optional $value hashref. Will be
    randomized otherwise.
    If needed, TestBuilder creates linked records for foreign keys.
    Returns the values of the new record as a hashref, or undef if
    the record could not be created.

    Note that build also supports recursive hash references inside the
    value hash for foreign key columns, like:
        value => {
            column1 => 'some_value',
            fk_col2 => {
                columnA => 'another_value',
    The hash for fk_col2 here means: create a linked record with build
    where columnA has this value. In case of a composite FK the hashes
    are merged.

    Realize that passing primary key values to build may result in undef
    if a record with that primary key already exists.


Given a plural Koha::Object-derived class, it creates a random element, and returns the corresponding Koha::Object.

    my $patron = $builder->build_object({ class => 'Koha::Patrons' [, value => { ... }] });


Yohann Dufour <yohann.dufour@biblibre.com>

Koha Development Team


Copyright 2014 - Biblibre SARL


This file is part of Koha.

Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Koha; if not, see <http://www.gnu.org/licenses>.