5 use Test::More tests => 49;
7 use t::common qw( new_fh );
11 my ($fh, $filename) = new_fh();
12 my $db = DBM::Deep->new(
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 # Verify that the keyval pairs are still correct.
36 is( $db->{key1}, "value1", "Key1 is still correct" );
37 is( $db->{key2}, undef, "Key2 is still correct" );
38 is( $db->{key3}, 'value3', "Key3 is still correct" );
40 ok( $db->exists("key1"), "exists() function works" );
41 ok( exists $db->{key2}, "exists() works against tied hash" );
43 ok( !exists $db->{key4}, "exists() function works for keys that aren't there" );
44 is( $db->{key4}, undef, "Autovivified key4" );
45 ok( exists $db->{key4}, "Autovivified key4 now exists" );
49 ok( !exists $db->{key4}, "And key4 doesn't exists anymore" );
51 # Keys will be done via an iterator that keeps a breadcrumb trail of the last
52 # key it provided. There will also be an "edit revision number" on the
53 # reference so that resetting the iterator can be done.
55 # Q: How do we make sure that the iterator is unique? Is it supposed to be?
60 is( scalar keys %$db, 3, "keys() works against tied hash" );
66 while ( my ($key, $value) = each %$db ) {
67 $temphash->{$key} = $value;
70 is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
71 is( $temphash->{key2}, undef, "Second key copied successfully" );
72 is( $temphash->{key3}, 'value3', "Third key copied successfully" );
75 my $key = $db->first_key();
77 $temphash->{$key} = $db->get($key);
78 $key = $db->next_key($key);
81 is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
82 is( $temphash->{key2}, undef, "Second key copied successfully" );
83 is( $temphash->{key3}, 'value3', "Third key copied successfully" );
88 is( delete $db->{key2}, undef, "delete through tied inteface works" );
89 is( $db->delete("key1"), 'value1', "delete through OO inteface works" );
90 is( $db->{key3}, 'value3', "The other key is still there" );
91 ok( !exists $db->{key1}, "key1 doesn't exist" );
92 ok( !exists $db->{key2}, "key2 doesn't exist" );
94 is( scalar keys %$db, 1, "After deleting two keys, 1 remains" );
99 ok( $db->clear(), "clear() returns true" );
101 is( scalar keys %$db, 0, "After clear(), everything is removed" );
106 $db->put("key1", "value1");
107 is( $db->get("key1"), "value1", "Assignment still works" );
109 $db->put("key1", "value2");
110 is( $db->get("key1"), "value2", "... and replacement works" );
112 $db->put("key1", "value222222222222222222222222");
113 is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" );
116 # Make sure DB still works after closing / opening
119 open $fh, '+<', $filename;
120 $db = DBM::Deep->new(
124 is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
127 # Make sure keys are still fetchable after replacing values
128 # with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
131 $db->put("key1", "long value here");
132 $db->put("key2", "longer value here");
134 $db->put("key1", "short value");
135 $db->put("key2", "shorter v");
137 my $first_key = $db->first_key();
138 my $next_key = $db->next_key($first_key);
141 (($first_key eq "key1") || ($first_key eq "key2")) &&
142 (($next_key eq "key1") || ($next_key eq "key2")) &&
143 ($first_key ne $next_key)
144 ,"keys() still works if you replace long values with shorter ones"
147 # Test autovivification
148 $db->{unknown}{bar} = 1;
149 ok( $db->{unknown}, 'Autovivified hash exists' );
150 cmp_ok( $db->{unknown}{bar}, '==', 1, 'And the value stored is there' );
155 } qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
159 } qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
163 } qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
166 $db->store(undef, undef);
167 } qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
171 } qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
175 } qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
179 } qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";
183 } qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";