Checked in the failure case for the retying
[dbsrgits/DBM-Deep.git] / t / 02_hash.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
7e2662ec 5use Test::More tests => 33;
ffed8b01 6use Test::Exception;
7
8use_ok( 'DBM::Deep' );
9
10unlink "t/test.db";
11my $db = DBM::Deep->new( "t/test.db" );
12if ($db->error()) {
13 die "ERROR: " . $db->error();
14}
15
16##
17# put/get key
18##
19$db->{key1} = "value1";
20is( $db->get("key1"), "value1", "get() works with hash assignment" );
21is( $db->fetch("key1"), "value1", "... fetch() works with hash assignment" );
22is( $db->{key1}, "value1", "... and hash-access also works" );
23
24$db->put("key2", undef);
25is( $db->get("key2"), undef, "get() works with put()" );
26is( $db->fetch("key2"), undef, "... fetch() works with put()" );
27is( $db->{key2}, undef, "... and hash-access also works" );
28
29$db->store( "key3", "value3" );
30is( $db->get("key3"), "value3", "get() works with store()" );
31is( $db->fetch("key3"), "value3", "... fetch() works with put()" );
32is( $db->{key3}, 'value3', "... and hash-access also works" );
33
34ok( $db->exists("key1"), "exists() function works" );
35ok( exists $db->{key2}, "exists() works against tied hash" );
36
37##
38# count keys
39##
40is( scalar keys %$db, 3, "keys() works against tied hash" );
41
42##
43# step through keys
44##
45my $temphash = {};
46while ( my ($key, $value) = each %$db ) {
47 $temphash->{$key} = $value;
48}
49
50is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
51is( $temphash->{key2}, undef, "Second key copied successfully" );
52is( $temphash->{key3}, 'value3', "Third key copied successfully" );
53
54$temphash = {};
55my $key = $db->first_key();
56while ($key) {
57 $temphash->{$key} = $db->get($key);
58 $key = $db->next_key($key);
59}
60
61is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
62is( $temphash->{key2}, undef, "Second key copied successfully" );
63is( $temphash->{key3}, 'value3', "Third key copied successfully" );
64
65##
66# delete keys
67##
81d3d316 68is( delete $db->{key1}, 'value1', "delete through tied inteface works" );
69is( $db->delete("key2"), undef, "delete through OO inteface works" );
ffed8b01 70
71is( scalar keys %$db, 1, "After deleting two keys, 1 remains" );
72
73##
74# delete all keys
75##
76ok( $db->clear(), "clear() returns true" );
77
78is( scalar keys %$db, 0, "After clear(), everything is removed" );
79
80##
81# replace key
82##
83$db->put("key1", "value1");
84is( $db->get("key1"), "value1", "Assignment still works" );
85
86$db->put("key1", "value2");
87is( $db->get("key1"), "value2", "... and replacement works" );
88
89$db->put("key1", "value222222222222222222222222");
90
91is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" );
92
93##
94# Make sure DB still works after closing / opening
95##
96undef $db;
97$db = DBM::Deep->new( "t/test.db" );
98if ($db->error()) {
99 die "ERROR: " . $db->error();
100}
101is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
102
103##
104# Make sure keys are still fetchable after replacing values
105# with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
106##
107$db->clear();
108$db->put("key1", "long value here");
109$db->put("key2", "longer value here");
110
111$db->put("key1", "short value");
112$db->put("key2", "shorter v");
113
114my $first_key = $db->first_key();
115my $next_key = $db->next_key($first_key);
116
117ok(
118 (($first_key eq "key1") || ($first_key eq "key2")) &&
119 (($next_key eq "key1") || ($next_key eq "key2")) &&
120 ($first_key ne $next_key)
121 ,"keys() still works if you replace long values with shorter ones"
122);
7e2662ec 123
124my %hash = ( a => 1 );
125$db->{hash} = \%hash;
126$hash{b} = 2;
127cmp_ok( $db->{hash}{b}, '==', 2 );
128
129# Test autovivification
130
131$db->{unknown}{bar} = 1;
132ok( $db->{unknown} );
133cmp_ok( $db->{unknown}{bar}, '==', 1 );
134
135$db->clear;
136$db->{foo}->{bar} = 'baz';
137is( $db->{foo}{bar}, 'baz' );