File: dbi-rowcount.t

package info (click to toggle)
libdbd-firebird-perl 0.91-2%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 680 kB
  • sloc: perl: 4,085; ansic: 2,262; makefile: 14
file content (142 lines) | stat: -rw-r--r-- 4,407 bytes parent folder | download | duplicates (8)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#/usr/bin/perl

# dbi-rowcount.t
#
# Verify behavior of interfaces which report number of rows affected

use strict;
use warnings;
use Test::More;
use DBI;
use vars qw($dbh $table);

use lib 't';

use TestFirebird;
my $T = TestFirebird->new;

END {
       if (defined $dbh and defined $table) {
               eval { $dbh->do("DROP TABLE $table"); };
       }
}

# is() with special case "zero but true" support
sub is_maybe_zbt {
       my ($value, $expected) = @_;
       return ($value == $expected) unless $expected == 0;

       return (($value == 0 and $value));
}

# == Test Initialization =========================================

plan tests => 84;

($dbh) = $T->connect_to_database({RaiseError => 1});
pass("connect");
$table = find_new_table($dbh);
$dbh->do("CREATE TABLE $table(ID INTEGER NOT NULL, NAME VARCHAR(16) NOT NULL)");
pass("CREATE TABLE $table");

my @TEST_PROGRAM = (
       {
               sql      => qq|INSERT INTO $table (ID, NAME) VALUES (1, 'unu')|,
               desc     => 'literal insert',
               expected => 1,
       },
       {
               sql      => qq|INSERT INTO $table (ID, NAME) VALUES (?, ?)|,
               desc     => 'parameterized insert',
               params   => [2, 'du'],
               expected => 1,
       },
       {
               sql      => qq|DELETE FROM $table WHERE 1=0|,
               desc     => 'DELETE WHERE (false)',
               expected => 0,
       },
       {
               sql      => qq|UPDATE $table SET NAME='nomo'|,
               desc     => 'UPDATE all',
               expected => 2,
       },
       {
               sql      => qq|DELETE FROM $table|,
               desc     => 'DELETE all',
               expected => 2,
       },
);

# == Tests ==

# == 1. do()

for my $spec (@TEST_PROGRAM) {
       my @bind = @{$spec->{params}} if $spec->{params};
       my $rv = $dbh->do($spec->{sql}, undef, @bind);

       ok(is_maybe_zbt($rv, $spec->{expected}), "do($spec->{desc})");
       # $DBI::rows is not guaranteed to be correct after $dbh->blah operations
}

# == 2a. single execute() and rows()

for my $spec (@TEST_PROGRAM) {
       my @bind = @{$spec->{params}} if $spec->{params};
       my $sth = $dbh->prepare($spec->{sql});
       my $rv = $sth->execute(@bind);

       ok(is_maybe_zbt($rv, $spec->{expected}), "execute($spec->{desc})");
       is($DBI::rows, $spec->{expected}, "execute($spec->{desc}) (\$DBI::rows)");
       is($sth->rows, $spec->{expected}, "\$sth->rows($spec->{desc})");
}

# == 2b. repeated execute() and rows()
{
    my $i   = 0;
    my $sth = $dbh->prepare("INSERT INTO $table(ID, NAME) VALUES (?, ?)");
    for my $name (qw|unu du tri kvar kvin ses sep ok naux dek|) {
        my $rv = $sth->execute( ++$i, $name );
        is( $rv, 1, "re-execute(INSERT one) -> 1" );
        is( $DBI::rows, 1, "re-execute(INSERT one) -> 1 (\$DBI::rows)" );
        is( $sth->rows, 1, "\$sth->rows(re-executed INSERT)" );
    }

    $sth = $dbh->prepare("DELETE FROM $table WHERE ID<?");
    for ( 6, 11 ) {
        my $rv = $sth->execute($_);
        is( $rv,        5, "re-execute(DELETE five) -> 1" );
        is( $DBI::rows, 5, "re-execute(DELETE five) -> 1 (\$DBI::rows)" );
        is( $sth->rows, 5, "\$sth->rows(re-executed DELETE)" );
    }
    my $rv = $sth->execute(16);
    ok( is_maybe_zbt( $rv, 0 ), "re-execute(DELETE on empty) zero but true" );
    is( $DBI::rows, 0,
        "re-execute(DELETE on empty) (\$DBI::rows) zero but true" );
    is( $sth->rows, 0,
        "\$sth->rows(re-executed DELETE on empty) zero but true" );
}

# == 3. special cases
#       DBD::InterBase tracks the number of FETCHes on a SELECT statement
#       in $sth->rows() as an extension to the DBI.

{
    my $i = 0;
    for my $name (qw|unu du tri kvar kvin ses sep ok naux dek|) {
        $dbh->do( "INSERT INTO $table(ID, NAME) VALUES (?, ?)",
            undef, ++$i, $name );
    }
    my $sth = $dbh->prepare("SELECT ID, NAME FROM $table");
    my $rv  = $sth->execute;
    ok( is_maybe_zbt( $rv, 0 ), "execute(SELECT) -> zero but true" );
    is( $DBI::rows, 0, "execute(SELECT) zero but true (\$DBI::rows)" );
    is( $sth->rows, 0, "\$sth->rows(SELECT) zero but true" );

    my $fetched = 0;
    while ( $sth->fetch ) {
        is( ++$fetched, $sth->rows, "\$sth->rows incrementing on SELECT" );
        is( $fetched,   $DBI::rows, "\$DBI::rows incrementing on SELECT" );
    }
}