]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei_store.t
c18a9620724b6a42870028da9d42ac7871bea5a7
[public-inbox.git] / t / lei_store.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 v5.10.1;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 require_mods(qw(DBD::SQLite Search::Xapian));
9 require_git 2.6;
10 require_ok 'PublicInbox::LeiStore';
11 require_ok 'PublicInbox::ExtSearch';
12 my ($home, $for_destroy) = tmpdir();
13 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
14 my $store_dir = "$home/lst";
15 my $lst = PublicInbox::LeiStore->new($store_dir, { creat => 1 });
16 ok($lst, '->new');
17 my $oid = $lst->add_eml(eml_load('t/data/0001.patch'));
18 like($oid, qr/\A[0-9a-f]+\z/, 'add returned OID');
19 my $eml = eml_load('t/data/0001.patch');
20 is($lst->add_eml($eml), undef, 'idempotent');
21 $lst->done;
22 {
23         my $es = $lst->search;
24         my $msgs = $es->over->query_xover(0, 1000);
25         is(scalar(@$msgs), 1, 'one message');
26         is($msgs->[0]->{blob}, $oid, 'blob matches');
27         my $mset = $es->mset("mid:$msgs->[0]->{mid}");
28         is($mset->size, 1, 'search works');
29         is_deeply($es->mset_to_artnums($mset), [ $msgs->[0]->{num} ],
30                 'mset_to_artnums');
31         my @kw = $es->msg_keywords(($mset->items)[0]);
32         is_deeply(\@kw, [], 'no flags');
33 }
34
35 for my $parallel (0, 1) {
36         $lst->{priv_eidx}->{parallel} = $parallel;
37         my $docids = $lst->set_eml_keywords($eml, qw(seen draft));
38         is(scalar @$docids, 1, 'set keywords on one doc');
39         $lst->done;
40         my @kw = $lst->search->msg_keywords($docids->[0]);
41         is_deeply(\@kw, [qw(draft seen)], 'kw matches');
42
43         $docids = $lst->add_eml_keywords($eml, qw(seen draft));
44         $lst->done;
45         is(scalar @$docids, 1, 'idempotently added keywords to doc');
46         @kw = $lst->search->msg_keywords($docids->[0]);
47         is_deeply(\@kw, [qw(draft seen)], 'kw matches after noop');
48
49         $docids = $lst->remove_eml_keywords($eml, qw(seen draft));
50         is(scalar @$docids, 1, 'removed from one doc');
51         $lst->done;
52         @kw = $lst->search->msg_keywords($docids->[0]);
53         is_deeply(\@kw, [], 'kw matches after remove');
54
55         $docids = $lst->remove_eml_keywords($eml, qw(answered));
56         is(scalar @$docids, 1, 'removed from one doc (idempotently)');
57         $lst->done;
58         @kw = $lst->search->msg_keywords($docids->[0]);
59         is_deeply(\@kw, [], 'kw matches after remove (idempotent)');
60
61         $docids = $lst->add_eml_keywords($eml, qw(answered));
62         is(scalar @$docids, 1, 'added to empty doc');
63         $lst->done;
64         @kw = $lst->search->msg_keywords($docids->[0]);
65         is_deeply(\@kw, ['answered'], 'kw matches after add');
66
67         $docids = $lst->set_eml_keywords($eml);
68         is(scalar @$docids, 1, 'set to clobber');
69         $lst->done;
70         @kw = $lst->search->msg_keywords($docids->[0]);
71         is_deeply(\@kw, [], 'set clobbers all');
72 }
73
74 done_testing;