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