...
 
Commits (25)
*\.sw?
/DBD-Mock-*
/.build
/_build/
/_build_params
/Build
/Build.bat
!Build/
!META.json
!LICENSE
/blib
/Makefile
/pm_to_blib
/carton.lock
/.carton/
/local/
nytprof.out
nytprof/
cover_db/
*.bak
*.old
*~
*.swp
*.o
*.obj
MYMETA.*
image: perl:latest
before_script:
- cpanm Minilla Test::Pod Test::Pod::Coverage
stages:
- test
unitTests:
stage: test
script:
- minil test
<JLCOOPER@cpan.org> <J.L.Cooper@lboro.ac.uk>
<JLCOOPER@cpan.org> <scrapheap@heckrothindustries.co.uk>
<DICHI@cpan.org> <dichoso@gmail.com>
<DROLSKY@cpan.org> <autarch@houseabsolute.urth.org>
<DROLSKY@cpan.org> <autarch@urth.org>
<RKINYON@cpan.org> <rob.kinyon@gmail.com>
<STEVAN@cpan.org> <stevan.little@iinteractive.com>
<CHISEL@cpan.org> <chisel.wright@net-a-porter.com>
# auto-generated shipit config file.
steps = FindVersion, ChangeVersion, CheckChangeLog, DistTest, Commit, Tag, MakeDist, DistClean
svn.tagpattern = %v
svn.tagpattern = https://svn.iinteractive.com/repos/cpan/DBD-Mock/tags/%v
CheckChangeLog.files = Changes
use Module::Build;
use 5.008001;
# =========================================================================
# THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA.
# DO NOT EDIT DIRECTLY.
# =========================================================================
use 5.008_001;
use strict;
use warnings;
my $build = Module::Build->new(
module_name => 'DBD::Mock',
license => 'perl',
requires => {
'perl' => '5.6.0',
'DBI' => 1.30,
},
optional => {},
build_requires => {
'Test::More' => '0.47',
'Test::Exception' => '0.31',
},
create_makefile_pl => 'traditional',
recursive_test_files => 1,
add_to_cleanup => [ '*.bak', ],
use Module::Build::Tiny 0.035;
meta_merge => {
resources => {
repository => 'https://github.com/bluescreen10/dbd-mock',
},
},
);
Build_PL();
$build->create_build_script;
Revision history for Perl extension DBD::Mock.
{{NEXT}}
- Add git-repo url to meta-data
{{$NEXT}}
1.46 2019-09-04T12:02:08Z
- Added git-repo url to meta-data
- Fixed bug rt70587 Spelling Mistake
- Added regex support to mock_add_resultset
1.43
- Segregated into different packages
......
This diff is collapsed.
Build.PL
Changes
lib/DBD/Mock.pm
lib/DBD/Mock/db.pm
lib/DBD/Mock/dr.pm
lib/DBD/Mock/Pool.pm
lib/DBD/Mock/Pool/db.pm
lib/DBD/Mock/Session.pm
lib/DBD/Mock/st.pm
lib/DBD/Mock/StatementTrack.pm
lib/DBD/Mock/StatementTrack/Iterator.pm
Makefile.PL
MANIFEST This list of files
MYMETA.json
MYMETA.yml
README
t/000_basic.t
t/001_db_handle.t
t/002_dr_handle.t
t/003_db_can_connect.t
t/004_misc_mock_attr.t
t/005_db_parser.t
t/006_prepare_cached.t
t/007_mock_attribute_aliases.t
t/008_db_connect_cached.t
t/009_info.t
t/010_rollback.t
t/011_st_execute_empty.t
t/012_st_handle.t
t/013_st_execute_bound_params.t
t/014_st_execute_pass_params.t
t/015_st_fetch_records.t
t/016_mock_add_resultset_test.t
t/017_st_can_connect.t
t/018_mock_statement_track.t
t/019_mock_statement_track_iterator.t
t/020_db_pool.t
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/998_pod.t
t/999_pod_coverage.t
t/bug_0001.t
t/bug_0002.t
t/bug_0003.t
\bRCS\b
\bCVS\b
,v$
\B\.svn\b
^_build
^Build$
^blibdirs$
# Makemaker generated files and dirs.
^MANIFEST\.
^Makefile$
^pm_to_blib$
^blib/?
^MakeMaker-\d
^blibdirs$
# Temp, old and emacs backup files.
~$
\.old$
^#.*#$
^\.#
\..*\.sw.?$
\.bak$
# Devel::Cover
^cover_db/?
# Mac OS X resource forks
\.DS_Store
\ No newline at end of file
{
"abstract" : "Mock database driver for testing",
"author" : [
"Chris Winters <CWINTERS@cpan.org>",
"Stevan Little <STEVAN@cpan.org>",
"Rob Kinyon <RKINYON@cpan.org>",
"Mariano Wahlmann <DICHI@cpan.org>",
"Jason Cooper <JLCOOPER@cpan.org>"
],
"dynamic_config" : 0,
"generated_by" : "Minilla/v3.1.6, CPAN::Meta::Converter version 2.150005",
"license" : [
"perl_5"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : "2"
},
"name" : "DBD-Mock",
"no_index" : {
"directory" : [
"t",
"xt",
"inc",
"share",
"eg",
"examples",
"author",
"builder"
]
},
"prereqs" : {
"build" : {
"requires" : {
"Test::Exception" : "0.31",
"Test::More" : "0.47"
}
},
"configure" : {
"requires" : {
"Module::Build::Tiny" : "0.035"
}
},
"develop" : {
"requires" : {
"Test::CPAN::Meta" : "0",
"Test::MinimumVersion::Fast" : "0.04",
"Test::PAUSE::Permissions" : "0.07",
"Test::Pod" : "1.41",
"Test::Spellunker" : "v0.2.7"
}
},
"runtime" : {
"requires" : {
"DBI" : "1.3",
"List::Util" : "1.27",
"perl" : "5.008001"
}
}
},
"release_status" : "unstable",
"resources" : {
"homepage" : "https://gitlab.com/scrapheap/DBD-Mock",
"repository" : {
"type" : "git",
"url" : "git://gitlab.com/scrapheap/DBD-Mock.git",
"web" : "https://gitlab.com/scrapheap/DBD-Mock"
}
},
"version" : "1.46",
"x_authority" : "cpan:JLCOOPER",
"x_contributors" : [
"Chisel <CHISEL@cpan.org>",
"Dave Rolsky <DROLSKY@cpan.org>",
"Gines R <ginesr@gmail.com>",
"gregor herrmann <gregoa@debian.org>",
"wu-lee <git.wu-lee@noodlefactory.co.uk>"
],
"x_serialization_backend" : "JSON::PP version 2.27300_01",
"x_static_install" : 1
}
# Note: this file was auto-generated by Module::Build::Compat version 0.30
require 5.6.0;
use ExtUtils::MakeMaker;
WriteMakefile(
'NAME' => 'DBD::Mock',
'VERSION_FROM' => 'lib/DBD/Mock.pm',
'PREREQ_PM' => {
'DBI' => '1.3',
'Test::More' => '0.47',
'Test::Exception' => '0.31'
},
'INSTALLDIRS' => 'site',
'EXE_FILES' => [],
'PL_FILES' => {}
);
DBD::Mock
=====================
This is a simple mock DBD implementation used for testing. It's
entirely self-contained so that you can extract the single library
file (DBD/Mock.pm), put it in your own distribution and be able to run
DBI-based tests even though you don't have information about a
database. (If you're doing so you should probably get rid of the docs
to save space...)
INSTALLATION
To install this module type the following:
perl Makefile.PL
make
make test
make install
DEPENDENCIES
This module requires these other modules and libraries:
DBI (wouldn't make too much sense without it...)
COPYRIGHT AND LICENCE
Copyright (C) 2004 Chris Winters <chris@cwinters.com>
Copyright (C) 2004-2007 Stevan Little <stevan@iinteractive.com>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
========================================
\ No newline at end of file
This diff is collapsed.
requires 'DBI', '1.3';
requires 'perl', 'v5.8.1';
requires 'List::Util', '1.27';
on build => sub {
requires 'Test::Exception', '0.31';
requires 'Test::More', '0.47';
};
......@@ -30,7 +30,7 @@ sub import {
if ( @_ && lc( $_[0] ) eq "pool" );
}
our $VERSION = '1.45';
our $VERSION = '1.46';
our $drh = undef; # will hold driver handle
our $err = 0; # will hold any error codes
......@@ -434,6 +434,53 @@ You can also associate a resultset with a particular SQL statement instead of ad
This will return the given results when the statement 'SELECT foo, bar FROM baz' is prepared. Note that they will be returned B<every time> the statement is prepared, 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 C<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 expresion 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 C<rows> method will return the number of records stocked in the result set. So if your code/application makes use of the C<$sth-E<gt>rows> method for things like UPDATE and DELETE calls you should stock the result set like so:
$dbh->{mock_add_resultset} = {
......@@ -846,7 +893,7 @@ The C<5> is the DBI error number, and C<'Ooops!'> is the error string passed to
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.
Right now this feature is highly experimental, and has been added as a first attempt to automatically handle some of the DBD specific attributes which are commonly used/accessed in DBI programming. The functionality is off by default so as to not cause any issues with backwards compatability, but can easily be turned on and off like this:
Right now this feature is highly experimental, and has been added as a first attempt to automatically handle some of the DBD specific attributes which are commonly used/accessed in DBI programming. 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++;
......@@ -912,24 +959,26 @@ L<http://groups.google.com/group/DBDMock>
=over 4
=item Thanks to Ryan Gerry for his patch in RT #26604
=item Thanks to Ryan Gerry for his patch in RT #26604.
=item Thanks to Marc Beyer for his patch in RT #16951
=item Thanks to Marc Beyer for his patch in RT #16951.
=item Thanks to Justin DeVuyst for the mock_connect_fail idea
=item Thanks to Justin DeVuyst for the mock_connect_fail idea.
=item Thanks to Thilo Planz for the code for C<bind_param_inout>
=item Thanks to Thilo Planz for the code for C<bind_param_inout>.
=item Thanks to Shlomi Fish for help tracking down RT Bug #11515
=item Thanks to Shlomi Fish for help tracking down RT Bug #11515.
=item Thanks to Collin Winter for the patch to fix the C<begin_work()>, C<commit()> and C<rollback()> methods.
=item Thanks to Andrew McHarg E<lt>amcharg@acm.orgE<gt> for C<fetchall_hashref()>, C<fetchrow_hashref()> and C<selectcol_arrayref()> methods and tests.
=item Thanks to Andrew W. Gibbs for the C<mock_last_insert_ids> patch and test
=item Thanks to Andrew W. Gibbs for the C<mock_last_insert_ids> patch and test.
=item Thanks to Chas Owens for patch and test for the C<mock_can_prepare>, C<mock_can_execute>, and C<mock_can_fetch> features.
=item Thanks to Tomas Zemresfor the unit test in RT #71438.
=back
=head1 COPYRIGHT
......@@ -942,6 +991,8 @@ Copyright (C) 2007 Rob Kinyon E<lt>rob.kinyon@gmail.comE<gt>
Copyright (C) 2011 Mariano Wahlmann E<lt>dichoso _at_ gmail.comE<gt>
Copyright (C) 2019 Jason Cooper E<lt>JLCOOPER@cpan.orgE<gt>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
......@@ -953,6 +1004,8 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
Rob Kinyon E<lt>rob.kinyon@gmail.comE<gt>
Mariano Wahlmann E<lt>dichoso _at_ gmail.com <gt>
Mariano Wahlmann E<lt>dichoso _at_ gmail.comE<gt>
Jason Cooper E<lt>JLCOOPER@cpan.orgE<gt>
=cut
......@@ -3,6 +3,8 @@ package DBD::Mock::db;
use strict;
use warnings;
use List::Util qw( first );
our $imp_data_size = 0;
sub ping {
......@@ -84,8 +86,7 @@ sub prepare {
my $rs;
if ( my $all_rs = $dbh->{mock_rs} ) {
if ( my $by_name = $all_rs->{named}{$statement} ) {
if ( my $by_name = $all_rs->{named}{$statement} // first { $statement =~ m/$_->{regexp}/ } @{ $all_rs->{matching} } ) {
# We want to copy this, because it is meant to be reusable
$rs = [ @{ $by_name->{results} } ];
if ( exists $by_name->{failure} ) {
......@@ -312,7 +313,8 @@ sub STORE {
elsif ( $attrib eq 'mock_add_resultset' ) {
$dbh->{mock_rs} ||= {
named => {},
ordered => []
ordered => [],
matching => [],
};
if ( ref $value eq 'ARRAY' ) {
my @copied_values = @{$value};
......@@ -325,8 +327,18 @@ sub STORE {
die "Indexing resultset by name requires passing in 'sql' ",
"as hashref key to 'mock_add_resultset'.\n";
}
my @copied_values = @{ $value->{results} };
$dbh->{mock_rs}{named}{$name} = { results => \@copied_values, };
if ( ref $name eq "Regexp" ) {
push @{ $dbh->{mock_rs}{matching} }, {
regexp => $name,
results => \@copied_values,
};
} else {
$dbh->{mock_rs}{named}{$name} = { results => \@copied_values, };
}
if ( exists $value->{failure} ) {
$dbh->{mock_rs}{named}{$name}{failure} =
[ @{ $value->{failure} }, ];
......
name = "DBD-Mock"
authors = [
"Chris Winters <CWINTERS@cpan.org>",
"Stevan Little <STEVAN@cpan.org>",
"Rob Kinyon <RKINYON@cpan.org>",
"Mariano Wahlmann <DICHI@cpan.org>",
"Jason Cooper <JLCOOPER@cpan.org>",
]
authority = "cpan:JLCOOPER"
module_maker = "ModuleBuildTiny"
static_install = "auto"
no_github_issues = true
use strict;
use Test::More tests => 19;
use Test::More tests => 25;
BEGIN {
use_ok('DBD::Mock');
......@@ -85,6 +85,47 @@ $dbh->{mock_add_resultset} = {
$sth->finish();
}
## test regular expression for query matching
$dbh->{mock_add_resultset} = {
sql => qr/^SELECT foo/,
results => [ [ 'foo' ], [ 200 ] ],
};
## This one should never be used as the above one will have precedence
$dbh->{mock_add_resultset} = {
sql => qr/^SELECT foo FROM/,
results => [ [ 'foo' ], [ 300 ] ],
};
{
my $sth = $dbh->prepare('SELECT foo FROM oof');
isa_ok($sth, 'DBI::st');
my $rows = $sth->execute();
is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
my ($result) = $sth->fetchrow_array();
is($result, 200, '... got the result we expected');
$sth->finish();
}
# check that statically assigned queries take precedence over regex matched ones
{
my $sth = $dbh->prepare('SELECT foo FROM bar');
isa_ok($sth, 'DBI::st');
my $rows = $sth->execute();
is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
my ($result) = $sth->fetchrow_array();
is($result, 50, '... got the result we expected');
$sth->finish();
}
## test the return value of execute
$dbh->{mock_add_resultset} = {
......
use 5.006;
use strict;
use warnings;
use Test::Exception;
use Test::More tests => 6;
BEGIN {
use_ok('DBD::Mock');
use_ok('DBI');
}
my $dbh = DBI->connect( 'DBI:Mock:', '', '', { RaiseError => 1 } );
my $mock_session = DBD::Mock::Session->new(
{
statement => qr/SELECT/,
bound_params => [ 'US', '%joe%' ],
results => [
[
'person.person_id', 'person.person_country',
'person.person_name'
],
[ 1, 'AR', 'Joe Something' ],
[ 2, 'UY', 'Joe That' ],
[ 3, 'AR', 'Joe' ],
]
}
);
$dbh->{mock_session} = $mock_session;
my $sth = $dbh->prepare("SELECT ...");
$sth->execute( 'US', '%joe%' );
my %row;
lives_ok(
sub {
$sth->bind_columns( \( @row{ @{ $sth->{NAME_lc} } } ) );
},
'Bind columns'
);
ok( exists $row{'person.person_name'}, 'First column' );
ok( exists $row{'person.person_country'}, 'Second column' );
ok( exists $row{'person.person_id'}, 'Third column' );
# this is bug RT #71438
use Test::More tests => 6;
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect('dbi:Mock:', '', '', { PrintError => 0, RaiseError => 1});
my $query = 'SELECT foo, bar FROM baz WHERE id=?';
my @session = (
{
statement => $query,
results => [
['foo', 'bar'],
[1, 'test1'],
[2, 'test2']
],
bound_params => [ 1 ],
},
{
statement => $query,
results => [
['abc', 'xyz'],
[7, 'test7'],
[8, 'test8']
],
bound_params => [ 2 ],
},
);
$dbh->{mock_session} = DBD::Mock::Session->new(@session);
# First query
my $sth = $dbh->prepare($query);
$sth->execute(1);
is_deeply(
$sth->fetchrow_hashref(),
{foo => 1, bar => 'test1'}
);
is_deeply(
$sth->fetchrow_hashref(),
{foo => 2, bar => 'test2'}
);
is_deeply(
$sth->fetchrow_hashref(),
undef
);
# Second query
$sth = $dbh->prepare($query);
$sth->execute(2);
is_deeply(
$sth->fetchrow_hashref(),
{abc => 7, xyz => 'test7'}
);
is_deeply(
$sth->fetchrow_hashref(),
{abc => 8, xyz => 'test8'}
);
is_deeply(
$sth->fetchrow_hashref(),
undef
);
......@@ -7,7 +7,7 @@ use DBI;
use DBD::Mock;
# This tests that spurious extra ->execute invocations fail with a
# useful message. This is because there was a bug in which
# useful message (RT #117162). This is because there was a bug in which
# DBD::Mock->verify_bound_params didn't check that the session had run
# out, and on return out-of-bounds element of the state array is
# accessed, causing an unhelpful error "Can't use an undefined value
......