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
|
use strict;
use warnings;
use Test::More;
use Test::Database::Util;
use File::Spec;
my @good = (
{ dsn => 'dbi:mysql:database=mydb;host=localhost;port=1234',
username => 'user',
password => 's3k r3t',
},
{ dsn => 'dbi:mysql:database=mydb;host=remotehost;port=5678',
username => 'otheruser',
},
{ dsn => 'dbi:SQLite:db.sqlite' },
{ driver_dsn => 'dbi:mysql:host=remotehost;port=5678',
username => 'otheruser',
},
);
my @bad = (
[ File::Spec->catfile(qw< t database.bad >),
qr/^Can't parse line at .*, line \d+:\n <bad format> at /
],
[ File::Spec->catfile(qw< t database.bad2 >),
qr/^Record doesn't start with dsn or driver_dsn .*, line \d+:\n <drh = dbi:mysql:> at /
],
[ 'missing', qr/^Can't open missing for reading: / ],
);
plan tests => 1 + @good + 2 * @bad + 1;
# load a correct file
my $file = File::Spec->catfile(qw< t database.good >);
my @config = _read_file($file);
is( scalar @config, scalar @good,
"Got @{[scalar @good]} handles from $file" );
for my $test (@good) {
my $args = shift @config;
is_deeply( $args, $test,
"Read args for handle " . ( $test->{dsn} || $test->{driver_dsn} ) );
}
# try to load a bad file
for my $t (@bad) {
my ( $file, $regex ) = @$t;
ok( !eval { _read_file($file); 1 }, "_read_file( $file ) failed" );
like( $@, $regex, 'Expected error message' );
}
# load an empty file
$file = File::Spec->catfile(qw< t database.empty >);
is( scalar _read_file($file), 0, 'Empty file' );
|