File: 015_st_fetch_records.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 (106 lines) | stat: -rw-r--r-- 3,004 bytes parent folder | download | duplicates (5)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use strict;

use Test::More tests => 43;

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

#use Data::Dumper qw( Dumper );

my @rs_foo = (
    [ 'this', 'that' ],
    [ 'this_one', 'that_one' ],
    [ 'this_two', 'that_two' ],
);
my $foo_sql = 'SELECT this, that FROM foo';

my @rs_login = (
    [ 'login', 'first_name', 'last_name' ],
    [ 'cwinters', 'Chris', 'Winters' ],
    [ 'bflay', 'Bobby', 'Flay' ],
    [ 'alincoln', 'Abe', 'Lincoln' ],
);
my $login_sql = 'SELECT login, first_name, last_name FROM foo';

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

# Seed the handle with two resultsets

# the first one ordered
$dbh->{mock_add_resultset} = [ @rs_foo ];

# the second one named
$dbh->{mock_add_resultset} = { sql     => $login_sql,
                               results => \@rs_login };

# run the first one
{
    my ( $sth );
    eval {
        $sth = $dbh->prepare( $foo_sql );
        $sth->execute();
    };
    check_resultset( $sth, [ @rs_foo ] );
}

{
    my ( $sth );
    eval {
        $sth = $dbh->prepare( $login_sql );
        $sth->execute();
    };
    check_resultset( $sth, [ @rs_login ] );
}

{
    my ( $sth );
    eval {
        $sth = $dbh->prepare( q{INSERT INTO foo VALUES ( 'Don Corleone' )} );
        $sth->execute();
    };
     ok( ! $sth->{Active},
        '...this should not be an active handle' );
}

sub check_resultset {
    my ( $sth, $check ) = @_;
    my $fields  = shift @{ $check };
    is( $sth->{mock_num_records}, scalar @{ $check },
        'Correct number of records reported by statement' );
    is( $sth->{mock_num_rows}, scalar @{ $check },
        'Correct number of rows reported by statement' );        
    is( $sth->rows, scalar @{ $check },
        'Correct number of rows reported by statement' );        
    is( $sth->{mock_current_record_num}, 0,
        'Current record number correct before fetching' );
    ok( $sth->{Active},
        '... this should be an active handle' );
    for ( my $i = 0; $i < scalar @{ $check }; $i++ ) {
        my $rec_num = $i + 1;
        my $this_check = $check->[$i];
        my $this_rec = $sth->fetchrow_arrayref;
        my $num_fields = scalar @{ $this_check };
        is( scalar @{ $this_rec }, $num_fields,
            "Record $rec_num, correct number of fields ($num_fields)" );
        for ( my $j = 0; $j <  $num_fields; $j++ ) {
            my $field_num = $j + 1;
            is( $this_rec->[$j], $this_check->[$j],
                "Record $rec_num, field $field_num" );
        }
        is( $sth->{mock_current_record_num}, $rec_num,
            "Record $rec_num, current record number tracked" );
        if ( $rec_num == scalar @{ $check } ) {
            ok( $sth->{mock_is_depleted},
                'Resultset depleted properly' );
            ok( ! $sth->{Active},
                '...this should not be an active handle anymore' );
        }
        else {
            ok( ! $sth->{mock_is_depleted},
                'Resultset not yet depleted' );
        }
    }

}