File: 017_st_can_connect.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-- 2,348 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 strict;

use Test::More tests => 23;

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

my $dbh = DBI->connect('DBI:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
isa_ok($dbh, "DBI::db");
ok($dbh->{RaiseError}, '... RaiseError is set correctly');
ok(! $dbh->{PrintError}, '... PrintError is set correctly');

my $sth_exec = $dbh->prepare('SELECT foo FROM bar');
isa_ok($sth_exec, "DBI::st");

# turn off the handle between the prepare and execute...
$dbh->{mock_can_connect} = 0;

# check our value is correctly set
is($dbh->{mock_can_connect}, 0, '... can connect is set to 0');

# and check the side effects of that
ok(!$dbh->{Active}, '... the handle is not Active');
ok(!$dbh->ping(), '... and ping returns false');

# now try to execute it

eval { $sth_exec->execute() };
ok($@, '... we got an exception');
like($@, qr/No connection present/, '... we got the expected execption');

# turn off the database between execute and fetch

$dbh->{mock_can_connect} = 1;

# check our value is correctly set
is($dbh->{mock_can_connect}, 1, '... can connect is set to 1');

# and check the side effects of that
ok($dbh->{Active}, '... the handle is Active');
ok($dbh->ping(), '... and ping returns true');

$dbh->{mock_add_resultset} = [[ qw(foo bar   ) ],  # column headers
                              [ qw(this that ) ],  # first row values
                              [ qw(never seen) ]]; # second row values
                               
my $sth_fetch = $dbh->prepare('SELECT foo, bar FROM baz');
isa_ok($sth_fetch, "DBI::st");

eval { $sth_fetch->execute() };
ok(!$@, '... executed without exception');

my $row = eval { $sth_fetch->fetchrow_arrayref() };
ok(!$@, '... the first row was returned without execption');
is_deeply($row, [ qw(this that) ], '... we got back the expected data in the first row');

# now turn off the database
$dbh->{mock_can_connect} = 0;

# check our value is correctly set
is($dbh->{mock_can_connect}, 0, '... can connect is set to 0');

# and check the side effects of that
ok(!$dbh->{Active}, '... the handle is not Active');
ok(!$dbh->ping(), '... and ping returns false');

$row = eval { $sth_fetch->fetchrow_arrayref() };
ok($@, '... we got the exception');
like($sth_fetch->errstr, 
     qr/^No connection present/,
     '... fetching row against inactive db throws expected exception' );