]> Sergey Matveev's repositories - public-inbox.git/blob - t/altid.t
f3c01520c6db9afbb4cb0442d73ce6bfa282b8bc
[public-inbox.git] / t / altid.t
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Eml;
8 require_mods(qw(DBD::SQLite Search::Xapian));
9 use_ok 'PublicInbox::Msgmap';
10 use_ok 'PublicInbox::SearchIdx';
11 use_ok 'PublicInbox::Import';
12 use_ok 'PublicInbox::Inbox';
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $git_dir = "$tmpdir/a.git";
15 my $alt_file = "$tmpdir/another-nntp.sqlite3";
16 my $altid = [ "serial:gmane:file=$alt_file" ];
17 my $ibx;
18
19 {
20         my $mm = PublicInbox::Msgmap->new_file($alt_file, 1);
21         is($mm->mid_set(1234, 'a@example.com'), 1, 'mid_set once OK');
22         ok(0 == $mm->mid_set(1234, 'a@example.com'), 'mid_set not idempotent');
23         ok(0 == $mm->mid_set(1, 'a@example.com'), 'mid_set fails with dup MID');
24 }
25
26 {
27         my $git = PublicInbox::Git->new($git_dir);
28         my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
29         $im->init_bare;
30         $im->add(PublicInbox::Eml->new(<<'EOF'));
31 From: a@example.com
32 To: b@example.com
33 Subject: boo!
34 Message-ID: <a@example.com>
35
36 hello world gmane:666
37 EOF
38         $im->done;
39 }
40 {
41         $ibx = PublicInbox::Inbox->new({inboxdir => $git_dir});
42         $ibx->{altid} = $altid;
43         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
44         $rw->index_sync;
45 }
46
47 {
48         my $ro = PublicInbox::Search->new($ibx);
49         my $msgs = $ro->query("gmane:1234");
50         $msgs = [ map { $_->{mid} } @$msgs ];
51         is_deeply($msgs, ['a@example.com'], 'got one match');
52
53         $msgs = $ro->query("gmane:666");
54         is_deeply([], $msgs, 'body did NOT match');
55 };
56
57 {
58         my $mm = PublicInbox::Msgmap->new_file($alt_file, 1);
59         my ($min, $max) = $mm->minmax;
60         my $num = $mm->mid_insert('b@example.com');
61         ok($num > $max, 'auto-increment goes beyond mid_set');
62 }
63
64 done_testing();
65
66 1;