NAME
Test::DataLoader::MySQL - Load testdata into MySQL database
SYNOPSIS
my $data = Test::DataLoader::MySQL->new($dbh);
$data->add('foo', #table name
1, # data id
{# data_href: column => value
id => 1,
name => 'aaa',
},
['id']); # primary keys
$data->add('foo', 2,
{
id => 2,
name => 'bbb',
},
['id']);
$data->load('foo', 1); #load data into database
# ... tests using database
$data->clear;# when finished
if table has auto_increment
$data->add('foo', 1,
{
name => 'aaa',
},
['id']);
my $keys = $data->load('foo', 1);#load data and get auto_increment
is( $keys->{id}, 2); # get key value(generated by auto_increment)
# ... tests using database
$data->clear;# when finished
read from external file
# data.pm
my $data = Test::DataLoader::MySQL->init(); # use init(not new)
$data->add('foo', 1,
{
id => 1,
name => 'aaa',
},
['id']);
# in your testcode
my $data = Test::DataLoader::MySQL->new($dbh);
$data->load('foo', 1);
# ... tests using database
$data->clear;# when finished
DESCRIPTION
Load testdata into MySQL database.
methods
new
create new instance parameter $dbh(provided by DBI) is required; If Keep option is NOT specified(default), loaded data is deleted when instance is destroyed, otherwise(specified Keep option) loaded data is remain.
#$dbh = DBI->connect(...);
my $data = Test::DataLoader::MySQL->new($dbh); # loaded data is deleted when $data is DESTROYed
# or
my $data = Test::DataLoader::MySQL->new($dbh, Keep => 1); # loaded data is remain
if you want to use external file and in external file, use init() instead of new().
add
add testdata into this modules (not loading testdata)
$data->add('foo', # table_name
1, # data_id,
{ # data which you want to load into database. specified by hash_ref
id => 1,
name => 'aaa',
},
['id'] #key(s), specified by array_ref, this is important.
);
table_name and data_id is like a database's key. For example, table_name is 'foo' and data_id is 1 and 'foo' and 2 is dealt with defferent data even if contained data is equal( ex id=>1, name=>'aaa').
Key is important, because when $data is DESTROYed, this module delete all data which had been loaded and deleted data is found by specified key(s) in this method.
load
load testdata from this module into database.
$data->load('foo', 1);
first parameter is table_name, second parameter is data_id. meaning of them are same as specified in add-method. third parameter is option href, if you want to alter data with add method. for example,
$data->add('foo', 1, { id=>1, name=>'aaa' }); #registered name is 'aaa'
$data->load('foo', 1, { name=>'bbb' }); #but loaded name is 'bbb' because option href is specified.
return hash_ref. it contains database key and value. this is useful for AUTO_INCREMENT key.
my $key = $data->load('foo', 1);
my $id = $key->{id};
load_file
add data from external file
$data->load_file('data.pm');
parameter is filename.
init
create new instance for external file
my $data = Test::DataLoader::MySQL->init();
#$data->add(...
do_select
do select statement
$data->do_select('foo', "id=1");
first parameter is table_name which you want to select. second parameter is where closure. Omitting second parameter is not allowed, if you want to use all data, use condition which is aloways true such as "1=1".
clear
clear all loaded data from database;
AUTHOR
Takuya Tsuchida <tsucchi@cpan.org>
REPOSITORY
http://github.com/tsucchi/Test-DataLoader-MySQL
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.