1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
6 use File::Temp qw/tempdir/;
8 foreach my $mod (qw(DBD::SQLite)) {
10 plan skip_all => "$mod missing for nntpd.t" if $@;
13 use_ok 'PublicInbox::Msgmap';
14 my $tmpdir = tempdir('pi-msgmap-XXXXXX', TMPDIR => 1, CLEANUP => 1);
15 my $d = PublicInbox::Msgmap->new($tmpdir, 1);
19 my @mids = qw(a@b c@d e@f g@h aa@bb aa@cc);
20 is_deeply([$d->minmax], [undef,undef], "empty min max on new DB");
22 foreach my $mid (@mids) {
23 my $n = $d->mid_insert($mid);
24 ok($n, "mid $mid inserted");
30 my $ret = $d->mid_insert('a@b');
31 is($ret, undef, 'duplicate mid_insert in undef result');
32 is($d->num_for('a@b'), $mid2num{'a@b'}, 'existing number not clobbered');
34 foreach my $n (keys %num2mid) {
35 is($d->mid_for($n), $num2mid{$n}, "num:$n maps correctly");
37 foreach my $mid (@mids) {
38 is($d->num_for($mid), $mid2num{$mid}, "mid:$mid maps correctly");
41 is_deeply($d->mid_prefixes('a'), [qw(aa@cc aa@bb a@b)], "mid_prefixes match");
42 is_deeply($d->mid_prefixes('A'), [], "mid_prefixes is case sensitive");
44 is(undef, $d->last_commit, "last commit not set");
45 my $lc = 'deadbeef' x 5;
46 is(undef, $d->last_commit($lc), 'previous last commit (undef) returned');
47 is($lc, $d->last_commit, 'last commit was set correctly');
49 my $nc = 'deaddead' x 5;
50 is($lc, $d->last_commit($nc), 'returned previously set commit');
51 is($nc, $d->last_commit, 'new commit was set correctly');
53 is($d->mid_delete('a@b'), 1, 'deleted a@b');
54 is($d->mid_delete('a@b') + 0, 0, 'delete again returns zero');
55 is(undef, $d->num_for('a@b'), 'num_for fails on deleted msg');
58 ok($d = PublicInbox::Msgmap->new($tmpdir, 1), 'idempotent DB creation');
59 my ($min, $max) = $d->minmax;
60 ok($min > 0, "article min OK");
61 ok($max > 0 && $max < 10, "article max OK");
62 ok($min < $max, "article counts OK");
64 my $orig = $d->mid_insert('spam@1');
65 $d->mid_delete('spam@1');
66 is($d->mid_insert('spam@2'), 1 + $orig, "last number not recycled");