Converted more to use _request_space() ... still more to go
[dbsrgits/DBM-Deep.git] / t / 02_hash.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
8db25060 5use Test::More tests => 30;
ffed8b01 6use Test::Exception;
2a81bf9e 7use File::Temp qw( tempfile tempdir );
ffed8b01 8
9use_ok( 'DBM::Deep' );
10
2a81bf9e 11my $dir = tempdir( CLEANUP => 1 );
12my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
13my $db = DBM::Deep->new( $filename );
ffed8b01 14
15##
16# put/get key
17##
18$db->{key1} = "value1";
19is( $db->get("key1"), "value1", "get() works with hash assignment" );
20is( $db->fetch("key1"), "value1", "... fetch() works with hash assignment" );
21is( $db->{key1}, "value1", "... and hash-access also works" );
22
23$db->put("key2", undef);
24is( $db->get("key2"), undef, "get() works with put()" );
25is( $db->fetch("key2"), undef, "... fetch() works with put()" );
26is( $db->{key2}, undef, "... and hash-access also works" );
27
28$db->store( "key3", "value3" );
29is( $db->get("key3"), "value3", "get() works with store()" );
30is( $db->fetch("key3"), "value3", "... fetch() works with put()" );
31is( $db->{key3}, 'value3', "... and hash-access also works" );
32
33ok( $db->exists("key1"), "exists() function works" );
34ok( exists $db->{key2}, "exists() works against tied hash" );
35
36##
37# count keys
38##
39is( scalar keys %$db, 3, "keys() works against tied hash" );
40
41##
42# step through keys
43##
44my $temphash = {};
45while ( my ($key, $value) = each %$db ) {
46 $temphash->{$key} = $value;
47}
48
49is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
50is( $temphash->{key2}, undef, "Second key copied successfully" );
51is( $temphash->{key3}, 'value3', "Third key copied successfully" );
52
53$temphash = {};
54my $key = $db->first_key();
55while ($key) {
56 $temphash->{$key} = $db->get($key);
57 $key = $db->next_key($key);
58}
59
60is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
61is( $temphash->{key2}, undef, "Second key copied successfully" );
62is( $temphash->{key3}, 'value3', "Third key copied successfully" );
63
64##
65# delete keys
66##
8db25060 67is( delete $db->{key2}, undef, "delete through tied inteface works" );
68is( $db->delete("key1"), 'value1', "delete through OO inteface works" );
69is( $db->{key3}, 'value3', "The other key is still there" );
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;
2a81bf9e 97$db = DBM::Deep->new( $filename );
ffed8b01 98is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
99
100##
101# Make sure keys are still fetchable after replacing values
102# with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
103##
104$db->clear();
105$db->put("key1", "long value here");
106$db->put("key2", "longer value here");
107
108$db->put("key1", "short value");
109$db->put("key2", "shorter v");
110
111my $first_key = $db->first_key();
112my $next_key = $db->next_key($first_key);
113
114ok(
115 (($first_key eq "key1") || ($first_key eq "key2")) &&
116 (($next_key eq "key1") || ($next_key eq "key2")) &&
117 ($first_key ne $next_key)
118 ,"keys() still works if you replace long values with shorter ones"
119);