]> Sergey Matveev's repositories - public-inbox.git/blob - t/msgmap.t
msgmap: add message mapping via SQLite
[public-inbox.git] / t / msgmap.t
1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 use strict;
4 use warnings;
5 use Test::More;
6 use File::Temp qw/tempdir/;
7
8 use_ok 'PublicInbox::Msgmap';
9 my $tmpdir = tempdir(CLEANUP => 1);
10 my $d = PublicInbox::Msgmap->new($tmpdir, 1);
11
12 my %mid2num;
13 my %num2mid;
14 my @mids = qw(a@b c@d e@f g@h aa@bb aa@cc);
15 foreach my $mid (@mids) {
16         my $n = $d->mid_insert($mid);
17         ok($n, "mid $mid inserted");
18         $mid2num{$mid} = $n;
19         $num2mid{$n} = $mid;
20 }
21
22 $@ = undef;
23 eval { $d->mid_insert('a@b') };
24 ok($@, 'error raised when attempting duplicate message ID');
25
26 foreach my $n (keys %num2mid) {
27         is($d->mid_for($n), $num2mid{$n}, "num:$n maps correctly");
28 }
29 foreach my $mid (@mids) {
30         is($d->num_for($mid), $mid2num{$mid}, "mid:$mid maps correctly");
31 }
32
33 is_deeply($d->mid_prefixes('a'), [qw(aa@cc aa@bb a@b)], "mid_prefixes match");
34 is_deeply($d->mid_prefixes('A'), [], "mid_prefixes is case sensitive");
35
36 is(undef, $d->last_commit, "last commit not set");
37 my $lc = 'deadbeef' x 5;
38 is(undef, $d->last_commit($lc), 'previous last commit (undef) returned');
39 is($lc, $d->last_commit, 'last commit was set correctly');
40
41 my $nc = 'deaddead' x 5;
42 is($lc, $d->last_commit($nc), 'returned previously set commit');
43 is($nc, $d->last_commit, 'new commit was set correctly');
44
45 is($d->mid_delete('a@b'), 1, 'deleted a@b');
46 is($d->mid_delete('a@b') + 0, 0, 'delete again returns zero');
47 is(undef, $d->num_for('a@b'), 'num_for fails on deleted msg');
48 $d = undef;
49
50 # idempotent
51 ok(PublicInbox::Msgmap->new($tmpdir, 1), 'idempotent DB creation');
52
53 done_testing();