perl - DBD::Mock specify output values for stored procedure -
i'm trying use dbd::mock test code uses database. far, normal sql queries work fine, i'm @ loss how can test code calls stored procedures. using bound_params
key dbd::mock::session->new
constructor, can specify input parameters, can't seem find way of setting mock results of parameters bound using dbi::statementhandle::bind_param_inout()
.
to provide example code going tested, have @ following:
use dbi; $dbh = dbi->connect('dbi:mock', '', '', { raiseerror => 1, printerror => 1 }); $sth = $dbh->prepare(q{ begin some_stored_proc(i_arg1 => :arg1, o_arg2 => :arg2); end; }); ($arg1, $arg2) = ('foo', 'bar'); $sth->bind_param(':arg1', $arg1); $sth->bind_param_inout(':arg2', \$arg2, 200); $sth->execute(); print stderr "output value of arg2 = $arg2\n";
now, want seed db 'frobnication'
arg2
parameter, such when above code executed, $arg2
variable contains string , output is
outputvalue of arg2 = frobnication
here ended doing. main work goes overriding of dbd::mock::st::bind_param_inout
method.
use dbi; use dbd::mock; use dbd::mock::st; use carp; # array of values bound on each invocation @values = qw/frobnication/; # dummy variable trick dbd::mock thinking got same reference # bind_param_inout , bound_params (the former not in control of # testing code, hence hack). $dummy = undef; # keep reference original bind_param_inout method $bind_param_inout_orig = \&dbd::mock::st::bind_param_inout; # override our mocked version assigns value reference. # notice @ bind_param_inout call, *not* execute call! local *dbd::mock::st::bind_param_inout = sub { ($self, $param_num, $val, $size) = (shift, shift, shift, shift); $bind_param_inout_orig->($self, $param_num, \$dummy, $size, @_); $$val = shift @values or carp::confess '@values array exhausted!'; }; # set mock session $dbh = dbi->connect('dbi:mock:', '', '', { raiseerror => 1, printerror => 1 }); $dbh->{mock_session} = dbd::mock::session->new('foo_session' => ( { statement => qr/begin\n\s*some_stored_proc/, results => [], bound_params => ['foo', \$dummy] })); # code tested $sth = $dbh->prepare(q{ begin some_stored_proc(i_arg1 => :arg1, o_arg2 => :arg2); end; }); ($arg1, $arg2) = ('foo', 'bar'); $sth->bind_param(':arg1', $arg1); $sth->bind_param_inout(':arg2', \$arg2, 200); $sth->execute(); print stderr "output value of arg2 = $arg2\n";
Comments
Post a Comment