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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
use strict;
use Test::More tests => 28;
BEGIN {
use_ok('DBD::Mock');
use_ok('DBI');
}
# test misc. attributes
{
my $dbh = DBI->connect('DBI:Mock:', 'user', 'pass');
isa_ok($dbh, 'DBI::db');
$dbh->{mock_add_resultset} = {
sql => 'SELECT foo FROM bar',
results => DBD::Mock->NULL_RESULTSET,
failure => [ 5, 'Ooops!' ],
};
$dbh->{PrintError} = 0;
$dbh->{RaiseError} = 1;
my $sth = eval { $dbh->prepare('SELECT foo FROM bar') };
ok(!$@, '$sth handle prepared correctly');
isa_ok($sth, 'DBI::st');
eval { $sth->execute() };
ok( $@, '$sth handled executed and died' );
$dbh->{mock_add_resultset} = {
sql => 'SELECT bar FROM foo',
results => [
[ 'bar' ],
[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]
]
};
#test new error generators
$dbh->{mock_can_prepare} = 0;
$dbh->{mock_can_execute} = 1;
$dbh->{mock_can_fetch} = 1;
eval {
my $sth = $dbh->prepare("SELECT bar FROM foo");
$sth->execute;
while (my $row = $sth->fetchrow_arrayref) {
1;
}
};
ok($@ =~ /Cannot prepare/, '$sth handle failed to prepare');
$dbh->{mock_can_prepare} = -3;
$dbh->{mock_can_execute} = 1;
$dbh->{mock_can_fetch} = 1;
my $i = 0;
for (1 .. 10) {
$i++;
eval {
my $sth = $dbh->prepare("SELECT bar FROM foo");
$sth->execute;
while (my $row = $sth->fetchrow_arrayref) {
1;
}
};
last if $@;
}
ok($@ =~ /Cannot prepare/, "$@ should contain 'Cannot prepare'");
ok($i == 4, "$i should be 4");
$dbh->{mock_can_prepare} = 1;
$dbh->{mock_can_execute} = 0;
$dbh->{mock_can_fetch} = 1;
eval {
my $sth = $dbh->prepare("SELECT bar FROM foo");
$sth->execute;
while (my $row = $sth->fetchrow_arrayref) {
1;
}
};
ok($@ =~ /Cannot execute/, '$sth handle failed to execute');
$dbh->{mock_can_prepare} = 1;
$dbh->{mock_can_execute} = -3;
$dbh->{mock_can_fetch} = 1;
$i = 0;
for (1 .. 10) {
$i++;
eval {
my $sth = $dbh->prepare("SELECT bar FROM foo");
$sth->execute;
while (my $row = $sth->fetchrow_arrayref) {
1;
}
};
last if $@;
}
ok($@ =~ /Cannot execute/, "$@ should contain 'Cannot execute'");
ok($i == 4, "$i should be 4");
$dbh->{mock_can_prepare} = 1;
$dbh->{mock_can_execute} = 1;
$dbh->{mock_can_fetch} = 0;
eval {
my $sth = $dbh->prepare("SELECT bar FROM foo");
$sth->execute;
while (my $row = $sth->fetchrow_arrayref) {
1;
}
};
ok($@ =~ /Cannot fetch/, '$sth handle failed to fetch');
$dbh->{mock_can_prepare} = 1;
$dbh->{mock_can_execute} = 1;
$dbh->{mock_can_fetch} = 0;
eval {
my $sth = $dbh->prepare("SELECT bar FROM foo");
$sth->execute;
while (my @row = $sth->fetchrow_array) {
1;
}
};
ok($@ =~ /Cannot fetch/, '$sth handle failed to fetch');
$dbh->{mock_can_prepare} = 1;
$dbh->{mock_can_execute} = 1;
$dbh->{mock_can_fetch} = 0;
eval {
my $sth = $dbh->prepare("SELECT bar FROM foo");
$sth->execute;
while (my $row = $sth->fetchrow_hashref) {
1;
}
};
ok($@ =~ /Cannot fetch/, '$sth handle failed to fetch');
$dbh->{mock_can_prepare} = 1;
$dbh->{mock_can_execute} = 1;
$dbh->{mock_can_fetch} = 0;
eval {
my $sth = $dbh->prepare("SELECT bar FROM foo");
$sth->execute;
my @row = $sth->fetchall_arrayref;
};
ok($@ =~ /Cannot fetch/, '$sth handle failed to fetch');
$dbh->{mock_can_prepare} = 1;
$dbh->{mock_can_execute} = 1;
$dbh->{mock_can_fetch} = -100;
{
my $sth;
eval {
$sth = $dbh->prepare("select bar from foo");
$sth->execute;
};
ok(!$@, "prepare and execute should work");
isa_ok($sth, 'DBI::st');
eval { my $row = $sth->fetch };
ok(!$@, "fetch should work");
ok($dbh->{mock_can_fetch}==-99, "$dbh->{mock_can_fetch} should be -99");
eval { my $row = $sth->fetchrow_arrayref };
ok(!$@, "fetch should work");
ok($dbh->{mock_can_fetch}==-98, "$dbh->{mock_can_fetch} should be -98");
eval { my @row = $sth->fetchrow_array };
ok(!$@, "fetch should work");
ok($dbh->{mock_can_fetch}==-97, "$dbh->{mock_can_fetch} should be -97");
eval { my $row = $sth->fetchrow_hashref };
ok(!$@, "fetch should work");
ok($dbh->{mock_can_fetch}==-96, "$dbh->{mock_can_fetch} should be -96");
eval { my @rows = $sth->fetchall_arrayref };
ok(!$@, "fetch should work");
ok($dbh->{mock_can_fetch}==-95, "$dbh->{mock_can_fetch} should be -95");
}
}
|