File: 10-drivers.t

package info (click to toggle)
libtest-database-perl 1.112-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 272 kB
  • ctags: 83
  • sloc: perl: 637; makefile: 2
file content (107 lines) | stat: -rw-r--r-- 3,435 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;
use Test::More;
use Test::Database;
use Test::Database::Driver;

# for file-based drivers, the dbd parameter is enough
# but for other drivers, we'll need the driver_dsn, username and password
my @drivers = (
    map {
        my $d = $_;
        +{ map { $_ => $d->{$_} }
                grep { exists $d->{$_} }
                qw( driver_dsn dbd username password ) }
        } Test::Database->drivers()
);

plan tests => 5 + @drivers * ( 1 + 2 * 10 ) + 2;

my $base = 'Test::Database::Driver';

# tests for Test::Database::Driver directly
{
    ok( !eval { Test::Database::Driver->new(); 1 },
        'Test::Database::Driver->new() failed'
    );
    like(
        $@,
        qr/^dbd or driver_dsn parameter required at/,
        'Expected error message'
    );
    my $dir = $base->base_dir();
    ok( $dir, "$base has a base_dir(): $dir" );
    like( $dir, qr/Test-Database-.*/,
        "$base\'s base_dir() looks like expected" );
    ok( -d $dir, "$base base_dir() is a directory" );
}

# now test the subclasses

for my $args (@drivers) {
    my $name  = $args->{dbd};
    my $class = "Test::Database::Driver::$name";
    use_ok($class);

    for my $t (
        [ $base  => eval { $base->new(%$args) }  || ( '', $@ ) ],
        [ $class => eval { $class->new(%$args) } || ( '', $@ ) ],
        )
    {
        my ( $created_by, $driver, $at ) = @$t;
        $at =~ s/ at .*\n// if $at;
    SKIP: {
            skip "Failed to create $name driver with $created_by ($at)", 12
                if !$driver;
            diag "$name driver (created by $created_by)";

            # class and name
            my $desc = "$name driver";
            isa_ok( $driver, $class, $desc );
            is( $driver->name(), $name, "$desc has the expected name()" );

            # base_dir
            my $dir = $driver->base_dir();
            ok( $dir, "$desc has a base_dir(): $dir" );
            like( $dir, qr/Test-Database-.*\Q$name\E/,
                "$desc\'s base_dir() looks like expected" );
            ok( -d $dir, "$desc base_dir() is a directory" );

            # version
            my $version;
            ok( eval { $version = $driver->version() },
                "$desc has a version(): $version"
            );
            diag $@ if $@;
            isa_ok( $version, 'version', "$desc version()" );

            # version_dbd
            my $version_dbd;
            ok( eval { $version_dbd = $driver->dbd_version() },
                "$desc has a dbd_version(): $version_dbd"
            );
            diag $@ if $@;

            # driver_dsn, username, password, connection_info
            ok( $driver->driver_dsn(),       "$desc has a driver_dsn()" );
            # skip these now that username and password default to undef
            #ok( defined $driver->username(), "$desc has a username()" );
            #ok( defined $driver->password(), "$desc has a password()" );
            is_deeply(
                [ $driver->connection_info() ],
                [ map { $driver->$_ } qw< driver_dsn username password > ],
                "$desc has a connection_info()"
            );
        }
    }
}

# get all loaded drivers
@drivers = Test::Database->list_drivers();
cmp_ok( scalar @drivers, '>=', 1, 'At least one driver loaded' );

# unload them
Test::Database->clean_config();
@drivers = Test::Database->list_drivers();
is( scalar @drivers, 0, 'All drivers were unloaded' );