Test::MockDBI(3pm)
π NAME π
Test::MockDBI β a whole mocked DBI interface, made for testing purposes. Periodt.
β¨ SYNOPSIS β¨
use Test::MockDBI;
my $mi = Test::MockDBI::get_instance();
Sets a fake return value for the rows statementhandler
$mi->set_retval( method => rows, retval => sub{ return scalar( @somearray ); });
$mi->set_retval( method => 'bind_param', retval => undef);
Same as:
$mi->bad_method('bind_param');
You can also specify return values for specific sqls
$mi->set_retval( method => rows, retval => sub{ return scalar( @somearray ); }, sql => 'select id from names');
$mi->set_retval( method => 'bind_param', retval => undef, sql => 'select id from names where id < ?');
Same as:
$mi->bad_method('bind_param', 'select id from names where id < ?');
πΈ PUBLIC INTERFACE πΈ
These are the methods you can call on your Test::MockDBI instance. Go off, bestie π«Ά
π
reset()
Wipes every mock returnvalue, every bad_param, all of it β a full reset of the vibes. Fresh start energy β¨
π
bad_method()
Basically an alias for calling set_retval with the return value undef. Same thing, shorter name, iconic behaviour.
Args:
$method_nameβ the name of the method which should returnundef$matching_sql(Optional) β the sql matching condition
Returns:
- On success:
1 - On failure:
undef
Heads up π this method also supports being called with these arguments:$method_name,$dbi_testing_type,$matching_sqlβ but that will issue a warning, because it is deprecated. Old-school, not slay.
π
bad_param()
Args:
$p_valueβ the value that will causebind_paramto returnundef$sql(Optional) β the sql matching condition
Returns:
- On success:
1 - On failure:
undef
Same warning energy π¨ this one also supports being called with$dbi_testing_type,$p_num,$p_valueβ and yeah, that issues a warning too, because it is deprecated.
π
set_retval()
The method for setting a return value for a specific method. This is the main character of the whole module, fr fr π
Args (keys in a hash):
methodβ the method that should return the provided valueretvalβ the data which should be returnedsql(Optional) β matching sql. The return value will only be returned for the provided method if the sql matches a regex compiled by using this string
Returnvalues:
- On success:
1 - On failure:
undef
Example usage:
#fetchrow_hashref will shift one hashref from the list each time its called if the sql matches the sql provided, this will happend
#until the return list is empty.
$inst->set_retval( method => 'fetchrow_hashref',
retval => [ { letter => 'a' }, { letter => 'b' }, { letter => 'c' } ],
sql => 'select * from letters' )
#execute will default return undef
$inst->set_retval( method => 'execute', retval => undef)
#Execute will return 10 for sql 'select * from cars'
$inst->set_retval( method => 'execute', retval => undef);
$inst->set_retval( method => 'execute', retval => 10, sql => 'select * from cars');
π
set_inout_value()
Special method for handling inout params. Here you can provide the value that the inout param should have after execute is called. Very specific, very intentional, we love a specialist π
Args:
$sqlβ the sql that this rule should apply for$p_numβ the parameter number of the inout parameter$valueβ the value that the inout parameter should have afterexecute
Returns:
- On success:
1 - On failure:
undef
Example:
...and that's literally it, the example section is just vibes and a colon π moving on.
π PRIVATE INTERFACE π
Methods used by the package internally. Should not be called from an external package. This is backstage, no fans allowed π«
π _clear_dbi_err_errstr()
Helper method used by the fake DBI::st and DBI::db to clear out the $obj->{err} and $obj->{errstr} on each method call.
Should not be called from an external script\package.
π _set_dbi_err_errstr()
Helper method used by the fake DBI::st and DBI::db to set the $obj->{err}, $obj->{errstr}, $DBI::err and $DBI::errstr. This method also handles RaiseError and PrintError attributes.
Args:
$objβ instance ofDBI::storDBI::db%argsβ a hash with the following keys:errβ the numeric error code to be seterrstrβ the user friendly DBI error message.
Returns:
- On success :
1 - On failure :
undef
π _set_fake_dbi_err_errstr
(no description, it's giving mystery π΅οΈβοΈ)
π _has_inout_value()
Helper method used by the DBI::db and DBI::st packages. The method searches to see if there is specified a value for a inout variable.
If called in SCALAR context it returns 1/undef based on if the parameter bound as $p_num has a predefined return value set.
If called in LIST context the method returns an array with 1/undef in position 0, which indicates the same as when the method is called in SCALAR context. The second element of the list is the value that should be applied to the inout parameter.
π _has_fake_retval()
Method for identifying if a method has a predefined return value set. If the SQL parameter is provided, this will have precedence over the default value. π
If the method is called in SCALAR context it will return 1`undef` based on if the method has a predefined return value set.
If the method is called in LIST context it will return a list with 1/undef at index 0, which indicates the same as when called in SCALAR context. Index 1 will contain a reference to the actual return value that should be returned by the method. This value may be undef.
π _is_bad_bind_param()
Method for identifying if a bind parameters value is predefined as unwanted. The configuration for the provided SQL will have precedence over the default configured behaviour.
When called it will return 1`undefbased on if the provided value should make thebind_param` method fail.
π CLASS INTERFACE π
β get_instance()
Method for retrieving the current Test::MockDBI instance. Your one and only π
This is basically a copy\paste from the DBI package itself.
The method is used inside the prepare_cached method
This is basically a copy\paste from the DBI package itself.
The method is used inside the prepare_cached method
This is basically a copy\paste from the DBI package itself.
The method is used inside the prepare_cached method
Mocked DBI->connect method.
The method takes the same arguments as the usual DBI->connect method.
It returns a $dbh which has ref DBI::db
π¦ AUTHOR π¦
Mark Leighton Fisher, <mark-fisher@fisherscreek.com>
Minor modifications (version 0.62 onwards) by Andreas Faafeng <aff@cpan.org>
π COPYRIGHT π
Copyright 2004, Fisher's Creek Consulting, LLC. Copyright 2004, DeepData, Inc.
π LICENSE π
This code is released under the same licenses as Perl itself.
okay bestie, that's the whole page π«Ά perl v5.36.0, dated 2022-11-27, and yeah the manual name really is Test::MockDBI(3pm) β like, the whole thing. slay, go write some tests π
β¨
π¦ where this page comes from
- debian bookworm: libtest-mockdbi-perl 0.70-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtest-mockdbi-perl 0.70-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtest-mockdbi-perl 0.70-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-mockdbi-perl 0.70-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-mockdbi-perl 0.70-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms