Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: How to mock results to repeated $sth->execute() calls (DBD::Mock::Session)

by ChemBoy (Priest)
on Dec 11, 2005 at 14:10 UTC ( #515838=note: print w/replies, xml ) Need Help??

in reply to How to mock results to repeated $sth->execute() calls (DBD::Mock::Session)

This is a slightly thorny problem—when I was working on some tests with DBD::Mock, I tried to come up with a good general solution for this issue and the possibility of failure during a prepare call, and ran out of time to do it. I did come up with an adequate hack for this part, though:

@@ -1081,7 +1087,11 @@ # print STDERR "Adding Results: " . (join " | " => map { join ", " + => @{$_} } @{$current_state->{results}}) . "\n"; # copy the result sets so that # we can re-use the session - $dbh->STORE('mock_add_resultset' => [ @{$current_state->{results} +} ]); + if (my @results = @{$current_state->{results}} ) { + $dbh->STORE('mock_add_resultset' => [ @results ]); + } else { + $self->{state_index}++; + } } sub verify_bound_params {

To use it, you'd put an entry in the session looking like {statement => "select ...", results => [] } at the place you expected the prepare to happen, then normal entries for the execute calls.

Hope this helps!

    --Michael Flanders

Replies are listed 'Best First'.
Re^2: How to mock results to repeated $sth->execute() calls (DBD::Mock::Session)
by tirwhan (Abbot) on Dec 11, 2005 at 16:27 UTC

    Thanks a lot ChemBoy, that put me on the right track. I still had to modify execute() to put the correct result set into the statement tracker, but now it seems to work fine.

    Here's the change I made, for posterity (it's a godawful hack, but maybe it'll still be useful to someone at some point):

    @@ -559,6 +559,10 @@ my $dbh = $sth->{Database}; eval { $session->verify_bound_params($dbh, $tracker->bound_param +s()); + my $idx=$session->{state_index}-1; + my @results=@{$session->{states}[$idx]{results}}; + shift @results; + $tracker->{return_data}=\@results; }; if ($@) { my $session_error = $@;


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://515838]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2020-05-26 17:39 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (150 votes). Check out past polls.