DBIx::DataFactory - factory method maker for inserting test data
# schema CREATE TABLE test_factory ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `int` int, `double` double, `string` varchar(255), `text` text DEFAULT NULL, PRIMARY KEY (id) ) DEFAULT CHARSET=binary; # in your t/*.t use DBIx::DataFactory; my $factory_maker = DBIx::DataFactory->new({ username => 'nobody', password => 'nobody', dsn => 'dbi:mysql:dbname=test_factory;host=localhost', }); $factory_maker->create_factory_method( method => 'create_factory_data', table => 'test_factory', auto_inserted_columns => { int => { type => 'Int', size => 8, }, double => sub { rand(100) }, string => { type => 'Str', size => 10, }, }, ); my $values = $factory_maker->create_factory_data( text => 'test text', ); # or you can use DBIx::DataFactory->create_factory_data() my $int = $values->{int}; my $text = $values->{text}; # will insert following data # +----+----------+------------------+------------+-----------+ # | id | int | double | string | text | # +----+----------+------------------+------------+-----------+ # | 1 | 60194256 | 3.03977754238112 | fHt4X0JDr9 | test text | # +----+----------+------------------+------------+-----------+ $values = $factory_maker->create_factory_data( int => 1, string => 'test', ); # will insert following data # +----+------+-----------------+--------+------+ # | id | int | double | string | text | # +----+------+-----------------+--------+------+ # | 2 | 1 | 71.159467713824 | test | NULL | # +----+------+-----------------+--------+------+
This module helps you to make factory method for inserting data into database. You can use this as fixture replacement.
Create a new DBIx::DataFactory object.
# set up by username, password, and dsn my $factory_maker = DBIx::DataFactory->new({ username => 'nobody', password => 'nobody', dsn => 'dbi:mysql:dbname=test_factory;host=localhost', }); # or set up by db handler my $dbh = DBI->connect( 'dbi:mysql:dbname=test_factory;host=localhost', 'nobody', 'nobody', ); my $factory_maker = DBIx::DataFactory->new({ dbh => $dbh, });
Set up initial state by following parameters.
username
Database username.
password
Database password
dsn
Database dsn
dbh
Database handler
This installs the method, which helps inserting data into database, in the DBIx::DataFactory package by default.
$factory_maker->create_factory_method( method => 'create_factory_data', table => 'test_factory', auto_inserted_columns => { int => { type => 'Int', size => 8, }, string => { type => 'Str', size => 10, }, }, );
if this is the case, this make the method named 'create_factory_data'. you can pass all columns value you defined in schema.
my $values = $factory_maker->create_factory_data( int => 5, string => 'string', text => 'test text', ); # this makes following data. +----+-----+--------+-----------+ | id | int | string | text | +----+-----+--------+-----------+ | 1 | 5 | string | test text | +----+-----+--------+-----------+ my $values = $factory_maker->create_factory_data; # this makes following data +----+----------+------------+------+ | id | int | string | text | +----+----------+------------+------+ | 2 | 59483011 | 9svzODgYyz | NULL | +----+----------+------------+------+
method
Required parameter. method name you want to create.
table
Required parameter. database table name.
optional parameter. database dsn.
optional parameter. database username.
optional parameter. database password.
auto_inserted_columns
optional parameter. if you have the table column which you want to insert data into automatically by default, you can specify this parameter.
for example, if you have columns named 'int', 'string', and 'text', you can specify following.
$factory_maker->create_factory_method( method => 'create_factory_data', table => 'test_factory', auto_inserted_columns => { int => { type => 'Int', size => 8, }, string => { type => 'Str', size => 10, }, text => sub { String::Random->new->randregex('[a-z]{50}') } }, );
if passed hashref, the method inserts data which is defined in specified type class automatically by default. see also DBIx::DataFactory::Type.
if passed coderef, the method inserts value which the code returns.
Of cource, if you specify column value in installed method, the setting for the column is not used.
install_package
optional parameter. if you want to install the factory method to package except DBIx::DataFactory, please specify.
$factory_maker->create_factory_method( method => 'create_factory_data', table => 'test_factory', install_package => 'test::DBIx::DataFactory', );
creator
optional parameter. if you want to use original method for creating data, please specify coderef.
DBIx::DataFactory passes values for inserting to code. the method created by create_factory_method returns values which passed coderef returns.
this is probably useful when you use ORM and set up trigger, or when you want to use blessed value as return value.
For example,
$factory_maker->create_factory_method( method => 'create_factory_data', table => 'test_factory', creator => sub { my ($values) = @_; my $db = DBIx::Simple->connect( 'dbi:mysql:test_factory', 'root', '', ); # your setting $db->abstract = SQL::Abstract->new; my $result = $db->insert('test_factory', $values); return $result; # this is used for return value of create_factory_data }, );
you can add type class which define the rule of inserting data. See also DBIx::DataFactory::Type.
DBIx::DataFactory->add_type('DBIx::DataFactory::Type::Test');
https://github.com/shibayu36/p5-DBIx-DataFactory
C<< <shibayu36 {at} gmail.com> >>
Copyright (c) 2011, Yuki Shibazaki <shibayu36 {at} gmail.com>. All rights reserved.
<shibayu36 {at} gmail.com>
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
To install DBIx::DataFactory, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::DataFactory
CPAN shell
perl -MCPAN -e shell install DBIx::DataFactory
For more information on module installation, please visit the detailed CPAN module installation guide.