Commit | Line | Data |
714618f0 |
1 | ## |
2 | # DBM::Deep Test |
3 | ## |
4 | use strict; |
2a81bf9e |
5 | use Test::More tests => 11; |
acd4faf2 |
6 | use Test::Exception; |
2a81bf9e |
7 | use File::Temp qw( tempfile tempdir ); |
714618f0 |
8 | |
2a81bf9e |
9 | use_ok( 'DBM::Deep' ); |
10 | |
11 | my $dir = tempdir( CLEANUP => 1 ); |
12 | my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); |
13 | |
14 | # Create the datafile to be used |
15 | { |
16 | my $db = DBM::Deep->new( $filename ); |
17 | $db->{hash} = { foo => [ 'a' .. 'c' ] }; |
18 | } |
714618f0 |
19 | |
70b55428 |
20 | { |
2a81bf9e |
21 | open(FILE, $filename) || die("Can't open '$filename' for reading: $!\n"); |
714618f0 |
22 | |
70b55428 |
23 | my $db; |
714618f0 |
24 | |
70b55428 |
25 | # test if we can open and read a db using its filehandle |
714618f0 |
26 | |
70b55428 |
27 | ok(($db = DBM::Deep->new(fh => *FILE)), "open db in filehandle"); |
28 | ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database"); |
29 | throws_ok { |
30 | $db->{foo} = 1; |
e06824f8 |
31 | } qr/Cannot write to a readonly filehandle/, |
32 | "Can't write to a read-only filehandle"; |
70b55428 |
33 | ok( !$db->exists( 'foo' ), "foo doesn't exist" ); |
714618f0 |
34 | |
70b55428 |
35 | my $db_obj = $db->_get_self; |
36 | ok( $db_obj->_root->{inode}, "The inode has been set" ); |
37 | |
38 | close(FILE); |
39 | } |
714618f0 |
40 | |
41 | # now the same, but with an offset into the file. Use the database that's |
42 | # embedded in the test for the DATA filehandle. First, find the database ... |
43 | open(FILE, "t/28_DATA.t") || die("Can't open t/28_DATA.t\n"); |
44 | while(my $line = <FILE>) { |
45 | last if($line =~ /^__DATA__/); |
46 | } |
47 | my $offset = tell(FILE); |
48 | close(FILE); |
49 | |
6ed2f3df |
50 | SKIP: { |
51 | skip "File format changed ... gah!", 5; |
70b55428 |
52 | open(FILE, "t/28_DATA.t"); |
53 | |
54 | my $db; |
714618f0 |
55 | |
70b55428 |
56 | ok(($db = DBM::Deep->new(fh => *FILE, file_offset => $offset)), "open db in filehandle with offset"); |
57 | |
58 | ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database"); |
59 | |
60 | ok( !$db->exists( 'foo' ), "foo doesn't exist yet" ); |
61 | throws_ok { |
62 | $db->{foo} = 1; |
63 | } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle"; |
64 | ok( !$db->exists( 'foo' ), "foo doesn't exist" ); |
65 | |
66 | close FILE; |
67 | } |