| 1 |
#!/pro/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More; |
| 7 |
|
| 8 |
$ENV{AUTOMATED_TESTING} and plan skip_all => "No folder scanning during automated tests"; |
| 9 |
|
| 10 |
use_ok ("DBI"); |
| 11 |
require "./t/lib.pl"; |
| 12 |
|
| 13 |
my $tstdir = DbDir (); |
| 14 |
my @extdir = ("t", File::Spec->tmpdir ()); |
| 15 |
if (open my $fh, "<", "tests.skip") { |
| 16 |
grep m/\b tmpdir \b/x => <$fh> and pop @extdir; |
| 17 |
} |
| 18 |
my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { |
| 19 |
f_schema => undef, |
| 20 |
f_dir => DbDir (), |
| 21 |
f_dir_search => \@extdir, |
| 22 |
f_ext => ".csv/r", |
| 23 |
f_lock => 2, |
| 24 |
f_encoding => "utf8", |
| 25 |
|
| 26 |
RaiseError => 1, |
| 27 |
PrintError => 1, |
| 28 |
FetchHashKeyName => "NAME_lc", |
| 29 |
}) or die $DBI::errstr || $DBI::errstr || "", "\n"; |
| 30 |
|
| 31 |
my @dsn = $dbh->data_sources; |
| 32 |
my %dir = map { |
| 33 |
m{^dbi:CSV:.*\bf_dir=([^;]+)}i; |
| 34 |
my $folder = $1; |
| 35 |
# data_sources returns the string just one level to many |
| 36 |
$folder =~ m{\\[;\\]} and $folder =~ s{\\(.)}{$1}g; |
| 37 |
($folder => 1); |
| 38 |
} @dsn; |
| 39 |
|
| 40 |
# Use $test_dir |
| 41 |
$dbh->do ("create table fox (c_fox integer, fox char (1))"); |
| 42 |
$dbh->do ("insert into fox values ($_, $_)") for 1, 2, 3; |
| 43 |
|
| 44 |
my @test_dirs = ($tstdir, @extdir); |
| 45 |
is ($dir{$_}, 1, "DSN for $_") for @test_dirs; |
| 46 |
|
| 47 |
my %tbl = map { $_ => 1 } $dbh->tables (undef, undef, undef, undef); |
| 48 |
|
| 49 |
is ($tbl{$_}, 1, "Table $_ found") for qw( tmp fox ); |
| 50 |
|
| 51 |
my %data = ( |
| 52 |
tmp => { # t/tmp.csv |
| 53 |
1 => "ape", |
| 54 |
2 => "monkey", |
| 55 |
3 => "gorilla", |
| 56 |
}, |
| 57 |
fox => { # output123/fox.csv |
| 58 |
1 => 1, |
| 59 |
2 => 2, |
| 60 |
3 => 3, |
| 61 |
}, |
| 62 |
); |
| 63 |
foreach my $tbl ("tmp", "fox") { |
| 64 |
my $sth = $dbh->prepare ("select * from $tbl"); |
| 65 |
$sth->execute; |
| 66 |
while (my $row = $sth->fetch) { |
| 67 |
is ($row->[1], $data{$tbl}{$row->[0]}, "$tbl ($row->[0], ...)"); |
| 68 |
} |
| 69 |
} |
| 70 |
# Do not drop table fox yet |
| 71 |
|
| 72 |
ok ($dbh->disconnect, "disconnect"); |
| 73 |
|
| 74 |
chdir DbDir (); |
| 75 |
my @f = grep m/^fox\.csv/i => glob "*.*"; |
| 76 |
is (scalar @f, 1, "fox.csv still here"); |
| 77 |
|
| 78 |
SKIP: { |
| 79 |
$DBD::File::VERSION < 0.43 and skip "DBD::File-0.43 required", 1; |
| 80 |
is (DBI->connect ("dbi:CSV:", undef, undef, { |
| 81 |
f_schema => undef, |
| 82 |
f_dir => "./undefined", |
| 83 |
f_ext => ".csv/r", |
| 84 |
|
| 85 |
RaiseError => 0, |
| 86 |
PrintError => 0, |
| 87 |
}), undef, "Should not be able to connect to non-exiting folder"); |
| 88 |
} |
| 89 |
|
| 90 |
# drop table fox; |
| 91 |
@f and unlink @f; |
| 92 |
|
| 93 |
done_testing; |