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