5b42189cb541ef5f35af3459e829261718d79e0f
[dbsrgits/DBM-Deep.git] / t / 23_misc.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 7;
6 use Test::Exception;
7 use File::Temp qw( tempfile tempdir );
8 use Fcntl qw( :flock );
9
10 use_ok( 'DBM::Deep' );
11
12 my $dir = tempdir( CLEANUP => 1 );
13 my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
14 flock $fh, LOCK_UN;
15 my $db = DBM::Deep->new( $filename );
16
17 $db->{key1} = "value1";
18 is( $db->{key1}, "value1", "Value set correctly" );
19
20 # Testing to verify that the close() will occur if open is called on an open DB.
21 #XXX WOW is this hacky ...
22 $db->_get_self->{engine}->open( $db->_get_self );
23 is( $db->{key1}, "value1", "Value still set after re-open" );
24
25 throws_ok {
26     my $db = DBM::Deep->new( 't' );
27 } qr/^DBM::Deep: Cannot sysopen file 't': /, "Can't open a file we aren't allowed to touch";
28
29 throws_ok {
30     my $db = DBM::Deep->new( __FILE__ );
31 } qr/^DBM::Deep: Signature not found -- file is not a Deep DB/, "Only DBM::Deep DB files will be opened";
32
33 {
34     my $db = DBM::Deep->new(
35         file => $filename,
36         locking => 1,
37     );
38     $db->_get_self->{engine}->close_fh( $db->_get_self );
39     ok( !$db->lock );
40 }
41
42 {
43     my $db = DBM::Deep->new(
44         file => $filename,
45         locking => 1,
46     );
47     $db->lock;
48     $db->_get_self->{engine}->close_fh( $db->_get_self );
49     ok( !$db->unlock );
50 }