File: 003_db_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 (79 lines) | stat: -rw-r--r-- 2,684 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
74
75
76
77
78
79
use strict;

use Test::More tests => 22;

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

# test this as an exception
{
    my $dbh = DBI->connect('DBI:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
    isa_ok($dbh, "DBI::db");
    # check to be sure this is set, otherwise 
    # the test wont be set up right
    is($dbh->{RaiseError}, 1, '... make sure RaiseError is set correctly');
        
    # check to see it is active in the first place
    ok($dbh->{Active}, '...our handle with the default settting is Active' );
    ok($dbh->ping(), '...and successfuly pinged handle' );
    
    $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}, '...our handle is no longer Active after setting mock_can_connect');
    ok(!$dbh->ping(), '...and unsuccessfuly pinged handle (good)');
    
    my $sth = eval { $dbh->prepare( "SELECT foo FROM bar" ) };
    ok($@, '... we should have an exception');
    
    like($@, 
        qr/No connection present/,
        'Preparing statement against inactive handle throws expected exception' );
        
    like($dbh->errstr, 
        qr/^No connection present/,
        'Preparing statement against inactive handle sets expected DBI error' );
     
    $dbh->disconnect();     
}

# and now test this as a warning
{
    
    my $dbh = DBI->connect('DBI:Mock:', '', '', { PrintError => 1 });
    isa_ok($dbh, "DBI::db");
    # check to be sure this is set, otherwise 
    # the test wont be set up right
    is($dbh->{PrintError}, 1, '... make sure PrintError is set correctly');
        
    # check to see it is active in the first place
    ok($dbh->{Active}, '...our handle with the default settting is Active' );
    ok($dbh->ping(), '...and successfuly pinged handle' );
    
    $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}, '...our handle is no longer Active after setting mock_can_connect');
    ok(!$dbh->ping(), '...and unsuccessfuly pinged handle (good)');    

    { # isolate the warn handler 
        local $SIG{__WARN__} = sub {
            my $msg = shift;
            like($msg, qr/No connection present/, '...got the expected warning');
        };
    
        my $sth = eval { $dbh->prepare( "SELECT foo FROM bar" ) };
        ok(!$@, '... we should not have an exception');
        ok(!defined($sth), '... and our statement should be undefined');
    }

    $dbh->disconnect();
}