DBD::Mock(3pm) | User Contributed Perl Documentation | DBD::Mock(3pm) |
DBD::Mock - Mock database driver for testing
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";
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)
"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:
This may be an incredibly naive implementation of a DBD. But it works for me...
Since this is a normal DBI statement handle we need to expose our tracking information as properties (accessed like a hash) rather than methods.
# install the DBD::Mock driver my $drh = DBI->install_driver('Mock'); $drh->{mock_connect_fail} = 1; # this connection will fail my $dbh = DBI->connect('dbi:Mock:', '', '') || die "Cannot connect"; # this connection will throw an exception my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1 }); $drh->{mock_connect_fail} = 0; # this will work now ... my $dbh = DBI->connect(...);
This feature is conceptually different from the "mock_can_connect" attribute of the $dbh in that it has a driver-wide scope, where "mock_can_connect" is handle-wide scope. It also only prevents the initial connection, any $dbh handles created prior to setting "mock_connect_fail" to true (1) will still go on working just fine.
my $dbh = get_handle( ... ); my $sth = $dbh->prepare( ... ); $dbh->{mock_clear_history} = 1; $sth->execute( 'Foo' );
You will have no way to learn from the database handle that the statement parameter 'Foo' was bound.
This is useful mainly to ensure you can isolate the statement histories from each other. A typical sequence will look like:
set handle to framework perform operations analyze mock database handle reset mock database handle history perform more operations analyze mock database handle reset mock database handle history ...
# turn the database off $dbh->{mock_can_connect} = 0; # turn it back on again $dbh->{mock_can_connect} = 1;
The statement handle checks this value as well, so something like this will fail in the expected way:
$dbh = DBI->connect( 'DBI:Mock:', '', '' ); $dbh->{mock_can_connect} = 0; # blows up! my $sth = eval { $dbh->prepare( 'SELECT foo FROM bar' ) }); if ( $@ ) { # Here, $DBI::errstr = 'No connection present' }
Turning off the database after a statement prepare will fail on the statement "execute()", which is hopefully what you would expect:
$dbh = DBI->connect( 'DBI:Mock:', '', '' ); # ok! my $sth = eval { $dbh->prepare( 'SELECT foo FROM bar' ) }); $dbh->{mock_can_connect} = 0; # blows up! $sth->execute;
Similarly:
$dbh = DBI->connect( 'DBI:Mock:', '', '' ); # ok! my $sth = eval { $dbh->prepare( 'SELECT foo FROM bar' ) }); # ok! $sth->execute; $dbh->{mock_can_connect} = 0; # blows up! my $row = $sth->fetchrow_arrayref;
Note: The handle attribute "Active" and the handle method "ping" will behave according to the value of "mock_can_connect". So if "mock_can_connect" were to be set to 0 (or off), then both "Active" and "ping" would return false values (or 0).
Here is a sample usage, partially from the test suite:
my @user_results = ( [ 'login', 'first_name', 'last_name' ], [ 'cwinters', 'Chris', 'Winters' ], [ 'bflay', 'Bobby', 'Flay' ], [ 'alincoln', 'Abe', 'Lincoln' ], ); my @generic_results = ( [ 'foo', 'bar' ], [ 'this_one', 'that_one' ], [ 'this_two', 'that_two' ], ); my $dbh = DBI->connect( 'DBI:Mock:', '', '' ); $dbh->{mock_add_resultset} = \@user_results; # add first resultset $dbh->{mock_add_resultset} = \@generic_results; # add second resultset my ( $sth ); eval { $sth = $dbh->prepare( 'SELECT login, first_name, last_name FROM foo' ); $sth->execute(); }; # this will fetch rows from the first resultset... my $row1 = $sth->fetchrow_arrayref; my $user1 = User->new( login => $row->[0], first => $row->[1], last => $row->[2] ); is( $user1->full_name, 'Chris Winters' ); my $row2 = $sth->fetchrow_arrayref; my $user2 = User->new( login => $row->[0], first => $row->[1], last => $row->[2] ); is( $user2->full_name, 'Bobby Flay' ); ... my $sth_generic = $dbh->prepare( 'SELECT foo, bar FROM baz' ); $sth_generic->execute; # this will fetch rows from the second resultset... my $row = $sth->fetchrow_arrayref;
It is possible to assign a hashref where the resultset must be given as value for the "results" key:
$dbh->{mock_add_resultset} = { results => [ [ 'foo', 'bar' ], [ 'this_one', 'that_one' ], [ 'this_two', 'that_two' ], ], };
The reason for the hashref form is that you can add options as described in the following.
You can associate a resultset with a particular SQL statement instead of adding them in the order they will be fetched:
$dbh->{mock_add_resultset} = { sql => 'SELECT foo, bar FROM baz', results => [ [ 'foo', 'bar' ], [ 'this_one', 'that_one' ], [ 'this_two', 'that_two' ], ], };
This will return the given results when the statement '"SELECT foo, bar FROM baz"' is prepared/executed. Note that they will be returned every time the statement is prepared/executed, not just the first. It should also be noted that if you want, for some reason, to change the result set bound to a particular SQL statement, all you need to do is add the result set again with the same SQL statement and "DBD::Mock" will overwrite it.
If the "sql" parameter is a regular expression reference then the results will be returned for any SQL statements that matches it:
$dbh->{mock_add_resultset} = { sql => qr/^SELECT foo FROM/i, results => [ [ 'foo' ], [ 'this_one' ], ], };
If an SQL statement matches both a specified SQL statement result set and a regular expression result set then the specified SQL statement takes precedence. If two regular expression result sets match then the first one added takes precedence:
# Set up our first regex matching result set $dbh->{mock_add_resultset} = { sql => qr/^SELECT foo/, results => [ [ 'foo' ], [ 200 ] ], }; # Set up our second regex matching result set # Note - This results set would never be used as the one above will match # and thus take precedence $dbh->{mock_add_resultset} = { sql => qr/^SELECT foo FROM/, results => [ [ 'foo' ], [ 300 ] ], }; # Set up our first statically defined result set # This result set will take precedence over the regex matching ones above $dbh->{mock_add_resultset} = { sql => 'SELECT foo FROM bar', results => [[ 'foo' ], [ 50 ]] }; # This query will be served by the first regex matching result set my $sth = $dbh->prepare('SELECT foo FROM oof'); $sth->execute() my ($result) = $sth->fetchrow_array(); is( $result, 200 ); # This quere will be served by the statically defined result set $sth = $dbh->prepare('SELECT foo FROM bar'); $sth->execute(); my ($result2) = $sth->fetchrow_array(); is( $result2, 50 );
It should also be noted that the "rows" method will return the number of records stocked in the result set. So if your code/application makes use of the "$sth->rows" method for things like "UPDATE" and "DELETE" calls you should stock the result set like so:
$dbh->{mock_add_resultset} = { sql => 'UPDATE foo SET baz = 1, bar = 2', # this will appear to have updated 3 rows results => [[ 'rows' ], [], [], []], }; # or ... $dbh->{mock_add_resultset} = { sql => 'DELETE FROM foo WHERE bar = 2', # this will appear to have deleted 1 row results => [[ 'rows' ], []], };
Now I admit this is not the most elegant way to go about this, but it works for me for now, and until I can come up with a better method, or someone sends me a patch ;) it will do for now.
If you want a given statement to fail, you will have to use the hashref method and add a "failure" key. That key must be handed an arrayref with the error number and error string, in that order.
$dbh->{mock_add_resultset} = { sql => 'SELECT foo FROM bar', results => DBD::Mock->NULL_RESULTSET, failure => [ 5, 'Ooops!' ], };
Without the "sql" attribute the next statement will fail in any case:
$dbh->{mock_add_resultset} = { results => DBD::Mock->NULL_RESULTSET, failure => [ 5, 'Ooops!' ], };
$dbh->{mock_start_insert_id} = 10; my $sth = $dbh->prepare('INSERT INTO Foo (foo, bar) VALUES(?, ?)'); $sth->execute(1, 2); # $dbh->{mock_last_insert_id} == 10 $sth->execute(3, 4); # $dbh->{mock_last_insert_id} == 11
For more examples, please refer to the test file t/025_mock_last_insert_id.t.
To access "last_insert_id" using driver specific attributes like "mysql_insertid" and "mariadb_insertid" then you can use "Attribute Aliasing".
This attribute also can be used with an ARRAY ref parameter, it's behavior is slightly different in that instead of incrementing the value for every "prepare" it will only increment for each "execute". This allows it to be used over multiple "execute" calls in a single $sth. It's usage looks like this:
$dbh->{mock_start_insert_id} = [ 'Foo', 10 ]; $dbh->{mock_start_insert_id} = [ 'Baz', 20 ]; my $sth1 = $dbh->prepare('INSERT INTO Foo (foo, bar) VALUES(?, ?)'); my $sth2 = $dbh->prepare('INSERT INTO Baz (baz, buz) VALUES(?, ?)'); $sth1->execute(1, 2); # $dbh->{mock_last_insert_id} == 10 $sth2->execute(3, 4); # $dbh->{mock_last_insert_id} == 20
Note that "DBD::Mock"'s matching of table names in "INSERT" statements is fairly simple, so if your table names are quoted in the insert statement ("INSERT INTO "Foo"") then you need to quote the name for "mock_start_insert_id":
$dbh->{mock_start_insert_id} = [ q{"Foo"}, 10 ];
But other times you may want to parse the statement as it is prepared rather than after the fact. There is a hook in this mock database driver for you to provide your own parsing routine or object.
The syntax is simple:
$dbh->{mock_add_parser} = sub { my ( $sql ) = @_; unless ( $sql =~ /some regex/ ) { die "does not contain secret fieldname"; } };
You can also add more than one for a handle. They will be called in order, and the first one to fail will halt the parsing process:
$dbh->{mock_add_parser} = \&parse_update_sql; $dbh->{mock_add-parser} = \&parse_insert_sql;
Depending on the "PrintError" and "RaiseError" settings in the database handle any parsing errors encountered will issue a "warn" or "die". No matter what the statement handle will be "undef".
Instead of providing a subroutine reference you can use an object. The only requirement is that it implements the method "parse()" and takes a SQL statement as the only argument. So you should be able to do something like the following (untested):
my $parser = SQL::Parser->new( 'mysql', { RaiseError => 1 } ); $dbh->{mock_add_parser} = $parser;
Basically this feature allows you to alias attributes to other attributes. So for instance, you can alias a commonly expected attribute like "mysql_insertid" to something "DBD::Mock" already has like "mock_last_insert_id". While you can also just set "mysql_insertid" yourself, this functionality allows it to take advantage of things like the autoincrementing of the "mock_last_insert_id" attribute.
The functionality is off by default so as to not cause any issues with backwards compatibility, but can easily be turned on and off like this:
# turn it on $DBD::Mock::AttributeAliasing++; # turn it off $DBD::Mock::AttributeAliasing = 0;
Once this is turned on, you will need to choose a database specific attribute aliasing table like so:
DBI->connect('dbi:Mock:MySQL', '', '');
Or, by using the database name if using driver DSNs:
DBI->connect('dbi:Mock:host=localhost;port=3306;database=MySQL', '', '');
The "MySQL" in the DSN will be picked up and the MySQL specific attribute aliasing will be used.
Right now there is only minimal support for MySQL and MariaDB:
It is possible to add more aliases though, using the "DBD::Mock:_set_mock_attribute_aliases" function (see the source code for details)
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.
my $sth = $dbh->prepare( 'SELECT * FROM foo' );
This would return:
SELECT * FROM foo
The original statement is unmodified so if you are checking against it in tests you may want to use a regex rather than a straight equality check. (However if you use a phrasebook to store your SQL externally you are a step ahead...)
Note that this returns the same thing as the normal statement property "FIELD".
my $sth = $dbh->prepare( 'SELECT * FROM foo WHERE id = ? AND is_active = ?' ); $sth->bind_param( 2, 'yes' ); $sth->bind_param( 1, 7783 );
This would return:
[ 7738, 'yes' ]
The same result will occur if you pass the parameters via "execute()" instead:
my $sth = $dbh->prepare( 'SELECT * FROM foo WHERE id = ? AND is_active = ?' ); $sth->execute( 7783, 'yes' );
The same using named parameters
my $sth = $dbh->prepare( 'SELECT * FROM foo WHERE id = :id AND is_active = :active' ); $sth->bind_param( ':id' => 7783 ); $sth->bind_param( ':active' => 'yes' );
my $sth = $dbh->prepare( 'SELECT * FROM foo WHERE id = ? AND is_active = ?' ); $sth->bind_param( 2, 'yes' ); $sth->bind_param( 1 7783, SQL_INTEGER );
This would return:
[ SQL_INTEGER, undef ]
Passing parameters via "execute()" will always populate the array with "undef", so for:
$sth->execute( 7783, 'yes' );
This would return:
[ undef, undef ]
my $sth = $dbh->prepare( 'SELECT * FROM foo WHERE id = ? AND is_active = ?' ); $sth->bind_param( 2, 'yes' ); $sth->bind_param( 1 7783, SQL_INTEGER ); $sth->execute(); $sth->execute( 1023, 'no' );
Then "$sth->{mock_execution_history}" would be:
[ { params => [ 7783, 'yes' ], attrs => [ SQL_INTEGER, undef ], }, { params => [ 1023, 'no' ], attrs => [ undef, undef ], } ]
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.
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.
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 the following methods.
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 fed 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 must 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.
All functionality listed here is highly experimental and should be used with great caution (if at all).
To set a series of callbacks you use the "DBD::Mock::dr::set_connect_callbacks" function
use DBD::Mock::dr; DBD::Mock::dr::set_connect_callbacks( sub { my ( $dbh, $dsn, $user, $password, $attributes ) = @_; $dbh->{mock_add_resultset} = { sql => 'SELECT foo FROM bar', results => [[ 'foo' ], [ 10 ]] }; } );
To set more than one callback to you can simply add extra callbacks to your call to "DBD::Mock::dr::set_connect_callbacks"
DBD::Mock::dr::set_connect_callbacks( sub { my ( $dbh, $dsn, $user, $password, $attributes ) = @_; $dbh->{mock_add_resultset} = { sql => 'SELECT foo FROM bar', results => [[ 'foo' ], [ 10 ]] }; }, sub { my ( $dbh, $dsn, $user, $password, $attributes ) = @_; $dbh->{mock_add_resultset} = { sql => 'SELECT foo FROM bar', results => [[ 'foo' ], [ 10 ]] }; } );
Or you can extend the existing set of callbacks with the "DBD::Mock::dr::add_connect_callbacks" function
DBD::Mock::dr::add_connect_callbacks( sub { ( my $dbh, $dsn, $user, $password, $attributes ) = @_; $dbh->{mock_add_resultset} = { sql => 'SELECT bar FROM foo', results => [[ 'bar' ], [ 50 ]] }; } );
To mock the table info for a search of the "testSchema" database schema you would use the following:
$dbh->{mock_add_table_info} = { cataloge => undef, schema => 'testSchema', table => undef, type => undef, table_info => [ [ 'TABLE_CAT', 'TABLE_SCHEM', 'TABLE_NAME', 'TABLE_TYPE', 'REMARKS' ], [ undef, 'testSchema', 'foo', 'TABLE', undef ], [ undef, 'testSchema', 'bar', 'VIEW', undef ], ], };
The "cataloge", "schema", "table" and "type" parameters need to explicitly match what you expect table_info to be called with (note: "table_info" treats "undef" and '' the same).
Similar to the "mock_results_sets", the "table_info" parameter's first entry is an arrayref of column names, and the rest are the values of the rows returned (one arrayref per row).
If you need to cover listing schemas then you'd use:
$dbh->{mock_add_table_info} = { schema => '%', table_info => [ [ 'TABLE_CAT', 'TABLE_SCHEM', 'TABLE_NAME', 'TABLE_TYPE', 'REMARKS' ], [ undef, 'testSchema', undef, undef, undef ], [ undef, 'testSchema_2', undef, undef, undef ], ], }
To clear the current mocked table info set the database handle's "mock_clear_table_info" attribute to 1
$dbh->{mock_clear_table_info} = 1;
$dbh->{mock_add_resultset} = { sql => 'SELECT a FROM b WHERE c = ?', callback => sub { my @bound_params = @_; my %result = ( fields => [ "a" ], rows => [[ 1] ] ); if ($bound_params[0] == 1) { $result{rows} = [ [32] ]; } elsif ($bound_params[0] == 2) { $result{rows} = [ [43] ]; } return %result; }, }; my $sth = $dbh->prepare('SELECT a FROM b WHERE c = ?'); my $rows = $sth->execute(1); my ($result) = $sth->fetchrow_array(); # $result will be 32 $rows = $sth->execute(2); ($result) = $sth->fetchrow_array(); # $result this time will be 43 $rows = $sth->execute(33); # $results this time will be 1 ($result) = $sth->fetchrow_array();
The callback needs to return a hash with a "rows" key that is an array ref of arrays containing the values to return as the answer to the query. In addition a "fields" key can also be returned with an array ref of field names. If a "fields" key isn't present in the returned the hash then the fields will be taken from the "mock_add_resultset"'s "results" parameter.
$dbh->{mock_add_resultset} = { sql => 'SELECT x FROM y WHERE z = ?', results => [ ["x"] ], callback => sub { my @bound_params = @_; my %result = ( rows => [[ 1] ] ); if ($bound_params[0] == 1) { $result{rows} = [ [32] ]; } elsif ($bound_params[0] == 2) { $result{rows} = [ [43] ]; } return %result; }, }; my $sth = $dbh->prepare('SELECT x FROM y WHERE z = ?'); my $rows = $sth->execute(1); my ($result) = $sth->fetchrow_array(); # $result will be 32 $rows = $sth->execute(2); ($result) = $sth->fetchrow_array(); # $result will be 43 $rows = $sth->execute(33); ($result) = $sth->fetchrow_array(); # $result will be 1
By default result sets which only define their field names in their callback return values will have a "NUM_OF_FIELDS" property of 0 until after the statement has actually been executed. This is to make sure that "DBD::Mock" stays compatible with previous versions. If you need the "NUM_OF_FIELDS" property to be undef in this situation then set the $DBD::Mock::DefaultFieldsToUndef flag to 1.
If you're mocking an INSERT statement with a callback and you want to explicitly set the database's "last_insert_id" value then you can use the "last_insert_id" key in the result set. If you don't specify a "last_insert_id" then the standard "DBD::Mock" logic for generating an value for the last inserted item will be followed. This will allow you to mock MySQL/MariaDB INSERT queries that use "ON DUPLICATE KEY UPDATE" logic to set the "last_insert_id".
$dbh->{mock_add_resultset} = { sql => 'INSERT INTO y ( x ) VALUES ( ? ) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID( id )', callback => sub { my @bound_params = @_; my %result = ( fields => [], rows => [], last_insert_id => 99, ); return %result; }, };
The "prepare_attributes" option takes a hashref that maps statement handle attribute names to their values. The attributes are set at the point that the statement is prepared.
$dbh->{mock_add_resultset} = { sql => 'SELECT foo FROM bar', prepare_attributes => { sqlite_unprepared_statements => ' ', }, results => [[ 'foo' ], [ 10 ]] };
The "execute_attributes" option also takes a hashref that maps statement handle attribute names to their values, however these will only be set when the statement is executed.
$dbh->{mock_add_resultset} = { sql => 'SELECT foo FROM bar', execute_attributes => { syb_result_type => 1, }, results => [[ 'foo' ], [ 10 ]] };
If an attribute is also present in the "prepare_attributes" option then the "prepare_attributes" version will take precedence up to the point the statement handle is executed, at which point the "execute_attributes" version will take precedence.
It is also possible to set "execute_attributes" from a result set's callback by returning them under the "execute_attributes" key in your callback's response.
$dbh->{mock_add_resultset} = { sql => 'SELECT baz FROM qux', callback => sub { my @bound_params = @_; my %result = ( fields => [ 'baz'], rows => [], execute_attributes => { foo => 'bar' }, ); return %result; } };
If a result set has an "execute_attributes" option and a callback that also returns an "execute_attributes" key then the callback's "execute_attributes" value will take precedence.
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>
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.
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>
2021-02-27 | perl v5.32.1 |