Added missing files to the MANIFEST
[dbsrgits/DBM-Deep.git] / t / 02_hash.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
4b603f25 5use Test::More tests => 32;
ffed8b01 6use Test::Exception;
2a81bf9e 7use File::Temp qw( tempfile tempdir );
58910373 8use Fcntl qw( :flock );
ffed8b01 9
10use_ok( 'DBM::Deep' );
11
2a81bf9e 12my $dir = tempdir( CLEANUP => 1 );
13my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
58910373 14flock $fh, LOCK_UN;
2a81bf9e 15my $db = DBM::Deep->new( $filename );
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" );
24
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
30$db->store( "key3", "value3" );
31is( $db->get("key3"), "value3", "get() works with store()" );
32is( $db->fetch("key3"), "value3", "... fetch() works with put()" );
33is( $db->{key3}, 'value3', "... and hash-access also works" );
34
35ok( $db->exists("key1"), "exists() function works" );
36ok( exists $db->{key2}, "exists() works against tied hash" );
37
38##
39# count keys
40##
41is( scalar keys %$db, 3, "keys() works against tied hash" );
42
43##
44# step through keys
45##
46my $temphash = {};
47while ( my ($key, $value) = each %$db ) {
48 $temphash->{$key} = $value;
49}
50
51is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
52is( $temphash->{key2}, undef, "Second key copied successfully" );
53is( $temphash->{key3}, 'value3', "Third key copied successfully" );
54
55$temphash = {};
56my $key = $db->first_key();
57while ($key) {
58 $temphash->{$key} = $db->get($key);
59 $key = $db->next_key($key);
60}
61
62is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
63is( $temphash->{key2}, undef, "Second key copied successfully" );
64is( $temphash->{key3}, 'value3', "Third key copied successfully" );
65
66##
67# delete keys
68##
8db25060 69is( delete $db->{key2}, undef, "delete through tied inteface works" );
70is( $db->delete("key1"), 'value1', "delete through OO inteface works" );
71is( $db->{key3}, 'value3', "The other key is still there" );
ffed8b01 72
73is( scalar keys %$db, 1, "After deleting two keys, 1 remains" );
74
75##
76# delete all keys
77##
78ok( $db->clear(), "clear() returns true" );
79
80is( scalar keys %$db, 0, "After clear(), everything is removed" );
81
82##
83# replace key
84##
85$db->put("key1", "value1");
86is( $db->get("key1"), "value1", "Assignment still works" );
87
88$db->put("key1", "value2");
89is( $db->get("key1"), "value2", "... and replacement works" );
90
91$db->put("key1", "value222222222222222222222222");
92
93is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" );
94
95##
96# Make sure DB still works after closing / opening
97##
98undef $db;
2a81bf9e 99$db = DBM::Deep->new( $filename );
ffed8b01 100is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
101
102##
103# Make sure keys are still fetchable after replacing values
104# with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
105##
106$db->clear();
107$db->put("key1", "long value here");
108$db->put("key2", "longer value here");
109
110$db->put("key1", "short value");
111$db->put("key2", "shorter v");
112
113my $first_key = $db->first_key();
114my $next_key = $db->next_key($first_key);
115
116ok(
117 (($first_key eq "key1") || ($first_key eq "key2")) &&
118 (($next_key eq "key1") || ($next_key eq "key2")) &&
119 ($first_key ne $next_key)
120 ,"keys() still works if you replace long values with shorter ones"
121);
4b603f25 122
123# Test autovivification
124
125$db->{unknown}{bar} = 1;
126ok( $db->{unknown} );
127cmp_ok( $db->{unknown}{bar}, '==', 1 );