An Introduction to Test::MockDBI
by Mark Leighton Fisher
|
Some Examples
To make:
$dbh = DBI->connect("dbi:AZ:universe", "mortal", "(none)");
fail, add the rule:
$tmd->bad_method("connect", 1,
"CONNECT TO dbi:AZ:universe AS mortal WITH \\(none\\)");
(where $tmd is the only Test::MockDBI object, which you obtain
through Test::MockDBI's get_instance() method).
To make a SQL SELECT failure when using
DBI::execute(), use the rule:
$tmd->bad_method("execute", 1,
"SELECT zip_plus_4 from zipcodes where state='IN'");
This rule implies that:
- The
DBI::connect() succeeded().
- The
DBI::prepare() succeeded().
- But the
DBI::execute() failed as it should.
A common use of direct scalar return values is returning configuration data,
such as a U.S. zip code for an address:
$tmd->set_retval_scalar(1,
"zip5.*'IN'.*'NOBLESVILLE'.*'170 WESTFIELD RD'",
[ 46062 ]);
This demonstrates using a regular expression, as matching SQL could then
look like this:
SELECT
zip5
FROM
zipcodes
WHERE
state='IN' AND
city='NOBLESVILLE' AND
street_address='170 WESTFIELD RD'
and the rule would match.
SELECTs that return one or more rows from the database are the
common case:
my $counter = 0; # name counter
sub possibly_evil_names {
$counter++;
if ($counter == 1) {
return ('Adolf', 'Germany');
} elsif ($counter == 2) {
return ('Josef', 'U.S.S.R.');
} else {
return ();
}
}
$tmd->set_retval_array(1,
"SELECT\\s+name,\\s+country.*possibly_evil_names",
\&possibly_evil_names);
Using a CODEREF (\&possibly_evil_names) lets you easily add
the state machine for implementing a return of two names followed by an empty
array (because the code uses fetchrow_array() to retrieve each
row). SQL for this query could look like:
SELECT
name,
country
FROM
possibly_evil_names
WHERE
year < 2000
Summary
Albert Einstein once said, "Everything should be made as simple as possible,
but no simpler." This is what I have striven for while developing Test::MockDBI--the simplest possible useful module for testing DBI programs by mocking up
the entire DBI.
Test::MockDBI gives you:
- Complete control of DBI return values and database-returned data.
- Returned database values from either direct value specifications or
CODEREF-generated values.
- Easy, regex-based rules that govern the DBI's behavior, along with
intelligent defaults for the common cases.
- Complete transparency to other code, so the code under test neither knows
nor cares that you are testing it with Test::MockDBI.
- Test data tightly bound to test code, which promotes cohesiveness in your
testing environment, thereby reducing the chance that your tests might silently
fail due to loss of synchronization between your test data and your test
code.
Test::MockDBI is a valuable addition to the arsenal of DBI testing
techniques.
Prev [1] [2] [3]