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