Test::DBIx::Class::Factory - Automatically create test data for DBIx::Class
use Test::DBIx::Class::Factory; # Assuming you use Test::DBIx::Class to create your test data schema use Test::DBIx::Class { schema_class => 'Test::DBIx::Class::Example::Schema', }; my $schema = Schema; my $factory = Test::DBIx::Class::Factory->new( schema => $schema ); my $person = $factory->create_record( 'Person', name => 'Gareth Harper' ); is($person->name,'Gareth Harper','Record was created correctly with appropriate name'); # This *should* fail unless we get extremely lucky with random data my $person = $factory->create_record( 'Person' ); is($person->name,'Gareth Harper','Record was created correctly with appropriate name'); # If you create a record which has parents they will automatically be created with random data my $employee = $factory->create_record( 'Company::Employee', employee => { person => { name => "Gareth Harper", } } ); is($employee->employee->person->name,'Gareth Harper','Record was created correctly with appropriate name'); # If you have an existing object you would like to use in the heiarchy my $person = $factory->create_record( 'Person', name => 'Gareth Harper' ); my $employee = $factory->create_record( 'Company::Employee', employee => { person => $person, } ); is($employee->employee->person->name,'Gareth Harper','Record was created correctly with appropriate person');
The goal of this distribution is to make creation of test data for DBIx::Class based applications/libraries much simpler, so your test cases can focus on the actual tests rather than creating the test data that they run on. It does this by allowing you to create objects in the database and automatically creating the object heiarchy (any parent objects required) for you. It will fill any unspecified columns with randomised data so that you do not rely on it in your tests and they will/should break if that data is relied upon.
This instantiates a new factory object, you need to pass in a DBIx::Class::Schema object for it to perform its work on.
my $factory = Test::DBIx::Class::Factory->new( schema => $schema );
This is the main method for this factory. This creates you a DBIx::Class::Row object of your specified type and will automatically create any required parent objects for you.
$factory->create_record( 'Person' );
If you want any specific data in the record you can pass those in as secondary arguments
$factory->create_record( 'Person', name => 'Gareth Harper' );
If you want specific data in a parent record for this entry you can do so by specifying it in the parent relationship name for that record.
$factory->create_record( 'Person::Employee', person => { name => 'Gareth Harper' } );
This also works at arbitrary nested levels.
$factory->create_record( 'Company::Employee', employee => { person => { name => 'Gareth Harper' }, } );
If you have an already created object of the appropriate type you would like to use instead you can also use that.
my $person = $factory->create_record( 'Person', name => 'Gareth Harper' ); $factory->create_record( 'Person::Employee', person => $person );
Given a source name this function will return all of the parent sources (belongs_to) of the source. It is used by create_record to determine which extra sources need creating. It returns an array of hashes of the following format.
my @parents = ({ class => 'Test::DBIx::Class::Example::Schema::Person', source => 'Person', relationship => 'person', });
It can be used as follows.
my @parents = $factory->get_belongs_to('Person');
This method will create some randomised data of the specified type (varchar, float, timestamp etc). It is used internally to fill in the unspecified fields
# This will return a random string of the format 'fds fdsdfas edqw nakqw' my $random_data = $factory->random_data('varchar'); # This will return a datetime object set to a random time/date my $random_data = $factory->random_data('timestamp');
This method will create a randomised word. It is used internally to by random_data.
# This will return a random word of the format 'fdsdfas' my $random_data = $factory->random_word();
This method will create a randomised string. It is used internally to by random_word. You can optionally specify how many words you would like.
# This will return a random word of the format 'fdsdfas fdsfs erwq da' my $random_data = $factory->random_string(); # This will return a random word of the format 'fdsdfas fdsfs' my $random_data = $factory->random_string(2);
The following modules or resources may be of interest.
DBIx::Class, Test::DBIx::Class
Gareth Harper CPAN ID: GHARPER cpan@spansh.co.uk
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Test::DBIx::Class::Factory, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::DBIx::Class::Factory
CPAN shell
perl -MCPAN -e shell install Test::DBIx::Class::Factory
For more information on module installation, please visit the detailed CPAN module installation guide.