Bigger Baby Moose

So it is write a little code day here at the Moose-Pen.

I now have my first test case checked in thanks to yesterdays post and a few min setting up a new GitHub repo.

I also found my first bug as I used the code from this post as a template for my first crack at 'Database::Accessor::Driver::DBI'. After hanging out the name-space from SQL to DBI I ran my test and got


first argument should be a role at C:/Dwimperl/perl/site/lib/MooseX/Test/Role.pm line 41
MooseX::Test::Role::consuming_class('Database::Accessor::Driver::DBI') called at 00_load.t line 16
# Looks like your test exited with 255 just after 2.
ok 1 - require Database::Accessor;
ok 2 - require DBI;

seem in my haste the other day to get post up I did this mistake

use Moose;

and I should have had

use Moose::Role;

so after correcting that I get

ok 1 - require Database::Accessor;
ok 2 - require DBI;
ok 3 - Does Driver Role

As I said yesterday this test does not give me much all it proves is that 'DBI.pm' consumes the correct role. I can infer from this that the two required subs 'DB_Class' and 'execute' from the 'Database::Accessor::Roles::Driver' are present. Just for a hoot I renamed one of them

--sub DB_Class {
++sub DB_Class_no {

to see what happens and then I get

ok 1 - require Database::Accessor;
ok 2 - require DBI;
ok 3 - Does Driver Role

ouch that's not good. After a few minutes of looking about and doing a little reading I realized that the 'consuming_class' call will not go any deeper than one level so it really is just a good check to see if my DBI.pm is a Moose::Role.

So I will have to expand that test somewhat if I want it to be really useful.

You may remember this pattern in my Accssor.pm test cases


my $in_hash = { view => { name => 'name' }};
my $da = Database::Accessor->new($in_hash);
my $return = {};
$da->retrieve( Data::Test->new(), $return );

Now in my DBI.pm case I can try the same sort of thing I just have to replace that call to 'Data::Test->new()' to a call to DBI. I a perfect world I wold just do this

$da->retrieve(DBI->new(), $return );

Oh course I can't do that as there is no 'new' on DBI. I need to connect some-how and then get a dbh (Database Handle) back that I will then pass into Accessor. Now I know I have and Oracle 11g box and DBD::Oracle installed on my box but I know the vast majority of other people don't so I have to come up with the generic solution.

Luckily DBI does install a DBD or two with its bundle and I can find those out with this


my @drivers = DBI->available_drivers();

on my box I see that I get

[
'ADO',
'DBM',
'ExampleP',
'Gofer',
'Oracle',
'Pg',
'Proxy',
];

Now the one I know is always going to be there is 'ExampleP' which DBI uses in it testing and after a quick peek in the 03handle.t test case from DBI I think I can just do this

my $dbh = DBI->connect("dbi:ExampleP:", '', '');
print ref($dbh);

and from another little test sctipt I know that $dbh will be a 'DBI::db'. So now that I know that I can set up my test like this

my $in_hash = { view => { name => 'name' }};
my $da = Database::Accessor->new($in_hash);
my $return = {};
my $dbh = DBI->connect("dbi:ExampleP:", '', '');
eval { $da->retrieve( $dbh, $return ); };
if ($@) {
fail("Can not load Database::Accessor::Driver::DBI erro=$@");
}
else {
pass("Database::Accessor::Driver::DBI Loaded");
}

here I know from my programming with 'Accessor.pm' this will load the Driver or not. So I give it a go and I get

not ok 4 - Can not load Database::Accessor::Driver::DBI error=Can't locate object method "new" via package "Database::Accessor::Driver::DBI" at D:\GitHub\database-accessor\lib/Database/Accessor.pm line 347.

opps!. Well after a little snooping about I remembered that I was correct with my first code post. The DBD drivers are not Moose::Roles they are Moose classes proper so I guess you can just ignore the first part of this post.

So in the end once I reverted the use Moose::Role to just plain use Moose, dropped out the role consumes test and updated DBI.pm so sub DB_Class returns 'DBI::db' I get this when I retest;


ok 1 - require Database::Accessor;
ok 2 - require DBI;
ok 3 - Database::Accessor::Driver::DBI Loaded

Well now at least I know that both the subs ' DB_Class' and 'execute' are present and at lease 'DB_Class' returns the correct value. Its a start.

facebook-com-15-Moose.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations