NAME
DBIx::DataFactory - factory method maker for inserting test data
SYNOPSIS
# 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 |
# +----+------+-----------------+--------+------+
DESCRIPTION
This module helps you to make factory method for inserting data into database.
METHODS
$class->new(%args)
Create a new DBIx::DataFactory object.
my $factory_maker = DBIx::DataFactory->new({
username => 'nobody',
password => 'nobody',
dsn => 'dbi:mysql:dbname=test_factory;host=localhost',
});
Set up initial state by following parameters.
username
Database username.
password
Database password
dsn
Database dsn
$self->create_factory_method(%args)
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 |
+----+----------+------------+------+
Parameters
method
Required parameter. method name you want to create.
table
Required parameter. database table name.
dsn
optional parameter. database dsn.
username
optional parameter. database username.
password
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', );
add_type
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');
REPOSITORY
https://github.com/shibayu36/p5-DBIx-DataFactory
AUTHOR
C<< <shibayu36 {at} gmail.com> >>
LICENCE AND COPYRIGHT
Copyright (c) 2011, Yuki Shibazaki <shibayu36 {at} gmail.com>
. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.