| 1 |
package DBD::Mock; |
| 2 |
|
| 3 |
# --------------------------------------------------------------------------- # |
| 4 |
# Copyright (c) 2004-2007 Stevan Little, Chris Winters |
| 5 |
# (spawned from original code Copyright (c) 1994 Tim Bunce) |
| 6 |
# --------------------------------------------------------------------------- # |
| 7 |
# You may distribute under the terms of either the GNU General Public |
| 8 |
# License or the Artistic License, as specified in the Perl README file. |
| 9 |
# --------------------------------------------------------------------------- # |
| 10 |
|
| 11 |
use 5.008001; |
| 12 |
|
| 13 |
use strict; |
| 14 |
use warnings; |
| 15 |
|
| 16 |
use DBI; |
| 17 |
|
| 18 |
use DBD::Mock::dr; |
| 19 |
use DBD::Mock::db; |
| 20 |
use DBD::Mock::st; |
| 21 |
use DBD::Mock::StatementTrack; |
| 22 |
use DBD::Mock::StatementTrack::Iterator; |
| 23 |
use DBD::Mock::Session; |
| 24 |
use DBD::Mock::Pool; |
| 25 |
use DBD::Mock::Pool::db; |
| 26 |
|
| 27 |
sub import { |
| 28 |
shift; |
| 29 |
$DBI::connect_via = "DBD::Mock::Pool::connect" |
| 30 |
if ( @_ && lc( $_[0] ) eq "pool" ); |
| 31 |
} |
| 32 |
|
| 33 |
our $VERSION = '1.60'; |
| 34 |
|
| 35 |
our $drh = undef; # will hold driver handle |
| 36 |
our $err = 0; # will hold any error codes |
| 37 |
our $errstr = ''; # will hold any error messages |
| 38 |
|
| 39 |
# Defaulting a result set's fields to undef changes the way DBD::Mock responds, so we default it to off |
| 40 |
our $DefaultFieldsToUndef = 0; |
| 41 |
|
| 42 |
|
| 43 |
sub driver { |
| 44 |
return $drh if defined $drh; |
| 45 |
my ( $class, $attributes ) = @_; |
| 46 |
$attributes = {} |
| 47 |
unless ( defined($attributes) && ( ref($attributes) eq 'HASH' ) ); |
| 48 |
$drh = DBI::_new_drh( |
| 49 |
"${class}::dr", |
| 50 |
{ |
| 51 |
Name => 'Mock', |
| 52 |
Version => $DBD::Mock::VERSION, |
| 53 |
Attribution => |
| 54 |
'DBD Mock driver by Chris Winters & Stevan Little (orig. from Tim Bunce)', |
| 55 |
Err => \$DBD::Mock::err, |
| 56 |
Errstr => \$DBD::Mock::errstr, |
| 57 |
|
| 58 |
# mock attributes |
| 59 |
mock_connect_fail => 0, |
| 60 |
|
| 61 |
# and pass in any extra attributes given |
| 62 |
%{$attributes} |
| 63 |
} |
| 64 |
); |
| 65 |
return $drh; |
| 66 |
} |
| 67 |
|
| 68 |
sub CLONE { undef $drh } |
| 69 |
|
| 70 |
# NOTE: |
| 71 |
# this feature is still quite experimental. It is defaulted to |
| 72 |
# be off, but it can be turned on by doing this: |
| 73 |
# $DBD::Mock::AttributeAliasing++; |
| 74 |
# and then turned off by doing: |
| 75 |
# $DBD::Mock::AttributeAliasing = 0; |
| 76 |
# we shall see how this feature works out. |
| 77 |
|
| 78 |
our $AttributeAliasing = 0; |
| 79 |
|
| 80 |
my %AttributeAliases = ( |
| 81 |
mysql => { |
| 82 |
db => { |
| 83 |
|
| 84 |
# aliases can either be a string which is obvious |
| 85 |
mysql_insertid => 'mock_last_insert_id' |
| 86 |
}, |
| 87 |
st => { |
| 88 |
|
| 89 |
# but they can also be a subroutine reference whose |
| 90 |
# first argument will be either the $dbh or the $sth |
| 91 |
# depending upon which context it is aliased in. |
| 92 |
mysql_insertid => |
| 93 |
sub { (shift)->{Database}->{'mock_last_insert_id'} } |
| 94 |
} |
| 95 |
}, |
| 96 |
|
| 97 |
mariadb => { |
| 98 |
db => { |
| 99 |
mariadb_insertid => 'mock_last_insert_id' |
| 100 |
}, |
| 101 |
st => { |
| 102 |
mariadb_insertid => |
| 103 |
sub { (shift)->{Database}->{'mock_last_insert_id'} } |
| 104 |
} |
| 105 |
} |
| 106 |
); |
| 107 |
|
| 108 |
sub _get_mock_attribute_aliases { |
| 109 |
my ($dbname) = @_; |
| 110 |
( exists $AttributeAliases{ lc($dbname) } ) |
| 111 |
|| die "Attribute aliases not available for '$dbname'"; |
| 112 |
return $AttributeAliases{ lc($dbname) }; |
| 113 |
} |
| 114 |
|
| 115 |
sub _set_mock_attribute_aliases { |
| 116 |
my ( $dbname, $dbh_or_sth, $key, $value ) = @_; |
| 117 |
return $AttributeAliases{ lc($dbname) }->{$dbh_or_sth}->{$key} = $value; |
| 118 |
} |
| 119 |
|
| 120 |
## Some useful constants |
| 121 |
|
| 122 |
use constant NULL_RESULTSET => [ [] ]; |
| 123 |
|
| 124 |
1; |
| 125 |
|
| 126 |
__END__ |
| 127 |
|
| 128 |
=head1 NAME |
| 129 |
|
| 130 |
DBD::Mock - Mock database driver for testing |
| 131 |
|
| 132 |
=head1 SYNOPSIS |
| 133 |
|
| 134 |
use DBI; |
| 135 |
|
| 136 |
# connect to your as normal, using 'Mock' as your driver name |
| 137 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
| 138 |
|| die "Cannot create handle: $DBI::errstr\n"; |
| 139 |
|
| 140 |
# create a statement handle as normal and execute with parameters |
| 141 |
my $sth = $dbh->prepare( 'SELECT this, that FROM foo WHERE id = ?' ); |
| 142 |
$sth->execute( 15 ); |
| 143 |
|
| 144 |
# Now query the statement handle as to what has been done with it |
| 145 |
my $mock_params = $sth->{mock_params}; |
| 146 |
print "Used statement: ", $sth->{mock_statement}, "\n", |
| 147 |
"Bound parameters: ", join( ', ', @{ $mock_params } ), "\n"; |
| 148 |
|
| 149 |
=head1 DESCRIPTION |
| 150 |
|
| 151 |
Testing with databases can be tricky. If you are developing a system married to |
| 152 |
a single database then you can make some assumptions about your environment and |
| 153 |
ask the user to provide relevant connection information. But if you need to |
| 154 |
test a framework that uses DBI, particularly a framework that uses different |
| 155 |
types of persistence schemes, then it may be more useful to simply verify what |
| 156 |
the framework is trying to do -- ensure the right SQL is generated and that the |
| 157 |
correct parameters are bound. C<DBD::Mock> makes it easy to just modify your |
| 158 |
configuration (presumably held outside your code) and just use it instead of |
| 159 |
C<DBD::Foo> (like L<DBD::Pg> or L<DBD::mysql>) in your framework. |
| 160 |
|
| 161 |
There is no distinct area where using this module makes sense. (Some people may |
| 162 |
successfully argue that this is a solution looking for a problem...) Indeed, if |
| 163 |
you can assume your users have something like L<DBD::AnyData> or L<DBD::SQLite> |
| 164 |
or if you do not mind creating a dependency on them then it makes far more sense |
| 165 |
to use these legitimate driver implementations and test your application in the |
| 166 |
real world -- at least as much of the real world as you can create in your |
| 167 |
tests... |
| 168 |
|
| 169 |
And if your database handle exists as a package variable or something else |
| 170 |
easily replaced at test-time then it may make more sense to use |
| 171 |
L<Test::MockObject> to create a fully dynamic handle. There is an excellent |
| 172 |
article by chromatic about using L<Test::MockObject> in this and other ways, |
| 173 |
strongly recommended. (See L<SEE ALSO> for a link) |
| 174 |
|
| 175 |
=head2 How does it work? |
| 176 |
|
| 177 |
C<DBD::Mock> comprises a set of classes used by DBI to implement a database |
| 178 |
driver. But instead of connecting to a datasource and manipulating data found |
| 179 |
there it tracks all the calls made to the database handle and any created |
| 180 |
statement handles. You can then inspect them to ensure what you wanted to |
| 181 |
happen actually happened. For instance, say you have a configuration file with |
| 182 |
your database connection information: |
| 183 |
|
| 184 |
[DBI] |
| 185 |
dsn = DBI:Pg:dbname=myapp |
| 186 |
user = foo |
| 187 |
password = bar |
| 188 |
|
| 189 |
And this file is read in at process startup and the handle stored for other |
| 190 |
procedures to use: |
| 191 |
|
| 192 |
package ObjectDirectory; |
| 193 |
|
| 194 |
my ( $DBH ); |
| 195 |
|
| 196 |
sub run_at_startup { |
| 197 |
my ( $class, $config ) = @_; |
| 198 |
$config ||= read_configuration( ... ); |
| 199 |
my $dsn = $config->{DBI}{dsn}; |
| 200 |
my $user = $config->{DBI}{user}; |
| 201 |
my $pass = $config->{DBI}{password}; |
| 202 |
$DBH = DBI->connect( $dsn, $user, $pass ) || die ...; |
| 203 |
} |
| 204 |
|
| 205 |
sub get_database_handle { |
| 206 |
return $DBH; |
| 207 |
} |
| 208 |
|
| 209 |
A procedure might use it like this (ignoring any error handling for the |
| 210 |
moment): |
| 211 |
|
| 212 |
package My::UserActions; |
| 213 |
|
| 214 |
sub fetch_user { |
| 215 |
my ( $class, $login ) = @_; |
| 216 |
my $dbh = ObjectDirectory->get_database_handle; |
| 217 |
my $sql = q{ |
| 218 |
SELECT login_name, first_name, last_name, creation_date, num_logins |
| 219 |
FROM users |
| 220 |
WHERE login_name = ? |
| 221 |
}; |
| 222 |
my $sth = $dbh->prepare( $sql ); |
| 223 |
$sth->execute( $login ); |
| 224 |
my $row = $sth->fetchrow_arrayref; |
| 225 |
return ( $row ) ? User->new( $row ) : undef; |
| 226 |
} |
| 227 |
|
| 228 |
So for the purposes of our tests we just want to ensure that: |
| 229 |
|
| 230 |
=over 4 |
| 231 |
|
| 232 |
=item 1. The right SQL is being executed |
| 233 |
|
| 234 |
=item 2. The right parameters are bound |
| 235 |
|
| 236 |
=back |
| 237 |
|
| 238 |
Assume whether the SQL actually B<works> or not is irrelevant for this test :-) |
| 239 |
|
| 240 |
To do that our test might look like: |
| 241 |
|
| 242 |
my $config = ObjectDirectory->read_configuration( ... ); |
| 243 |
$config->{DBI}{dsn} = 'DBI:Mock:'; |
| 244 |
ObjectDirectory->run_at_startup( $config ); |
| 245 |
|
| 246 |
my $login_name = 'foobar'; |
| 247 |
my $user = My::UserActions->fetch_user( $login_name ); |
| 248 |
|
| 249 |
# Get the handle from ObjectDirectory; |
| 250 |
# this is the same handle used in the |
| 251 |
# 'fetch_user()' procedure above |
| 252 |
my $dbh = ObjectDirectory->get_database_handle(); |
| 253 |
|
| 254 |
# Ask the database handle for the history |
| 255 |
# of all statements executed against it |
| 256 |
my $history = $dbh->{mock_all_history}; |
| 257 |
|
| 258 |
# Now query that history record to |
| 259 |
# see if our expectations match reality |
| 260 |
is(scalar(@{$history}), 1, 'Correct number of statements executed' ; |
| 261 |
|
| 262 |
my $login_st = $history->[0]; |
| 263 |
like($login_st->statement, |
| 264 |
qr/SELECT login_name.*FROM users WHERE login_name = ?/sm, |
| 265 |
'Correct statement generated' ); |
| 266 |
|
| 267 |
my $params = $login_st->bound_params; |
| 268 |
is(scalar(@{$params}), 1, 'Correct number of parameters bound'); |
| 269 |
is($params->[0], $login_name, 'Correct value for parameter 1' ); |
| 270 |
|
| 271 |
# Reset the handle for future operations |
| 272 |
$dbh->{mock_clear_history} = 1; |
| 273 |
|
| 274 |
The list of properties and what they return is listed below. But in an overall |
| 275 |
view: |
| 276 |
|
| 277 |
=over 4 |
| 278 |
|
| 279 |
=item * |
| 280 |
|
| 281 |
A database handle contains the history of all statements created against it. |
| 282 |
Other properties set for the handle (e.g., C<PrintError>, C<RaiseError>) are |
| 283 |
left alone and can be queried as normal, but they do not affect anything. (A |
| 284 |
future feature may track the sequence/history of these assignments but if there |
| 285 |
is no demand it probably will not get implemented.) |
| 286 |
|
| 287 |
=item * |
| 288 |
|
| 289 |
A statement handle contains the statement it was prepared with plus all bound |
| 290 |
parameters or parameters passed via C<execute()>. It can also contain |
| 291 |
predefined results for the statement handle to C<fetch>, track how many fetches |
| 292 |
were called and what its current record is. |
| 293 |
|
| 294 |
=back |
| 295 |
|
| 296 |
=head2 A Word of Warning |
| 297 |
|
| 298 |
This may be an incredibly naive implementation of a DBD. But it works for me... |
| 299 |
|
| 300 |
=head1 DBD::Mock |
| 301 |
|
| 302 |
Since this is a normal DBI statement handle we need to expose our tracking |
| 303 |
information as properties (accessed like a hash) rather than methods. |
| 304 |
|
| 305 |
=head2 Database Driver Properties |
| 306 |
|
| 307 |
=over 4 |
| 308 |
|
| 309 |
=item B<C<mock_connect_fail>> |
| 310 |
|
| 311 |
This is a boolean property which when set to true (C<1>) will not allow DBI to |
| 312 |
connect. This can be used to simulate a DSN error or authentication failure. |
| 313 |
This can then be set back to false (C<0>) to resume normal DBI operations. Here |
| 314 |
is an example of how this works: |
| 315 |
|
| 316 |
# install the DBD::Mock driver |
| 317 |
my $drh = DBI->install_driver('Mock'); |
| 318 |
|
| 319 |
$drh->{mock_connect_fail} = 1; |
| 320 |
|
| 321 |
# this connection will fail |
| 322 |
my $dbh = DBI->connect('dbi:Mock:', '', '') || die "Cannot connect"; |
| 323 |
|
| 324 |
# this connection will throw an exception |
| 325 |
my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1 }); |
| 326 |
|
| 327 |
$drh->{mock_connect_fail} = 0; |
| 328 |
|
| 329 |
# this will work now ... |
| 330 |
my $dbh = DBI->connect(...); |
| 331 |
|
| 332 |
This feature is conceptually different from the C<mock_can_connect> attribute |
| 333 |
of the C<$dbh> in that it has a driver-wide scope, where C<mock_can_connect> |
| 334 |
is handle-wide scope. It also only prevents the initial connection, any C<$dbh> |
| 335 |
handles created prior to setting C<mock_connect_fail> to true (C<1>) will still |
| 336 |
go on working just fine. |
| 337 |
|
| 338 |
=item B<C<mock_data_sources>> |
| 339 |
|
| 340 |
This is an ARRAY reference which holds fake data sources which are returned by |
| 341 |
the Driver and Database Handle's C<data_source()> method. |
| 342 |
|
| 343 |
=item B<C<mock_add_data_sources>> |
| 344 |
|
| 345 |
This takes a string and adds it to the C<mock_data_sources> attribute. |
| 346 |
|
| 347 |
=back |
| 348 |
|
| 349 |
=head2 Database Handle Properties |
| 350 |
|
| 351 |
=over 4 |
| 352 |
|
| 353 |
=item B<C<mock_all_history>> |
| 354 |
|
| 355 |
Returns an array reference with all history (a.k.a. |
| 356 |
C<DBD::Mock::StatementTrack>) objects created against the database handle in |
| 357 |
the order they were created. Each history object can then report information |
| 358 |
about the SQL statement used to create it, the bound parameters, etc.. |
| 359 |
|
| 360 |
=item B<C<mock_all_history_iterator>> |
| 361 |
|
| 362 |
Returns a C<DBD::Mock::StatementTrack::Iterator> object which will iterate |
| 363 |
through the current set of C<DBD::Mock::StatementTrack> object in the history. |
| 364 |
See the L</DBD::Mock::StatementTrack::Iterator> documentation below for more |
| 365 |
information. |
| 366 |
|
| 367 |
=item B<C<mock_clear_history>> |
| 368 |
|
| 369 |
If set to a true value all previous statement history operations will be |
| 370 |
erased. This B<includes> the history of currently open handles, so if you do |
| 371 |
something like: |
| 372 |
|
| 373 |
my $dbh = get_handle( ... ); |
| 374 |
my $sth = $dbh->prepare( ... ); |
| 375 |
$dbh->{mock_clear_history} = 1; |
| 376 |
$sth->execute( 'Foo' ); |
| 377 |
|
| 378 |
You will have no way to learn from the database handle that the statement |
| 379 |
parameter 'Foo' was bound. |
| 380 |
|
| 381 |
This is useful mainly to ensure you can isolate the statement histories from |
| 382 |
each other. A typical sequence will look like: |
| 383 |
|
| 384 |
set handle to framework |
| 385 |
perform operations |
| 386 |
analyze mock database handle |
| 387 |
reset mock database handle history |
| 388 |
perform more operations |
| 389 |
analyze mock database handle |
| 390 |
reset mock database handle history |
| 391 |
... |
| 392 |
|
| 393 |
=item B<C<mock_can_connect>> |
| 394 |
|
| 395 |
This statement allows you to simulate a downed database connection. This is |
| 396 |
useful in testing how your application/tests will perform in the face of some |
| 397 |
kind of catastrophic event such as a network outage or database server failure. |
| 398 |
It is a simple boolean value which defaults to on, and can be set like this: |
| 399 |
|
| 400 |
# turn the database off |
| 401 |
$dbh->{mock_can_connect} = 0; |
| 402 |
|
| 403 |
# turn it back on again |
| 404 |
$dbh->{mock_can_connect} = 1; |
| 405 |
|
| 406 |
The statement handle checks this value as well, so something like this |
| 407 |
will fail in the expected way: |
| 408 |
|
| 409 |
$dbh = DBI->connect( 'DBI:Mock:', '', '' ); |
| 410 |
$dbh->{mock_can_connect} = 0; |
| 411 |
|
| 412 |
# blows up! |
| 413 |
my $sth = eval { $dbh->prepare( 'SELECT foo FROM bar' ) }); |
| 414 |
if ( $@ ) { |
| 415 |
# Here, $DBI::errstr = 'No connection present' |
| 416 |
} |
| 417 |
|
| 418 |
Turning off the database after a statement prepare will fail on the statement |
| 419 |
C<execute()>, which is hopefully what you would expect: |
| 420 |
|
| 421 |
$dbh = DBI->connect( 'DBI:Mock:', '', '' ); |
| 422 |
|
| 423 |
# ok! |
| 424 |
my $sth = eval { $dbh->prepare( 'SELECT foo FROM bar' ) }); |
| 425 |
$dbh->{mock_can_connect} = 0; |
| 426 |
|
| 427 |
# blows up! |
| 428 |
$sth->execute; |
| 429 |
|
| 430 |
Similarly: |
| 431 |
|
| 432 |
$dbh = DBI->connect( 'DBI:Mock:', '', '' ); |
| 433 |
|
| 434 |
# ok! |
| 435 |
my $sth = eval { $dbh->prepare( 'SELECT foo FROM bar' ) }); |
| 436 |
|
| 437 |
# ok! |
| 438 |
$sth->execute; |
| 439 |
|
| 440 |
$dbh->{mock_can_connect} = 0; |
| 441 |
|
| 442 |
# blows up! |
| 443 |
my $row = $sth->fetchrow_arrayref; |
| 444 |
|
| 445 |
Note: The handle attribute C<Active> and the handle method C<ping> will behave |
| 446 |
according to the value of C<mock_can_connect>. So if C<mock_can_connect> were |
| 447 |
to be set to C<0> (or off), then both C<Active> and C<ping> would return false |
| 448 |
values (or C<0>). |
| 449 |
|
| 450 |
=item B<C<mock_add_resultset( \@resultset | \%resultset_and_options )>> |
| 451 |
|
| 452 |
This stocks the database handle with a record set, allowing you to seed data |
| 453 |
for your application to see if it works properly. Each recordset is a simple |
| 454 |
arrayref of arrays with the first arrayref being the field names used. Every |
| 455 |
time a statement handle is created it asks the database handle if it has any |
| 456 |
resultsets available and if so uses it. |
| 457 |
|
| 458 |
Here is a sample usage, partially from the test suite: |
| 459 |
|
| 460 |
my @user_results = ( |
| 461 |
[ 'login', 'first_name', 'last_name' ], |
| 462 |
[ 'cwinters', 'Chris', 'Winters' ], |
| 463 |
[ 'bflay', 'Bobby', 'Flay' ], |
| 464 |
[ 'alincoln', 'Abe', 'Lincoln' ], |
| 465 |
); |
| 466 |
my @generic_results = ( |
| 467 |
[ 'foo', 'bar' ], |
| 468 |
[ 'this_one', 'that_one' ], |
| 469 |
[ 'this_two', 'that_two' ], |
| 470 |
); |
| 471 |
|
| 472 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ); |
| 473 |
$dbh->{mock_add_resultset} = \@user_results; # add first resultset |
| 474 |
$dbh->{mock_add_resultset} = \@generic_results; # add second resultset |
| 475 |
my ( $sth ); |
| 476 |
eval { |
| 477 |
$sth = $dbh->prepare( 'SELECT login, first_name, last_name FROM foo' ); |
| 478 |
$sth->execute(); |
| 479 |
}; |
| 480 |
|
| 481 |
# this will fetch rows from the first resultset... |
| 482 |
my $row1 = $sth->fetchrow_arrayref; |
| 483 |
my $user1 = User->new( login => $row->[0], |
| 484 |
first => $row->[1], |
| 485 |
last => $row->[2] ); |
| 486 |
is( $user1->full_name, 'Chris Winters' ); |
| 487 |
|
| 488 |
my $row2 = $sth->fetchrow_arrayref; |
| 489 |
my $user2 = User->new( login => $row->[0], |
| 490 |
first => $row->[1], |
| 491 |
last => $row->[2] ); |
| 492 |
is( $user2->full_name, 'Bobby Flay' ); |
| 493 |
... |
| 494 |
|
| 495 |
my $sth_generic = $dbh->prepare( 'SELECT foo, bar FROM baz' ); |
| 496 |
$sth_generic->execute; |
| 497 |
|
| 498 |
# this will fetch rows from the second resultset... |
| 499 |
my $row = $sth->fetchrow_arrayref; |
| 500 |
|
| 501 |
It is possible to assign a hashref where the resultset must be given as |
| 502 |
value for the C<results> key: |
| 503 |
|
| 504 |
$dbh->{mock_add_resultset} = { |
| 505 |
results => [ |
| 506 |
[ 'foo', 'bar' ], |
| 507 |
[ 'this_one', 'that_one' ], |
| 508 |
[ 'this_two', 'that_two' ], |
| 509 |
], |
| 510 |
}; |
| 511 |
|
| 512 |
The reason for the hashref form is that you can add options as described |
| 513 |
in the following. |
| 514 |
|
| 515 |
You can associate a resultset with a particular SQL statement instead of |
| 516 |
adding them in the order they will be fetched: |
| 517 |
|
| 518 |
$dbh->{mock_add_resultset} = { |
| 519 |
sql => 'SELECT foo, bar FROM baz', |
| 520 |
results => [ |
| 521 |
[ 'foo', 'bar' ], |
| 522 |
[ 'this_one', 'that_one' ], |
| 523 |
[ 'this_two', 'that_two' ], |
| 524 |
], |
| 525 |
}; |
| 526 |
|
| 527 |
This will return the given results when the statement |
| 528 |
'C<SELECT foo, bar FROM baz>' is prepared/executed. Note that they will be |
| 529 |
returned B<every time> the statement is prepared/executed, not just the first. |
| 530 |
It should also be noted that if you want, for some reason, to change the result |
| 531 |
set bound to a particular SQL statement, all you need to do is add the result |
| 532 |
set again with the same SQL statement and C<DBD::Mock> will overwrite it. |
| 533 |
|
| 534 |
If the C<sql> parameter is a regular expression reference then the results will |
| 535 |
be returned for any SQL statements that matches it: |
| 536 |
|
| 537 |
$dbh->{mock_add_resultset} = { |
| 538 |
sql => qr/^SELECT foo FROM/i, |
| 539 |
results => [ |
| 540 |
[ 'foo' ], |
| 541 |
[ 'this_one' ], |
| 542 |
], |
| 543 |
}; |
| 544 |
|
| 545 |
If an SQL statement matches both a specified SQL statement result set and a |
| 546 |
regular expression result set then the specified SQL statement takes precedence. |
| 547 |
If two regular expression result sets match then the first one added takes |
| 548 |
precedence: |
| 549 |
|
| 550 |
# Set up our first regex matching result set |
| 551 |
$dbh->{mock_add_resultset} = { |
| 552 |
sql => qr/^SELECT foo/, |
| 553 |
results => [ [ 'foo' ], [ 200 ] ], |
| 554 |
}; |
| 555 |
|
| 556 |
# Set up our second regex matching result set |
| 557 |
# Note - This results set would never be used as the one above will match |
| 558 |
# and thus take precedence |
| 559 |
$dbh->{mock_add_resultset} = { |
| 560 |
sql => qr/^SELECT foo FROM/, |
| 561 |
results => [ [ 'foo' ], [ 300 ] ], |
| 562 |
}; |
| 563 |
|
| 564 |
# Set up our first statically defined result set |
| 565 |
# This result set will take precedence over the regex matching ones above |
| 566 |
$dbh->{mock_add_resultset} = { |
| 567 |
sql => 'SELECT foo FROM bar', |
| 568 |
results => [[ 'foo' ], [ 50 ]] |
| 569 |
}; |
| 570 |
|
| 571 |
# This query will be served by the first regex matching result set |
| 572 |
my $sth = $dbh->prepare('SELECT foo FROM oof'); |
| 573 |
$sth->execute() |
| 574 |
|
| 575 |
my ($result) = $sth->fetchrow_array(); |
| 576 |
is( $result, 200 ); |
| 577 |
|
| 578 |
# This quere will be served by the statically defined result set |
| 579 |
$sth = $dbh->prepare('SELECT foo FROM bar'); |
| 580 |
$sth->execute(); |
| 581 |
|
| 582 |
my ($result2) = $sth->fetchrow_array(); |
| 583 |
is( $result2, 50 ); |
| 584 |
|
| 585 |
It should also be noted that the C<rows> method will return the number of |
| 586 |
records stocked in the result set. So if your code/application makes use of the |
| 587 |
C<< $sth->rows >> method for things like C<UPDATE> and C<DELETE> calls you |
| 588 |
should stock the result set like so: |
| 589 |
|
| 590 |
$dbh->{mock_add_resultset} = { |
| 591 |
sql => 'UPDATE foo SET baz = 1, bar = 2', |
| 592 |
# this will appear to have updated 3 rows |
| 593 |
results => [[ 'rows' ], [], [], []], |
| 594 |
}; |
| 595 |
|
| 596 |
# or ... |
| 597 |
|
| 598 |
$dbh->{mock_add_resultset} = { |
| 599 |
sql => 'DELETE FROM foo WHERE bar = 2', |
| 600 |
# this will appear to have deleted 1 row |
| 601 |
results => [[ 'rows' ], []], |
| 602 |
}; |
| 603 |
|
| 604 |
Now I admit this is not the most elegant way to go about this, but it works for |
| 605 |
me for now, and until I can come up with a better method, or someone sends me a |
| 606 |
patch ;) it will do for now. |
| 607 |
|
| 608 |
If you want a given statement to fail, you will have to use the hashref method |
| 609 |
and add a C<failure> key. That key must be handed an arrayref with the error |
| 610 |
number and error string, in that order. |
| 611 |
|
| 612 |
$dbh->{mock_add_resultset} = { |
| 613 |
sql => 'SELECT foo FROM bar', |
| 614 |
results => DBD::Mock->NULL_RESULTSET, |
| 615 |
failure => [ 5, 'Ooops!' ], |
| 616 |
}; |
| 617 |
|
| 618 |
Without the C<sql> attribute the next statement will fail in any case: |
| 619 |
|
| 620 |
$dbh->{mock_add_resultset} = { |
| 621 |
results => DBD::Mock->NULL_RESULTSET, |
| 622 |
failure => [ 5, 'Ooops!' ], |
| 623 |
}; |
| 624 |
|
| 625 |
=item B<C<mock_get_info>> |
| 626 |
|
| 627 |
This attribute can be used to set up values for C<get_info()>. It takes a |
| 628 |
hashref of attribute_name/value pairs. See L<DBI> for more information on the |
| 629 |
information types and their meaning. |
| 630 |
|
| 631 |
=item B<C<mock_session>> |
| 632 |
|
| 633 |
This attribute can be used to set a current C<DBD::Mock::Session> object. For |
| 634 |
more information on this, see the L</DBD::Mock::Session> docs below. This |
| 635 |
attribute can also be used to remove the current session from the C<$dbh> |
| 636 |
simply by setting it to C<undef>. |
| 637 |
|
| 638 |
=item B<C<mock_last_insert_id>> |
| 639 |
|
| 640 |
This attribute is incremented each time an C<INSERT> statement is passed to |
| 641 |
C<prepare> on a per-handle basis. It's starting value can be set with the |
| 642 |
C<mock_start_insert_id> attribute (see below). |
| 643 |
|
| 644 |
$dbh->{mock_start_insert_id} = 10; |
| 645 |
|
| 646 |
my $sth = $dbh->prepare('INSERT INTO Foo (foo, bar) VALUES(?, ?)'); |
| 647 |
|
| 648 |
$sth->execute(1, 2); |
| 649 |
# $dbh->{mock_last_insert_id} == 10 |
| 650 |
|
| 651 |
$sth->execute(3, 4); |
| 652 |
# $dbh->{mock_last_insert_id} == 11 |
| 653 |
|
| 654 |
For more examples, please refer to the test file |
| 655 |
F<t/025_mock_last_insert_id.t>. |
| 656 |
|
| 657 |
To access C<last_insert_id> using driver specific attributes like |
| 658 |
C<mysql_insertid> and C<mariadb_insertid> then you can use |
| 659 |
L<Attribute Aliasing>. |
| 660 |
|
| 661 |
=item B<C<mock_start_insert_id>> |
| 662 |
|
| 663 |
This attribute can be used to set a start value for the C<mock_last_insert_id> |
| 664 |
attribute. It can also be used to effectively reset the C<mock_last_insert_id> |
| 665 |
attribute as well. |
| 666 |
|
| 667 |
This attribute also can be used with an ARRAY ref parameter, it's behavior is |
| 668 |
slightly different in that instead of incrementing the value for every |
| 669 |
C<prepare> it will only increment for each C<execute>. This allows it to be |
| 670 |
used over multiple C<execute> calls in a single C<$sth>. It's usage looks like |
| 671 |
this: |
| 672 |
|
| 673 |
$dbh->{mock_start_insert_id} = [ 'Foo', 10 ]; |
| 674 |
$dbh->{mock_start_insert_id} = [ 'Baz', 20 ]; |
| 675 |
|
| 676 |
my $sth1 = $dbh->prepare('INSERT INTO Foo (foo, bar) VALUES(?, ?)'); |
| 677 |
|
| 678 |
my $sth2 = $dbh->prepare('INSERT INTO Baz (baz, buz) VALUES(?, ?)'); |
| 679 |
|
| 680 |
$sth1->execute(1, 2); |
| 681 |
# $dbh->{mock_last_insert_id} == 10 |
| 682 |
|
| 683 |
$sth2->execute(3, 4); |
| 684 |
# $dbh->{mock_last_insert_id} == 20 |
| 685 |
|
| 686 |
Note that C<DBD::Mock>'s matching of table names in C<INSERT> statements is |
| 687 |
fairly simple, so if your table names are quoted in the insert statement |
| 688 |
(C<INSERT INTO "Foo">) then you need to quote the name for |
| 689 |
C<mock_start_insert_id>: |
| 690 |
|
| 691 |
$dbh->{mock_start_insert_id} = [ q{"Foo"}, 10 ]; |
| 692 |
|
| 693 |
=item B<C<mock_add_parser>> |
| 694 |
|
| 695 |
DBI provides some simple parsing capabilities for C<SELECT> statements to |
| 696 |
ensure that placeholders are bound properly. And typically you may simply want |
| 697 |
to check after the fact that a statement is syntactically correct, or at least |
| 698 |
what you expect. |
| 699 |
|
| 700 |
But other times you may want to parse the statement as it is prepared rather |
| 701 |
than after the fact. There is a hook in this mock database driver for you to |
| 702 |
provide your own parsing routine or object. |
| 703 |
|
| 704 |
The syntax is simple: |
| 705 |
|
| 706 |
$dbh->{mock_add_parser} = sub { |
| 707 |
my ( $sql ) = @_; |
| 708 |
unless ( $sql =~ /some regex/ ) { |
| 709 |
die "does not contain secret fieldname"; |
| 710 |
} |
| 711 |
}; |
| 712 |
|
| 713 |
You can also add more than one for a handle. They will be called in order, and |
| 714 |
the first one to fail will halt the parsing process: |
| 715 |
|
| 716 |
$dbh->{mock_add_parser} = \&parse_update_sql; |
| 717 |
$dbh->{mock_add-parser} = \&parse_insert_sql; |
| 718 |
|
| 719 |
Depending on the C<PrintError> and C<RaiseError> settings in the database |
| 720 |
handle any parsing errors encountered will issue a C<warn> or C<die>. No matter |
| 721 |
what the statement handle will be C<undef>. |
| 722 |
|
| 723 |
Instead of providing a subroutine reference you can use an object. The only |
| 724 |
requirement is that it implements the method C<parse()> and takes a SQL |
| 725 |
statement as the only argument. So you should be able to do something like the |
| 726 |
following (untested): |
| 727 |
|
| 728 |
my $parser = SQL::Parser->new( 'mysql', { RaiseError => 1 } ); |
| 729 |
$dbh->{mock_add_parser} = $parser; |
| 730 |
|
| 731 |
=item B<C<mock_data_sources>> & B<C<mock_add_data_sources>> |
| 732 |
|
| 733 |
These properties will dispatch to the Driver's properties of the same name. |
| 734 |
|
| 735 |
=back |
| 736 |
|
| 737 |
=head2 Attribute Aliasing |
| 738 |
|
| 739 |
Basically this feature allows you to alias attributes to other attributes. So |
| 740 |
for instance, you can alias a commonly expected attribute like |
| 741 |
C<mysql_insertid> to something C<DBD::Mock> already has like |
| 742 |
C<mock_last_insert_id>. While you can also just set C<mysql_insertid> yourself, |
| 743 |
this functionality allows it to take advantage of things like the |
| 744 |
autoincrementing of the C<mock_last_insert_id> attribute. |
| 745 |
|
| 746 |
The functionality is off by default so as to not cause any issues with |
| 747 |
backwards compatibility, but can easily be turned on and off like this: |
| 748 |
|
| 749 |
# turn it on |
| 750 |
$DBD::Mock::AttributeAliasing++; |
| 751 |
|
| 752 |
# turn it off |
| 753 |
$DBD::Mock::AttributeAliasing = 0; |
| 754 |
|
| 755 |
Once this is turned on, you will need to choose a database specific attribute |
| 756 |
aliasing table like so: |
| 757 |
|
| 758 |
DBI->connect('dbi:Mock:MySQL', '', ''); |
| 759 |
|
| 760 |
Or, by using the database name if using driver DSNs: |
| 761 |
|
| 762 |
DBI->connect('dbi:Mock:host=localhost;port=3306;database=MySQL', '', ''); |
| 763 |
|
| 764 |
The C<MySQL> in the DSN will be picked up and the MySQL specific attribute |
| 765 |
aliasing will be used. |
| 766 |
|
| 767 |
Right now there is only minimal support for MySQL and MariaDB: |
| 768 |
|
| 769 |
=over 4 |
| 770 |
|
| 771 |
=item MySQL |
| 772 |
|
| 773 |
Currently the C<mysql_insertid> attribute for C<$dbh> and C<$sth> are aliased |
| 774 |
to the C<$dbh> attribute C<mock_last_insert_id>. |
| 775 |
|
| 776 |
=item MariaDB |
| 777 |
|
| 778 |
Currently the C<mariadb_insertid> attribute for C<$dbh> and C<$sth> are aliased |
| 779 |
to the C<$dbh> attribute C<mock_last_insert_id>. |
| 780 |
|
| 781 |
=back |
| 782 |
|
| 783 |
It is possible to add more aliases though, using the |
| 784 |
C<DBD::Mock:_set_mock_attribute_aliases> function (see the source code for |
| 785 |
details) |
| 786 |
|
| 787 |
=head2 Database Driver Methods |
| 788 |
|
| 789 |
=over 4 |
| 790 |
|
| 791 |
=item B<C<last_insert_id>> |
| 792 |
|
| 793 |
This returns the value of C<mock_last_insert_id>. |
| 794 |
|
| 795 |
=back |
| 796 |
|
| 797 |
In order to capture C<begin_work()>, C<commit()>, and C<rollback()>, |
| 798 |
C<DBD::Mock> will create statements for them, as if you had issued them in the |
| 799 |
appropriate SQL command line program. They will go through the standard |
| 800 |
C<prepare()>-C<execute()> cycle, meaning that any custom SQL parsers will be |
| 801 |
triggered and C<DBD::Mock::Session> will need to know about these statements. |
| 802 |
|
| 803 |
=over 4 |
| 804 |
|
| 805 |
=item B<C<begin_work>> |
| 806 |
|
| 807 |
This will create a statement with SQL of C<BEGIN WORK> and no parameters. |
| 808 |
|
| 809 |
=item B<C<commit>> |
| 810 |
|
| 811 |
This will create a statement with SQL of C<COMMIT> and no parameters. |
| 812 |
|
| 813 |
=item B<C<rollback>> |
| 814 |
|
| 815 |
This will create a statement with SQL of C<ROLLBACK> and no parameters. |
| 816 |
|
| 817 |
=back |
| 818 |
|
| 819 |
|
| 820 |
=head2 Statement Handle Properties |
| 821 |
|
| 822 |
=over 4 |
| 823 |
|
| 824 |
=item B<C<Active>> |
| 825 |
|
| 826 |
Returns true if the handle is a C<SELECT> and has more records to fetch, false |
| 827 |
otherwise. (From the DBI.) |
| 828 |
|
| 829 |
=item B<C<mock_statement>> |
| 830 |
|
| 831 |
The SQL statement this statement handle was C<prepare>d with. So if the handle |
| 832 |
was created with: |
| 833 |
|
| 834 |
my $sth = $dbh->prepare( 'SELECT * FROM foo' ); |
| 835 |
|
| 836 |
This would return: |
| 837 |
|
| 838 |
SELECT * FROM foo |
| 839 |
|
| 840 |
The original statement is unmodified so if you are checking against it in tests |
| 841 |
you may want to use a regex rather than a straight equality check. (However if |
| 842 |
you use a phrasebook to store your SQL externally you are a step ahead...) |
| 843 |
|
| 844 |
=item B<C<mock_fields>> |
| 845 |
|
| 846 |
Fields used by the statement. As said elsewhere we do no analysis or parsing to |
| 847 |
find these, you need to define them beforehand. That said, you do not actually |
| 848 |
need this very often. |
| 849 |
|
| 850 |
Note that this returns the same thing as the normal statement property |
| 851 |
C<FIELD>. |
| 852 |
|
| 853 |
=item B<C<mock_params>> |
| 854 |
|
| 855 |
Returns an arrayref of parameters bound to this statement in the order |
| 856 |
specified by the bind type. For instance, if you created and stocked a handle |
| 857 |
with: |
| 858 |
|
| 859 |
my $sth = $dbh->prepare( 'SELECT * FROM foo WHERE id = ? AND is_active = ?' ); |
| 860 |
$sth->bind_param( 2, 'yes' ); |
| 861 |
$sth->bind_param( 1, 7783 ); |
| 862 |
|
| 863 |
This would return: |
| 864 |
|
| 865 |
[ 7738, 'yes' ] |
| 866 |
|
| 867 |
The same result will occur if you pass the parameters via C<execute()> instead: |
| 868 |
|
| 869 |
my $sth = $dbh->prepare( 'SELECT * FROM foo WHERE id = ? AND is_active = ?' ); |
| 870 |
$sth->execute( 7783, 'yes' ); |
| 871 |
|
| 872 |
The same using named parameters |
| 873 |
|
| 874 |
my $sth = $dbh->prepare( 'SELECT * FROM foo WHERE id = :id AND is_active = :active' ); |
| 875 |
$sth->bind_param( ':id' => 7783 ); |
| 876 |
$sth->bind_param( ':active' => 'yes' ); |
| 877 |
|
| 878 |
=item B<C<mock_param_attrs>> |
| 879 |
|
| 880 |
Returns an arrayref of any attributes (parameter type) defined for bound |
| 881 |
parameters (note: you rarely need to define attributes for bound parameters). |
| 882 |
Where an attribute/type hasn't been that slot in the returned arrayref will be |
| 883 |
C<undef>. e.g. for: |
| 884 |
|
| 885 |
my $sth = $dbh->prepare( 'SELECT * FROM foo WHERE id = ? AND is_active = ?' ); |
| 886 |
$sth->bind_param( 2, 'yes' ); |
| 887 |
$sth->bind_param( 1 7783, SQL_INTEGER ); |
| 888 |
|
| 889 |
This would return: |
| 890 |
|
| 891 |
[ SQL_INTEGER, undef ] |
| 892 |
|
| 893 |
Passing parameters via C<execute()> will always populate the array with |
| 894 |
C<undef>, so for: |
| 895 |
|
| 896 |
$sth->execute( 7783, 'yes' ); |
| 897 |
|
| 898 |
This would return: |
| 899 |
|
| 900 |
[ undef, undef ] |
| 901 |
|
| 902 |
=item B<C<mock_execution_history>> |
| 903 |
|
| 904 |
Returns an arrayref where each entry contains the details for an execution of |
| 905 |
the prepared statement. e.g. after: |
| 906 |
|
| 907 |
my $sth = $dbh->prepare( 'SELECT * FROM foo WHERE id = ? AND is_active = ?' ); |
| 908 |
$sth->bind_param( 2, 'yes' ); |
| 909 |
$sth->bind_param( 1 7783, SQL_INTEGER ); |
| 910 |
$sth->execute(); |
| 911 |
|
| 912 |
$sth->execute( 1023, 'no' ); |
| 913 |
|
| 914 |
Then C<< $sth->{mock_execution_history} >> would be: |
| 915 |
|
| 916 |
[ |
| 917 |
{ |
| 918 |
params => [ 7783, 'yes' ], |
| 919 |
attrs => [ SQL_INTEGER, undef ], |
| 920 |
}, { |
| 921 |
params => [ 1023, 'no' ], |
| 922 |
attrs => [ undef, undef ], |
| 923 |
} |
| 924 |
] |
| 925 |
|
| 926 |
=item B<C<mock_records>> |
| 927 |
|
| 928 |
An arrayref of arrayrefs representing the records the mock statement was |
| 929 |
stocked with. |
| 930 |
|
| 931 |
=item B<C<mock_num_records>> |
| 932 |
|
| 933 |
Number of records the mock statement was stocked with; if never stocked it is |
| 934 |
still C<0>. (Some weirdos might expect undef...) |
| 935 |
|
| 936 |
=item B<C<mock_num_rows>> |
| 937 |
|
| 938 |
This returns the same value as I<mock_num_records>. And is what is returned by |
| 939 |
the C<rows> method of the statement handle. |
| 940 |
|
| 941 |
=item B<C<mock_current_record_num>> |
| 942 |
|
| 943 |
Current record the statement is on; returns C<0> in the instances when you have |
| 944 |
not yet called C<execute()> and if you have not yet called a C<fetch> method |
| 945 |
after the execute. |
| 946 |
|
| 947 |
=item B<C<mock_is_executed>> |
| 948 |
|
| 949 |
Whether C<execute()> has been called against the statement handle. Returns |
| 950 |
'yes' if so, 'no' if not. |
| 951 |
|
| 952 |
=item B<C<mock_is_finished>> |
| 953 |
|
| 954 |
Whether C<finish()> has been called against the statement handle. Returns 'yes' |
| 955 |
if so, 'no' if not. |
| 956 |
|
| 957 |
=item B<C<mock_is_depleted>> |
| 958 |
|
| 959 |
Returns 'yes' if all the records in the recordset have been returned. If no |
| 960 |
C<fetch()> was executed against the statement, or If no return data was set |
| 961 |
this will return 'no'. |
| 962 |
|
| 963 |
=item B<C<mock_my_history>> |
| 964 |
|
| 965 |
Returns a C<DBD::Mock::StatementTrack> object which tracks the actions |
| 966 |
performed by this statement handle. Most of the actions are separately |
| 967 |
available from the properties listed above, so you should never need this. |
| 968 |
|
| 969 |
=back |
| 970 |
|
| 971 |
=head1 DBD::Mock::Pool |
| 972 |
|
| 973 |
This module can be used to emulate L<Apache::DBI> style DBI connection pooling. |
| 974 |
Just as with C<Apache::DBI>, you must enable C<DBD::Mock::Pool> before loading |
| 975 |
DBI. |
| 976 |
|
| 977 |
use DBD::Mock qw(Pool); |
| 978 |
# followed by ... |
| 979 |
use DBI; |
| 980 |
|
| 981 |
While this may not seem to make a lot of sense in a single-process testing |
| 982 |
scenario, it can be useful when testing code which assumes a multi-process |
| 983 |
C<Apache::DBI> pooled environment. |
| 984 |
|
| 985 |
=head1 DBD::Mock::StatementTrack |
| 986 |
|
| 987 |
Under the hood this module does most of the work with a |
| 988 |
C<DBD::Mock::StatementTrack> object. This is most useful when you are reviewing |
| 989 |
multiple statements at a time, otherwise you might want to use the C<mock_*> |
| 990 |
statement handle attributes instead. |
| 991 |
|
| 992 |
=over 4 |
| 993 |
|
| 994 |
=item B<C<new( %params )>> |
| 995 |
|
| 996 |
Takes the following parameters: |
| 997 |
|
| 998 |
=over 4 |
| 999 |
|
| 1000 |
=item * |
| 1001 |
|
| 1002 |
B<C<return_data>>: Arrayref of return data records |
| 1003 |
|
| 1004 |
=item * |
| 1005 |
|
| 1006 |
B<C<fields>>: Arrayref of field names |
| 1007 |
|
| 1008 |
=item * |
| 1009 |
|
| 1010 |
B<C<bound_params>>: Arrayref of bound parameters |
| 1011 |
|
| 1012 |
=item * |
| 1013 |
|
| 1014 |
B<C<bound_param_attrs>>: Arrayref of bound parameter attributes |
| 1015 |
|
| 1016 |
=back |
| 1017 |
|
| 1018 |
=item B<C<statement>> (Statement attribute C<mock_statement>) |
| 1019 |
|
| 1020 |
Gets/sets the SQL statement used. |
| 1021 |
|
| 1022 |
=item B<C<fields>> (Statement attribute C<mock_fields>) |
| 1023 |
|
| 1024 |
Gets/sets the fields to use for this statement. |
| 1025 |
|
| 1026 |
=item B<C<bound_params>> (Statement attribute C<mock_params>) |
| 1027 |
|
| 1028 |
Gets/set the bound parameters to use for this statement. |
| 1029 |
|
| 1030 |
=item B<C<return_data>> (Statement attribute C<mock_records>) |
| 1031 |
|
| 1032 |
Gets/sets the data to return when asked (that is, when someone calls C<fetch> |
| 1033 |
on the statement handle). |
| 1034 |
|
| 1035 |
=item B<C<current_record_num>> (Statement attribute C<mock_current_record_num>) |
| 1036 |
|
| 1037 |
Gets/sets the current record number. |
| 1038 |
|
| 1039 |
=item B<C<is_active()>> (Statement attribute C<Active>) |
| 1040 |
|
| 1041 |
Returns true if the statement is a C<SELECT> and has more records to fetch, |
| 1042 |
false otherwise. (This is from the DBI, see the 'Active' docs under 'ATTRIBUTES |
| 1043 |
COMMON TO ALL HANDLES'.) |
| 1044 |
|
| 1045 |
=item B<C<is_executed( $yes_or_no )>> (Statement attribute C<mock_is_executed>) |
| 1046 |
|
| 1047 |
Sets the state of the tracker C<executed> flag. |
| 1048 |
|
| 1049 |
=item B<C<is_finished( $yes_or_no )>> (Statement attribute C<mock_is_finished>) |
| 1050 |
|
| 1051 |
If set to C<yes> tells the tracker that the statement is finished. This resets |
| 1052 |
the current record number to C<0> and clears out the array ref of returned |
| 1053 |
records. |
| 1054 |
|
| 1055 |
=item B<C<is_depleted()>> (Statement attribute C<mock_is_depleted>) |
| 1056 |
|
| 1057 |
Returns true if the current record number is greater than the number of records |
| 1058 |
set to return. |
| 1059 |
|
| 1060 |
=item B<C<num_fields>> |
| 1061 |
|
| 1062 |
Returns the number of fields set in the C<fields> parameter. |
| 1063 |
|
| 1064 |
=item B<C<num_rows>> |
| 1065 |
|
| 1066 |
Returns the number of records in the current result set. |
| 1067 |
|
| 1068 |
=item B<C<num_params>> |
| 1069 |
|
| 1070 |
Returns the number of parameters set in the C<bound_params> parameter. |
| 1071 |
|
| 1072 |
=item B<C<bound_param( $param_num, $value )>> |
| 1073 |
|
| 1074 |
Sets bound parameter C<$param_num> to C<$value>. Returns the arrayref of |
| 1075 |
currently-set bound parameters. This corresponds to the C<bind_param> statement |
| 1076 |
handle call. |
| 1077 |
|
| 1078 |
=item B<C<bound_param_trailing( @params )>> |
| 1079 |
|
| 1080 |
Pushes C<@params> onto the list of already-set bound parameters. |
| 1081 |
|
| 1082 |
=item B<C<mark_executed()>> |
| 1083 |
|
| 1084 |
Tells the tracker that the statement has been executed and resets the current |
| 1085 |
record number to C<0>. |
| 1086 |
|
| 1087 |
=item B<C<next_record()>> |
| 1088 |
|
| 1089 |
If the statement has been depleted (all records returned) returns C<undef>; |
| 1090 |
otherwise it gets the current record for returning, increments the current |
| 1091 |
record number and returns the current record. |
| 1092 |
|
| 1093 |
=item B<C<to_string()>> |
| 1094 |
|
| 1095 |
Tries to give a decent depiction of the object state for use in debugging. |
| 1096 |
|
| 1097 |
=back |
| 1098 |
|
| 1099 |
=head1 DBD::Mock::StatementTrack::Iterator |
| 1100 |
|
| 1101 |
This object can be used to iterate through the current set of |
| 1102 |
C<DBD::Mock::StatementTrack> objects in the history by fetching the |
| 1103 |
C<mock_all_history_iterator> attribute from a database handle. This object is |
| 1104 |
very simple and is meant to be a convenience to make writing long test script |
| 1105 |
easier. Aside from the constructor (C<new>) this object has the following |
| 1106 |
methods. |
| 1107 |
|
| 1108 |
=over 4 |
| 1109 |
|
| 1110 |
=item B<C<next()>> |
| 1111 |
|
| 1112 |
Calling C<next> will return the next C<DBD::Mock::StatementTrack> object in the |
| 1113 |
history. If there are no more C<DBD::Mock::StatementTrack> objects available, |
| 1114 |
then this method will return false. |
| 1115 |
|
| 1116 |
=item B<C<reset()>> |
| 1117 |
|
| 1118 |
This will reset the internal pointer to the beginning of the statement history. |
| 1119 |
|
| 1120 |
=back |
| 1121 |
|
| 1122 |
=head1 DBD::Mock::Session |
| 1123 |
|
| 1124 |
The C<DBD::Mock::Session> object is an alternate means of specifying the SQL |
| 1125 |
statements and result sets for C<DBD::Mock>. The idea is that you can specify a |
| 1126 |
complete 'session' of usage, which will be verified through C<DBD::Mock>. Here |
| 1127 |
is an example: |
| 1128 |
|
| 1129 |
my $session = DBD::Mock::Session->new('my_session' => ( |
| 1130 |
{ |
| 1131 |
statement => "SELECT foo FROM bar", # as a string |
| 1132 |
results => [[ 'foo' ], [ 'baz' ]] |
| 1133 |
}, |
| 1134 |
{ |
| 1135 |
statement => qr/UPDATE bar SET foo \= \'bar\'/, # as a reg-exp |
| 1136 |
results => [[]] |
| 1137 |
}, |
| 1138 |
{ |
| 1139 |
statement => sub { # as a CODE ref |
| 1140 |
my ($SQL, $state) = @_; |
| 1141 |
return $SQL eq "SELECT foo FROM bar"; |
| 1142 |
}, |
| 1143 |
results => [[ 'foo' ], [ 'bar' ]] |
| 1144 |
}, |
| 1145 |
{ |
| 1146 |
# with bound parameters |
| 1147 |
statement => "SELECT foo FROM bar WHERE baz = ? AND borg = ?", |
| 1148 |
# check exact bound param value, |
| 1149 |
# then check it against regexp |
| 1150 |
bound_params => [ 10, qr/\d+/ ], |
| 1151 |
results => [[ 'foo' ], [ 'baz' ]] |
| 1152 |
} |
| 1153 |
)); |
| 1154 |
|
| 1155 |
As you can see, a session is essentially made up a list of HASH references we |
| 1156 |
call 'states'. Each state has a C<statement> and a set of C<results>. If |
| 1157 |
C<DBD::Mock> finds a session in the C<mock_session> attribute, then it will |
| 1158 |
pass the current C<$dbh> and SQL statement to that C<DBD::Mock::Session>. The |
| 1159 |
SQL statement will be checked against the C<statement> field in the current |
| 1160 |
state. If it passes, then the C<results> of the current state will get fed to |
| 1161 |
C<DBD::Mock> through the C<mock_add_resultset> attribute. We then advance to |
| 1162 |
the next state in the session, and wait for the next call through C<DBD::Mock>. |
| 1163 |
If at any time the SQL statement does not match the current state's |
| 1164 |
C<statement>, or the session runs out of available states, an error will be |
| 1165 |
raised (and propagated through the normal DBI error handling based on your |
| 1166 |
values for C<RaiseError> and C<PrintError>). |
| 1167 |
|
| 1168 |
As can be seen in the session element, bound parameters can also be supplied |
| 1169 |
and tested. In this statement, the SQL is compared, then when the statement is |
| 1170 |
executed, the bound parameters are also checked. The bound parameters must |
| 1171 |
match in both number of parameters and the parameters themselves, or an error |
| 1172 |
will be raised. |
| 1173 |
|
| 1174 |
As can also be seen in the example above, C<statement> fields can come in many |
| 1175 |
forms. The simplest is a string, which will be compared using C<eq> against the |
| 1176 |
currently running statement. The next is a reg-exp reference, this too will get |
| 1177 |
compared against the currently running statement. The last option is a CODE |
| 1178 |
ref, this is sort of a catch-all to allow for a wide range of SQL comparison |
| 1179 |
approaches (including using modules like L<SQL::Statement> or L<SQL::Parser> |
| 1180 |
for detailed functional comparisons). The first argument to the CODE ref will |
| 1181 |
be the currently active SQL statement to compare against, the second argument |
| 1182 |
is a reference to the current state HASH (in case you need to alter the |
| 1183 |
results, or store extra information). The CODE is evaluated in boolean context |
| 1184 |
and throws and exception if it is false. |
| 1185 |
|
| 1186 |
=over 4 |
| 1187 |
|
| 1188 |
=item B<C<new ($session_name, @session_states)>> |
| 1189 |
|
| 1190 |
A C<$session_name> can be optionally be specified, along with at least one |
| 1191 |
C<@session_states>. If you don't specify a C<$session_name>, then a default one |
| 1192 |
will be created for you. The C<@session_states> must all be HASH references as |
| 1193 |
well, if this conditions fail, an exception will be thrown. |
| 1194 |
|
| 1195 |
=item B<C<verify_statement ($dbh, $SQL)>> |
| 1196 |
|
| 1197 |
This will check the C<$SQL> against the current state's C<statement> value, and |
| 1198 |
if it passes will add the current state's C<results> to the C<$dbh>. If for |
| 1199 |
some reason the C<statement> value is bad, not of the prescribed type, an |
| 1200 |
exception is thrown. See above for more details. |
| 1201 |
|
| 1202 |
=item B<C<verify_bound_params ($dbh, $params)>> |
| 1203 |
|
| 1204 |
If the C<bound_params> slot is available in the current state, this will check |
| 1205 |
the C<$params> against the current state's C<bound_params> value. Both number |
| 1206 |
of parameters and the parameters themselves must match, or an error will be |
| 1207 |
raised. |
| 1208 |
|
| 1209 |
Parameters are compared using string comparison or, when a regular expression |
| 1210 |
is supplied in the parameter position, matched against the regular expression. |
| 1211 |
|
| 1212 |
When a PostgreSQL database is mocked |
| 1213 |
|
| 1214 |
my $dbh = DBI->connect('dbi:Mock:PostgreSQL', ...); |
| 1215 |
|
| 1216 |
support for bound array parameters (a feature specific to L<DBD::Pg>) is |
| 1217 |
enabled. Array elements are compared using the comparison method above. Array |
| 1218 |
elements which are themselves arrays are I<not> traversed into. |
| 1219 |
|
| 1220 |
my $session = DBD::Mock::Session->new( 'my_session' => ( |
| 1221 |
{ |
| 1222 |
statement => 'SELECT foo FROM bar WHERE baz = ANY(?)', |
| 1223 |
bound_params => [ |
| 1224 |
[ 'uvw', 'xyz' ] # bound array param |
| 1225 |
] |
| 1226 |
} |
| 1227 |
)); |
| 1228 |
|
| 1229 |
=item B<C<reset>> |
| 1230 |
|
| 1231 |
Calling this method will reset the state of the session object so that it can |
| 1232 |
be reused. |
| 1233 |
|
| 1234 |
=back |
| 1235 |
|
| 1236 |
=head1 EXPERIMENTAL FUNCTIONALITY |
| 1237 |
|
| 1238 |
All functionality listed here is highly experimental and should be used with |
| 1239 |
great caution (if at all). |
| 1240 |
|
| 1241 |
=over 4 |
| 1242 |
|
| 1243 |
=item Connection Callbacks |
| 1244 |
|
| 1245 |
This feature allows you to define callbacks that get executed when |
| 1246 |
C<< DBI->connect >> is called. |
| 1247 |
|
| 1248 |
To set a series of callbacks you use the |
| 1249 |
C<DBD::Mock::dr::set_connect_callbacks> function |
| 1250 |
|
| 1251 |
use DBD::Mock::dr; |
| 1252 |
|
| 1253 |
DBD::Mock::dr::set_connect_callbacks( sub { |
| 1254 |
my ( $dbh, $dsn, $user, $password, $attributes ) = @_; |
| 1255 |
|
| 1256 |
$dbh->{mock_add_resultset} = { |
| 1257 |
sql => 'SELECT foo FROM bar', |
| 1258 |
results => [[ 'foo' ], [ 10 ]] |
| 1259 |
}; |
| 1260 |
} ); |
| 1261 |
|
| 1262 |
To set more than one callback to you can simply add extra callbacks to your |
| 1263 |
call to C<DBD::Mock::dr::set_connect_callbacks> |
| 1264 |
|
| 1265 |
DBD::Mock::dr::set_connect_callbacks( |
| 1266 |
sub { |
| 1267 |
my ( $dbh, $dsn, $user, $password, $attributes ) = @_; |
| 1268 |
|
| 1269 |
$dbh->{mock_add_resultset} = { |
| 1270 |
sql => 'SELECT foo FROM bar', |
| 1271 |
results => [[ 'foo' ], [ 10 ]] |
| 1272 |
}; |
| 1273 |
}, |
| 1274 |
|
| 1275 |
sub { |
| 1276 |
my ( $dbh, $dsn, $user, $password, $attributes ) = @_; |
| 1277 |
|
| 1278 |
$dbh->{mock_add_resultset} = { |
| 1279 |
sql => 'SELECT foo FROM bar', |
| 1280 |
results => [[ 'foo' ], [ 10 ]] |
| 1281 |
}; |
| 1282 |
} |
| 1283 |
); |
| 1284 |
|
| 1285 |
Or you can extend the existing set of callbacks with the |
| 1286 |
C<DBD::Mock::dr::add_connect_callbacks> function |
| 1287 |
|
| 1288 |
DBD::Mock::dr::add_connect_callbacks( sub { |
| 1289 |
( my $dbh, $dsn, $user, $password, $attributes ) = @_; |
| 1290 |
|
| 1291 |
$dbh->{mock_add_resultset} = { |
| 1292 |
sql => 'SELECT bar FROM foo', |
| 1293 |
results => [[ 'bar' ], [ 50 ]] |
| 1294 |
}; |
| 1295 |
} ); |
| 1296 |
|
| 1297 |
=item table_info |
| 1298 |
|
| 1299 |
This feature adds support for DBI's C<table_info> method ( |
| 1300 |
I<Note this functionality is unstable when used with DBI version 1.634 and below>). |
| 1301 |
|
| 1302 |
To mock the table info for a search of the C<testSchema> database schema you |
| 1303 |
would use the following: |
| 1304 |
|
| 1305 |
$dbh->{mock_add_table_info} = { |
| 1306 |
cataloge => undef, |
| 1307 |
schema => 'testSchema', |
| 1308 |
table => undef, |
| 1309 |
type => undef, |
| 1310 |
table_info => [ |
| 1311 |
[ 'TABLE_CAT', 'TABLE_SCHEM', 'TABLE_NAME', 'TABLE_TYPE', 'REMARKS' ], |
| 1312 |
[ undef, 'testSchema', 'foo', 'TABLE', undef ], |
| 1313 |
[ undef, 'testSchema', 'bar', 'VIEW', undef ], |
| 1314 |
], |
| 1315 |
}; |
| 1316 |
|
| 1317 |
The C<cataloge>, C<schema>, C<table> and C<type> parameters need to explicitly |
| 1318 |
match what you expect table_info to be called with (note: C<table_info> treats |
| 1319 |
C<undef> and C<''> the same). |
| 1320 |
|
| 1321 |
Similar to the C<mock_results_sets>, the C<table_info> parameter's first entry |
| 1322 |
is an arrayref of column names, and the rest are the values of the rows |
| 1323 |
returned (one arrayref per row). |
| 1324 |
|
| 1325 |
If you need to cover listing schemas then you'd use: |
| 1326 |
|
| 1327 |
$dbh->{mock_add_table_info} = { |
| 1328 |
schema => '%', |
| 1329 |
table_info => [ |
| 1330 |
[ 'TABLE_CAT', 'TABLE_SCHEM', 'TABLE_NAME', 'TABLE_TYPE', 'REMARKS' ], |
| 1331 |
[ undef, 'testSchema', undef, undef, undef ], |
| 1332 |
[ undef, 'testSchema_2', undef, undef, undef ], |
| 1333 |
], |
| 1334 |
} |
| 1335 |
|
| 1336 |
To clear the current mocked table info set the database handle's |
| 1337 |
C<mock_clear_table_info> attribute to C<1> |
| 1338 |
|
| 1339 |
$dbh->{mock_clear_table_info} = 1; |
| 1340 |
|
| 1341 |
=item Result Set Callbacks |
| 1342 |
|
| 1343 |
If you need your result sets to be more dynamic (e.g. if they need to return |
| 1344 |
different results based upon bound parameters) then you can use a callback. |
| 1345 |
|
| 1346 |
$dbh->{mock_add_resultset} = { |
| 1347 |
sql => 'SELECT a FROM b WHERE c = ?', |
| 1348 |
callback => sub { |
| 1349 |
my @bound_params = @_; |
| 1350 |
|
| 1351 |
my %result = ( |
| 1352 |
fields => [ "a" ], |
| 1353 |
rows => [[ 1] ] |
| 1354 |
); |
| 1355 |
|
| 1356 |
if ($bound_params[0] == 1) { |
| 1357 |
$result{rows} = [ [32] ]; |
| 1358 |
} elsif ($bound_params[0] == 2) { |
| 1359 |
$result{rows} = [ [43] ]; |
| 1360 |
} |
| 1361 |
|
| 1362 |
return %result; |
| 1363 |
}, |
| 1364 |
}; |
| 1365 |
|
| 1366 |
my $sth = $dbh->prepare('SELECT a FROM b WHERE c = ?'); |
| 1367 |
|
| 1368 |
my $rows = $sth->execute(1); |
| 1369 |
my ($result) = $sth->fetchrow_array(); # $result will be 32 |
| 1370 |
|
| 1371 |
$rows = $sth->execute(2); |
| 1372 |
($result) = $sth->fetchrow_array(); # $result this time will be 43 |
| 1373 |
|
| 1374 |
$rows = $sth->execute(33); # $results this time will be 1 |
| 1375 |
|
| 1376 |
($result) = $sth->fetchrow_array(); |
| 1377 |
|
| 1378 |
The callback needs to return a hash with a C<rows> key that is an array ref of |
| 1379 |
arrays containing the values to return as the answer to the query. In addition |
| 1380 |
a C<fields> key can also be returned with an array ref of field names. If a |
| 1381 |
C<fields> key isn't present in the returned the hash then the fields will be |
| 1382 |
taken from the C<mock_add_resultset>'s C<results> parameter. |
| 1383 |
|
| 1384 |
$dbh->{mock_add_resultset} = { |
| 1385 |
sql => 'SELECT x FROM y WHERE z = ?', |
| 1386 |
results => [ ["x"] ], |
| 1387 |
callback => sub { |
| 1388 |
my @bound_params = @_; |
| 1389 |
|
| 1390 |
my %result = ( rows => [[ 1] ] ); |
| 1391 |
|
| 1392 |
if ($bound_params[0] == 1) { |
| 1393 |
$result{rows} = [ [32] ]; |
| 1394 |
} elsif ($bound_params[0] == 2) { |
| 1395 |
$result{rows} = [ [43] ]; |
| 1396 |
} |
| 1397 |
|
| 1398 |
return %result; |
| 1399 |
}, |
| 1400 |
}; |
| 1401 |
|
| 1402 |
my $sth = $dbh->prepare('SELECT x FROM y WHERE z = ?'); |
| 1403 |
|
| 1404 |
my $rows = $sth->execute(1); |
| 1405 |
my ($result) = $sth->fetchrow_array(); # $result will be 32 |
| 1406 |
|
| 1407 |
$rows = $sth->execute(2); |
| 1408 |
($result) = $sth->fetchrow_array(); # $result will be 43 |
| 1409 |
|
| 1410 |
$rows = $sth->execute(33); |
| 1411 |
($result) = $sth->fetchrow_array(); # $result will be 1 |
| 1412 |
|
| 1413 |
|
| 1414 |
By default result sets which only define their field names in their callback |
| 1415 |
return values will have a C<NUM_OF_FIELDS> property of C<0> until after the |
| 1416 |
statement has actually been executed. This is to make sure that C<DBD::Mock> |
| 1417 |
stays compatible with previous versions. If you need the C<NUM_OF_FIELDS> |
| 1418 |
property to be undef in this situation then set the |
| 1419 |
C<$DBD::Mock::DefaultFieldsToUndef> flag to C<1>. |
| 1420 |
|
| 1421 |
If you're mocking an INSERT statement with a callback and you want to |
| 1422 |
explicitly set the database's C<last_insert_id> value then you can use the |
| 1423 |
C<last_insert_id> key in the result set. If you don't specify a |
| 1424 |
C<last_insert_id> then the standard C<DBD::Mock> logic for generating an value |
| 1425 |
for the last inserted item will be followed. This will allow you to mock |
| 1426 |
MySQL/MariaDB INSERT queries that use C<ON DUPLICATE KEY UPDATE> logic to set |
| 1427 |
the C<last_insert_id>. |
| 1428 |
|
| 1429 |
|
| 1430 |
$dbh->{mock_add_resultset} = { |
| 1431 |
sql => 'INSERT INTO y ( x ) VALUES ( ? ) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID( id )', |
| 1432 |
callback => sub { |
| 1433 |
my @bound_params = @_; |
| 1434 |
|
| 1435 |
my %result = ( |
| 1436 |
fields => [], |
| 1437 |
rows => [], |
| 1438 |
last_insert_id => 99, |
| 1439 |
); |
| 1440 |
|
| 1441 |
return %result; |
| 1442 |
}, |
| 1443 |
}; |
| 1444 |
|
| 1445 |
=item Result Set Custom Attributes |
| 1446 |
|
| 1447 |
If you're mocking a database driver that has it's own custom attributes |
| 1448 |
attached to its statement handles then you can use the result sets |
| 1449 |
C<prepare_attributes> and C<execute_attributes> options. |
| 1450 |
|
| 1451 |
The C<prepare_attributes> option takes a hashref that maps statement handle |
| 1452 |
attribute names to their values. The attributes are set at the point that |
| 1453 |
the statement is prepared. |
| 1454 |
|
| 1455 |
$dbh->{mock_add_resultset} = { |
| 1456 |
sql => 'SELECT foo FROM bar', |
| 1457 |
prepare_attributes => { |
| 1458 |
sqlite_unprepared_statements => ' ', |
| 1459 |
}, |
| 1460 |
results => [[ 'foo' ], [ 10 ]] |
| 1461 |
}; |
| 1462 |
|
| 1463 |
|
| 1464 |
The C<execute_attributes> option also takes a hashref that maps statement |
| 1465 |
handle attribute names to their values, however these will only be set when the |
| 1466 |
statement is executed. |
| 1467 |
|
| 1468 |
$dbh->{mock_add_resultset} = { |
| 1469 |
sql => 'SELECT foo FROM bar', |
| 1470 |
execute_attributes => { |
| 1471 |
syb_result_type => 1, |
| 1472 |
}, |
| 1473 |
results => [[ 'foo' ], [ 10 ]] |
| 1474 |
}; |
| 1475 |
|
| 1476 |
If an attribute is also present in the C<prepare_attributes> option then the |
| 1477 |
C<prepare_attributes> version will take precedence up to the point the |
| 1478 |
statement handle is executed, at which point the C<execute_attributes> version |
| 1479 |
will take precedence. |
| 1480 |
|
| 1481 |
It is also possible to set C<execute_attributes> from a result set's callback |
| 1482 |
by returning them under the C<execute_attributes> key in your callback's |
| 1483 |
response. |
| 1484 |
|
| 1485 |
$dbh->{mock_add_resultset} = { |
| 1486 |
sql => 'SELECT baz FROM qux', |
| 1487 |
callback => sub { |
| 1488 |
my @bound_params = @_; |
| 1489 |
|
| 1490 |
my %result = ( |
| 1491 |
fields => [ 'baz'], |
| 1492 |
rows => [], |
| 1493 |
execute_attributes => { |
| 1494 |
foo => 'bar' |
| 1495 |
}, |
| 1496 |
); |
| 1497 |
|
| 1498 |
return %result; |
| 1499 |
} |
| 1500 |
}; |
| 1501 |
|
| 1502 |
If a result set has an C<execute_attributes> option and a callback that also |
| 1503 |
returns an C<execute_attributes> key then the callback's C<execute_attributes> |
| 1504 |
value will take precedence. |
| 1505 |
|
| 1506 |
=back |
| 1507 |
|
| 1508 |
=head1 BUGS |
| 1509 |
|
| 1510 |
=over |
| 1511 |
|
| 1512 |
=item Odd C<$dbh> attribute behavior |
| 1513 |
|
| 1514 |
When writing the test suite I encountered some odd behavior with some C<$dbh> |
| 1515 |
attributes. I still need to get deeper into how DBD's work to understand what |
| 1516 |
it is that is actually doing wrong. |
| 1517 |
|
| 1518 |
=back |
| 1519 |
|
| 1520 |
=head1 TO DO |
| 1521 |
|
| 1522 |
=over |
| 1523 |
|
| 1524 |
=item Make DBD specific handlers |
| 1525 |
|
| 1526 |
Each DBD has its own quirks and issues, it would be nice to be able to handle |
| 1527 |
those issues with C<DBD::Mock> in some way. I have an number of ideas already, |
| 1528 |
but little time to sit down and really flesh them out. If you have any |
| 1529 |
suggestions or thoughts, feel free to email me with them. |
| 1530 |
|
| 1531 |
=item Enhance the C<DBD::Mock::StatementTrack> object |
| 1532 |
|
| 1533 |
I would like to have the C<DBD::Mock::StatementTrack> object handle more of the |
| 1534 |
C<mock_*> attributes. This would encapsulate much of the C<mock_*> behavior in |
| 1535 |
one place, which would be a good thing. |
| 1536 |
|
| 1537 |
=back |
| 1538 |
|
| 1539 |
=head1 SEE ALSO |
| 1540 |
|
| 1541 |
L<DBI> |
| 1542 |
|
| 1543 |
L<DBD::NullP>, which provided a good starting point |
| 1544 |
|
| 1545 |
L<Test::MockObject>, which provided the approach |
| 1546 |
|
| 1547 |
Test::MockObject article - L<http://www.perl.com/pub/a/2002/07/10/tmo.html> |
| 1548 |
|
| 1549 |
Perl Code Kata: Testing Databases - L<http://www.perl.com/pub/a/2005/02/10/database_kata.html> |
| 1550 |
|
| 1551 |
=head1 ACKNOWLEDGEMENTS |
| 1552 |
|
| 1553 |
=over 4 |
| 1554 |
|
| 1555 |
=item Thanks to Ryan Gerry for his patch in RT #26604. |
| 1556 |
|
| 1557 |
=item Thanks to Marc Beyer for his patch in RT #16951. |
| 1558 |
|
| 1559 |
=item Thanks to Justin DeVuyst for the mock_connect_fail idea. |
| 1560 |
|
| 1561 |
=item Thanks to Thilo Planz for the code for C<bind_param_inout>. |
| 1562 |
|
| 1563 |
=item Thanks to Shlomi Fish for help tracking down RT Bug #11515. |
| 1564 |
|
| 1565 |
=item Thanks to Collin Winter for the patch to fix the C<begin_work()>, |
| 1566 |
C<commit()> and C<rollback()> methods. |
| 1567 |
|
| 1568 |
=item Thanks to Andrew McHarg E<lt>[email protected]<gt> for |
| 1569 |
C<fetchall_hashref()>, C<fetchrow_hashref()> and C<selectcol_arrayref()> |
| 1570 |
methods and tests. |
| 1571 |
|
| 1572 |
=item Thanks to Andrew W. Gibbs for the C<mock_last_insert_ids> patch and test. |
| 1573 |
|
| 1574 |
=item Thanks to Chas Owens for patch and test for the C<mock_can_prepare>, |
| 1575 |
C<mock_can_execute>, and C<mock_can_fetch> features. |
| 1576 |
|
| 1577 |
=item Thanks to Tomas Zemresfor the unit test in RT #71438. |
| 1578 |
|
| 1579 |
=item Thanks to Bernhard Graf for multiple patches fixing a range of issues |
| 1580 |
and adding a new I<One Shot Failure> feature to C<mock_add_resultset>. |
| 1581 |
|
| 1582 |
=item Thanks to Erik Huelsmann for testing the new result set custom attributes |
| 1583 |
feature and also adding support for using arrayref's as bound parameters with |
| 1584 |
PostgreSQL databases. |
| 1585 |
|
| 1586 |
=back |
| 1587 |
|
| 1588 |
=head1 COPYRIGHT |
| 1589 |
|
| 1590 |
Copyright (C) 2004 Chris Winters E<lt>[email protected]<gt> |
| 1591 |
|
| 1592 |
Copyright (C) 2004-2007 Stevan Little E<lt>[email protected]<gt> |
| 1593 |
|
| 1594 |
Copyright (C) 2007 Rob Kinyon E<lt>[email protected]<gt> |
| 1595 |
|
| 1596 |
Copyright (C) 2011 Mariano Wahlmann E<lt>dichoso _at_ gmail.comE<gt> |
| 1597 |
|
| 1598 |
Copyright (C) 2019 Jason Cooper E<lt>[email protected]<gt> |
| 1599 |
|
| 1600 |
This library is free software; you can redistribute it and/or modify |
| 1601 |
it under the same terms as Perl itself. |
| 1602 |
|
| 1603 |
=head1 AUTHORS |
| 1604 |
|
| 1605 |
Chris Winters E<lt>[email protected]<gt> |
| 1606 |
|
| 1607 |
Stevan Little E<lt>[email protected]<gt> |
| 1608 |
|
| 1609 |
Rob Kinyon E<lt>[email protected]<gt> |
| 1610 |
|
| 1611 |
Mariano Wahlmann E<lt>dichoso _at_ gmail.comE<gt> |
| 1612 |
|
| 1613 |
Jason Cooper E<lt>[email protected]<gt> |
| 1614 |
|
| 1615 |
=cut |