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