Commit | Line | Data |
ffed8b01 |
1 | ## |
2 | # DBM::Deep Test |
3 | ## |
4 | use strict; |
6fe26b29 |
5 | use Test::More tests => 29; |
ffed8b01 |
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" ); |
ffed8b01 |
12 | |
13 | ## |
14 | # put/get key |
15 | ## |
16 | $db->{key1} = "value1"; |
17 | is( $db->get("key1"), "value1", "get() works with hash assignment" ); |
18 | is( $db->fetch("key1"), "value1", "... fetch() works with hash assignment" ); |
19 | is( $db->{key1}, "value1", "... and hash-access also works" ); |
20 | |
21 | $db->put("key2", undef); |
22 | is( $db->get("key2"), undef, "get() works with put()" ); |
23 | is( $db->fetch("key2"), undef, "... fetch() works with put()" ); |
24 | is( $db->{key2}, undef, "... and hash-access also works" ); |
25 | |
26 | $db->store( "key3", "value3" ); |
27 | is( $db->get("key3"), "value3", "get() works with store()" ); |
28 | is( $db->fetch("key3"), "value3", "... fetch() works with put()" ); |
29 | is( $db->{key3}, 'value3', "... and hash-access also works" ); |
30 | |
31 | ok( $db->exists("key1"), "exists() function works" ); |
32 | ok( exists $db->{key2}, "exists() works against tied hash" ); |
33 | |
34 | ## |
35 | # count keys |
36 | ## |
37 | is( scalar keys %$db, 3, "keys() works against tied hash" ); |
38 | |
39 | ## |
40 | # step through keys |
41 | ## |
42 | my $temphash = {}; |
43 | while ( my ($key, $value) = each %$db ) { |
44 | $temphash->{$key} = $value; |
45 | } |
46 | |
47 | is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" ); |
48 | is( $temphash->{key2}, undef, "Second key copied successfully" ); |
49 | is( $temphash->{key3}, 'value3', "Third key copied successfully" ); |
50 | |
51 | $temphash = {}; |
52 | my $key = $db->first_key(); |
53 | while ($key) { |
54 | $temphash->{$key} = $db->get($key); |
55 | $key = $db->next_key($key); |
56 | } |
57 | |
58 | is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" ); |
59 | is( $temphash->{key2}, undef, "Second key copied successfully" ); |
60 | is( $temphash->{key3}, 'value3', "Third key copied successfully" ); |
61 | |
62 | ## |
63 | # delete keys |
64 | ## |
81d3d316 |
65 | is( delete $db->{key1}, 'value1', "delete through tied inteface works" ); |
66 | is( $db->delete("key2"), undef, "delete through OO inteface works" ); |
ffed8b01 |
67 | |
68 | is( scalar keys %$db, 1, "After deleting two keys, 1 remains" ); |
69 | |
70 | ## |
71 | # delete all keys |
72 | ## |
73 | ok( $db->clear(), "clear() returns true" ); |
74 | |
75 | is( scalar keys %$db, 0, "After clear(), everything is removed" ); |
76 | |
77 | ## |
78 | # replace key |
79 | ## |
80 | $db->put("key1", "value1"); |
81 | is( $db->get("key1"), "value1", "Assignment still works" ); |
82 | |
83 | $db->put("key1", "value2"); |
84 | is( $db->get("key1"), "value2", "... and replacement works" ); |
85 | |
86 | $db->put("key1", "value222222222222222222222222"); |
87 | |
88 | is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" ); |
89 | |
90 | ## |
91 | # Make sure DB still works after closing / opening |
92 | ## |
93 | undef $db; |
94 | $db = DBM::Deep->new( "t/test.db" ); |
ffed8b01 |
95 | is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" ); |
96 | |
97 | ## |
98 | # Make sure keys are still fetchable after replacing values |
99 | # with smaller ones (bug found by John Cardenas, DBM::Deep 0.93) |
100 | ## |
101 | $db->clear(); |
102 | $db->put("key1", "long value here"); |
103 | $db->put("key2", "longer value here"); |
104 | |
105 | $db->put("key1", "short value"); |
106 | $db->put("key2", "shorter v"); |
107 | |
108 | my $first_key = $db->first_key(); |
109 | my $next_key = $db->next_key($first_key); |
110 | |
111 | ok( |
112 | (($first_key eq "key1") || ($first_key eq "key2")) && |
113 | (($next_key eq "key1") || ($next_key eq "key2")) && |
114 | ($first_key ne $next_key) |
115 | ,"keys() still works if you replace long values with shorter ones" |
116 | ); |