View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
José Diaz Seng > DBIx-Table-TestDataGenerator-0.002 > DBIx::Table::TestDataGenerator



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.002   Source   Latest Release: DBIx-Table-TestDataGenerator-0.005


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


Version 0.0.1


    use DBIx::Table::TestDataGenerator;

    my $generator = DBIx::Table::TestDataGenerator->new(
            dbh                => $dbi_database_handle,
            schema             => $schema_name,
            table              => $target_table_name,

    #simple usage:
        target_size    => $target_size,
        num_random     => $num_random,
        seed           => $seed,

    #extended usage handling a self-reference of the target table:
            target_size    => $target_size,
            num_random     => $num_random,
            seed           => $seed,
            max_tree_depth => $max_tree_depth,
            min_children   => $min_children,
            min_roots      => $min_roots,

    #instantiation using a custom DBMS handling class
    my $generator = DBIx::Table::TestDataGenerator->new(
            dbh                => $dbi_database_handle,
            schema             => $schema_name,
            table              => $target_table_name,
            custom_probe_class => $custom_probe_class_name,


There is often the need to create test data in database tables, e.g. to test database client performance. The existence of constraints on a table makes it non-trivial to come up with a way to add records to it.

The current module inspects the tables' constraints and adds a desired number of records. The values of the fields either come from the table itself (possibly incremented to satisfy uniqueness constraints) or from tables referenced by foreign key constraints. The choice of the copied values is random for a number of runs the user can choose, afterwards the values are chosen randomly from a cache, reducing database traffic for performance reasons. The user can define seeds for the randomization to be able to reproduce a test run. One nice thing about this way to construct new records is that at least at first sight, the added data looks like real data, at least as real as the data initially present in the table was.

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. Another goal is to support as many DBMSs as possible. Currently Oracle, PostgreSQL and SQLite are supported, further DBMSs are in the work and one can add further databases or change the default behaviour by writing a class satisfying the role defined in NOTE: A major refactoring is on its way, see section FURTHER DEVELOPMENT.

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 rootless tree and when generating test data it may be useful to have some control over the growth of this tree. 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. Note that the nodes are being added in a depth-first manner.




Return value:

a new TestDataGenerator object

Creates a new TestDataGenerator object. If the DBMS in question does not support the concept of a schema, the corresponding argument may be omitted. If a DBMS currently not supported by DBI::Table::TestDataGenerator is to be supported, or the behaviour of the current TableProbe class responsible for handling the DBMS must be changed, one may provide the optional custom_probe_class parameter. custom_probe_class being the name of a custom class impersonating the TableProbe role.


Accessor for the DBI database handle.


Accessor for the database schema name.


Accessor for the name of the target table.


Accessor for the name of a custom class impersonating the TableProbe role.


This is the main method, it creates and adds new records to the target table. 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.)


To install this module, run the following commands:

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

When installing from CPAN, the install tests look for the environment variables TDG_DSN (connection string), TDG_USER (user), TDG_PWD (password) and TDG_SCHEMA (schema) which may be used to test the installation against an existing database. If TDG_DSN is found, the install will try to use this connection string and the tests will fail if no valid database connection can be established. If TDG_DSN is not found, the installation creates an in-memory SQLite database provided for free by the DBD::SQLite module and tests against this database.



The module has been tested on a Windows 7 32-bit machine under Strawberry Perl and in an Ubuntu 12.04 VirtualBox image using perlbrew with naked Perl versions 5.17.6 and 5.10.1.





Jose Diaz Seng, <josediazseng at>


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:


Copyright 2012 Jose Diaz Seng.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See for more information.

syntax highlighting: