X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=t%2Fmsgmap.t;h=a3b7200f16a566c39041f586c9fc9f97b665de3e;hb=refs%2Fheads%2Fmaster;hp=a34fd712ce3c7db90095f3e781af32175514a25d;hpb=a94b930f4804cd2019b69e1371753810e6e97ff8;p=public-inbox.git diff --git a/t/msgmap.t b/t/msgmap.t index a34fd712..a3b7200f 100644 --- a/t/msgmap.t +++ b/t/msgmap.t @@ -1,17 +1,20 @@ -# Copyright (C) 2015 all contributors -# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt) +# Copyright (C) 2015-2021 all contributors +# License: AGPL-3.0+ use strict; use warnings; use Test::More; -use File::Temp qw/tempdir/; - +use PublicInbox::TestCommon; +require_mods('DBD::SQLite'); use_ok 'PublicInbox::Msgmap'; -my $tmpdir = tempdir(CLEANUP => 1); -my $d = PublicInbox::Msgmap->new($tmpdir, 1); +my ($tmpdir, $for_destroy) = tmpdir(); +my $f = "$tmpdir/msgmap.sqlite3"; +my $d = PublicInbox::Msgmap->new_file($f, 1); my %mid2num; my %num2mid; my @mids = qw(a@b c@d e@f g@h aa@bb aa@cc); +is_deeply([$d->minmax], [0,0], "zero min max on new DB"); + foreach my $mid (@mids) { my $n = $d->mid_insert($mid); ok($n, "mid $mid inserted"); @@ -20,8 +23,12 @@ foreach my $mid (@mids) { } $@ = undef; -eval { $d->mid_insert('a@b') }; -ok($@, 'error raised when attempting duplicate message ID'); +my $ret = $d->mid_insert('a@b'); +is($ret, undef, 'duplicate mid_insert in undef result'); +is($d->num_for('a@b'), $mid2num{'a@b'}, 'existing number not clobbered'); +my $next = (sort(keys %num2mid))[-1]; +is($d->mid_insert('ok@unique'), $next + 1, + 'got expected num after failing mid_insert'); foreach my $n (keys %num2mid) { is($d->mid_for($n), $num2mid{$n}, "num:$n maps correctly"); @@ -30,9 +37,6 @@ foreach my $mid (@mids) { is($d->num_for($mid), $mid2num{$mid}, "mid:$mid maps correctly"); } -is_deeply($d->mid_prefixes('a'), [qw(aa@cc aa@bb a@b)], "mid_prefixes match"); -is_deeply($d->mid_prefixes('A'), [], "mid_prefixes is case sensitive"); - is(undef, $d->last_commit, "last commit not set"); my $lc = 'deadbeef' x 5; is(undef, $d->last_commit($lc), 'previous last commit (undef) returned'); @@ -47,7 +51,24 @@ is($d->mid_delete('a@b') + 0, 0, 'delete again returns zero'); is(undef, $d->num_for('a@b'), 'num_for fails on deleted msg'); $d = undef; -# idempotent -ok(PublicInbox::Msgmap->new($tmpdir, 1), 'idempotent DB creation'); +ok($d = PublicInbox::Msgmap->new_file($f, 1), 'idempotent DB creation'); +my ($min, $max) = $d->minmax; +ok($min > 0, "article min OK"); +ok($max > 0 && $max < 10, "article max OK"); +ok($min < $max, "article counts OK"); + +my $orig = $d->mid_insert('spam@1'); +$d->mid_delete('spam@1'); +is($d->mid_insert('spam@2'), 1 + $orig, "last number not recycled"); + +my $tmp = $d->tmp_clone($tmpdir); +is_deeply([$d->minmax], [$tmp->minmax], 'Cloned temporary DB matches'); +ok($tmp->mid_delete('spam@2'), 'temporary DB is writable'); + +is(eval { + $tmp->atfork_prepare; + $tmp->atfork_parent; + 'ok' +}, 'ok', 'atfork_* work on tmp_clone'); done_testing();