Debian Codegraph @

t/02-named.t

raw
1 #!perl
2 use Test::More tests => 50;
3 use Test::Exception;
4 use strict;
5 use warnings;
6 use utf8;
7
8 use File::Temp;
9
10 use LMDB_File qw(:flags :cursor_op);
11
12 my $dir = File::Temp->newdir('mdbtXXXX', TMPDIR => 1, EXLOCK => 0);
13 ok(-d $dir, "Created test dir $dir");
14 my $env = LMDB::Env->new($dir, { maxdbs => 5 });
15 {
16 is($env->BeginTxn->OpenDB->stat->{entries}, 0, 'Empty');
17 }
18 {
19 my $txn = $env->BeginTxn;
20 my $mdb = $txn->OpenDB;
21 ok($mdb, "Main DB Opened");
22 is($mdb->stat->{entries}, 0, 'Empty');
23 throws_ok {
24 $txn->OpenDB('SOME');
25 } qr/NOTFOUND/, 'No created yet';
26 my $DB = $txn->OpenDB('SOME', MDB_CREATE);
27 ok($DB, "SOME DB Opened");
28 is($mdb->stat->{entries}, 1, 'Created');
29 }
30 {
31 my $txn = $env->BeginTxn;
32 is($txn->OpenDB->stat->{entries}, 0, 'Empty');
33 throws_ok {
34 $txn->OpenDB('SOME');
35 } qr/NOTFOUND/, 'No preserved';
36 }
37 {
38 my $txn = $env->BeginTxn;
39 $txn->AutoCommit(1);
40 ok(my $odb = $txn->OpenDB({dbname => 'ONE', flags => MDB_CREATE}), 'ONE Created');
41 is($odb->[1], 2, 'First One');
42 $odb->put(Test => 'Hello World');
43 $odb->put(Test2 => 'A simple string');
44 is($odb->stat->{entries}, 2, 'In there');
45 }
46 {
47 my $txn = $env->BeginTxn;
48 ok(my $db = $txn->OpenDB({dbname => 'TWO', flags => MDB_CREATE}), 'TWO Created');
49 is($db->[1], 3, 'Second One');
50 is($db->stat->{entries}, 0, 'Empty');
51 ok(!$db->get('Test'), "No in this");
52 }
53 {
54 my $txn = $env->BeginTxn;
55 ok(my $odb = $txn->OpenDB('ONE'), 'Preserved');
56 is($odb->stat->{entries}, 2, "With 2 keys");
57 is($odb->get('Test'), 'Hello World', 'In there');
58 is(LMDB_File->open($txn)->stat->{entries}, 1, 'ONE DB');
59 throws_ok {
60 $odb->open('TWO');
61 } qr/NOTFOUND/, 'NO TWO DB';
62 lives_ok { $odb->drop; } 'ONE emptied';
63 is($odb->stat->{entries}, 0, 'Removed');
64 }
65 {
66 my $txn = $env->BeginTxn;
67 # A case insensitive DB
68 ok(my $DBN = $txn->OpenDB('CI', MDB_CREATE), 'CI Created');
69 $DBN->set_compare(sub { lc($a) cmp lc($b) });
70
71 # A Reversed key order DB
72 ok(my $DBR = $DBN->open('RK', MDB_CREATE|MDB_REVERSEKEY), 'RK Created');
73
74 my %data;
75 my $c;
76 foreach('A' .. 'Z') {
77 $c = ord($_) - ord('A') + 1;
78 my $k = $_ . chr(ord('Z')+1-$c);
79 my $v = sprintf('Datum #%d', $c);
80 $data{$k} = $v;
81 if($c < 4) {
82 is($DBN->put($k, $v), $v, "Put in CI $k");
83 is($DBN->stat->{entries}, $c, "Entry CI $c");
84 is($DBR->put($k, $v), $v, "Put in RK $k");
85 is($DBR->stat->{entries}, $c, "Entry RK $c");
86 } else {
87 # Don't be verbose
88 $DBN->put($k, $v);
89 $DBR->put($k, $v);
90 }
91 }
92 is($c, 26, 'All in');
93 # Check data in random HASH order
94 $c = 5; # Don't be verbose
95 while(my($k, $v) = each %data) {
96 is($DBN->get(lc $k), $v, "Get CI \L$k");
97 is($DBR->get($k), $v, "Get RK $k");
98 --$c or last;
99 }
100 my $ordkey = [ sort keys %data ];
101 tie %data, $DBN;
102 is_deeply( $ordkey, [ keys %data ], 'Ordered');
103 untie %data;
104 tie %data, $DBR;
105 is_deeply( $ordkey, [ reverse keys %data ], 'Reversed' );
106 untie %data;
107 }
108 END {
109 unless($ENV{KEEP_TMPS}) {
110 for($dir) {
111 unlink glob("$_/*");
112 rmdir $_;
113 #warn "Removed $_\n";
114 }
115 }
116 }