Bigger Baby Moose Steps
So back to test case day here in the Moose-Pen
So in my last post I was successfully passing my first test case 00_load.t now before I move on to my next test case I have to do a little planning.
I have to keep in mind my end goal, pass in a $dbh (a DBI Database Handle) to one of the CRUD functions on an instantiated Database::Accessor calss, parse the various Accessor, attributes convert that to the appropriate SQL, the with DBI, prepare the query to the a $sth (DBI Statement Handle), bind any variables to the $sth, execute the query and return the results.
Now the DBD I am going to work with is "ExampleP" now I am not sure what will happen if I try any of the above DBI actions will have. So I have to do a little experimenting with DBI before I go any further.
What I need to do is mimic the process I would normally do like this;
my $dbh = DBI->connect("dbi:ExampleP:", '', '');
my $sth = $dbh->prepare("select * from test");
$sth->execute();
Now fortunately the above worked as long as I created a folder called 'test' in my 't' folder because that is what that DBD::ExampleP expects from that SQL statement. Next I treied;
my $sth = $dbh->prepare("update test");
and I got;
DBD::ExampleP::db prepare failed: Syntax error in select statement ("update test") at 00_load.t line 17.
So that 'ExampleP' DBD is very limited in scope, seems it only has a select in it,pity. I could get at lest the 'selects' tested but that would only test a small part of my application. I think I will still keep the test case as is. I just will not be avle to use the 'ExampleP' DBD for futher tests.
Well I went back and had a look at some of the other 'default' DBDs that are installed as part of the DBI bundle. by checking on a clean Perl system for both Windows and Linux type systems. I found two that will be present on every Perl install, DBD::File and DBD::DBM.
So I checked out DBD::File and this one is really limited in what one can do with it so I skipped over to DBD::DBM and read this on the first few lines;
DBD::DBM is a database management system that works right out of the box. If you have a standard installation of Perl and DBI you can begin creating, accessing, and modifying simple database tables without any further modules.
Ok so that is one for me. I can use this one to create a test Database and create a few canned Database::Accessor classes that I can then test against. As a bonus I will have some example Accessor classes that I can point people to.
Now what I will need to do is set up a script that will create a test DB if it does not exist, then it will drop a set of tables and then recreate them so I know I have a clean working set. Next I will add in a few records to work with. At this stage in the game I will just start with a single table as I get more involved in my tested I will add more in so I can test SQL Joins or as we call them in Accessor 'Links'.
So my little test script looks like this
#!perl
use DBI;
my $dbh = DBI->connect('dbi:DBM:',undef,undef,{f_dir=>"test/db"});
my @sql = qw {DROP TABLE IF EXISTS users;
CREATE TABLE user ( username TEXT, address TEXT);
INSERT INTO user VALUES ( 'user1', 1);
INSERT INTO user VALUES ( 'user2', 2);
};
foreach my $sql (@sql ){
$dbh->do($sql);
}
and next I will have to create a Accessor.pm class for this which I will put in the Test::User name-space and that will look like this;
package Test::DB::User;
use Moose;
use parent qw(Database::Accessor);
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
return $class->$orig({
view=>{name=>'user'},
elements=>[{name=> 'username'},
{name=> 'address'}]});
};
1;
and finally the stubbed in test
use Test::More;
use Test::Fatal;
use DBI;
use Database::Accessor;
use Test::DB::User;
do 'scripts\new_db.pl';
my $user = Test::DB::User->new();
my $dbh = DBI->connect('dbi:DBM:',undef,undef,{f_dir=>"db"});
my $container = {};
eval{
$user->create($dbh,{username=>'user_new',
address =>'address_new'});
};
if ($@) {
fail("Create function error=$@");
}
else {
pass("Create function");
}
Now I will really be cooking with gas if I can get at least to the test part.
Leave a comment