File: 026_st_bind_col.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 (73 lines) | stat: -rw-r--r-- 1,912 bytes parent folder | download | duplicates (4)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use 5.006;

use strict;
use warnings;

use Test::More tests => 12;

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

my $dbh = DBI->connect( 'DBI:Mock:', '', '' );

$dbh->{mock_add_resultset} = [
                              [ 'id', 'type', 'inventory_id' ],
                              [ '1',  'european', '42' ],
                              [ '27', 'african',  '2' ],
                             ];

my $sth = $dbh->prepare( 'SELECT id, type, inventory_id FROM Swallow' );

$sth->execute();

{
    my ($id, $type, $inventory_id);

    $sth->bind_col( 1, \$id );
    $sth->bind_col( 2, \$type );
    $sth->bind_col( 3, \$inventory_id );

    ok( $sth->fetch(), 'fetch() returned data' );
    is( $id, 1, 'bind_col to $id == 1' );
    is( $type, 'european', 'bind_col to $type == "european"' );
    is( $inventory_id, 42, 'bind_col to $inventory_id == 42' );
}

{
    my %hash;

    $sth->bind_columns( \( @hash{ qw( id type inventory_id ) } ) );

    ok( $sth->fetch(), 'fetch() returned data' );
    is( $hash{id}, 27, 'bind_columns with hash, id == 1' );
    is( $hash{type}, 'african', 'bind_columns with hash, type == "african"' );
    is( $hash{inventory_id}, 2, 'bind_columns with hash, inventory_id == 2' );
}

{
    ok( ! $sth->fetchrow_arrayref(),
        'fetchrow_arrayref returns false after data is exhausted, even with bound columns' );
}

{
    $dbh->{mock_clear_history} = 1;

    my @rows =
        ( [ '1',  'european', '42' ],
          [ '27', 'african',  '2' ],
        );

    $dbh->{mock_add_resultset} = [
                                  [ 'id', 'type', 'inventory_id' ],
                                  @rows,
                                 ];

    my $results = $dbh->selectall_arrayref( 'SELECT id, type, inventory_id FROM Swallow' );

    is_deeply( $results,
               \@rows,
               'bind_col implementation does not break selectall_* methods' );
}