File: 029_multiple_prepare_statements.t

package info (click to toggle)
libdbd-mock-perl 1.59-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 512 kB
  • sloc: perl: 1,251; makefile: 7
file content (56 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download
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
46
47
48
49
50
51
52
53
54
55
56
use 5.008;

use strict;
use warnings;

use Test::More;
use Test::Exception;

BEGIN {
    use_ok('DBD::Mock');
    use_ok('DBI');
}

my $dbh = DBI->connect( 'DBI:Mock:', '', '', { RaiseError => 1 } );
my $mock_session = DBD::Mock::Session->new(
    {
        statement => qr/SELECT name, id FROM person/,
        results   => [ [ 'name', 'id' ], [ 'Charles', 2 ], [ 'Wall', 3 ], ]
    },
    {
        statement => qr/SELECT email FROM client/,
        results   => [
            [ 'name',    'email' ],
            [ 'Charles', 'noreply@nodomain.com' ],
            [ 'Wall',    'noreply@nodomain.com' ],
        ]
    }
);

$dbh->{mock_session} = $mock_session;

my $first_sth;
my $second_sth;

lives_ok(
    sub {
        $second_sth = $dbh->prepare("SELECT email FROM client");
        $first_sth  = $dbh->prepare("SELECT name, id FROM person");

        $first_sth->execute();
        $second_sth->execute();

        my $row = $first_sth->fetchrow_hashref;
        is( $row->{name}, 'Charles', 'First statement first column' );
        is( $row->{id},   '2',       'First statement second column' );

        $row = $second_sth->fetchrow_hashref;
        is( $row->{name}, 'Charles', 'Second statement first column' );
        is( $row->{email}, 'noreply@nodomain.com',
            'Second statement second column' );

    },
    'Prepare two statements'
);

done_testing();