José Diaz Seng > DBIx-Table-TestDataGenerator > DBIx::Table::TestDataGenerator



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.005   Source  


DBIx::Table::TestDataGenerator - Automatic test data creation, cross DBMS


Version 0.0.5


        use DBIx::Table::TestDataGenerator;

        my $generator = DBIx::Table::TestDataGenerator->new(
                dsn                    => 'dbi:Pg:dbname=testdb',
                user                   => 'jose',
                password               => '1234',
                table                  => 'employees',

        #simple usage:
                target_size            => 1000000,
                num_random             => 1000,

        #extended usage handling a self-reference of the target table:
                target_size        => 1000000,
                num_random         => 1000,
                max_tree_depth     => 8,
                min_children       => 2,
                min_roots          => 20,

        #dump test data to a csv file instead
                target_size        => 1000000,
                num_random         => 1000,
                csv_dir            => '/usr/local/tdg_dump',


The purpose of this module is fuss-free adding of (bulk) test data to database tables and having test data resembling the data already present in the table.

The current module analyzes the structure of a target table and adds a desired number of records. The column values come either from the table itself (incremented if necessary to satisfy uniqueness constraints) or from tables referenced by foreign key constraints. These values are chosen randomly for a number of runs chosen by the user, and afterwards the values are chosen randomly from a cache containing the already chosen values, thus reducing database traffic for performance reasons. (Even when using the cache, there is some randomness involved since the choices from the different caches are random, too.)

A main goal of the module is to reduce configuration to the absolute minimum by automatically determining information about the target table, in particular its constraints and if a primary key column is an auto-increment column or not. Another goal is to support as many DBMSs as possible, which has been achieved by basing it on DBIx::Class modules.

In the synopsis, an extended usage has been mentioned. This refers to the common case of having a self-reference on a table, i.e. a one-column wide foreign key of a table to itself where the referenced column constitutes the primary key. Such a parent-child relationship defines a forest, i.e. a collection of trees. When generating test data it may be useful to have some control over the growth of the trees. One such case is when the parent-child relation represents a navigation tree and a client application processes this structure. In this case, one would like to have a meaningful, balanced tree structure since this corresponds to real-world examples. To control tree creation the parameters max_tree_depth, min_children and min_roots are provided. The tree nodes are being added in a depth-first, right-to-left manner.

You can find an example in the "examples" folder. It contains a script example.sql defining and filling some tables in your target database as well as a small program using the current module.




Return value:

A new TestDataGenerator object


Read-only accessor for the DBI data source name.


Read-only accessor for the database user.


Read-only accessor for the database user's password.


Read-only accessor for the name of the target table.


Read-only accessor for path to a file containing the definition of the install test database.


Externally read-only accessor for the DBIx::Class schema created from the target database.


This is the main method, it creates and adds new records to the target table resp. dumps the data to a csv file, see below. In case one of the arguments max_tree_depth, min_children or min_roots has been provided, the other two must be provided as well.



Nothing, only called for the side-effect of adding new records to the target table. (This may change, see the section "FURTHER DEVELOPMENT".)


Arguments: none

Allows to disconnect the connection to the target database in case keep_connection_alive was set to true before when calling create_testdata.


To install this module, run the following commands:

        perl Build.PL
        ./Build test
        ./Build install





Please report any bugs or feature requests to bug-dbix-table-testdatagenerator at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc DBIx::Table::TestDataGenerator

You can also look for information at:


Jose Diaz Seng, <josediazseng at>


Copyright (C) 2012-2013, Jose Diaz Seng.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES.

This program 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.

syntax highlighting: