File: bug_0003.t

package info (click to toggle)
libdbd-mock-perl 1.43-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 416 kB
  • sloc: perl: 1,135; makefile: 2
file content (45 lines) | stat: -rwxr-xr-x 1,396 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/perl
use Test::More tests => 3; 
use strict;
use warnings;
use Test::Exception;
use DBI;
use DBD::Mock;

# This tests that spurious extra ->execute invocations fail with a
# useful message. This is because there was a bug in which
# DBD::Mock->verify_bound_params didn't check that the session had run
# out, and on return out-of-bounds element of the state array is
# accessed, causing an unhelpful error "Can't use an undefined value
# as an ARRAY reference at ../lib/DBD/Mock.pm line 635."

my @session = (
    {
        'statement' => 'INSERT INTO foo (bar) values (?);',
        'results' => [],
        'bound_params' => [1]
    },
);

my $dbh = DBI->connect('dbi:Mock:', '',  '', { PrintError => 0, RaiseError => 1});

# Add just part of the expected session, such that the next step would be a 'BEGIN WORK'
$dbh->{mock_session} = DBD::Mock::Session->new(@session);

# now execute the steps in the session
my $step = $session[0];

my $sth = $dbh->prepare($step->{statement});
ok $sth, 
    "prepare statement";

my $params = $step->{bound_params} || [];
ok $sth->execute(@$params),
    "execute statement";

# Session expects that to be all.  So let's surprise it with another
# ->execute.  It should fail appropriately.
throws_ok {
    ok $sth->execute(@$params),
} qr/\QSession states exhausted, only '1' in DBD::Mock::Session\E/,
    "fails on executing one too many times";