Renamings
[dbsrgits/DBM-Deep.git] / t / 02_hash.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 44;
6 use Test::Exception;
7
8 use_ok( 'DBM::Deep' );
9
10 unlink "t/test.db";
11 my $db = DBM::Deep->new( "t/test.db" );
12 if ($db->error()) {
13         die "ERROR: " . $db->error();
14 }
15
16 ##
17 # put/get key
18 ##
19 $db->{key1} = "value1";
20 is( $db->get("key1"), "value1", "get() works with hash assignment" );
21 is( $db->fetch("key1"), "value1", "... fetch() works with hash assignment" );
22 is( $db->{key1}, "value1", "... and hash-access also works" );
23
24 $db->put("key2", undef);
25 is( $db->get("key2"), undef, "get() works with put()" );
26 is( $db->fetch("key2"), undef, "... fetch() works with put()" );
27 is( $db->{key2}, undef, "... and hash-access also works" );
28
29 $db->store( "key3", "value3" );
30 is( $db->get("key3"), "value3", "get() works with store()" );
31 is( $db->fetch("key3"), "value3", "... fetch() works with put()" );
32 is( $db->{key3}, 'value3', "... and hash-access also works" );
33
34 ok( $db->exists("key1"), "exists() function works" );
35 ok( exists $db->{key2}, "exists() works against tied hash" );
36
37 ##
38 # count keys
39 ##
40 is( scalar keys %$db, 3, "keys() works against tied hash" );
41
42 ##
43 # step through keys
44 ##
45 my $temphash = {};
46 while ( my ($key, $value) = each %$db ) {
47         $temphash->{$key} = $value;
48 }
49
50 is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
51 is( $temphash->{key2}, undef, "Second key copied successfully" );
52 is( $temphash->{key3}, 'value3', "Third key copied successfully" );
53
54 $temphash = {};
55 my $key = $db->first_key();
56 while ($key) {
57         $temphash->{$key} = $db->get($key);
58         $key = $db->next_key($key);
59 }
60
61 is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
62 is( $temphash->{key2}, undef, "Second key copied successfully" );
63 is( $temphash->{key3}, 'value3', "Third key copied successfully" );
64
65 ##
66 # delete keys
67 ##
68 TODO: {
69     local $TODO = "Delete should return the deleted value";
70     is( delete $db->{key1}, 'value1', "delete through tied inteface works" );
71     is( $db->delete("key2"), undef, "delete through OO inteface works" );
72 }
73
74 is( scalar keys %$db, 1, "After deleting two keys, 1 remains" );
75
76 ##
77 # delete all keys
78 ##
79 ok( $db->clear(), "clear() returns true" );
80
81 is( scalar keys %$db, 0, "After clear(), everything is removed" );
82
83 ##
84 # replace key
85 ##
86 $db->put("key1", "value1");
87 is( $db->get("key1"), "value1", "Assignment still works" );
88
89 $db->put("key1", "value2");
90 is( $db->get("key1"), "value2", "... and replacement works" );
91
92 $db->put("key1", "value222222222222222222222222");
93
94 is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" );
95
96 ##
97 # Make sure DB still works after closing / opening
98 ##
99 undef $db;
100 $db = DBM::Deep->new( "t/test.db" );
101 if ($db->error()) {
102         die "ERROR: " . $db->error();
103 }
104 is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
105
106 ##
107 # Make sure keys are still fetchable after replacing values
108 # with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
109 ##
110 $db->clear();
111 $db->put("key1", "long value here");
112 $db->put("key2", "longer value here");
113
114 $db->put("key1", "short value");
115 $db->put("key2", "shorter v");
116
117 my $first_key = $db->first_key();
118 my $next_key = $db->next_key($first_key);
119
120 ok(
121         (($first_key eq "key1") || ($first_key eq "key2")) && 
122         (($next_key eq "key1") || ($next_key eq "key2")) && 
123         ($first_key ne $next_key)
124     ,"keys() still works if you replace long values with shorter ones"
125 );
126
127 # These tests verify that the array methods cannot be called on hashtypes.
128 # They will be removed once the ARRAY and HASH types are refactored into their own classes.
129
130 throws_ok {
131     $db->splice();
132 } qr/SPLICE method only supported for arrays/, "Cannot call splice on a hash type";
133
134 throws_ok {
135     $db->SPLICE();
136 } qr/SPLICE method only supported for arrays/, "Cannot call SPLICE on a hash type";
137
138 throws_ok {
139     $db->length();
140 } qr/FETCHSIZE method only supported for arrays/, "Cannot call length on a hash type";
141
142 throws_ok {
143     $db->FETCHSIZE();
144 } qr/FETCHSIZE method only supported for arrays/, "Cannot call FETCHSIZE on a hash type";
145
146 throws_ok {
147     $db->STORESIZE();
148 } qr/STORESIZE method only supported for arrays/, "Cannot call STORESIZE on a hash type";
149
150 throws_ok {
151     $db->POP();
152 } qr/POP method only supported for arrays/, "Cannot call POP on a hash type";
153
154 throws_ok {
155     $db->pop();
156 } qr/POP method only supported for arrays/, "Cannot call pop on a hash type";
157
158 throws_ok {
159     $db->PUSH();
160 } qr/PUSH method only supported for arrays/, "Cannot call PUSH on a hash type";
161
162 throws_ok {
163     $db->push();
164 } qr/PUSH method only supported for arrays/, "Cannot call push on a hash type";
165
166 throws_ok {
167     $db->SHIFT();
168 } qr/SHIFT method only supported for arrays/, "Cannot call SHIFT on a hash type";
169
170 throws_ok {
171     $db->shift();
172 } qr/SHIFT method only supported for arrays/, "Cannot call shift on a hash type";
173
174 throws_ok {
175     $db->UNSHIFT();
176 } qr/UNSHIFT method only supported for arrays/, "Cannot call UNSHIFT on a hash type";
177
178 throws_ok {
179     $db->unshift();
180 } qr/UNSHIFT method only supported for arrays/, "Cannot call unshift on a hash type";
181
182 ok( $db->error, "We have an error ..." );
183 $db->clear_error();
184 ok( !$db->error(), "... and we cleared the error" );