1 |
dpavlin |
20 |
#!/usr/bin/perl -w |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
|
6 |
|
|
use Test::More; |
7 |
|
|
use blib; |
8 |
|
|
|
9 |
|
|
eval "use DBD::Pg"; |
10 |
|
|
plan skip_all => "DBD::Pg required for testing" if $@; |
11 |
|
|
plan tests => 12; |
12 |
|
|
|
13 |
|
|
use_ok('DBI'); |
14 |
|
|
use_ok('Fuse::DBI'); |
15 |
|
|
|
16 |
|
|
my $test_db = 'test'; |
17 |
|
|
my $dsn = "DBI:Pg:dbname=$test_db"; |
18 |
|
|
my $mount = '/tmp/fuse_dbi_mnt'; |
19 |
|
|
|
20 |
|
|
ok((! -e $mount || rmdir $mount), "mount point $mount"); |
21 |
|
|
|
22 |
|
|
mkdir $mount || die "mkdir $mount: $!"; |
23 |
|
|
ok(-d $mount, "mkdir $mount"); |
24 |
|
|
|
25 |
|
|
ok(my $dbh = DBI->connect($dsn, , '', '', { RaiseError => 1 }), |
26 |
|
|
"connect fusedbi test database"); |
27 |
|
|
|
28 |
|
|
my $drop = eval { $dbh->do(qq{ drop table files }) }; |
29 |
|
|
diag "drop table files" if ($drop); |
30 |
|
|
|
31 |
|
|
ok($dbh->do(qq{ |
32 |
|
|
create table files ( |
33 |
|
|
name text primary key, |
34 |
|
|
data text |
35 |
|
|
) |
36 |
|
|
}), "create table files"); |
37 |
|
|
|
38 |
|
|
ok(my $sth = $dbh->prepare(qq{ |
39 |
|
|
insert into files (name,data) values (?,?) |
40 |
|
|
}), "prepare"); |
41 |
|
|
|
42 |
|
|
foreach my $file (qw(file dir/file dir/subdir/file)) { |
43 |
|
|
my $data = "this is test data\n" x length($file); |
44 |
|
|
ok($sth->execute($file,$data), "insert $file"); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
my $sql_filenames = qq{ |
48 |
|
|
select |
49 |
|
|
name as id, |
50 |
|
|
name as filename, |
51 |
|
|
length(data) as size, |
52 |
|
|
1 as writable |
53 |
|
|
from files |
54 |
|
|
}; |
55 |
|
|
|
56 |
|
|
my $sql_read = qq{ |
57 |
|
|
select data |
58 |
|
|
from files |
59 |
|
|
where name = ?; |
60 |
|
|
}; |
61 |
|
|
|
62 |
|
|
my $sql_update = qq{ |
63 |
|
|
update files |
64 |
|
|
set data = ? |
65 |
|
|
where name = ?; |
66 |
|
|
}; |
67 |
|
|
|
68 |
|
|
system "fusermount -q -u $mount" || diag "nothing mounted at $mount, ok"; |
69 |
|
|
|
70 |
|
|
my $mnt = Fuse::DBI->mount({ |
71 |
|
|
filenames => $sql_filenames, |
72 |
|
|
read => $sql_read, |
73 |
|
|
update => $sql_update, |
74 |
|
|
dsn => $dsn, |
75 |
|
|
mount => $mount, |
76 |
dpavlin |
22 |
fork => 1, |
77 |
dpavlin |
20 |
}); |
78 |
|
|
|
79 |
|
|
ok($mnt, "mount"); |
80 |
|
|
|
81 |
|
|
diag "press enter to continue"; |
82 |
|
|
my $foo = <STDIN>; |
83 |
|
|
|
84 |
|
|
ok($mnt->umount,"umount"); |
85 |
|
|
|