0.08: support for filenames which are null (it will ne named NULL-id)
[Fuse-DBI] / t / 03pgsql.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use File::Find;
8 use blib;
9
10 eval "use DBD::Pg";
11 plan skip_all => "DBD::Pg required for testing" if $@;
12 plan tests => 34;
13
14 use_ok('DBI');
15 use_ok('Fuse::DBI');
16
17 my $test_db = 'test';
18 my $dsn = "DBI:Pg:dbname=$test_db";
19 my $mount = '/tmp/fuse_dbi_mnt';
20
21 ok((! -e $mount || rmdir $mount), "mount point $mount");
22
23 mkdir $mount || die "mkdir $mount: $!";
24 ok(-d $mount, "mkdir $mount");
25
26 ok(my $dbh = DBI->connect($dsn, , '', '', { RaiseError => 1 }),
27         "connect fusedbi test database");
28
29 my $drop = eval { $dbh->do(qq{ drop table files }) };
30 diag "drop table files" if ($drop);
31
32 ok($dbh->do(qq{
33         create table files (
34                 name text primary key,
35                 data text
36         )
37 }), "create table files");
38
39 ok(my $sth = $dbh->prepare(qq{
40         insert into files (name,data) values (?,?)
41 }), "prepare");
42
43 my @files = qw(file dir/file dir/subdir/file);
44 my %file_data;
45
46 foreach my $file (@files) {
47         $file_data{$file} = ("this is test data on ".localtime()."\n") x length($file);
48         ok($sth->execute($file,$file_data{$file}), "insert $file");
49 }
50
51 ok($dbh->disconnect, "disconnect after insert");
52
53 my $sql_filenames = qq{
54         select
55                 name as id,
56                 name as filename,
57                 length(data) as size,
58                 1 as writable
59         from files
60 };
61
62 my $sql_read = qq{
63         select data
64                 from files
65                 where name = ?;
66 };
67
68 my $sql_update = qq{
69         update files
70                 set data = ?    
71                 where name = ?;
72 };
73
74 system "fusermount -q -u $mount" || diag "nothing mounted at $mount, ok";
75
76 my $mnt = Fuse::DBI->mount({
77         filenames => $sql_filenames,
78         read => $sql_read,
79         update => $sql_update,
80         dsn => $dsn,
81         mount => $mount,
82         fork => 1,
83 });
84
85 ok($mnt, "mount");
86
87 sub test_file {
88         my $f = $File::Find::name;
89
90         ok($f, "file $f");
91
92         return unless (-f $f);
93
94         ok(open(F, $f), "open");
95         my $tmp = '';
96         while(<F>) {
97                 $tmp .= $_;
98         }
99         ok(close(F), "close");
100
101         # strip mountpoint
102         $f =~ s#^\Q$mount\E/##;
103
104         ok($file_data{$f}, "$f exists");
105
106         cmp_ok(length($file_data{$f}), '==', length($tmp), "size");
107         cmp_ok($file_data{$f}, 'eq', $tmp, "content");
108 }
109
110 # small delay so that filesystem could mount
111 sleep(1);
112
113 find({ wanted => \&test_file, no_chdir => 1 }, $mount);
114
115 ok($mnt->umount,"umount");
116