1 # Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 require_mods('DBD::SQLite');
8 use_ok 'PublicInbox::Msgmap';
9 my ($tmpdir, $for_destroy) = tmpdir();
10 my $f = "$tmpdir/msgmap.sqlite3";
11 my $d = PublicInbox::Msgmap->new_file($f, 1);
15 my @mids = qw(a@b c@d e@f g@h aa@bb aa@cc);
16 is_deeply([$d->minmax], [0,0], "zero min max on new DB");
18 foreach my $mid (@mids) {
19 my $n = $d->mid_insert($mid);
20 ok($n, "mid $mid inserted");
26 my $ret = $d->mid_insert('a@b');
27 is($ret, undef, 'duplicate mid_insert in undef result');
28 is($d->num_for('a@b'), $mid2num{'a@b'}, 'existing number not clobbered');
29 my $next = (sort(keys %num2mid))[-1];
30 is($d->mid_insert('ok@unique'), $next + 1,
31 'got expected num after failing mid_insert');
33 foreach my $n (keys %num2mid) {
34 is($d->mid_for($n), $num2mid{$n}, "num:$n maps correctly");
36 foreach my $mid (@mids) {
37 is($d->num_for($mid), $mid2num{$mid}, "mid:$mid maps correctly");
40 is(undef, $d->last_commit, "last commit not set");
41 my $lc = 'deadbeef' x 5;
42 is(undef, $d->last_commit($lc), 'previous last commit (undef) returned');
43 is($lc, $d->last_commit, 'last commit was set correctly');
45 my $nc = 'deaddead' x 5;
46 is($lc, $d->last_commit($nc), 'returned previously set commit');
47 is($nc, $d->last_commit, 'new commit was set correctly');
49 is($d->mid_delete('a@b'), 1, 'deleted a@b');
50 is($d->mid_delete('a@b') + 0, 0, 'delete again returns zero');
51 is(undef, $d->num_for('a@b'), 'num_for fails on deleted msg');
54 ok($d = PublicInbox::Msgmap->new_file($f, 1), 'idempotent DB creation');
55 my ($min, $max) = $d->minmax;
56 ok($min > 0, "article min OK");
57 ok($max > 0 && $max < 10, "article max OK");
58 ok($min < $max, "article counts OK");
60 my $orig = $d->mid_insert('spam@1');
61 $d->mid_delete('spam@1');
62 is($d->mid_insert('spam@2'), 1 + $orig, "last number not recycled");
64 my $tmp = $d->tmp_clone($tmpdir);
65 is_deeply([$d->minmax], [$tmp->minmax], 'Cloned temporary DB matches');
66 ok($tmp->mid_delete('spam@2'), 'temporary DB is writable');
72 }, 'ok', 'atfork_* work on tmp_clone');