5 use Test::More tests => 30;
7 use File::Temp qw( tempfile tempdir );
8 use Fcntl qw( :flock );
10 use_ok( 'DBM::Deep' );
12 my $dir = tempdir( CLEANUP => 1 );
13 my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
15 my $db = DBM::Deep->new( $filename );
20 $db->{key1} = "value1";
21 is( $db->get("key1"), "value1", "get() works with hash assignment" );
22 is( $db->fetch("key1"), "value1", "... fetch() works with hash assignment" );
23 is( $db->{key1}, "value1", "... and hash-access also works" );
25 $db->put("key2", undef);
26 is( $db->get("key2"), undef, "get() works with put()" );
27 is( $db->fetch("key2"), undef, "... fetch() works with put()" );
28 is( $db->{key2}, undef, "... and hash-access also works" );
30 $db->store( "key3", "value3" );
31 is( $db->get("key3"), "value3", "get() works with store()" );
32 is( $db->fetch("key3"), "value3", "... fetch() works with put()" );
33 is( $db->{key3}, 'value3', "... and hash-access also works" );
35 ok( $db->exists("key1"), "exists() function works" );
36 ok( exists $db->{key2}, "exists() works against tied hash" );
41 is( scalar keys %$db, 3, "keys() works against tied hash" );
47 while ( my ($key, $value) = each %$db ) {
48 $temphash->{$key} = $value;
51 is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
52 is( $temphash->{key2}, undef, "Second key copied successfully" );
53 is( $temphash->{key3}, 'value3', "Third key copied successfully" );
56 my $key = $db->first_key();
58 $temphash->{$key} = $db->get($key);
59 $key = $db->next_key($key);
62 is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
63 is( $temphash->{key2}, undef, "Second key copied successfully" );
64 is( $temphash->{key3}, 'value3', "Third key copied successfully" );
69 is( delete $db->{key2}, undef, "delete through tied inteface works" );
70 is( $db->delete("key1"), 'value1', "delete through OO inteface works" );
71 is( $db->{key3}, 'value3', "The other key is still there" );
73 is( scalar keys %$db, 1, "After deleting two keys, 1 remains" );
78 ok( $db->clear(), "clear() returns true" );
80 is( scalar keys %$db, 0, "After clear(), everything is removed" );
85 $db->put("key1", "value1");
86 is( $db->get("key1"), "value1", "Assignment still works" );
88 $db->put("key1", "value2");
89 is( $db->get("key1"), "value2", "... and replacement works" );
91 $db->put("key1", "value222222222222222222222222");
93 is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" );
96 # Make sure DB still works after closing / opening
99 $db = DBM::Deep->new( $filename );
100 is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
103 # Make sure keys are still fetchable after replacing values
104 # with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
107 $db->put("key1", "long value here");
108 $db->put("key2", "longer value here");
110 $db->put("key1", "short value");
111 $db->put("key2", "shorter v");
113 my $first_key = $db->first_key();
114 my $next_key = $db->next_key($first_key);
117 (($first_key eq "key1") || ($first_key eq "key2")) &&
118 (($next_key eq "key1") || ($next_key eq "key2")) &&
119 ($first_key ne $next_key)
120 ,"keys() still works if you replace long values with shorter ones"