]> Sergey Matveev's repositories - public-inbox.git/blob - t/miscsearch.t
0ba7919411e6140dd87faba40e96066015ceed5b
[public-inbox.git] / t / miscsearch.t
1 #!perl -w
2 # Copyright (C) 2020 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 Test::More;
6 use PublicInbox::TestCommon;
7 use PublicInbox::InboxWritable;
8 require_mods(qw(Search::Xapian DBD::SQLite));
9 use_ok 'PublicInbox::MiscSearch';
10 use_ok 'PublicInbox::MiscIdx';
11
12 my ($tmp, $for_destroy) = tmpdir();
13 my $eidx = { xpfx => "$tmp/eidx", -no_fsync => 1 }; # mock ExtSearchIdx
14 {
15         mkdir "$tmp/v1" or BAIL_OUT "mkdir $!";
16         open my $fh, '>', "$tmp/v1/description" or BAIL_OUT "open: $!";
17         print $fh "Everything sucks this year\n" or BAIL_OUT "print $!";
18         close $fh or BAIL_OUT "close $!";
19 }
20 {
21         my $v1 = PublicInbox::InboxWritable->new({
22                 inboxdir => "$tmp/v1",
23                 name => 'hope',
24                 address => [ 'nope@example.com' ],
25                 indexlevel => 'basic',
26                 version => 1,
27         });
28         $v1->init_inbox;
29         my $mi = PublicInbox::MiscIdx->new($eidx);
30         $mi->begin_txn;
31         $mi->index_ibx($v1);
32         $mi->commit_txn;
33 }
34
35 my $ms = PublicInbox::MiscSearch->new("$tmp/eidx/misc");
36 my $mset = $ms->mset('"everything sucks today"');
37 is(scalar($mset->items), 0, 'no match on description phrase');
38
39 $mset = $ms->mset('"everything sucks this year"');
40 is(scalar($mset->items), 1, 'match phrase on description');
41
42 $mset = $ms->mset('everything sucks');
43 is(scalar($mset->items), 1, 'match words in description');
44
45 $mset = $ms->mset('nope@example.com');
46 is(scalar($mset->items), 1, 'match full address');
47
48 $mset = $ms->mset('nope');
49 is(scalar($mset->items), 1, 'match partial address');
50
51 $mset = $ms->mset('hope');
52 is(scalar($mset->items), 1, 'match name');
53 my $mi = ($mset->items)[0];
54 my $doc = $mi->get_document;
55 is($doc->get_data, '{}', 'stored empty data');
56
57 done_testing;