DBD-Mock
view release on metacpan or search on metacpan
1.58 2020-11-02T13:34:48Z
- Added the ability for mock result sets to set custom attributes for
statement handles. Thanks to Erik Huelsmann for testing the new feature.
1.57 2020-09-18T06:57:48Z
- Fixed bug rt133358 t/016_mock_add_resultset_test.t fails (with older DBI)
1.56 2020-09-17T14:35:10Z
- The DBD::st module now supports the last_insert_id method
- Result sets with callbacks can now specify a last_insert_id
1.55 2019-12-30T14:20:00Z
- Fixed bug rt131264 t/033_table_info.t fails (with older DBI)
1.54 2019-12-23T12:44:22Z
- Added Bernhard Graf's 'Feature: one shot failure' merge request.
- Fixed description of the failure attribute of mock_add_resultset. It
no longer claims to support a hash ref (as it doesn't). Thanks to
Bernhard Graf for both the bug report and a merge request that fixes it.
- Fixed bug where it wasn't possible to replace a regular expression
t/021_DBD_Mock_Session.t
t/022_DBD_Mock_Session_bound_params.t
t/023_statement_failure.t
t/024_selcol_fetchhash.t
t/025_mock_last_insert_id.t
t/026_st_bind_col.t
t/027_named_parameters.t
t/028_bind_columns.t
t/029_multiple_prepare_statements.t
t/030_st_execute_array.t
t/031_setup_callbacks.t
t/032_selectall_arrayref.t
t/033_table_info.t
t/034_custom_attributes.t
t/998_pod.t
t/999_pod_coverage.t
t/bug_015602.t
t/bug_066815.t
t/bug_071438.t
t/bug_082243.t
t/bug_117162.t
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).
- Connection Callbacks
This feature allows you to define callbacks that get executed when
`DBI->connect` is called.
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 ]]
};
} );
- table\_info
lib/DBD/Mock.pm view on Meta::CPAN
=head1 EXPERIMENTAL FUNCTIONALITY
All functionality listed here is highly experimental and should be used with
great caution (if at all).
=over 4
=item Connection Callbacks
This feature allows you to define callbacks that get executed when
C<< DBI->connect >> is called.
To set a series of callbacks you use the
C<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 C<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
C<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 ]]
};
} );
=item table_info
lib/DBD/Mock/dr.pm view on Meta::CPAN
package DBD::Mock::dr;
use strict;
use warnings;
use List::Util qw(reduce);
our $imp_data_size = 0;
my @connect_callbacks;
sub connect {
my ( $drh, $dbname, $user, $auth, $attributes ) = @_;
if ( $drh->{'mock_connect_fail'} == 1 ) {
$drh->set_err( 1, "Could not connect to mock database" );
return;
}
$attributes ||= {};
lib/DBD/Mock/dr.pm view on Meta::CPAN
$attributes->{mock_can_connect} = 1;
# ability to make other things fail :)
$attributes->{mock_can_prepare} = 1;
$attributes->{mock_can_execute} = 1;
$attributes->{mock_can_fetch} = 1;
my $dbh = DBI::_new_dbh( $drh, { Name => $dbname } )
|| return;
foreach my $callback (@connect_callbacks) {
$callback->( $dbh, $dbname, $user, $auth, $attributes );
}
return $dbh;
}
sub FETCH {
my ( $drh, $attr ) = @_;
if ( $attr =~ /^mock_/ ) {
if ( $attr eq 'mock_connect_fail' ) {
lib/DBD/Mock/dr.pm view on Meta::CPAN
# Necessary to support DBI < 1.34
# from CPAN RT bug #7057
sub disconnect_all {
# no-op
}
sub DESTROY { undef }
sub set_connect_callbacks {
@connect_callbacks = map { die "connect callbacks needs to be a reference to a function " unless ref $_ eq "CODE"; $_ } @_;
}
sub add_connect_callbacks {
push @connect_callbacks, map { die "connect callbacks needs to be a reference to a function " unless ref $_ eq "CODE"; $_ } @_;
}
sub _parse_driver_dsn {
my ( $driverDsn ) = @_;
$driverDsn = $driverDsn ? $driverDsn : '';
my %driverParameters;
foreach my $parameter ( split /;/, $driverDsn ) {
t/031_setup_callbacks.t view on Meta::CPAN
use warnings;
use Test::More;
use DBD::Mock;
use DBD::Mock::dr;
use DBI;
my ( $dsn, $user, $password, $attributes );
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 ]]
};
} );
{
my $dbh = DBI->connect('dbi:Mock:', '', '');
t/031_setup_callbacks.t view on Meta::CPAN
my ($result) = $sth->fetchrow_array();
is($result, 10, '... got the result we expected');
$sth->finish();
}
# now let's check that we can reset the callbacks
DBD::Mock::dr::set_connect_callbacks( sub {
( my $dbh, $dsn, $user, $password, $attributes ) = @_;
$dbh->{mock_add_resultset} = {
sql => 'SELECT bar FROM foo',
results => [[ 'bar' ], [ 50 ]]
};
} );
{
my $dbh = DBI->connect('dbi:Mock:', '', '');
t/031_setup_callbacks.t view on Meta::CPAN
$sth->finish();
$sth = $dbh->prepare('SELECT foo FROM bar');
isa_ok($sth, 'DBI::st');
$rows = $sth->execute();
is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
($result) = $sth->fetchrow_array();
is($result, undef, "... as we have reset the callbacks this SELECT shouldn't match a result set ");
$sth->finish();
}
# add_connect_callbacks adds a new callback to the list
DBD::Mock::dr::add_connect_callbacks( sub {
( my $dbh, $dsn, $user, $password, $attributes ) = @_;
$dbh->{mock_add_resultset} = {
sql => 'SELECT foo FROM bar',
results => [[ 'foo' ], [ 10 ]]
};
} );
{
my $dbh = DBI->connect('dbi:Mock:', '', '');
t/031_setup_callbacks.t view on Meta::CPAN
$rows = $sth->execute();
is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
($result) = $sth->fetchrow_array();
is($result, 10, "... this should return a value as we've added its connect callback in");
$sth->finish();
}
DBD::Mock::dr::set_connect_callbacks( sub {
( my $dbh, $dsn, $user, $password, $attributes ) = @_;
} );
{
my $dbh = DBI->connect('dbi:Mock:database=TEST_DATABASE;hostname=localhost', 'TEST_USER', 'TEST_PASSWORD', { customAttribute => 1 });
isa_ok($dbh, 'DBI::db');
is ( $dsn, "database=TEST_DATABASE;hostname=localhost", "The database from the DSN should be passed through to the callback" );
is ( $user, "TEST_USER", "The username should be passed through to the callback" );
( run in 0.557 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )