NAME

DBD::Mock - Mock database driver for testing

SYNOPSIS

use DBI;

# connect to your as normal, using 'Mock' as your driver name
my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
              || die "Cannot create handle: $DBI::errstr\n";

# create a statement handle as normal and execute with parameters
my $sth = $dbh->prepare( 'SELECT this, that FROM foo WHERE id = ?' );
$sth->execute( 15 );

# Now query the statement handle as to what has been done with it
my $mock_params = $sth->{mock_params};
print "Used statement: ", $sth->{mock_statement}, "\n",
      "Bound parameters: ", join( ', ', @{ $mock_params } ), "\n";

DESCRIPTION

Testing with databases can be tricky. If you are developing a system married to a single database then you can make some assumptions about your environment and ask the user to provide relevant connection information. But if you need to test a framework that uses DBI, particularly a framework that uses different types of persistence schemes, then it may be more useful to simply verify what the framework is trying to do -- ensure the right SQL is generated and that the correct parameters are bound. DBD::Mock makes it easy to just modify your configuration (presumably held outside your code) and just use it instead of DBD::Foo (like DBD::Pg or DBD::mysql) in your framework.

There is no distinct area where using this module makes sense. (Some people may successfully argue that this is a solution looking for a problem...) Indeed, if you can assume your users have something like DBD::AnyData or DBD::SQLite or if you do not mind creating a dependency on them then it makes far more sense to use these legitimate driver implementations and test your application in the real world -- at least as much of the real world as you can create in your tests...

And if your database handle exists as a package variable or something else easily replaced at test-time then it may make more sense to use Test::MockObject to create a fully dynamic handle. There is an excellent article by chromatic about using Test::MockObject in this and other ways, strongly recommended. (See "SEE ALSO" for a link)

How does it work?

DBD::Mock comprises a set of classes used by DBI to implement a database driver. But instead of connecting to a datasource and manipulating data found there it tracks all the calls made to the database handle and any created statement handles. You can then inspect them to ensure what you wanted to happen actually happened. For instance, say you have a configuration file with your database connection information:

[DBI]
dsn      = DBI:Pg:dbname=myapp
user     = foo
password = bar

And this file is read in at process startup and the handle stored for other procedures to use:

package ObjectDirectory;

my ( $DBH );

sub run_at_startup {
   my ( $class, $config ) = @_;
   $config ||= read_configuration( ... );
   my $dsn  = $config->{DBI}{dsn};
   my $user = $config->{DBI}{user};
   my $pass = $config->{DBI}{password};
   $DBH = DBI->connect( $dsn, $user, $pass ) || die ...;
}

sub get_database_handle {
   return $DBH;
}

A procedure might use it like this (ignoring any error handling for the moment):

package My::UserActions;

sub fetch_user {
   my ( $class, $login ) = @_;
   my $dbh = ObjectDirectory->get_database_handle;
   my $sql = q{
       SELECT login_name, first_name, last_name, creation_date, num_logins
         FROM users
        WHERE login_name = ?
   };
   my $sth = $dbh->prepare( $sql );
   $sth->execute( $login );
   my $row = $sth->fetchrow_arrayref;
   return ( $row ) ? User->new( $row ) : undef;
}

So for the purposes of our tests we just want to ensure that:

Assume whether the SQL actually works or not is irrelevant for this test :-)

To do that our test might look like:

my $config = ObjectDirectory->read_configuration( ... );
$config->{DBI}{dsn} = 'DBI:Mock:';
ObjectDirectory->run_at_startup( $config );

my $login_name = 'foobar';
my $user = My::UserActions->fetch_user( $login_name );

# Get the handle from ObjectDirectory;
# this is the same handle used in the
# 'fetch_user()' procedure above
my $dbh = ObjectDirectory->get_database_handle();

# Ask the database handle for the history
# of all statements executed against it
my $history = $dbh->{mock_all_history};

# Now query that history record to
# see if our expectations match reality
is(scalar(@{$history}), 1, 'Correct number of statements executed' ;

my $login_st = $history->[0];
like($login_st->statement,
    qr/SELECT login_name.*FROM users WHERE login_name = ?/sm,
    'Correct statement generated' );

my $params = $login_st->bound_params;
is(scalar(@{$params}), 1, 'Correct number of parameters bound');
is($params->[0], $login_name, 'Correct value for parameter 1' );

# Reset the handle for future operations
$dbh->{mock_clear_history} = 1;

The list of properties and what they return is listed below. But in an overall view:

A Word of Warning

This may be an incredibly naive implementation of a DBD. But it works for me ...

DBD::Mock

Since this is a normal DBI statement handle we need to expose our tracking information as properties (accessed like a hash) rather than methods.

Database Driver Properties

Database Handle Properties

Database Driver Methods

In order to capture begin_work(), commit(), and rollback(), DBD::Mock will create statements for them, as if you had issued them in the appropriate SQL command line program. They will go through the standard prepare()-execute() cycle, meaning that any custom SQL parsers will be triggered and DBD::Mock::Session will need to know about these statements.

Statement Handle Properties

DBD::Mock::Pool

This module can be used to emulate Apache::DBI style DBI connection pooling. Just as with Apache::DBI, you must enable DBD::Mock::Pool before loading DBI.

use DBD::Mock qw(Pool);
# followed by ...
use DBI;

While this may not seem to make a lot of sense in a single-process testing scenario, it can be useful when testing code which assumes a multi-process Apache::DBI pooled environment.

DBD::Mock::StatementTrack

Under the hood this module does most of the work with a DBD::Mock::StatementTrack object. This is most useful when you are reviewing multiple statements at a time, otherwise you might want to use the mock_* statement handle attributes instead.

DBD::Mock::StatementTrack::Iterator

This object can be used to iterate through the current set of DBD::Mock::StatementTrack objects in the history by fetching the 'mock_all_history_iterator' attribute from a database handle. This object is very simple and is meant to be a convenience to make writing long test script easier. Aside from the constructor (new) this object has only one method.

next

Calling next will return the next DBD::Mock::StatementTrack object in the history. If there are no more DBD::Mock::StatementTrack objects available, then this method will return false.

reset

This will reset the internal pointer to the beginning of the statement history.

DBD::Mock::Session

The DBD::Mock::Session object is an alternate means of specifying the SQL statements and result sets for DBD::Mock. The idea is that you can specify a complete 'session' of usage, which will be verified through DBD::Mock. Here is an example:

my $session = DBD::Mock::Session->new('my_session' => (
      {
          statement => "SELECT foo FROM bar", # as a string
          results   => [[ 'foo' ], [ 'baz' ]]
      },
      {
          statement => qr/UPDATE bar SET foo \= \'bar\'/, # as a reg-exp
          results   => [[]]
      },
      {
          statement => sub {  # as a CODE ref
                  my ($SQL, $state) = @_;
                  return $SQL eq "SELECT foo FROM bar";
                  },
          results   => [[ 'foo' ], [ 'bar' ]]
      },
      {
          # with bound parameters
          statement    => "SELECT foo FROM bar WHERE baz = ? AND borg = ?",
          # check exact bound param value,
          # then check it against regexp
          bound_params => [ 10, qr/\d+/ ],
          results      => [[ 'foo' ], [ 'baz' ]]
      }
));

As you can see, a session is essentially made up a list of HASH references we call 'states'. Each state has a 'statement' and a set of 'results'. If DBD::Mock finds a session in the 'mock_session' attribute, then it will pass the current $dbh and SQL statement to that DBD::Mock::Session. The SQL statement will be checked against the 'statement' field in the current state. If it passes, then the 'results' of the current state will get feed to DBD::Mock through the 'mock_add_resultset' attribute. We then advance to the next state in the session, and wait for the next call through DBD::Mock. If at any time the SQL statement does not match the current state's 'statement', or the session runs out of available states, an error will be raised (and propagated through the normal DBI error handling based on your values for RaiseError and PrintError).

As can be seen in the session element, bound parameters can also be supplied and tested. In this statement, the SQL is compared, then when the statement is executed, the bound parameters are also checked. The bound parameters much match in both number of parameters and the parameters themselves, or an error will be raised.

As can also be seen in the example above, 'statement' fields can come in many forms. The simplest is a string, which will be compared using eq against the currently running statement. The next is a reg-exp reference, this too will get compared against the currently running statement. The last option is a CODE ref, this is sort of a catch-all to allow for a wide range of SQL comparison approaches (including using modules like SQL::Statement or SQL::Parser for detailed functional comparisons). The first argument to the CODE ref will be the currently active SQL statement to compare against, the second argument is a reference to the current state HASH (in case you need to alter the results, or store extra information). The CODE is evaluated in boolean context and throws and exception if it is false.

new ($session_name, @session_states)

A $session_name can be optionally be specified, along with at least one @session_states. If you don't specify a $session_name, then a default one will be created for you. The @session_states must all be HASH references as well, if this conditions fail, an exception will be thrown.

verify_statement ($dbh, $SQL)

This will check the $SQL against the current state's 'statement' value, and if it passes will add the current state's 'results' to the $dbh. If for some reason the 'statement' value is bad, not of the prescribed type, an exception is thrown. See above for more details.

verify_bound_params ($dbh, $params)

If the 'bound_params' slot is available in the current state, this will check the $params against the current state's 'bound_params' value. Both number of parameters and the parameters themselves must match, or an error will be raised.

reset

Calling this method will reset the state of the session object so that it can be reused.

EXPERIMENTAL FUNCTIONALITY

All functionality listed here is highly experimental and should be used with great caution (if at all).

BUGS

TO DO

SEE ALSO

DBI

DBD::NullP, which provided a good starting point

Test::MockObject, which provided the approach

Test::MockObject article - http://www.perl.com/pub/a/2002/07/10/tmo.html

Perl Code Kata: Testing Databases - http://www.perl.com/pub/a/2005/02/10/database_kata.html

ACKNOWLEDGEMENTS

COPYRIGHT

Copyright (C) 2004 Chris Winters chris@cwinters.com

Copyright (C) 2004-2007 Stevan Little stevan@iinteractive.com

Copyright (C) 2007 Rob Kinyon rob.kinyon@gmail.com

Copyright (C) 2011 Mariano Wahlmann <dichoso _at_ gmail.com>

Copyright (C) 2019 Jason Cooper JLCOOPER@cpan.org

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHORS

Chris Winters chris@cwinters.com

Stevan Little stevan@iinteractive.com

Rob Kinyon rob.kinyon@gmail.com

Mariano Wahlmann <dichoso _at_ gmail.com>

Jason Cooper JLCOOPER@cpan.org