Added setup_fh that handles inodes separate from open()
[dbsrgits/DBM-Deep.git] / t / 27_filehandle.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 10;
6 use Test::Exception;
7
8 use DBM::Deep;
9
10 {
11     open(FILE, "t/27_filehandle.t.db") || die("Can't open t/27_filehandle.t.db\n");
12
13     my $db;
14
15     # test if we can open and read a db using its filehandle
16
17     ok(($db = DBM::Deep->new(fh => *FILE)), "open db in filehandle");
18     ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
19     throws_ok {
20         $db->{foo} = 1;
21     } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
22     ok( !$db->exists( 'foo' ), "foo doesn't exist" );
23
24     my $db_obj = $db->_get_self;
25     ok( $db_obj->_root->{inode}, "The inode has been set" );
26
27     close(FILE);
28 }
29
30 # now the same, but with an offset into the file.  Use the database that's
31 # embedded in the test for the DATA filehandle.  First, find the database ...
32 open(FILE, "t/28_DATA.t") || die("Can't open t/28_DATA.t\n");
33 while(my $line = <FILE>) {
34     last if($line =~ /^__DATA__/);
35 }
36 my $offset = tell(FILE);
37 close(FILE);
38
39 {
40     open(FILE, "t/28_DATA.t");
41
42     my $db;
43
44     ok(($db = DBM::Deep->new(fh => *FILE, file_offset => $offset)), "open db in filehandle with offset");
45
46     ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
47
48     ok( !$db->exists( 'foo' ), "foo doesn't exist yet" );
49     throws_ok {
50         $db->{foo} = 1;
51     } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
52     ok( !$db->exists( 'foo' ), "foo doesn't exist" );
53
54     close FILE;
55 }