a317fa39e919dc0e6bcbefa330812c52b8086732
[dbsrgits/DBM-Deep.git] / t / 02_hash.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More;
5 use Test::Exception;
6 use t::common qw( new_dbm );
7
8 use_ok( 'DBM::Deep' );
9
10 my $dbm_factory = new_dbm();
11 while ( my $dbm_maker = $dbm_factory->() ) {
12     my $db = $dbm_maker->();
13
14     ##
15     # put/get key
16     ##
17     $db->{key1} = "value1";
18     is( $db->get("key1"), "value1", "get() works with hash assignment" );
19     is( $db->fetch("key1"), "value1", "... fetch() works with hash assignment" );
20     is( $db->{key1}, "value1", "... and hash-access also works" );
21
22     $db->put("key2", undef);
23     is( $db->get("key2"), undef, "get() works with put()" );
24     is( $db->fetch("key2"), undef, "... fetch() works with put()" );
25     is( $db->{key2}, undef, "... and hash-access also works" );
26
27     $db->store( "key3", "value3" );
28     is( $db->get("key3"), "value3", "get() works with store()" );
29     is( $db->fetch("key3"), "value3", "... fetch() works with put()" );
30     is( $db->{key3}, 'value3', "... and hash-access also works" );
31
32     # Verify that the keyval pairs are still correct.
33     is( $db->{key1}, "value1", "Key1 is still correct" );
34     is( $db->{key2}, undef, "Key2 is still correct" );
35     is( $db->{key3}, 'value3', "Key3 is still correct" );
36
37     ok( $db->exists("key1"), "exists() function works" );
38     ok( exists $db->{key2}, "exists() works against tied hash" );
39
40     ok( !exists $db->{key4}, "exists() function works for keys that aren't there" );
41     is( $db->{key4}, undef, "Autovivified key4" );
42     ok( exists $db->{key4}, "Autovivified key4 now exists" );
43
44     delete $db->{key4};
45     ok( !exists $db->{key4}, "And key4 doesn't exists anymore" );
46
47     # Keys will be done via an iterator that keeps a breadcrumb trail of the last
48     # key it provided. There will also be an "edit revision number" on the
49     # reference so that resetting the iterator can be done.
50     #
51     # Q: How do we make sure that the iterator is unique? Is it supposed to be?
52
53     ##
54     # count keys
55     ##
56     is( scalar keys %$db, 3, "keys() works against tied hash" );
57
58     ##
59     # step through keys
60     ##
61     my $temphash = {};
62     while ( my ($key, $value) = each %$db ) {
63         $temphash->{$key} = $value;
64     }
65
66     is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
67     is( $temphash->{key2}, undef, "Second key copied successfully" );
68     is( $temphash->{key3}, 'value3', "Third key copied successfully" );
69
70     $temphash = {};
71     my $key = $db->first_key();
72     while ($key) {
73         $temphash->{$key} = $db->get($key);
74         $key = $db->next_key($key);
75     }
76
77     is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
78     is( $temphash->{key2}, undef, "Second key copied successfully" );
79     is( $temphash->{key3}, 'value3', "Third key copied successfully" );
80
81     ##
82     # delete keys
83     ##
84     is( delete $db->{key2}, undef, "delete through tied inteface works" );
85     is( $db->delete("key1"), 'value1', "delete through OO inteface works" );
86     is( $db->{key3}, 'value3', "The other key is still there" );
87     ok( !exists $db->{key1}, "key1 doesn't exist" );
88     ok( !exists $db->{key2}, "key2 doesn't exist" );
89
90     is( scalar keys %$db, 1, "After deleting two keys, 1 remains" );
91
92     ##
93     # delete all keys
94     ##
95     ok( $db->clear(), "clear() returns true" );
96
97     is( scalar keys %$db, 0, "After clear(), everything is removed" );
98
99     ##
100     # replace key
101     ##
102     $db->put("key1", "value1");
103     is( $db->get("key1"), "value1", "Assignment still works" );
104
105     $db->put("key1", "value2");
106     is( $db->get("key1"), "value2", "... and replacement works" );
107
108     $db->put("key1", "value222222222222222222222222");
109     is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" );
110
111     ##
112     # Make sure DB still works after closing / opening
113     ##
114     undef $db;
115     $db = $dbm_maker->();
116     is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
117
118     ##
119     # Make sure keys are still fetchable after replacing values
120     # with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
121     ##
122     $db->clear();
123     $db->put("key1", "long value here");
124     $db->put("key2", "longer value here");
125
126     $db->put("key1", "short value");
127     $db->put("key2", "shorter v");
128
129     my $first_key = $db->first_key();
130     my $next_key = $db->next_key($first_key);
131
132     ok(
133         (($first_key eq "key1") || ($first_key eq "key2")) && 
134         (($next_key eq "key1") || ($next_key eq "key2")) && 
135         ($first_key ne $next_key)
136         ,"keys() still works if you replace long values with shorter ones"
137     );
138
139     # Test autovivification
140     $db->{unknown}{bar} = 1;
141     ok( $db->{unknown}, 'Autovivified hash exists' );
142     cmp_ok( $db->{unknown}{bar}, '==', 1, 'And the value stored is there' );
143
144     # Test failures
145     throws_ok {
146         $db->fetch();
147     } qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
148
149     throws_ok {
150         $db->fetch(undef);
151     } qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
152
153     throws_ok {
154         $db->store();
155     } qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
156
157     throws_ok {
158         $db->store(undef, undef);
159     } qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
160
161     throws_ok {
162         $db->delete();
163     } qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
164
165     throws_ok {
166         $db->delete(undef);
167     } qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
168
169     throws_ok {
170         $db->exists();
171     } qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";
172
173     throws_ok {
174         $db->exists(undef);
175     } qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";
176 }
177
178 done_testing;