Debian Codegraph @

t/015_st_fetch_records.t

raw
1 use 5.008;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9 use_ok('DBD::Mock');
10 use_ok('DBI');
11 }
12
13 #use Data::Dumper qw( Dumper );
14
15 my @rs_foo = (
16 [ 'this', 'that' ],
17 [ 'this_one', 'that_one' ],
18 [ 'this_two', 'that_two' ],
19 );
20 my $foo_sql = 'SELECT this, that FROM foo';
21
22 my @rs_login = (
23 [ 'login', 'first_name', 'last_name' ],
24 [ 'cwinters', 'Chris', 'Winters' ],
25 [ 'bflay', 'Bobby', 'Flay' ],
26 [ 'alincoln', 'Abe', 'Lincoln' ],
27 );
28 my $login_sql = 'SELECT login, first_name, last_name FROM foo';
29
30 my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
31
32 # Seed the handle with two resultsets
33
34 # the first one ordered
35 $dbh->{mock_add_resultset} = [ @rs_foo ];
36
37 # the second one named
38 $dbh->{mock_add_resultset} = { sql => $login_sql,
39 results => \@rs_login };
40
41 # run the first one
42 {
43 my ( $sth );
44 eval {
45 $sth = $dbh->prepare( $foo_sql );
46 $sth->execute();
47 };
48 check_resultset( $sth, [ @rs_foo ] );
49 }
50
51 {
52 my ( $sth );
53 eval {
54 $sth = $dbh->prepare( $login_sql );
55 $sth->execute();
56 };
57 check_resultset( $sth, [ @rs_login ] );
58 }
59
60 {
61 my ( $sth );
62 eval {
63 $sth = $dbh->prepare( q{INSERT INTO foo VALUES ( 'Don Corleone' )} );
64 $sth->execute();
65 };
66 ok( ! $sth->{Active},
67 '...this should not be an active handle' );
68 }
69
70 sub check_resultset {
71 my ( $sth, $check ) = @_;
72 my $fields = shift @{ $check };
73 is( $sth->{mock_num_records}, scalar @{ $check },
74 'Correct number of records reported by statement' );
75 is( $sth->{mock_num_rows}, scalar @{ $check },
76 'Correct number of rows reported by statement' );
77 is( $sth->rows, scalar @{ $check },
78 'Correct number of rows reported by statement' );
79 is( $sth->{mock_current_record_num}, 0,
80 'Current record number correct before fetching' );
81 ok( $sth->{Active},
82 '... this should be an active handle' );
83 for ( my $i = 0; $i < scalar @{ $check }; $i++ ) {
84 my $rec_num = $i + 1;
85 my $this_check = $check->[$i];
86 my $this_rec = $sth->fetchrow_arrayref;
87 my $num_fields = scalar @{ $this_check };
88 is( scalar @{ $this_rec }, $num_fields,
89 "Record $rec_num, correct number of fields ($num_fields)" );
90 for ( my $j = 0; $j < $num_fields; $j++ ) {
91 my $field_num = $j + 1;
92 is( $this_rec->[$j], $this_check->[$j],
93 "Record $rec_num, field $field_num" );
94 }
95 is( $sth->{mock_current_record_num}, $rec_num,
96 "Record $rec_num, current record number tracked" );
97 if ( $rec_num == scalar @{ $check } ) {
98 ok( $sth->{mock_is_depleted},
99 'Resultset depleted properly' );
100 ok( ! $sth->{Active},
101 '...this should not be an active handle anymore' );
102 }
103 else {
104 ok( ! $sth->{mock_is_depleted},
105 'Resultset not yet depleted' );
106 }
107 }
108
109 }
110
111 done_testing();