/[fuse_dbi]/trunk/t/02sqlite.t
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/t/02sqlite.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 59 - (hide annotations)
Mon Jun 20 21:15:05 2005 UTC (18 years, 9 months ago) by dpavlin
File MIME type: application/x-troff
File size: 2604 byte(s)
added tests for write to file (update database)

1 dpavlin 11 #!/usr/bin/perl -w
2    
3     use strict;
4     use warnings;
5    
6 dpavlin 15 use Test::More;
7 dpavlin 33 use File::Find;
8 dpavlin 11 use blib;
9    
10 dpavlin 15 eval "use DBD::SQLite";
11     plan skip_all => "DBD::SQLite required for testing" if $@;
12 dpavlin 59 plan tests => 50;
13 dpavlin 15
14     use_ok('DBI');
15 dpavlin 11 use_ok('Fuse::DBI');
16    
17 dpavlin 15 my $test_db = 'fusedbi.db';
18     my $dsn = "DBI:SQLite:dbname=$test_db";
19     my $mount = '/tmp/fuse_dbi_mnt';
20    
21     ok((! -e $test_db || unlink $test_db), "test db: $test_db");
22    
23     ok((! -e $mount || rmdir $mount), "mount point $mount");
24    
25     mkdir $mount || die "mkdir $mount: $!";
26     ok(-d $mount, "mkdir $mount");
27    
28     ok(my $dbh = DBI->connect($dsn, , '', '', { RaiseError => 1 }),
29     "connect fusedbi test database");
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 dpavlin 59 ok(my $sth_insert = $dbh->prepare(qq{
39 dpavlin 15 insert into files (name,data) values (?,?)
40 dpavlin 59 }), "prepare insert");
41 dpavlin 15
42 dpavlin 59 ok(my $sth_select = $dbh->prepare(qq{
43     select data from files where name = ?
44     }), "prepare select");
45    
46 dpavlin 33 my @files = qw(file dir/file dir/subdir/file);
47     my %file_data;
48    
49     foreach my $file (@files) {
50     $file_data{$file} = ("this is test data on ".localtime()."\n") x length($file);
51 dpavlin 59 ok($sth_insert->execute($file,$file_data{$file}), "insert $file");
52 dpavlin 15 }
53    
54     my $sql_filenames = qq{
55 dpavlin 11 select
56 dpavlin 15 name as id,
57     name as filename,
58     length(data) as size,
59 dpavlin 17 1 as writable
60 dpavlin 15 from files
61 dpavlin 11 };
62    
63 dpavlin 15 my $sql_read = qq{
64     select data
65     from files
66     where name = ?;
67 dpavlin 11 };
68    
69 dpavlin 15 my $sql_update = qq{
70     update files
71     set data = ?
72     where name = ?;
73 dpavlin 11 };
74    
75 dpavlin 15 system "fusermount -q -u $mount" || diag "nothing mounted at $mount, ok";
76    
77 dpavlin 11 my $mnt = Fuse::DBI->mount({
78     filenames => $sql_filenames,
79     read => $sql_read,
80     update => $sql_update,
81 dpavlin 15 dsn => $dsn,
82     mount => $mount,
83 dpavlin 22 fork => 1,
84 dpavlin 11 });
85    
86     ok($mnt, "mount");
87    
88 dpavlin 33 sub test_file {
89     my $f = $File::Find::name;
90 dpavlin 11
91 dpavlin 33 ok($f, "file $f");
92    
93     return unless (-f $f);
94    
95 dpavlin 59 ok(open(F, $f), "open read $f");
96 dpavlin 33 my $tmp = '';
97     while(<F>) {
98     $tmp .= $_;
99     }
100     ok(close(F), "close");
101    
102     # strip mountpoint
103     $f =~ s#^\Q$mount\E/##;
104    
105     ok($file_data{$f}, "$f exists");
106    
107     cmp_ok(length($file_data{$f}), '==', length($tmp), "size");
108     cmp_ok($file_data{$f}, 'eq', $tmp, "content");
109 dpavlin 59
110     $tmp =~ tr/a-z/A-Z/;
111     $tmp .= $f;
112    
113     ok(open(F, "> $mount/$f"), "open write $mount/$f");
114     print F $tmp;
115     ok(close(F), "close");
116    
117     ok($sth_select->execute($f), "select $f");
118     cmp_ok($sth_select->fetchrow_array(), 'eq', $tmp, "updated content");
119 dpavlin 33 }
120    
121     # small delay so that filesystem could mount
122     sleep(1);
123    
124     find({ wanted => \&test_file, no_chdir => 1 }, $mount);
125    
126 dpavlin 11 ok($mnt->umount,"umount");
127 dpavlin 12
128 dpavlin 59 undef $sth_select;
129     undef $sth_insert;
130    
131     ok($dbh->disconnect, "disconnect");
132    
133 dpavlin 15 ok(unlink $test_db,"rm $test_db");
134    
135 dpavlin 48 ok(!-e $test_db,"cleanup");
136 dpavlin 59

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26