]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2reindex.t
t/v[12]reindex.t: Place expected second in Xapian tests
[public-inbox.git] / t / v2reindex.t
1 # Copyright (C) 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 PublicInbox::MIME;
7 use PublicInbox::ContentId qw(content_digest);
8 use File::Temp qw/tempdir/;
9 use File::Path qw(remove_tree);
10
11 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
12         eval "require $mod";
13         plan skip_all => "$mod missing for v2reindex.t" if $@;
14 }
15 use_ok 'PublicInbox::V2Writable';
16 my $mainrepo = tempdir('pi-v2reindex-XXXXXX', TMPDIR => 1, CLEANUP => 1);
17 my $ibx_config = {
18         mainrepo => $mainrepo,
19         name => 'test-v2writable',
20         version => 2,
21         -primary_address => 'test@example.com',
22         indexlevel => 'full',
23 };
24 my $mime = PublicInbox::MIME->create(
25         header => [
26                 From => 'a@example.com',
27                 To => 'test@example.com',
28                 Subject => 'this is a subject',
29                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
30         ],
31         body => "hello world\n",
32 );
33 local $ENV{NPROC} = 2;
34 my $minmax;
35 {
36         my %config = %$ibx_config;
37         my $ibx = PublicInbox::Inbox->new(\%config);
38         my $im = PublicInbox::V2Writable->new($ibx, 1);
39         foreach my $i (1..10) {
40                 $mime->header_set('Message-Id', "<$i\@example.com>");
41                 ok($im->add($mime), "message $i added");
42                 if ($i == 4) {
43                         $im->remove($mime);
44                 }
45         }
46
47         if ('test remove later') {
48                 $mime->header_set('Message-Id', "<5\@example.com>");
49                 $im->remove($mime);
50         }
51
52         $im->done;
53         $minmax = [ $ibx->mm->minmax ];
54         ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
55         is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
56 }
57
58 {
59         my %config = %$ibx_config;
60         my $ibx = PublicInbox::Inbox->new(\%config);
61         my $im = PublicInbox::V2Writable->new($ibx, 1);
62         eval { $im->index_sync({reindex => 1}) };
63         is($@, '', 'no error from reindexing');
64         $im->done;
65
66         delete $ibx->{mm};
67         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
68 }
69
70 my $xap = "$mainrepo/xap".PublicInbox::Search::SCHEMA_VERSION();
71 remove_tree($xap);
72 ok(!-d $xap, 'Xapian directories removed');
73 {
74         my %config = %$ibx_config;
75         my $ibx = PublicInbox::Inbox->new(\%config);
76         my $im = PublicInbox::V2Writable->new($ibx, 1);
77         eval { $im->index_sync({reindex => 1}) };
78         is($@, '', 'no error from reindexing');
79         $im->done;
80         ok(-d $xap, 'Xapian directories recreated');
81
82         delete $ibx->{mm};
83         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
84 }
85
86 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
87 remove_tree($xap);
88 ok(!-d $xap, 'Xapian directories removed again');
89 {
90         my @warn;
91         local $SIG{__WARN__} = sub { push @warn, @_ };
92         my %config = %$ibx_config;
93         my $ibx = PublicInbox::Inbox->new(\%config);
94         my $im = PublicInbox::V2Writable->new($ibx, 1);
95         eval { $im->index_sync({reindex => 1}) };
96         is($@, '', 'no error from reindexing without msgmap');
97         is(scalar(@warn), 0, 'no warnings from reindexing');
98         $im->done;
99         ok(-d $xap, 'Xapian directories recreated');
100         delete $ibx->{mm};
101         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
102 }
103
104 my %sizes;
105 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
106 remove_tree($xap);
107 ok(!-d $xap, 'Xapian directories removed again');
108 {
109         my @warn;
110         local $SIG{__WARN__} = sub { push @warn, @_ };
111         my %config = %$ibx_config;
112         my $ibx = PublicInbox::Inbox->new(\%config);
113         my $im = PublicInbox::V2Writable->new($ibx, 1);
114         eval { $im->index_sync({reindex => 1}) };
115         is($@, '', 'no error from reindexing without msgmap');
116         is_deeply(\@warn, [], 'no warnings');
117         $im->done;
118         ok(-d $xap, 'Xapian directories recreated');
119         delete $ibx->{mm};
120         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
121         my $mset = $ibx->search->query('"hello world"', {mset=>1});
122         isnt($mset->size, 0, "phrase search succeeds on indexlevel=full");
123         for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
124 }
125
126 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
127 remove_tree($xap);
128 ok(!-d $xap, 'Xapian directories removed again');
129 {
130         my @warn;
131         local $SIG{__WARN__} = sub { push @warn, @_ };
132         my %config = %$ibx_config;
133         $config{indexlevel} = 'medium';
134         my $ibx = PublicInbox::Inbox->new(\%config);
135         my $im = PublicInbox::V2Writable->new($ibx);
136         eval { $im->index_sync({reindex => 1}) };
137         is($@, '', 'no error from reindexing without msgmap');
138         is_deeply(\@warn, [], 'no warnings');
139         $im->done;
140         ok(-d $xap, 'Xapian directories recreated');
141         delete $ibx->{mm};
142         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
143
144         if (0) {
145                 # not sure why, but Xapian seems to fallback to terms and
146                 # phrase searches still work
147                 delete $ibx->{search};
148                 my $mset = $ibx->search->query('"hello world"', {mset=>1});
149                 is($mset->size, 0, 'phrase search does not work on medium');
150         }
151
152         my $mset = $ibx->search->query('hello world', {mset=>1});
153         isnt($mset->size, 0, "normal search works on indexlevel=medium");
154         for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
155         ok($sizes{full} > $sizes{medium}, 'medium is smaller than full');
156 }
157
158 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
159 remove_tree($xap);
160 ok(!-d $xap, 'Xapian directories removed again');
161 {
162         my @warn;
163         local $SIG{__WARN__} = sub { push @warn, @_ };
164         my %config = %$ibx_config;
165         $config{indexlevel} = 'basic';
166         my $ibx = PublicInbox::Inbox->new(\%config);
167         my $im = PublicInbox::V2Writable->new($ibx);
168         eval { $im->index_sync({reindex => 1}) };
169         is($@, '', 'no error from reindexing without msgmap');
170         is_deeply(\@warn, [], 'no warnings');
171         $im->done;
172         ok(-d $xap, 'Xapian directories recreated');
173         delete $ibx->{mm};
174         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
175         my $mset = $ibx->search->query('hello', {mset=>1});
176         is($mset->size, 0, "search fails on indexlevel='basic'");
177         for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
178         ok($sizes{medium} > $sizes{basic}, 'basic is smaller than medium');
179 }
180
181 done_testing();