]> Sergey Matveev's repositories - public-inbox.git/blob - t/v1reindex.t
t/v[12]reindex.t: Place expected second in Xapian tests
[public-inbox.git] / t / v1reindex.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 v1reindex.t" if $@;
14 }
15 use_ok 'PublicInbox::SearchIdx';
16 use_ok 'PublicInbox::Import';
17 my $mainrepo = tempdir('pi-v1reindex-XXXXXX', TMPDIR => 1, CLEANUP => 1);
18 is(system(qw(git init -q --bare), $mainrepo), 0);
19 my $ibx_config = {
20         mainrepo => $mainrepo,
21         name => 'test-v1reindex',
22         -primary_address => 'test@example.com',
23         indexlevel => 'full',
24 };
25 my $mime = PublicInbox::MIME->create(
26         header => [
27                 From => 'a@example.com',
28                 To => 'test@example.com',
29                 Subject => 'this is a subject',
30                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
31         ],
32         body => "hello world\n",
33 );
34 my $minmax;
35 {
36         my %config = %$ibx_config;
37         my $ibx = PublicInbox::Inbox->new(\%config);
38         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
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         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
54         eval { $rw->index_sync() };
55         is($@, '', 'no error from indexing');
56
57         $minmax = [ $ibx->mm->minmax ];
58         ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
59         is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
60 }
61
62 {
63         my %config = %$ibx_config;
64         my $ibx = PublicInbox::Inbox->new(\%config);
65         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
66         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
67         eval { $rw->index_sync({reindex => 1}) };
68         is($@, '', 'no error from reindexing');
69         $im->done;
70 }
71
72 my $xap = "$mainrepo/public-inbox/xapian".PublicInbox::Search::SCHEMA_VERSION();
73 remove_tree($xap);
74 ok(!-d $xap, 'Xapian directories removed');
75 {
76         my %config = %$ibx_config;
77         my $ibx = PublicInbox::Inbox->new(\%config);
78         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
79         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
80
81         eval { $rw->index_sync({reindex => 1}) };
82         is($@, '', 'no error from reindexing');
83         $im->done;
84         ok(-d $xap, 'Xapian directories recreated');
85
86         delete $ibx->{mm};
87         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
88 }
89
90 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
91 remove_tree($xap);
92 ok(!-d $xap, 'Xapian directories removed again');
93 {
94         my @warn;
95         local $SIG{__WARN__} = sub { push @warn, @_ };
96         my %config = %$ibx_config;
97         my $ibx = PublicInbox::Inbox->new(\%config);
98         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
99         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
100         eval { $rw->index_sync({reindex => 1}) };
101         is($@, '', 'no error from reindexing without msgmap');
102         is(scalar(@warn), 0, 'no warnings from reindexing');
103         $im->done;
104         ok(-d $xap, 'Xapian directories recreated');
105         delete $ibx->{mm};
106         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
107 }
108
109 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
110 remove_tree($xap);
111 ok(!-d $xap, 'Xapian directories removed again');
112 {
113         my @warn;
114         local $SIG{__WARN__} = sub { push @warn, @_ };
115         my %config = %$ibx_config;
116         my $ibx = PublicInbox::Inbox->new(\%config);
117         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
118         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
119         eval { $rw->index_sync({reindex => 1}) };
120         is($@, '', 'no error from reindexing without msgmap');
121         is_deeply(\@warn, [], 'no warnings');
122         $im->done;
123         ok(-d $xap, 'Xapian directories recreated');
124         delete $ibx->{mm};
125         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
126 }
127
128 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
129 remove_tree($xap);
130 ok(!-d $xap, 'Xapian directories removed again');
131 {
132         my @warn;
133         local $SIG{__WARN__} = sub { push @warn, @_ };
134         my %config = %$ibx_config;
135         $config{indexlevel} = 'medium';
136         my $ibx = PublicInbox::Inbox->new(\%config);
137         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
138         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
139         eval { $rw->index_sync({reindex => 1}) };
140         is($@, '', 'no error from reindexing without msgmap');
141         is_deeply(\@warn, [], 'no warnings');
142         $im->done;
143         ok(-d $xap, 'Xapian directories recreated');
144         delete $ibx->{mm};
145         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
146         my $mset = $ibx->search->query('hello world', {mset=>1});
147         isnt($mset->size, 0, 'got Xapian search results');
148 }
149
150 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
151 remove_tree($xap);
152 ok(!-d $xap, 'Xapian directories removed again');
153 {
154         my @warn;
155         local $SIG{__WARN__} = sub { push @warn, @_ };
156         my %config = %$ibx_config;
157         $config{indexlevel} = 'basic';
158         my $ibx = PublicInbox::Inbox->new(\%config);
159         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
160         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
161         eval { $rw->index_sync({reindex => 1}) };
162         is($@, '', 'no error from reindexing without msgmap');
163         is_deeply(\@warn, [], 'no warnings');
164         $im->done;
165         ok(-d $xap, 'Xapian directories recreated');
166         delete $ibx->{mm};
167         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
168         my $mset = $ibx->search->reopen->query('hello world', {mset=>1});
169         is($mset->size, 0, "no Xapian search results");
170 }
171
172 # upgrade existing basic to medium
173 # note: changing indexlevels is not yet supported in v2,
174 # and may not be without more effort
175 # no removals
176 {
177         my @warn;
178         local $SIG{__WARN__} = sub { push @warn, @_ };
179         my %config = %$ibx_config;
180         $config{indexleve} = 'medium';
181         my $ibx = PublicInbox::Inbox->new(\%config);
182         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
183         eval { $rw->index_sync };
184         is($@, '', 'no error from indexing');
185         is_deeply(\@warn, [], 'no warnings');
186         my $mset = $ibx->search->reopen->query('hello world', {mset=>1});
187         isnt($mset->size, 0, 'search OK after basic -> medium');
188 }
189
190 done_testing();