Remove temporary workaround for clear() and "0" key bug
[dbsrgits/DBM-Deep.git] / t / 02_hash.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
39baa1fd 5use Test::More tests => 53;
ffed8b01 6use Test::Exception;
fde3db1a 7use t::common qw( new_fh );
ffed8b01 8
9use_ok( 'DBM::Deep' );
10
fde3db1a 11my ($fh, $filename) = new_fh();
45f047f8 12my $db = DBM::Deep->new(
13 file => $filename,
14 fh => $fh,
15);
ffed8b01 16
17##
18# put/get key
19##
20$db->{key1} = "value1";
21is( $db->get("key1"), "value1", "get() works with hash assignment" );
22is( $db->fetch("key1"), "value1", "... fetch() works with hash assignment" );
23is( $db->{key1}, "value1", "... and hash-access also works" );
2120a181 24
ffed8b01 25$db->put("key2", undef);
26is( $db->get("key2"), undef, "get() works with put()" );
27is( $db->fetch("key2"), undef, "... fetch() works with put()" );
28is( $db->{key2}, undef, "... and hash-access also works" );
29
c11b7bfb 30$db->store( "0", "value3" );
31is( $db->get("0"), "value3", "get() works with store()" );
32is( $db->fetch("0"), "value3", "... fetch() works with put()" );
33is( $db->{0}, 'value3', "... and hash-access also works" );
ffed8b01 34
2120a181 35# Verify that the keyval pairs are still correct.
36is( $db->{key1}, "value1", "Key1 is still correct" );
37is( $db->{key2}, undef, "Key2 is still correct" );
c11b7bfb 38is( $db->{0}, 'value3', "Key3 is still correct" );
2120a181 39
ffed8b01 40ok( $db->exists("key1"), "exists() function works" );
41ok( exists $db->{key2}, "exists() works against tied hash" );
42
94e8af14 43ok( !exists $db->{key4}, "exists() function works for keys that aren't there" );
44is( $db->{key4}, undef, "Autovivified key4" );
2120a181 45ok( exists $db->{key4}, "Autovivified key4 now exists" );
46
94e8af14 47delete $db->{key4};
48ok( !exists $db->{key4}, "And key4 doesn't exists anymore" );
49
2120a181 50# Keys will be done via an iterator that keeps a breadcrumb trail of the last
51# key it provided. There will also be an "edit revision number" on the
52# reference so that resetting the iterator can be done.
53#
54# Q: How do we make sure that the iterator is unique? Is it supposed to be?
55
ffed8b01 56##
57# count keys
58##
59is( scalar keys %$db, 3, "keys() works against tied hash" );
60
61##
62# step through keys
63##
64my $temphash = {};
65while ( my ($key, $value) = each %$db ) {
ea2f6d67 66 $temphash->{$key} = $value;
ffed8b01 67}
68
69is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
70is( $temphash->{key2}, undef, "Second key copied successfully" );
c11b7bfb 71is( $temphash->{0}, 'value3', "Third key copied successfully" );
ffed8b01 72
73$temphash = {};
74my $key = $db->first_key();
c11b7bfb 75while (defined $key) {
ea2f6d67 76 $temphash->{$key} = $db->get($key);
77 $key = $db->next_key($key);
ffed8b01 78}
79
80is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
81is( $temphash->{key2}, undef, "Second key copied successfully" );
c11b7bfb 82is( $temphash->{0}, 'value3', "Third key copied successfully" );
ffed8b01 83
84##
85# delete keys
86##
8db25060 87is( delete $db->{key2}, undef, "delete through tied inteface works" );
88is( $db->delete("key1"), 'value1', "delete through OO inteface works" );
c11b7bfb 89is( $db->{0}, 'value3', "The other key is still there" );
ea2f6d67 90ok( !exists $db->{key1}, "key1 doesn't exist" );
91ok( !exists $db->{key2}, "key2 doesn't exist" );
ffed8b01 92
93is( scalar keys %$db, 1, "After deleting two keys, 1 remains" );
94
95##
96# delete all keys
97##
98ok( $db->clear(), "clear() returns true" );
99
100is( scalar keys %$db, 0, "After clear(), everything is removed" );
101
102##
103# replace key
104##
105$db->put("key1", "value1");
106is( $db->get("key1"), "value1", "Assignment still works" );
107
108$db->put("key1", "value2");
109is( $db->get("key1"), "value2", "... and replacement works" );
110
111$db->put("key1", "value222222222222222222222222");
ffed8b01 112is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" );
113
114##
115# Make sure DB still works after closing / opening
116##
117undef $db;
45f047f8 118open $fh, '+<', $filename;
119$db = DBM::Deep->new(
120 file => $filename,
121 fh => $fh,
122);
ffed8b01 123is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
124
125##
126# Make sure keys are still fetchable after replacing values
127# with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
128##
129$db->clear();
130$db->put("key1", "long value here");
131$db->put("key2", "longer value here");
132
133$db->put("key1", "short value");
134$db->put("key2", "shorter v");
135
136my $first_key = $db->first_key();
137my $next_key = $db->next_key($first_key);
138
139ok(
ea2f6d67 140 (($first_key eq "key1") || ($first_key eq "key2")) &&
141 (($next_key eq "key1") || ($next_key eq "key2")) &&
142 ($first_key ne $next_key)
ffed8b01 143 ,"keys() still works if you replace long values with shorter ones"
144);
4b603f25 145
616df1be 146# Make sure we do not trigger a deep recursion warning [RT #53575]
147{
148 my $w;
149 local $SIG{__WARN__} = sub { $w = shift };
150 my ($fh, $filename) = new_fh();
151 my $db = DBM::Deep->new( file => $filename, fh => $fh, );
152 my $h = {};
153 my $tmp = $h;
154 for(1..100) {
155 %$tmp = ("" => {});
156 $tmp = $$tmp{""};
157 }
158 ok eval {
159 $db->{""} = $h;
160 }, 'deep recursion in hash assignment' or diag $@;
161 is $w, undef, 'no warnings with deep recursion in hash assignment';
162}
163
4b603f25 164# Test autovivification
4b603f25 165$db->{unknown}{bar} = 1;
2120a181 166ok( $db->{unknown}, 'Autovivified hash exists' );
129ea236 167cmp_ok( $db->{unknown}{bar}, '==', 1, 'And the value stored is there' );
2120a181 168
169# Test failures
170throws_ok {
171 $db->fetch();
172} qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
173
174throws_ok {
175 $db->fetch(undef);
176} qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
177
178throws_ok {
179 $db->store();
180} qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
181
182throws_ok {
183 $db->store(undef, undef);
184} qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
185
186throws_ok {
187 $db->delete();
188} qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
189
190throws_ok {
191 $db->delete(undef);
192} qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
193
194throws_ok {
195 $db->exists();
196} qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";
197
198throws_ok {
199 $db->exists(undef);
200} qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";
9c87a079 201
39baa1fd 202{
203 # RT# 50541 (reported by Peter Scott)
204 # clear() leaves one key unless there's only one
205 my ($fh, $filename) = new_fh();
206 my $db = DBM::Deep->new(
207 file => $filename,
208 fh => $fh,
209 );
210
211 $db->{block} = { };
212 $db->{critical} = { };
213 $db->{minor} = { };
214
215 cmp_ok( scalar(keys( %$db )), '==', 3, "Have 3 keys" );
216
217 $db->clear;
218
219 cmp_ok( scalar(keys( %$db )), '==', 0, "clear clears everything" );
220}