]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei_xsearch.t
t/lei_xsearch: use create_inbox
[public-inbox.git] / t / lei_xsearch.t
1 #!perl -w
2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use List::Util qw(shuffle max);
7 use PublicInbox::TestCommon;
8 use PublicInbox::Eml;
9 use PublicInbox::InboxWritable;
10 require_mods(qw(DBD::SQLite Search::Xapian));
11 require PublicInbox::ExtSearchIdx;
12 require_git 2.6;
13 require_ok 'PublicInbox::LeiXSearch';
14 my ($home, $for_destroy) = tmpdir();
15 my @ibx;
16 for my $V (1..2) {
17         for my $i (3..6) {
18                 push @ibx, create_inbox("v$V-$i", indexlevel => 'full',
19                                         version => $V, sub {
20                         my ($im, $ibx) = @_;
21                         for my $j (0..9) {
22                                 my $eml = PublicInbox::Eml->new(<<EOM);
23 From: x\@example.com
24 To: $ibx->{-primary_address}
25 Date: Fri, 02 Oct 1993 0$V:0$i:0$j +0000
26 Subject: v${V}i${i}j$j
27 Message-ID: <v${V}i${i}j$j\@example>
28
29 ${V}er ${i}on j$j
30 EOM
31                                 $im->add($eml) or BAIL_OUT '->add';
32                         }
33                 }); # create_inbox
34         }
35 }
36 my $first = shift @ibx; is($first->{name}, 'v1-3', 'first plucked');
37 my $last = pop @ibx; is($last->{name}, 'v2-6', 'last plucked');
38 my $eidx = PublicInbox::ExtSearchIdx->new("$home/eidx");
39 $eidx->attach_inbox($first);
40 $eidx->attach_inbox($last);
41 $eidx->eidx_sync({fsync => 0});
42 my $es = PublicInbox::ExtSearch->new("$home/eidx");
43 my $lxs = PublicInbox::LeiXSearch->new;
44 for my $ibxish (shuffle($es, @ibx)) {
45         $lxs->prepare_external($ibxish);
46 }
47 for my $loc ($lxs->locals) {
48         $lxs->attach_external($loc);
49 }
50 my $nr = $lxs->xdb->get_doccount;
51 my $mset = $lxs->mset('d:19931002..19931003', { limit => $nr });
52 is($mset->size, $nr, 'got all messages');
53 my @msgs;
54 for my $mi ($mset->items) {
55         if (my $smsg = $lxs->smsg_for($mi)) {
56                 push @msgs, $smsg;
57         } else {
58                 diag "E: ${\$mi->get_docid} missing";
59         }
60 }
61 is(scalar(@msgs), $nr, 'smsgs retrieved for all');
62
63 $mset = $lxs->recent(undef, { limit => 1 });
64 is($mset->size, 1, 'one result');
65 my $max = max(map { $_->{docid} } @msgs);
66 is($lxs->smsg_for(($mset->items)[0])->{docid}, $max,
67         'got highest docid');
68
69 my @ibxish = $lxs->locals;
70 is(scalar(@ibxish), scalar(@ibx) + 1, 'got locals back');
71 is($lxs->search, $lxs, '->search works');
72 is($lxs->over, undef, '->over fails');
73
74 {
75         $lxs = PublicInbox::LeiXSearch->new;
76         my $v2ibx = PublicInbox::InboxWritable->new({
77                 inboxdir => "$home/v2full",
78                 name => 'v2full',
79                 version => 2,
80                 indexlevel => 'full',
81                 -primary_address => 'v2full@example.com',
82         }, {});
83         my $im = $v2ibx->importer(0);
84         $im->add(eml_load('t/plack-qp.eml'));
85         $im->done;
86         my $v1ibx = PublicInbox::InboxWritable->new({
87                 inboxdir => "$home/v1medium",
88                 name => 'v1medium',
89                 version => 1,
90                 indexlevel => 'medium',
91                 -primary_address => 'v1medium@example.com',
92         }, {});
93         $im = $v1ibx->importer(0);
94         $im->add(eml_load('t/utf8.eml'));
95         $im->done;
96         $lxs->prepare_external($v1ibx);
97         $lxs->prepare_external($v2ibx);
98         for my $loc ($lxs->locals) {
99                 $lxs->attach_external($loc);
100         }
101         my $mset = $lxs->mset('m:testmessage@example.com');
102         is($mset->size, 1, 'got m: match on medium+full XSearch mix');
103 }
104
105 done_testing;