.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "DBD::Mock 3pm" .TH DBD::Mock 3pm "2021-02-27" "perl v5.32.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" DBD::Mock \- Mock database driver for testing .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use DBI; \& \& # connect to your as normal, using \*(AqMock\*(Aq as your driver name \& my $dbh = DBI\->connect( \*(AqDBI:Mock:\*(Aq, \*(Aq\*(Aq, \*(Aq\*(Aq ) \& || die "Cannot create handle: $DBI::errstr\en"; \& \& # create a statement handle as normal and execute with parameters \& my $sth = $dbh\->prepare( \*(AqSELECT this, that FROM foo WHERE id = ?\*(Aq ); \& $sth\->execute( 15 ); \& \& # Now query the statement handle as to what has been done with it \& my $mock_params = $sth\->{mock_params}; \& print "Used statement: ", $sth\->{mock_statement}, "\en", \& "Bound parameters: ", join( \*(Aq, \*(Aq, @{ $mock_params } ), "\en"; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Testing with databases can be tricky. If you are developing a system married to a single database then you can make some assumptions about your environment and ask the user to provide relevant connection information. But if you need to test a framework that uses \s-1DBI,\s0 particularly a framework that uses different types of persistence schemes, then it may be more useful to simply verify what the framework is trying to do \*(-- ensure the right \s-1SQL\s0 is generated and that the correct parameters are bound. \f(CW\*(C`DBD::Mock\*(C'\fR makes it easy to just modify your configuration (presumably held outside your code) and just use it instead of \&\f(CW\*(C`DBD::Foo\*(C'\fR (like DBD::Pg or DBD::mysql) in your framework. .PP There is no distinct area where using this module makes sense. (Some people may successfully argue that this is a solution looking for a problem...) Indeed, if you can assume your users have something like DBD::AnyData or DBD::SQLite or if you do not mind creating a dependency on them then it makes far more sense to use these legitimate driver implementations and test your application in the real world \*(-- at least as much of the real world as you can create in your tests... .PP And if your database handle exists as a package variable or something else easily replaced at test-time then it may make more sense to use Test::MockObject to create a fully dynamic handle. There is an excellent article by chromatic about using Test::MockObject in this and other ways, strongly recommended. (See \*(L"\s-1SEE ALSO\*(R"\s0 for a link) .SS "How does it work?" .IX Subsection "How does it work?" \&\f(CW\*(C`DBD::Mock\*(C'\fR comprises a set of classes used by \s-1DBI\s0 to implement a database driver. But instead of connecting to a datasource and manipulating data found there it tracks all the calls made to the database handle and any created statement handles. You can then inspect them to ensure what you wanted to happen actually happened. For instance, say you have a configuration file with your database connection information: .PP .Vb 4 \& [DBI] \& dsn = DBI:Pg:dbname=myapp \& user = foo \& password = bar .Ve .PP And this file is read in at process startup and the handle stored for other procedures to use: .PP .Vb 1 \& package ObjectDirectory; \& \& my ( $DBH ); \& \& sub run_at_startup { \& my ( $class, $config ) = @_; \& $config ||= read_configuration( ... ); \& my $dsn = $config\->{DBI}{dsn}; \& my $user = $config\->{DBI}{user}; \& my $pass = $config\->{DBI}{password}; \& $DBH = DBI\->connect( $dsn, $user, $pass ) || die ...; \& } \& \& sub get_database_handle { \& return $DBH; \& } .Ve .PP A procedure might use it like this (ignoring any error handling for the moment): .PP .Vb 1 \& package My::UserActions; \& \& sub fetch_user { \& my ( $class, $login ) = @_; \& my $dbh = ObjectDirectory\->get_database_handle; \& my $sql = q{ \& SELECT login_name, first_name, last_name, creation_date, num_logins \& FROM users \& WHERE login_name = ? \& }; \& my $sth = $dbh\->prepare( $sql ); \& $sth\->execute( $login ); \& my $row = $sth\->fetchrow_arrayref; \& return ( $row ) ? User\->new( $row ) : undef; \& } .Ve .PP So for the purposes of our tests we just want to ensure that: .IP "1. The right \s-1SQL\s0 is being executed" 4 .IX Item "1. The right SQL is being executed" .PD 0 .IP "2. The right parameters are bound" 4 .IX Item "2. The right parameters are bound" .PD .PP Assume whether the \s-1SQL\s0 actually \fBworks\fR or not is irrelevant for this test :\-) .PP To do that our test might look like: .PP .Vb 3 \& my $config = ObjectDirectory\->read_configuration( ... ); \& $config\->{DBI}{dsn} = \*(AqDBI:Mock:\*(Aq; \& ObjectDirectory\->run_at_startup( $config ); \& \& my $login_name = \*(Aqfoobar\*(Aq; \& my $user = My::UserActions\->fetch_user( $login_name ); \& \& # Get the handle from ObjectDirectory; \& # this is the same handle used in the \& # \*(Aqfetch_user()\*(Aq procedure above \& my $dbh = ObjectDirectory\->get_database_handle(); \& \& # Ask the database handle for the history \& # of all statements executed against it \& my $history = $dbh\->{mock_all_history}; \& \& # Now query that history record to \& # see if our expectations match reality \& is(scalar(@{$history}), 1, \*(AqCorrect number of statements executed\*(Aq ; \& \& my $login_st = $history\->[0]; \& like($login_st\->statement, \& qr/SELECT login_name.*FROM users WHERE login_name = ?/sm, \& \*(AqCorrect statement generated\*(Aq ); \& \& my $params = $login_st\->bound_params; \& is(scalar(@{$params}), 1, \*(AqCorrect number of parameters bound\*(Aq); \& is($params\->[0], $login_name, \*(AqCorrect value for parameter 1\*(Aq ); \& \& # Reset the handle for future operations \& $dbh\->{mock_clear_history} = 1; .Ve .PP The list of properties and what they return is listed below. But in an overall view: .IP "\(bu" 4 A database handle contains the history of all statements created against it. Other properties set for the handle (e.g., \f(CW\*(C`PrintError\*(C'\fR, \f(CW\*(C`RaiseError\*(C'\fR) are left alone and can be queried as normal, but they do not affect anything. (A future feature may track the sequence/history of these assignments but if there is no demand it probably will not get implemented.) .IP "\(bu" 4 A statement handle contains the statement it was prepared with plus all bound parameters or parameters passed via \f(CW\*(C`execute()\*(C'\fR. It can also contain predefined results for the statement handle to \f(CW\*(C`fetch\*(C'\fR, track how many fetches were called and what its current record is. .SS "A Word of Warning" .IX Subsection "A Word of Warning" This may be an incredibly naive implementation of a \s-1DBD.\s0 But it works for me... .SH "DBD::Mock" .IX Header "DBD::Mock" Since this is a normal \s-1DBI\s0 statement handle we need to expose our tracking information as properties (accessed like a hash) rather than methods. .SS "Database Driver Properties" .IX Subsection "Database Driver Properties" .ie n .IP "\fB\f(CB""mock_connect_fail""\fB\fR" 4 .el .IP "\fB\f(CBmock_connect_fail\fB\fR" 4 .IX Item "mock_connect_fail" This is a boolean property which when set to true (\f(CW1\fR) will not allow \s-1DBI\s0 to connect. This can be used to simulate a \s-1DSN\s0 error or authentication failure. This can then be set back to false (\f(CW0\fR) to resume normal \s-1DBI\s0 operations. Here is an example of how this works: .Sp .Vb 2 \& # install the DBD::Mock driver \& my $drh = DBI\->install_driver(\*(AqMock\*(Aq); \& \& $drh\->{mock_connect_fail} = 1; \& \& # this connection will fail \& my $dbh = DBI\->connect(\*(Aqdbi:Mock:\*(Aq, \*(Aq\*(Aq, \*(Aq\*(Aq) || die "Cannot connect"; \& \& # this connection will throw an exception \& my $dbh = DBI\->connect(\*(Aqdbi:Mock:\*(Aq, \*(Aq\*(Aq, \*(Aq\*(Aq, { RaiseError => 1 }); \& \& $drh\->{mock_connect_fail} = 0; \& \& # this will work now ... \& my $dbh = DBI\->connect(...); .Ve .Sp This feature is conceptually different from the \f(CW\*(C`mock_can_connect\*(C'\fR attribute of the \f(CW$dbh\fR in that it has a driver-wide scope, where \f(CW\*(C`mock_can_connect\*(C'\fR is handle-wide scope. It also only prevents the initial connection, any \f(CW$dbh\fR handles created prior to setting \f(CW\*(C`mock_connect_fail\*(C'\fR to true (\f(CW1\fR) will still go on working just fine. .ie n .IP "\fB\f(CB""mock_data_sources""\fB\fR" 4 .el .IP "\fB\f(CBmock_data_sources\fB\fR" 4 .IX Item "mock_data_sources" This is an \s-1ARRAY\s0 reference which holds fake data sources which are returned by the Driver and Database Handle's \f(CW\*(C`data_source()\*(C'\fR method. .ie n .IP "\fB\f(CB""mock_add_data_sources""\fB\fR" 4 .el .IP "\fB\f(CBmock_add_data_sources\fB\fR" 4 .IX Item "mock_add_data_sources" This takes a string and adds it to the \f(CW\*(C`mock_data_sources\*(C'\fR attribute. .SS "Database Handle Properties" .IX Subsection "Database Handle Properties" .ie n .IP "\fB\f(CB""mock_all_history""\fB\fR" 4 .el .IP "\fB\f(CBmock_all_history\fB\fR" 4 .IX Item "mock_all_history" Returns an array reference with all history (a.k.a. \&\f(CW\*(C`DBD::Mock::StatementTrack\*(C'\fR) objects created against the database handle in the order they were created. Each history object can then report information about the \s-1SQL\s0 statement used to create it, the bound parameters, etc.. .ie n .IP "\fB\f(CB""mock_all_history_iterator""\fB\fR" 4 .el .IP "\fB\f(CBmock_all_history_iterator\fB\fR" 4 .IX Item "mock_all_history_iterator" Returns a \f(CW\*(C`DBD::Mock::StatementTrack::Iterator\*(C'\fR object which will iterate through the current set of \f(CW\*(C`DBD::Mock::StatementTrack\*(C'\fR object in the history. See the \*(L"DBD::Mock::StatementTrack::Iterator\*(R" documentation below for more information. .ie n .IP "\fB\f(CB""mock_clear_history""\fB\fR" 4 .el .IP "\fB\f(CBmock_clear_history\fB\fR" 4 .IX Item "mock_clear_history" If set to a true value all previous statement history operations will be erased. This \fBincludes\fR the history of currently open handles, so if you do something like: .Sp .Vb 4 \& my $dbh = get_handle( ... ); \& my $sth = $dbh\->prepare( ... ); \& $dbh\->{mock_clear_history} = 1; \& $sth\->execute( \*(AqFoo\*(Aq ); .Ve .Sp You will have no way to learn from the database handle that the statement parameter 'Foo' was bound. .Sp This is useful mainly to ensure you can isolate the statement histories from each other. A typical sequence will look like: .Sp .Vb 8 \& set handle to framework \& perform operations \& analyze mock database handle \& reset mock database handle history \& perform more operations \& analyze mock database handle \& reset mock database handle history \& ... .Ve .ie n .IP "\fB\f(CB""mock_can_connect""\fB\fR" 4 .el .IP "\fB\f(CBmock_can_connect\fB\fR" 4 .IX Item "mock_can_connect" This statement allows you to simulate a downed database connection. This is useful in testing how your application/tests will perform in the face of some kind of catastrophic event such as a network outage or database server failure. It is a simple boolean value which defaults to on, and can be set like this: .Sp .Vb 2 \& # turn the database off \& $dbh\->{mock_can_connect} = 0; \& \& # turn it back on again \& $dbh\->{mock_can_connect} = 1; .Ve .Sp The statement handle checks this value as well, so something like this will fail in the expected way: .Sp .Vb 2 \& $dbh = DBI\->connect( \*(AqDBI:Mock:\*(Aq, \*(Aq\*(Aq, \*(Aq\*(Aq ); \& $dbh\->{mock_can_connect} = 0; \& \& # blows up! \& my $sth = eval { $dbh\->prepare( \*(AqSELECT foo FROM bar\*(Aq ) }); \& if ( $@ ) { \& # Here, $DBI::errstr = \*(AqNo connection present\*(Aq \& } .Ve .Sp Turning off the database after a statement prepare will fail on the statement \&\f(CW\*(C`execute()\*(C'\fR, which is hopefully what you would expect: .Sp .Vb 1 \& $dbh = DBI\->connect( \*(AqDBI:Mock:\*(Aq, \*(Aq\*(Aq, \*(Aq\*(Aq ); \& \& # ok! \& my $sth = eval { $dbh\->prepare( \*(AqSELECT foo FROM bar\*(Aq ) }); \& $dbh\->{mock_can_connect} = 0; \& \& # blows up! \& $sth\->execute; .Ve .Sp Similarly: .Sp .Vb 1 \& $dbh = DBI\->connect( \*(AqDBI:Mock:\*(Aq, \*(Aq\*(Aq, \*(Aq\*(Aq ); \& \& # ok! \& my $sth = eval { $dbh\->prepare( \*(AqSELECT foo FROM bar\*(Aq ) }); \& \& # ok! \& $sth\->execute; \& \& $dbh\->{mock_can_connect} = 0; \& \& # blows up! \& my $row = $sth\->fetchrow_arrayref; .Ve .Sp Note: The handle attribute \f(CW\*(C`Active\*(C'\fR and the handle method \f(CW\*(C`ping\*(C'\fR will behave according to the value of \f(CW\*(C`mock_can_connect\*(C'\fR. So if \f(CW\*(C`mock_can_connect\*(C'\fR were to be set to \f(CW0\fR (or off), then both \f(CW\*(C`Active\*(C'\fR and \f(CW\*(C`ping\*(C'\fR would return false values (or \f(CW0\fR). .ie n .IP "\fB\f(CB""mock_add_resultset( \e@resultset | \e%resultset_and_options )""\fB\fR" 4 .el .IP "\fB\f(CBmock_add_resultset( \e@resultset | \e%resultset_and_options )\fB\fR" 4 .IX Item "mock_add_resultset( @resultset | %resultset_and_options )" This stocks the database handle with a record set, allowing you to seed data for your application to see if it works properly. Each recordset is a simple arrayref of arrays with the first arrayref being the field names used. Every time a statement handle is created it asks the database handle if it has any resultsets available and if so uses it. .Sp Here is a sample usage, partially from the test suite: .Sp .Vb 11 \& my @user_results = ( \& [ \*(Aqlogin\*(Aq, \*(Aqfirst_name\*(Aq, \*(Aqlast_name\*(Aq ], \& [ \*(Aqcwinters\*(Aq, \*(AqChris\*(Aq, \*(AqWinters\*(Aq ], \& [ \*(Aqbflay\*(Aq, \*(AqBobby\*(Aq, \*(AqFlay\*(Aq ], \& [ \*(Aqalincoln\*(Aq, \*(AqAbe\*(Aq, \*(AqLincoln\*(Aq ], \& ); \& my @generic_results = ( \& [ \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq ], \& [ \*(Aqthis_one\*(Aq, \*(Aqthat_one\*(Aq ], \& [ \*(Aqthis_two\*(Aq, \*(Aqthat_two\*(Aq ], \& ); \& \& my $dbh = DBI\->connect( \*(AqDBI:Mock:\*(Aq, \*(Aq\*(Aq, \*(Aq\*(Aq ); \& $dbh\->{mock_add_resultset} = \e@user_results; # add first resultset \& $dbh\->{mock_add_resultset} = \e@generic_results; # add second resultset \& my ( $sth ); \& eval { \& $sth = $dbh\->prepare( \*(AqSELECT login, first_name, last_name FROM foo\*(Aq ); \& $sth\->execute(); \& }; \& \& # this will fetch rows from the first resultset... \& my $row1 = $sth\->fetchrow_arrayref; \& my $user1 = User\->new( login => $row\->[0], \& first => $row\->[1], \& last => $row\->[2] ); \& is( $user1\->full_name, \*(AqChris Winters\*(Aq ); \& \& my $row2 = $sth\->fetchrow_arrayref; \& my $user2 = User\->new( login => $row\->[0], \& first => $row\->[1], \& last => $row\->[2] ); \& is( $user2\->full_name, \*(AqBobby Flay\*(Aq ); \& ... \& \& my $sth_generic = $dbh\->prepare( \*(AqSELECT foo, bar FROM baz\*(Aq ); \& $sth_generic\->execute; \& \& # this will fetch rows from the second resultset... \& my $row = $sth\->fetchrow_arrayref; .Ve .Sp It is possible to assign a hashref where the resultset must be given as value for the \f(CW\*(C`results\*(C'\fR key: .Sp .Vb 7 \& $dbh\->{mock_add_resultset} = { \& results => [ \& [ \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq ], \& [ \*(Aqthis_one\*(Aq, \*(Aqthat_one\*(Aq ], \& [ \*(Aqthis_two\*(Aq, \*(Aqthat_two\*(Aq ], \& ], \& }; .Ve .Sp The reason for the hashref form is that you can add options as described in the following. .Sp You can associate a resultset with a particular \s-1SQL\s0 statement instead of adding them in the order they will be fetched: .Sp .Vb 8 \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT foo, bar FROM baz\*(Aq, \& results => [ \& [ \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq ], \& [ \*(Aqthis_one\*(Aq, \*(Aqthat_one\*(Aq ], \& [ \*(Aqthis_two\*(Aq, \*(Aqthat_two\*(Aq ], \& ], \& }; .Ve .Sp This will return the given results when the statement \&'\f(CW\*(C`SELECT foo, bar FROM baz\*(C'\fR' is prepared/executed. Note that they will be returned \fBevery time\fR the statement is prepared/executed, not just the first. It should also be noted that if you want, for some reason, to change the result set bound to a particular \s-1SQL\s0 statement, all you need to do is add the result set again with the same \s-1SQL\s0 statement and \f(CW\*(C`DBD::Mock\*(C'\fR will overwrite it. .Sp If the \f(CW\*(C`sql\*(C'\fR parameter is a regular expression reference then the results will be returned for any \s-1SQL\s0 statements that matches it: .Sp .Vb 7 \& $dbh\->{mock_add_resultset} = { \& sql => qr/^SELECT foo FROM/i, \& results => [ \& [ \*(Aqfoo\*(Aq ], \& [ \*(Aqthis_one\*(Aq ], \& ], \& }; .Ve .Sp If an \s-1SQL\s0 statement matches both a specified \s-1SQL\s0 statement result set and a regular expression result set then the specified \s-1SQL\s0 statement takes precedence. If two regular expression result sets match then the first one added takes precedence: .Sp .Vb 5 \& # Set up our first regex matching result set \& $dbh\->{mock_add_resultset} = { \& sql => qr/^SELECT foo/, \& results => [ [ \*(Aqfoo\*(Aq ], [ 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 => [ [ \*(Aqfoo\*(Aq ], [ 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 => \*(AqSELECT foo FROM bar\*(Aq, \& results => [[ \*(Aqfoo\*(Aq ], [ 50 ]] \& }; \& \& # This query will be served by the first regex matching result set \& my $sth = $dbh\->prepare(\*(AqSELECT foo FROM oof\*(Aq); \& $sth\->execute() \& \& my ($result) = $sth\->fetchrow_array(); \& is( $result, 200 ); \& \& # This quere will be served by the statically defined result set \& $sth = $dbh\->prepare(\*(AqSELECT foo FROM bar\*(Aq); \& $sth\->execute(); \& \& my ($result2) = $sth\->fetchrow_array(); \& is( $result2, 50 ); .Ve .Sp It should also be noted that the \f(CW\*(C`rows\*(C'\fR method will return the number of records stocked in the result set. So if your code/application makes use of the \&\f(CW\*(C`$sth\->rows\*(C'\fR method for things like \f(CW\*(C`UPDATE\*(C'\fR and \f(CW\*(C`DELETE\*(C'\fR calls you should stock the result set like so: .Sp .Vb 5 \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqUPDATE foo SET baz = 1, bar = 2\*(Aq, \& # this will appear to have updated 3 rows \& results => [[ \*(Aqrows\*(Aq ], [], [], []], \& }; \& \& # or ... \& \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqDELETE FROM foo WHERE bar = 2\*(Aq, \& # this will appear to have deleted 1 row \& results => [[ \*(Aqrows\*(Aq ], []], \& }; .Ve .Sp Now I admit this is not the most elegant way to go about this, but it works for me for now, and until I can come up with a better method, or someone sends me a patch ;) it will do for now. .Sp If you want a given statement to fail, you will have to use the hashref method and add a \f(CW\*(C`failure\*(C'\fR key. That key must be handed an arrayref with the error number and error string, in that order. .Sp .Vb 5 \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT foo FROM bar\*(Aq, \& results => DBD::Mock\->NULL_RESULTSET, \& failure => [ 5, \*(AqOoops!\*(Aq ], \& }; .Ve .Sp Without the \f(CW\*(C`sql\*(C'\fR attribute the next statement will fail in any case: .Sp .Vb 4 \& $dbh\->{mock_add_resultset} = { \& results => DBD::Mock\->NULL_RESULTSET, \& failure => [ 5, \*(AqOoops!\*(Aq ], \& }; .Ve .ie n .IP "\fB\f(CB""mock_get_info""\fB\fR" 4 .el .IP "\fB\f(CBmock_get_info\fB\fR" 4 .IX Item "mock_get_info" This attribute can be used to set up values for \f(CW\*(C`get_info()\*(C'\fR. It takes a hashref of attribute_name/value pairs. See \s-1DBI\s0 for more information on the information types and their meaning. .ie n .IP "\fB\f(CB""mock_session""\fB\fR" 4 .el .IP "\fB\f(CBmock_session\fB\fR" 4 .IX Item "mock_session" This attribute can be used to set a current \f(CW\*(C`DBD::Mock::Session\*(C'\fR object. For more information on this, see the \*(L"DBD::Mock::Session\*(R" docs below. This attribute can also be used to remove the current session from the \f(CW$dbh\fR simply by setting it to \f(CW\*(C`undef\*(C'\fR. .ie n .IP "\fB\f(CB""mock_last_insert_id""\fB\fR" 4 .el .IP "\fB\f(CBmock_last_insert_id\fB\fR" 4 .IX Item "mock_last_insert_id" This attribute is incremented each time an \f(CW\*(C`INSERT\*(C'\fR statement is passed to \&\f(CW\*(C`prepare\*(C'\fR on a per-handle basis. It's starting value can be set with the \&\f(CW\*(C`mock_start_insert_id\*(C'\fR attribute (see below). .Sp .Vb 1 \& $dbh\->{mock_start_insert_id} = 10; \& \& my $sth = $dbh\->prepare(\*(AqINSERT INTO Foo (foo, bar) VALUES(?, ?)\*(Aq); \& \& $sth\->execute(1, 2); \& # $dbh\->{mock_last_insert_id} == 10 \& \& $sth\->execute(3, 4); \& # $dbh\->{mock_last_insert_id} == 11 .Ve .Sp For more examples, please refer to the test file \&\fIt/025_mock_last_insert_id.t\fR. .Sp To access \f(CW\*(C`last_insert_id\*(C'\fR using driver specific attributes like \&\f(CW\*(C`mysql_insertid\*(C'\fR and \f(CW\*(C`mariadb_insertid\*(C'\fR then you can use \&\*(L"Attribute Aliasing\*(R". .ie n .IP "\fB\f(CB""mock_start_insert_id""\fB\fR" 4 .el .IP "\fB\f(CBmock_start_insert_id\fB\fR" 4 .IX Item "mock_start_insert_id" This attribute can be used to set a start value for the \f(CW\*(C`mock_last_insert_id\*(C'\fR attribute. It can also be used to effectively reset the \f(CW\*(C`mock_last_insert_id\*(C'\fR attribute as well. .Sp This attribute also can be used with an \s-1ARRAY\s0 ref parameter, it's behavior is slightly different in that instead of incrementing the value for every \&\f(CW\*(C`prepare\*(C'\fR it will only increment for each \f(CW\*(C`execute\*(C'\fR. This allows it to be used over multiple \f(CW\*(C`execute\*(C'\fR calls in a single \f(CW$sth\fR. It's usage looks like this: .Sp .Vb 2 \& $dbh\->{mock_start_insert_id} = [ \*(AqFoo\*(Aq, 10 ]; \& $dbh\->{mock_start_insert_id} = [ \*(AqBaz\*(Aq, 20 ]; \& \& my $sth1 = $dbh\->prepare(\*(AqINSERT INTO Foo (foo, bar) VALUES(?, ?)\*(Aq); \& \& my $sth2 = $dbh\->prepare(\*(AqINSERT INTO Baz (baz, buz) VALUES(?, ?)\*(Aq); \& \& $sth1\->execute(1, 2); \& # $dbh\->{mock_last_insert_id} == 10 \& \& $sth2\->execute(3, 4); \& # $dbh\->{mock_last_insert_id} == 20 .Ve .Sp Note that \f(CW\*(C`DBD::Mock\*(C'\fR's matching of table names in \f(CW\*(C`INSERT\*(C'\fR statements is fairly simple, so if your table names are quoted in the insert statement (\f(CW\*(C`INSERT INTO "Foo"\*(C'\fR) then you need to quote the name for \&\f(CW\*(C`mock_start_insert_id\*(C'\fR: .Sp .Vb 1 \& $dbh\->{mock_start_insert_id} = [ q{"Foo"}, 10 ]; .Ve .ie n .IP "\fB\f(CB""mock_add_parser""\fB\fR" 4 .el .IP "\fB\f(CBmock_add_parser\fB\fR" 4 .IX Item "mock_add_parser" \&\s-1DBI\s0 provides some simple parsing capabilities for \f(CW\*(C`SELECT\*(C'\fR statements to ensure that placeholders are bound properly. And typically you may simply want to check after the fact that a statement is syntactically correct, or at least what you expect. .Sp But other times you may want to parse the statement as it is prepared rather than after the fact. There is a hook in this mock database driver for you to provide your own parsing routine or object. .Sp The syntax is simple: .Sp .Vb 6 \& $dbh\->{mock_add_parser} = sub { \& my ( $sql ) = @_; \& unless ( $sql =~ /some regex/ ) { \& die "does not contain secret fieldname"; \& } \& }; .Ve .Sp You can also add more than one for a handle. They will be called in order, and the first one to fail will halt the parsing process: .Sp .Vb 2 \& $dbh\->{mock_add_parser} = \e&parse_update_sql; \& $dbh\->{mock_add\-parser} = \e&parse_insert_sql; .Ve .Sp Depending on the \f(CW\*(C`PrintError\*(C'\fR and \f(CW\*(C`RaiseError\*(C'\fR settings in the database handle any parsing errors encountered will issue a \f(CW\*(C`warn\*(C'\fR or \f(CW\*(C`die\*(C'\fR. No matter what the statement handle will be \f(CW\*(C`undef\*(C'\fR. .Sp Instead of providing a subroutine reference you can use an object. The only requirement is that it implements the method \f(CW\*(C`parse()\*(C'\fR and takes a \s-1SQL\s0 statement as the only argument. So you should be able to do something like the following (untested): .Sp .Vb 2 \& my $parser = SQL::Parser\->new( \*(Aqmysql\*(Aq, { RaiseError => 1 } ); \& $dbh\->{mock_add_parser} = $parser; .Ve .ie n .IP "\fB\f(CB""mock_data_sources""\fB\fR & \fB\f(CB""mock_add_data_sources""\fB\fR" 4 .el .IP "\fB\f(CBmock_data_sources\fB\fR & \fB\f(CBmock_add_data_sources\fB\fR" 4 .IX Item "mock_data_sources & mock_add_data_sources" These properties will dispatch to the Driver's properties of the same name. .SS "Attribute Aliasing" .IX Subsection "Attribute Aliasing" Basically this feature allows you to alias attributes to other attributes. So for instance, you can alias a commonly expected attribute like \&\f(CW\*(C`mysql_insertid\*(C'\fR to something \f(CW\*(C`DBD::Mock\*(C'\fR already has like \&\f(CW\*(C`mock_last_insert_id\*(C'\fR. While you can also just set \f(CW\*(C`mysql_insertid\*(C'\fR yourself, this functionality allows it to take advantage of things like the autoincrementing of the \f(CW\*(C`mock_last_insert_id\*(C'\fR attribute. .PP 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: .PP .Vb 2 \& # turn it on \& $DBD::Mock::AttributeAliasing++; \& \& # turn it off \& $DBD::Mock::AttributeAliasing = 0; .Ve .PP Once this is turned on, you will need to choose a database specific attribute aliasing table like so: .PP .Vb 1 \& DBI\->connect(\*(Aqdbi:Mock:MySQL\*(Aq, \*(Aq\*(Aq, \*(Aq\*(Aq); .Ve .PP Or, by using the database name if using driver DSNs: .PP .Vb 1 \& DBI\->connect(\*(Aqdbi:Mock:host=localhost;port=3306;database=MySQL\*(Aq, \*(Aq\*(Aq, \*(Aq\*(Aq); .Ve .PP The \f(CW\*(C`MySQL\*(C'\fR in the \s-1DSN\s0 will be picked up and the MySQL specific attribute aliasing will be used. .PP Right now there is only minimal support for MySQL and MariaDB: .IP "MySQL" 4 .IX Item "MySQL" Currently the \f(CW\*(C`mysql_insertid\*(C'\fR attribute for \f(CW$dbh\fR and \f(CW$sth\fR are aliased to the \f(CW$dbh\fR attribute \f(CW\*(C`mock_last_insert_id\*(C'\fR. .IP "MariaDB" 4 .IX Item "MariaDB" Currently the \f(CW\*(C`mariadb_insertid\*(C'\fR attribute for \f(CW$dbh\fR and \f(CW$sth\fR are aliased to the \f(CW$dbh\fR attribute \f(CW\*(C`mock_last_insert_id\*(C'\fR. .PP It is possible to add more aliases though, using the \&\f(CW\*(C`DBD::Mock:_set_mock_attribute_aliases\*(C'\fR function (see the source code for details) .SS "Database Driver Methods" .IX Subsection "Database Driver Methods" .ie n .IP "\fB\f(CB""last_insert_id""\fB\fR" 4 .el .IP "\fB\f(CBlast_insert_id\fB\fR" 4 .IX Item "last_insert_id" This returns the value of \f(CW\*(C`mock_last_insert_id\*(C'\fR. .PP In order to capture \f(CW\*(C`begin_work()\*(C'\fR, \f(CW\*(C`commit()\*(C'\fR, and \f(CW\*(C`rollback()\*(C'\fR, \&\f(CW\*(C`DBD::Mock\*(C'\fR will create statements for them, as if you had issued them in the appropriate \s-1SQL\s0 command line program. They will go through the standard \&\f(CW\*(C`prepare()\*(C'\fR\-\f(CW\*(C`execute()\*(C'\fR cycle, meaning that any custom \s-1SQL\s0 parsers will be triggered and \f(CW\*(C`DBD::Mock::Session\*(C'\fR will need to know about these statements. .ie n .IP "\fB\f(CB""begin_work""\fB\fR" 4 .el .IP "\fB\f(CBbegin_work\fB\fR" 4 .IX Item "begin_work" This will create a statement with \s-1SQL\s0 of \f(CW\*(C`BEGIN WORK\*(C'\fR and no parameters. .ie n .IP "\fB\f(CB""commit""\fB\fR" 4 .el .IP "\fB\f(CBcommit\fB\fR" 4 .IX Item "commit" This will create a statement with \s-1SQL\s0 of \f(CW\*(C`COMMIT\*(C'\fR and no parameters. .ie n .IP "\fB\f(CB""rollback""\fB\fR" 4 .el .IP "\fB\f(CBrollback\fB\fR" 4 .IX Item "rollback" This will create a statement with \s-1SQL\s0 of \f(CW\*(C`ROLLBACK\*(C'\fR and no parameters. .SS "Statement Handle Properties" .IX Subsection "Statement Handle Properties" .ie n .IP "\fB\f(CB""Active""\fB\fR" 4 .el .IP "\fB\f(CBActive\fB\fR" 4 .IX Item "Active" Returns true if the handle is a \f(CW\*(C`SELECT\*(C'\fR and has more records to fetch, false otherwise. (From the \s-1DBI.\s0) .ie n .IP "\fB\f(CB""mock_statement""\fB\fR" 4 .el .IP "\fB\f(CBmock_statement\fB\fR" 4 .IX Item "mock_statement" The \s-1SQL\s0 statement this statement handle was \f(CW\*(C`prepare\*(C'\fRd with. So if the handle was created with: .Sp .Vb 1 \& my $sth = $dbh\->prepare( \*(AqSELECT * FROM foo\*(Aq ); .Ve .Sp This would return: .Sp .Vb 1 \& SELECT * FROM foo .Ve .Sp The original statement is unmodified so if you are checking against it in tests you may want to use a regex rather than a straight equality check. (However if you use a phrasebook to store your \s-1SQL\s0 externally you are a step ahead...) .ie n .IP "\fB\f(CB""mock_fields""\fB\fR" 4 .el .IP "\fB\f(CBmock_fields\fB\fR" 4 .IX Item "mock_fields" Fields used by the statement. As said elsewhere we do no analysis or parsing to find these, you need to define them beforehand. That said, you do not actually need this very often. .Sp Note that this returns the same thing as the normal statement property \&\f(CW\*(C`FIELD\*(C'\fR. .ie n .IP "\fB\f(CB""mock_params""\fB\fR" 4 .el .IP "\fB\f(CBmock_params\fB\fR" 4 .IX Item "mock_params" Returns an arrayref of parameters bound to this statement in the order specified by the bind type. For instance, if you created and stocked a handle with: .Sp .Vb 3 \& my $sth = $dbh\->prepare( \*(AqSELECT * FROM foo WHERE id = ? AND is_active = ?\*(Aq ); \& $sth\->bind_param( 2, \*(Aqyes\*(Aq ); \& $sth\->bind_param( 1, 7783 ); .Ve .Sp This would return: .Sp .Vb 1 \& [ 7738, \*(Aqyes\*(Aq ] .Ve .Sp The same result will occur if you pass the parameters via \f(CW\*(C`execute()\*(C'\fR instead: .Sp .Vb 2 \& my $sth = $dbh\->prepare( \*(AqSELECT * FROM foo WHERE id = ? AND is_active = ?\*(Aq ); \& $sth\->execute( 7783, \*(Aqyes\*(Aq ); .Ve .Sp The same using named parameters .Sp .Vb 3 \& my $sth = $dbh\->prepare( \*(AqSELECT * FROM foo WHERE id = :id AND is_active = :active\*(Aq ); \& $sth\->bind_param( \*(Aq:id\*(Aq => 7783 ); \& $sth\->bind_param( \*(Aq:active\*(Aq => \*(Aqyes\*(Aq ); .Ve .ie n .IP "\fB\f(CB""mock_param_attrs""\fB\fR" 4 .el .IP "\fB\f(CBmock_param_attrs\fB\fR" 4 .IX Item "mock_param_attrs" Returns an arrayref of any attributes (parameter type) defined for bound parameters (note: you rarely need to define attributes for bound parameters). Where an attribute/type hasn't been that slot in the returned arrayref will be \&\f(CW\*(C`undef\*(C'\fR. e.g. for: .Sp .Vb 3 \& my $sth = $dbh\->prepare( \*(AqSELECT * FROM foo WHERE id = ? AND is_active = ?\*(Aq ); \& $sth\->bind_param( 2, \*(Aqyes\*(Aq ); \& $sth\->bind_param( 1 7783, SQL_INTEGER ); .Ve .Sp This would return: .Sp .Vb 1 \& [ SQL_INTEGER, undef ] .Ve .Sp Passing parameters via \f(CW\*(C`execute()\*(C'\fR will always populate the array with \&\f(CW\*(C`undef\*(C'\fR, so for: .Sp .Vb 1 \& $sth\->execute( 7783, \*(Aqyes\*(Aq ); .Ve .Sp This would return: .Sp .Vb 1 \& [ undef, undef ] .Ve .ie n .IP "\fB\f(CB""mock_execution_history""\fB\fR" 4 .el .IP "\fB\f(CBmock_execution_history\fB\fR" 4 .IX Item "mock_execution_history" Returns an arrayref where each entry contains the details for an execution of the prepared statement. e.g. after: .Sp .Vb 4 \& my $sth = $dbh\->prepare( \*(AqSELECT * FROM foo WHERE id = ? AND is_active = ?\*(Aq ); \& $sth\->bind_param( 2, \*(Aqyes\*(Aq ); \& $sth\->bind_param( 1 7783, SQL_INTEGER ); \& $sth\->execute(); \& \& $sth\->execute( 1023, \*(Aqno\*(Aq ); .Ve .Sp Then \f(CW\*(C`$sth\->{mock_execution_history}\*(C'\fR would be: .Sp .Vb 9 \& [ \& { \& params => [ 7783, \*(Aqyes\*(Aq ], \& attrs => [ SQL_INTEGER, undef ], \& }, { \& params => [ 1023, \*(Aqno\*(Aq ], \& attrs => [ undef, undef ], \& } \& ] .Ve .ie n .IP "\fB\f(CB""mock_records""\fB\fR" 4 .el .IP "\fB\f(CBmock_records\fB\fR" 4 .IX Item "mock_records" An arrayref of arrayrefs representing the records the mock statement was stocked with. .ie n .IP "\fB\f(CB""mock_num_records""\fB\fR" 4 .el .IP "\fB\f(CBmock_num_records\fB\fR" 4 .IX Item "mock_num_records" Number of records the mock statement was stocked with; if never stocked it is still \f(CW0\fR. (Some weirdos might expect undef...) .ie n .IP "\fB\f(CB""mock_num_rows""\fB\fR" 4 .el .IP "\fB\f(CBmock_num_rows\fB\fR" 4 .IX Item "mock_num_rows" This returns the same value as \fImock_num_records\fR. And is what is returned by the \f(CW\*(C`rows\*(C'\fR method of the statement handle. .ie n .IP "\fB\f(CB""mock_current_record_num""\fB\fR" 4 .el .IP "\fB\f(CBmock_current_record_num\fB\fR" 4 .IX Item "mock_current_record_num" Current record the statement is on; returns \f(CW0\fR in the instances when you have not yet called \f(CW\*(C`execute()\*(C'\fR and if you have not yet called a \f(CW\*(C`fetch\*(C'\fR method after the execute. .ie n .IP "\fB\f(CB""mock_is_executed""\fB\fR" 4 .el .IP "\fB\f(CBmock_is_executed\fB\fR" 4 .IX Item "mock_is_executed" Whether \f(CW\*(C`execute()\*(C'\fR has been called against the statement handle. Returns \&'yes' if so, 'no' if not. .ie n .IP "\fB\f(CB""mock_is_finished""\fB\fR" 4 .el .IP "\fB\f(CBmock_is_finished\fB\fR" 4 .IX Item "mock_is_finished" Whether \f(CW\*(C`finish()\*(C'\fR has been called against the statement handle. Returns 'yes' if so, 'no' if not. .ie n .IP "\fB\f(CB""mock_is_depleted""\fB\fR" 4 .el .IP "\fB\f(CBmock_is_depleted\fB\fR" 4 .IX Item "mock_is_depleted" Returns 'yes' if all the records in the recordset have been returned. If no \&\f(CW\*(C`fetch()\*(C'\fR was executed against the statement, or If no return data was set this will return 'no'. .ie n .IP "\fB\f(CB""mock_my_history""\fB\fR" 4 .el .IP "\fB\f(CBmock_my_history\fB\fR" 4 .IX Item "mock_my_history" Returns a \f(CW\*(C`DBD::Mock::StatementTrack\*(C'\fR object which tracks the actions performed by this statement handle. Most of the actions are separately available from the properties listed above, so you should never need this. .SH "DBD::Mock::Pool" .IX Header "DBD::Mock::Pool" This module can be used to emulate Apache::DBI style \s-1DBI\s0 connection pooling. Just as with \f(CW\*(C`Apache::DBI\*(C'\fR, you must enable \f(CW\*(C`DBD::Mock::Pool\*(C'\fR before loading \&\s-1DBI.\s0 .PP .Vb 3 \& use DBD::Mock qw(Pool); \& # followed by ... \& use DBI; .Ve .PP While this may not seem to make a lot of sense in a single-process testing scenario, it can be useful when testing code which assumes a multi-process \&\f(CW\*(C`Apache::DBI\*(C'\fR pooled environment. .SH "DBD::Mock::StatementTrack" .IX Header "DBD::Mock::StatementTrack" Under the hood this module does most of the work with a \&\f(CW\*(C`DBD::Mock::StatementTrack\*(C'\fR object. This is most useful when you are reviewing multiple statements at a time, otherwise you might want to use the \f(CW\*(C`mock_*\*(C'\fR statement handle attributes instead. .ie n .IP "\fB\f(CB""new( %params )""\fB\fR" 4 .el .IP "\fB\f(CBnew( %params )\fB\fR" 4 .IX Item "new( %params )" Takes the following parameters: .RS 4 .IP "\(bu" 4 \&\fB\f(CB\*(C`return_data\*(C'\fB\fR: Arrayref of return data records .IP "\(bu" 4 \&\fB\f(CB\*(C`fields\*(C'\fB\fR: Arrayref of field names .IP "\(bu" 4 \&\fB\f(CB\*(C`bound_params\*(C'\fB\fR: Arrayref of bound parameters .IP "\(bu" 4 \&\fB\f(CB\*(C`bound_param_attrs\*(C'\fB\fR: Arrayref of bound parameter attributes .RE .RS 4 .RE .ie n .IP "\fB\f(CB""statement""\fB\fR (Statement attribute ""mock_statement"")" 4 .el .IP "\fB\f(CBstatement\fB\fR (Statement attribute \f(CWmock_statement\fR)" 4 .IX Item "statement (Statement attribute mock_statement)" Gets/sets the \s-1SQL\s0 statement used. .ie n .IP "\fB\f(CB""fields""\fB\fR (Statement attribute ""mock_fields"")" 4 .el .IP "\fB\f(CBfields\fB\fR (Statement attribute \f(CWmock_fields\fR)" 4 .IX Item "fields (Statement attribute mock_fields)" Gets/sets the fields to use for this statement. .ie n .IP "\fB\f(CB""bound_params""\fB\fR (Statement attribute ""mock_params"")" 4 .el .IP "\fB\f(CBbound_params\fB\fR (Statement attribute \f(CWmock_params\fR)" 4 .IX Item "bound_params (Statement attribute mock_params)" Gets/set the bound parameters to use for this statement. .ie n .IP "\fB\f(CB""return_data""\fB\fR (Statement attribute ""mock_records"")" 4 .el .IP "\fB\f(CBreturn_data\fB\fR (Statement attribute \f(CWmock_records\fR)" 4 .IX Item "return_data (Statement attribute mock_records)" Gets/sets the data to return when asked (that is, when someone calls \f(CW\*(C`fetch\*(C'\fR on the statement handle). .ie n .IP "\fB\f(CB""current_record_num""\fB\fR (Statement attribute ""mock_current_record_num"")" 4 .el .IP "\fB\f(CBcurrent_record_num\fB\fR (Statement attribute \f(CWmock_current_record_num\fR)" 4 .IX Item "current_record_num (Statement attribute mock_current_record_num)" Gets/sets the current record number. .ie n .IP "\fB\f(CB""is_active()""\fB\fR (Statement attribute ""Active"")" 4 .el .IP "\fB\f(CBis_active()\fB\fR (Statement attribute \f(CWActive\fR)" 4 .IX Item "is_active() (Statement attribute Active)" Returns true if the statement is a \f(CW\*(C`SELECT\*(C'\fR and has more records to fetch, false otherwise. (This is from the \s-1DBI,\s0 see the 'Active' docs under '\s-1ATTRIBUTES COMMON TO ALL HANDLES\s0'.) .ie n .IP "\fB\f(CB""is_executed( $yes_or_no )""\fB\fR (Statement attribute ""mock_is_executed"")" 4 .el .IP "\fB\f(CBis_executed( $yes_or_no )\fB\fR (Statement attribute \f(CWmock_is_executed\fR)" 4 .IX Item "is_executed( $yes_or_no ) (Statement attribute mock_is_executed)" Sets the state of the tracker \f(CW\*(C`executed\*(C'\fR flag. .ie n .IP "\fB\f(CB""is_finished( $yes_or_no )""\fB\fR (Statement attribute ""mock_is_finished"")" 4 .el .IP "\fB\f(CBis_finished( $yes_or_no )\fB\fR (Statement attribute \f(CWmock_is_finished\fR)" 4 .IX Item "is_finished( $yes_or_no ) (Statement attribute mock_is_finished)" If set to \f(CW\*(C`yes\*(C'\fR tells the tracker that the statement is finished. This resets the current record number to \f(CW0\fR and clears out the array ref of returned records. .ie n .IP "\fB\f(CB""is_depleted()""\fB\fR (Statement attribute ""mock_is_depleted"")" 4 .el .IP "\fB\f(CBis_depleted()\fB\fR (Statement attribute \f(CWmock_is_depleted\fR)" 4 .IX Item "is_depleted() (Statement attribute mock_is_depleted)" Returns true if the current record number is greater than the number of records set to return. .ie n .IP "\fB\f(CB""num_fields""\fB\fR" 4 .el .IP "\fB\f(CBnum_fields\fB\fR" 4 .IX Item "num_fields" Returns the number of fields set in the \f(CW\*(C`fields\*(C'\fR parameter. .ie n .IP "\fB\f(CB""num_rows""\fB\fR" 4 .el .IP "\fB\f(CBnum_rows\fB\fR" 4 .IX Item "num_rows" Returns the number of records in the current result set. .ie n .IP "\fB\f(CB""num_params""\fB\fR" 4 .el .IP "\fB\f(CBnum_params\fB\fR" 4 .IX Item "num_params" Returns the number of parameters set in the \f(CW\*(C`bound_params\*(C'\fR parameter. .ie n .IP "\fB\f(CB""bound_param( $param_num, $value )""\fB\fR" 4 .el .IP "\fB\f(CBbound_param( $param_num, $value )\fB\fR" 4 .IX Item "bound_param( $param_num, $value )" Sets bound parameter \f(CW$param_num\fR to \f(CW$value\fR. Returns the arrayref of currently-set bound parameters. This corresponds to the \f(CW\*(C`bind_param\*(C'\fR statement handle call. .ie n .IP "\fB\f(CB""bound_param_trailing( @params )""\fB\fR" 4 .el .IP "\fB\f(CBbound_param_trailing( @params )\fB\fR" 4 .IX Item "bound_param_trailing( @params )" Pushes \f(CW@params\fR onto the list of already-set bound parameters. .ie n .IP "\fB\f(CB""mark_executed()""\fB\fR" 4 .el .IP "\fB\f(CBmark_executed()\fB\fR" 4 .IX Item "mark_executed()" Tells the tracker that the statement has been executed and resets the current record number to \f(CW0\fR. .ie n .IP "\fB\f(CB""next_record()""\fB\fR" 4 .el .IP "\fB\f(CBnext_record()\fB\fR" 4 .IX Item "next_record()" If the statement has been depleted (all records returned) returns \f(CW\*(C`undef\*(C'\fR; otherwise it gets the current record for returning, increments the current record number and returns the current record. .ie n .IP "\fB\f(CB""to_string()""\fB\fR" 4 .el .IP "\fB\f(CBto_string()\fB\fR" 4 .IX Item "to_string()" Tries to give a decent depiction of the object state for use in debugging. .SH "DBD::Mock::StatementTrack::Iterator" .IX Header "DBD::Mock::StatementTrack::Iterator" This object can be used to iterate through the current set of \&\f(CW\*(C`DBD::Mock::StatementTrack\*(C'\fR objects in the history by fetching the \&\f(CW\*(C`mock_all_history_iterator\*(C'\fR attribute from a database handle. This object is very simple and is meant to be a convenience to make writing long test script easier. Aside from the constructor (\f(CW\*(C`new\*(C'\fR) this object has the following methods. .ie n .IP "\fB\f(CB""next()""\fB\fR" 4 .el .IP "\fB\f(CBnext()\fB\fR" 4 .IX Item "next()" Calling \f(CW\*(C`next\*(C'\fR will return the next \f(CW\*(C`DBD::Mock::StatementTrack\*(C'\fR object in the history. If there are no more \f(CW\*(C`DBD::Mock::StatementTrack\*(C'\fR objects available, then this method will return false. .ie n .IP "\fB\f(CB""reset()""\fB\fR" 4 .el .IP "\fB\f(CBreset()\fB\fR" 4 .IX Item "reset()" This will reset the internal pointer to the beginning of the statement history. .SH "DBD::Mock::Session" .IX Header "DBD::Mock::Session" The \f(CW\*(C`DBD::Mock::Session\*(C'\fR object is an alternate means of specifying the \s-1SQL\s0 statements and result sets for \f(CW\*(C`DBD::Mock\*(C'\fR. The idea is that you can specify a complete 'session' of usage, which will be verified through \f(CW\*(C`DBD::Mock\*(C'\fR. Here is an example: .PP .Vb 10 \& my $session = DBD::Mock::Session\->new(\*(Aqmy_session\*(Aq => ( \& { \& statement => "SELECT foo FROM bar", # as a string \& results => [[ \*(Aqfoo\*(Aq ], [ \*(Aqbaz\*(Aq ]] \& }, \& { \& statement => qr/UPDATE bar SET foo \e= \e\*(Aqbar\e\*(Aq/, # as a reg\-exp \& results => [[]] \& }, \& { \& statement => sub { # as a CODE ref \& my ($SQL, $state) = @_; \& return $SQL eq "SELECT foo FROM bar"; \& }, \& results => [[ \*(Aqfoo\*(Aq ], [ \*(Aqbar\*(Aq ]] \& }, \& { \& # with bound parameters \& statement => "SELECT foo FROM bar WHERE baz = ? AND borg = ?", \& # check exact bound param value, \& # then check it against regexp \& bound_params => [ 10, qr/\ed+/ ], \& results => [[ \*(Aqfoo\*(Aq ], [ \*(Aqbaz\*(Aq ]] \& } \& )); .Ve .PP As you can see, a session is essentially made up a list of \s-1HASH\s0 references we call 'states'. Each state has a \f(CW\*(C`statement\*(C'\fR and a set of \f(CW\*(C`results\*(C'\fR. If \&\f(CW\*(C`DBD::Mock\*(C'\fR finds a session in the \f(CW\*(C`mock_session\*(C'\fR attribute, then it will pass the current \f(CW$dbh\fR and \s-1SQL\s0 statement to that \f(CW\*(C`DBD::Mock::Session\*(C'\fR. The \&\s-1SQL\s0 statement will be checked against the \f(CW\*(C`statement\*(C'\fR field in the current state. If it passes, then the \f(CW\*(C`results\*(C'\fR of the current state will get fed to \&\f(CW\*(C`DBD::Mock\*(C'\fR through the \f(CW\*(C`mock_add_resultset\*(C'\fR attribute. We then advance to the next state in the session, and wait for the next call through \f(CW\*(C`DBD::Mock\*(C'\fR. If at any time the \s-1SQL\s0 statement does not match the current state's \&\f(CW\*(C`statement\*(C'\fR, or the session runs out of available states, an error will be raised (and propagated through the normal \s-1DBI\s0 error handling based on your values for \f(CW\*(C`RaiseError\*(C'\fR and \f(CW\*(C`PrintError\*(C'\fR). .PP As can be seen in the session element, bound parameters can also be supplied and tested. In this statement, the \s-1SQL\s0 is compared, then when the statement is executed, the bound parameters are also checked. The bound parameters must match in both number of parameters and the parameters themselves, or an error will be raised. .PP As can also be seen in the example above, \f(CW\*(C`statement\*(C'\fR fields can come in many forms. The simplest is a string, which will be compared using \f(CW\*(C`eq\*(C'\fR against the currently running statement. The next is a reg-exp reference, this too will get compared against the currently running statement. The last option is a \s-1CODE\s0 ref, this is sort of a catch-all to allow for a wide range of \s-1SQL\s0 comparison approaches (including using modules like SQL::Statement or SQL::Parser for detailed functional comparisons). The first argument to the \s-1CODE\s0 ref will be the currently active \s-1SQL\s0 statement to compare against, the second argument is a reference to the current state \s-1HASH\s0 (in case you need to alter the results, or store extra information). The \s-1CODE\s0 is evaluated in boolean context and throws and exception if it is false. .ie n .IP "\fB\f(CB""new ($session_name, @session_states)""\fB\fR" 4 .el .IP "\fB\f(CBnew ($session_name, @session_states)\fB\fR" 4 .IX Item "new ($session_name, @session_states)" A \f(CW$session_name\fR can be optionally be specified, along with at least one \&\f(CW@session_states\fR. If you don't specify a \f(CW$session_name\fR, then a default one will be created for you. The \f(CW@session_states\fR must all be \s-1HASH\s0 references as well, if this conditions fail, an exception will be thrown. .ie n .IP "\fB\f(CB""verify_statement ($dbh, $SQL)""\fB\fR" 4 .el .IP "\fB\f(CBverify_statement ($dbh, $SQL)\fB\fR" 4 .IX Item "verify_statement ($dbh, $SQL)" This will check the \f(CW$SQL\fR against the current state's \f(CW\*(C`statement\*(C'\fR value, and if it passes will add the current state's \f(CW\*(C`results\*(C'\fR to the \f(CW$dbh\fR. If for some reason the \f(CW\*(C`statement\*(C'\fR value is bad, not of the prescribed type, an exception is thrown. See above for more details. .ie n .IP "\fB\f(CB""verify_bound_params ($dbh, $params)""\fB\fR" 4 .el .IP "\fB\f(CBverify_bound_params ($dbh, $params)\fB\fR" 4 .IX Item "verify_bound_params ($dbh, $params)" If the \f(CW\*(C`bound_params\*(C'\fR slot is available in the current state, this will check the \f(CW$params\fR against the current state's \f(CW\*(C`bound_params\*(C'\fR value. Both number of parameters and the parameters themselves must match, or an error will be raised. .ie n .IP "\fB\f(CB""reset""\fB\fR" 4 .el .IP "\fB\f(CBreset\fB\fR" 4 .IX Item "reset" Calling this method will reset the state of the session object so that it can be reused. .SH "EXPERIMENTAL FUNCTIONALITY" .IX Header "EXPERIMENTAL FUNCTIONALITY" All functionality listed here is highly experimental and should be used with great caution (if at all). .IP "Connection Callbacks" 4 .IX Item "Connection Callbacks" This feature allows you to define callbacks that get executed when \&\f(CW\*(C`DBI\->connect\*(C'\fR is called. .Sp To set a series of callbacks you use the \&\f(CW\*(C`DBD::Mock::dr::set_connect_callbacks\*(C'\fR function .Sp .Vb 1 \& use DBD::Mock::dr; \& \& DBD::Mock::dr::set_connect_callbacks( sub { \& my ( $dbh, $dsn, $user, $password, $attributes ) = @_; \& \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT foo FROM bar\*(Aq, \& results => [[ \*(Aqfoo\*(Aq ], [ 10 ]] \& }; \& } ); .Ve .Sp To set more than one callback to you can simply add extra callbacks to your call to \f(CW\*(C`DBD::Mock::dr::set_connect_callbacks\*(C'\fR .Sp .Vb 3 \& DBD::Mock::dr::set_connect_callbacks( \& sub { \& my ( $dbh, $dsn, $user, $password, $attributes ) = @_; \& \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT foo FROM bar\*(Aq, \& results => [[ \*(Aqfoo\*(Aq ], [ 10 ]] \& }; \& }, \& \& sub { \& my ( $dbh, $dsn, $user, $password, $attributes ) = @_; \& \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT foo FROM bar\*(Aq, \& results => [[ \*(Aqfoo\*(Aq ], [ 10 ]] \& }; \& } \& ); .Ve .Sp Or you can extend the existing set of callbacks with the \&\f(CW\*(C`DBD::Mock::dr::add_connect_callbacks\*(C'\fR function .Sp .Vb 2 \& DBD::Mock::dr::add_connect_callbacks( sub { \& ( my $dbh, $dsn, $user, $password, $attributes ) = @_; \& \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT bar FROM foo\*(Aq, \& results => [[ \*(Aqbar\*(Aq ], [ 50 ]] \& }; \& } ); .Ve .IP "table_info" 4 .IX Item "table_info" This feature adds support for \s-1DBI\s0's \f(CW\*(C`table_info\*(C'\fR method ( \&\fINote this functionality is unstable when used with \s-1DBI\s0 version 1.634 and below\fR). .Sp To mock the table info for a search of the \f(CW\*(C`testSchema\*(C'\fR database schema you would use the following: .Sp .Vb 11 \& $dbh\->{mock_add_table_info} = { \& cataloge => undef, \& schema => \*(AqtestSchema\*(Aq, \& table => undef, \& type => undef, \& table_info => [ \& [ \*(AqTABLE_CAT\*(Aq, \*(AqTABLE_SCHEM\*(Aq, \*(AqTABLE_NAME\*(Aq, \*(AqTABLE_TYPE\*(Aq, \*(AqREMARKS\*(Aq ], \& [ undef, \*(AqtestSchema\*(Aq, \*(Aqfoo\*(Aq, \*(AqTABLE\*(Aq, undef ], \& [ undef, \*(AqtestSchema\*(Aq, \*(Aqbar\*(Aq, \*(AqVIEW\*(Aq, undef ], \& ], \& }; .Ve .Sp The \f(CW\*(C`cataloge\*(C'\fR, \f(CW\*(C`schema\*(C'\fR, \f(CW\*(C`table\*(C'\fR and \f(CW\*(C`type\*(C'\fR parameters need to explicitly match what you expect table_info to be called with (note: \f(CW\*(C`table_info\*(C'\fR treats \&\f(CW\*(C`undef\*(C'\fR and \f(CW\*(Aq\*(Aq\fR the same). .Sp Similar to the \f(CW\*(C`mock_results_sets\*(C'\fR, the \f(CW\*(C`table_info\*(C'\fR parameter's first entry is an arrayref of column names, and the rest are the values of the rows returned (one arrayref per row). .Sp If you need to cover listing schemas then you'd use: .Sp .Vb 8 \& $dbh\->{mock_add_table_info} = { \& schema => \*(Aq%\*(Aq, \& table_info => [ \& [ \*(AqTABLE_CAT\*(Aq, \*(AqTABLE_SCHEM\*(Aq, \*(AqTABLE_NAME\*(Aq, \*(AqTABLE_TYPE\*(Aq, \*(AqREMARKS\*(Aq ], \& [ undef, \*(AqtestSchema\*(Aq, undef, undef, undef ], \& [ undef, \*(AqtestSchema_2\*(Aq, undef, undef, undef ], \& ], \& } .Ve .Sp To clear the current mocked table info set the database handle's \&\f(CW\*(C`mock_clear_table_info\*(C'\fR attribute to \f(CW1\fR .Sp .Vb 1 \& $dbh\->{mock_clear_table_info} = 1; .Ve .IP "Result Set Callbacks" 4 .IX Item "Result Set Callbacks" If you need your result sets to be more dynamic (e.g. if they need to return different results based upon bound parameters) then you can use a callback. .Sp .Vb 4 \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT a FROM b WHERE c = ?\*(Aq, \& callback => sub { \& my @bound_params = @_; \& \& my %result = ( \& fields => [ "a" ], \& rows => [[ 1] ] \& ); \& \& if ($bound_params[0] == 1) { \& $result{rows} = [ [32] ]; \& } elsif ($bound_params[0] == 2) { \& $result{rows} = [ [43] ]; \& } \& \& return %result; \& }, \& }; \& \& my $sth = $dbh\->prepare(\*(AqSELECT a FROM b WHERE c = ?\*(Aq); \& \& my $rows = $sth\->execute(1); \& my ($result) = $sth\->fetchrow_array(); # $result will be 32 \& \& $rows = $sth\->execute(2); \& ($result) = $sth\->fetchrow_array(); # $result this time will be 43 \& \& $rows = $sth\->execute(33); # $results this time will be 1 \& \& ($result) = $sth\->fetchrow_array(); .Ve .Sp The callback needs to return a hash with a \f(CW\*(C`rows\*(C'\fR key that is an array ref of arrays containing the values to return as the answer to the query. In addition a \f(CW\*(C`fields\*(C'\fR key can also be returned with an array ref of field names. If a \&\f(CW\*(C`fields\*(C'\fR key isn't present in the returned the hash then the fields will be taken from the \f(CW\*(C`mock_add_resultset\*(C'\fR's \f(CW\*(C`results\*(C'\fR parameter. .Sp .Vb 5 \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT x FROM y WHERE z = ?\*(Aq, \& results => [ ["x"] ], \& callback => sub { \& my @bound_params = @_; \& \& my %result = ( rows => [[ 1] ] ); \& \& if ($bound_params[0] == 1) { \& $result{rows} = [ [32] ]; \& } elsif ($bound_params[0] == 2) { \& $result{rows} = [ [43] ]; \& } \& \& return %result; \& }, \& }; \& \& my $sth = $dbh\->prepare(\*(AqSELECT x FROM y WHERE z = ?\*(Aq); \& \& my $rows = $sth\->execute(1); \& my ($result) = $sth\->fetchrow_array(); # $result will be 32 \& \& $rows = $sth\->execute(2); \& ($result) = $sth\->fetchrow_array(); # $result will be 43 \& \& $rows = $sth\->execute(33); \& ($result) = $sth\->fetchrow_array(); # $result will be 1 .Ve .Sp By default result sets which only define their field names in their callback return values will have a \f(CW\*(C`NUM_OF_FIELDS\*(C'\fR property of \f(CW0\fR until after the statement has actually been executed. This is to make sure that \f(CW\*(C`DBD::Mock\*(C'\fR stays compatible with previous versions. If you need the \f(CW\*(C`NUM_OF_FIELDS\*(C'\fR property to be undef in this situation then set the \&\f(CW$DBD::Mock::DefaultFieldsToUndef\fR flag to \f(CW1\fR. .Sp If you're mocking an \s-1INSERT\s0 statement with a callback and you want to explicitly set the database's \f(CW\*(C`last_insert_id\*(C'\fR value then you can use the \&\f(CW\*(C`last_insert_id\*(C'\fR key in the result set. If you don't specify a \&\f(CW\*(C`last_insert_id\*(C'\fR then the standard \f(CW\*(C`DBD::Mock\*(C'\fR logic for generating an value for the last inserted item will be followed. This will allow you to mock MySQL/MariaDB \s-1INSERT\s0 queries that use \f(CW\*(C`ON DUPLICATE KEY UPDATE\*(C'\fR logic to set the \f(CW\*(C`last_insert_id\*(C'\fR. .Sp .Vb 4 \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqINSERT INTO y ( x ) VALUES ( ? ) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID( id )\*(Aq, \& callback => sub { \& my @bound_params = @_; \& \& my %result = ( \& fields => [], \& rows => [], \& last_insert_id => 99, \& ); \& \& return %result; \& }, \& }; .Ve .IP "Result Set Custom Attributes" 4 .IX Item "Result Set Custom Attributes" If you're mocking a database driver that has it's own custom attributes attached to its statement handles then you can use the result sets \&\f(CW\*(C`prepare_attributes\*(C'\fR and \f(CW\*(C`execute_attributes\*(C'\fR options. .Sp The \f(CW\*(C`prepare_attributes\*(C'\fR option takes a hashref that maps statement handle attribute names to their values. The attributes are set at the point that the statement is prepared. .Sp .Vb 7 \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT foo FROM bar\*(Aq, \& prepare_attributes => { \& sqlite_unprepared_statements => \*(Aq \*(Aq, \& }, \& results => [[ \*(Aqfoo\*(Aq ], [ 10 ]] \& }; .Ve .Sp The \f(CW\*(C`execute_attributes\*(C'\fR option also takes a hashref that maps statement handle attribute names to their values, however these will only be set when the statement is executed. .Sp .Vb 7 \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT foo FROM bar\*(Aq, \& execute_attributes => { \& syb_result_type => 1, \& }, \& results => [[ \*(Aqfoo\*(Aq ], [ 10 ]] \& }; .Ve .Sp If an attribute is also present in the \f(CW\*(C`prepare_attributes\*(C'\fR option then the \&\f(CW\*(C`prepare_attributes\*(C'\fR version will take precedence up to the point the statement handle is executed, at which point the \f(CW\*(C`execute_attributes\*(C'\fR version will take precedence. .Sp It is also possible to set \f(CW\*(C`execute_attributes\*(C'\fR from a result set's callback by returning them under the \f(CW\*(C`execute_attributes\*(C'\fR key in your callback's response. .Sp .Vb 4 \& $dbh\->{mock_add_resultset} = { \& sql => \*(AqSELECT baz FROM qux\*(Aq, \& callback => sub { \& my @bound_params = @_; \& \& my %result = ( \& fields => [ \*(Aqbaz\*(Aq], \& rows => [], \& execute_attributes => { \& foo => \*(Aqbar\*(Aq \& }, \& ); \& \& return %result; \& } \& }; .Ve .Sp If a result set has an \f(CW\*(C`execute_attributes\*(C'\fR option and a callback that also returns an \f(CW\*(C`execute_attributes\*(C'\fR key then the callback's \f(CW\*(C`execute_attributes\*(C'\fR value will take precedence. .SH "BUGS" .IX Header "BUGS" .ie n .IP "Odd $dbh attribute behavior" 4 .el .IP "Odd \f(CW$dbh\fR attribute behavior" 4 .IX Item "Odd $dbh attribute behavior" When writing the test suite I encountered some odd behavior with some \f(CW$dbh\fR attributes. I still need to get deeper into how \s-1DBD\s0's work to understand what it is that is actually doing wrong. .SH "TO DO" .IX Header "TO DO" .IP "Make \s-1DBD\s0 specific handlers" 4 .IX Item "Make DBD specific handlers" Each \s-1DBD\s0 has its own quirks and issues, it would be nice to be able to handle those issues with \f(CW\*(C`DBD::Mock\*(C'\fR in some way. I have an number of ideas already, but little time to sit down and really flesh them out. If you have any suggestions or thoughts, feel free to email me with them. .ie n .IP "Enhance the ""DBD::Mock::StatementTrack"" object" 4 .el .IP "Enhance the \f(CWDBD::Mock::StatementTrack\fR object" 4 .IX Item "Enhance the DBD::Mock::StatementTrack object" I would like to have the \f(CW\*(C`DBD::Mock::StatementTrack\*(C'\fR object handle more of the \&\f(CW\*(C`mock_*\*(C'\fR attributes. This would encapsulate much of the \f(CW\*(C`mock_*\*(C'\fR behavior in one place, which would be a good thing. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\s-1DBI\s0 .PP DBD::NullP, which provided a good starting point .PP Test::MockObject, which provided the approach .PP Test::MockObject article \- .PP Perl Code Kata: Testing Databases \- .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" .IP "Thanks to Ryan Gerry for his patch in \s-1RT\s0 #26604." 4 .IX Item "Thanks to Ryan Gerry for his patch in RT #26604." .PD 0 .IP "Thanks to Marc Beyer for his patch in \s-1RT\s0 #16951." 4 .IX Item "Thanks to Marc Beyer for his patch in RT #16951." .IP "Thanks to Justin DeVuyst for the mock_connect_fail idea." 4 .IX Item "Thanks to Justin DeVuyst for the mock_connect_fail idea." .ie n .IP "Thanks to Thilo Planz for the code for ""bind_param_inout""." 4 .el .IP "Thanks to Thilo Planz for the code for \f(CWbind_param_inout\fR." 4 .IX Item "Thanks to Thilo Planz for the code for bind_param_inout." .IP "Thanks to Shlomi Fish for help tracking down \s-1RT\s0 Bug #11515." 4 .IX Item "Thanks to Shlomi Fish for help tracking down RT Bug #11515." .ie n .IP "Thanks to Collin Winter for the patch to fix the ""begin_work()"", ""commit()"" and ""rollback()"" methods." 4 .el .IP "Thanks to Collin Winter for the patch to fix the \f(CWbegin_work()\fR, \f(CWcommit()\fR and \f(CWrollback()\fR methods." 4 .IX Item "Thanks to Collin Winter for the patch to fix the begin_work(), commit() and rollback() methods." .ie n .IP "Thanks to Andrew McHarg for ""fetchall_hashref()"", ""fetchrow_hashref()"" and ""selectcol_arrayref()"" methods and tests." 4 .el .IP "Thanks to Andrew McHarg for \f(CWfetchall_hashref()\fR, \f(CWfetchrow_hashref()\fR and \f(CWselectcol_arrayref()\fR methods and tests." 4 .IX Item "Thanks to Andrew McHarg for fetchall_hashref(), fetchrow_hashref() and selectcol_arrayref() methods and tests." .ie n .IP "Thanks to Andrew W. Gibbs for the ""mock_last_insert_ids"" patch and test." 4 .el .IP "Thanks to Andrew W. Gibbs for the \f(CWmock_last_insert_ids\fR patch and test." 4 .IX Item "Thanks to Andrew W. Gibbs for the mock_last_insert_ids patch and test." .ie n .IP "Thanks to Chas Owens for patch and test for the ""mock_can_prepare"", ""mock_can_execute"", and ""mock_can_fetch"" features." 4 .el .IP "Thanks to Chas Owens for patch and test for the \f(CWmock_can_prepare\fR, \f(CWmock_can_execute\fR, and \f(CWmock_can_fetch\fR features." 4 .IX Item "Thanks to Chas Owens for patch and test for the mock_can_prepare, mock_can_execute, and mock_can_fetch features." .IP "Thanks to Tomas Zemresfor the unit test in \s-1RT\s0 #71438." 4 .IX Item "Thanks to Tomas Zemresfor the unit test in RT #71438." .ie n .IP "Thanks to Bernhard Graf for multiple patches fixing a range of issues and adding a new \fIOne Shot Failure\fR feature to ""mock_add_resultset""." 4 .el .IP "Thanks to Bernhard Graf for multiple patches fixing a range of issues and adding a new \fIOne Shot Failure\fR feature to \f(CWmock_add_resultset\fR." 4 .IX Item "Thanks to Bernhard Graf for multiple patches fixing a range of issues and adding a new One Shot Failure feature to mock_add_resultset." .IP "Thanks to Erik Huelsmann for testing the new result set custom attributes feature." 4 .IX Item "Thanks to Erik Huelsmann for testing the new result set custom attributes feature." .PD .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (C) 2004 Chris Winters .PP Copyright (C) 2004\-2007 Stevan Little .PP Copyright (C) 2007 Rob Kinyon .PP Copyright (C) 2011 Mariano Wahlmann .PP Copyright (C) 2019 Jason Cooper .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .SH "AUTHORS" .IX Header "AUTHORS" Chris Winters .PP Stevan Little .PP Rob Kinyon .PP Mariano Wahlmann .PP Jason Cooper