I’m trying to modify my test so that any database work is rolled back at completion. However, it seems that somewhere in the code I’m testing $dbh->commit is being explicitly called. Is there a way to override the commit method of a DBI::db object to turn of commits entirely?
I’ve tried using Test::MockObject::Extends, but that seems to cripple the $dbh:
$dbh = ConnectToDB();
$dbh = Test::MockObject::Extends->new( $dbh );
$dbh->mock( 'commit', sub { warn("Caught you committing.") } );
Later on when I try to use the $dbh to do some work, I get:
Can’t locate object method “fetchrow_array” via package “T::MO::::st”
Looks like the $sth’s I ended up creating with the mocked $dbh aren’t set up correctly. Does anybody have a good method for turning commits off completely for a particular $dbh?
The database I’m connecting to is Oracle, for what it’s worth.
Update
As it turns out, we were overriding &DBI::db::commit in a method very similar to Axeman’s answer, but the commit was actually happening due to a rogue commit in a stored procedure that was being called during the run of my test.
You could do this Q&D idea:
Yes, this is a fair degree of Perl black magic.
You will have to sanity test it. Since this should only be used when testing, you should be able to partition the code so there is no chance of messing around like this in any running code.