]> Sergey Matveev's repositories - public-inbox.git/blob - t/altid.t
cleanup: use '$ibx' consistently when referring to Inbox refs
[public-inbox.git] / t / altid.t
1 # Copyright (C) 2016-2018 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 File::Temp qw/tempdir/;
7 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
8         eval "require $mod";
9         plan skip_all => "$mod missing for altid.t" if $@;
10 }
11
12 use_ok 'PublicInbox::Msgmap';
13 use_ok 'PublicInbox::SearchIdx';
14 use_ok 'PublicInbox::Import';
15 use_ok 'PublicInbox::Inbox';
16 my $tmpdir = tempdir('pi-altid-XXXXXX', TMPDIR => 1, CLEANUP => 1);
17 my $git_dir = "$tmpdir/a.git";
18 my $alt_file = "$tmpdir/another-nntp.sqlite3";
19 my $altid = [ "serial:gmane:file=$alt_file" ];
20
21 {
22         my $mm = PublicInbox::Msgmap->new_file($alt_file, 1);
23         is($mm->mid_set(1234, 'a@example.com'), 1, 'mid_set once OK');
24         ok(0 == $mm->mid_set(1234, 'a@example.com'), 'mid_set not idempotent');
25         ok(0 == $mm->mid_set(1, 'a@example.com'), 'mid_set fails with dup MID');
26 }
27
28 {
29         is(system(qw(git init -q --bare), $git_dir), 0, 'git init ok');
30         my $git = PublicInbox::Git->new($git_dir);
31         my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
32         $im->add(Email::MIME->create(
33                 header => [
34                         From => 'a@example.com',
35                         To => 'b@example.com',
36                         'Content-Type' => 'text/plain',
37                         Subject => 'boo!',
38                         'Message-ID' => '<a@example.com>',
39                 ],
40                 body => "hello world gmane:666\n",
41         ));
42         $im->done;
43 }
44 {
45         my $ibx = PublicInbox::Inbox->new({mainrepo => $git_dir});
46         $ibx->{altid} = $altid;
47         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
48         $rw->index_sync;
49 }
50
51 {
52         my $ro = PublicInbox::Search->new($git_dir, $altid);
53         my $msgs = $ro->query("gmane:1234");
54         is_deeply([map { $_->mid } @$msgs], ['a@example.com'], 'got one match');
55
56         $msgs = $ro->query("gmane:666");
57         is_deeply([], $msgs, 'body did NOT match');
58 };
59
60 {
61         my $mm = PublicInbox::Msgmap->new_file($alt_file, 1);
62         my ($min, $max) = $mm->minmax;
63         my $num = $mm->mid_insert('b@example.com');
64         ok($num > $max, 'auto-increment goes beyond mid_set');
65 }
66
67 done_testing();
68
69 1;