]> Sergey Matveev's repositories - public-inbox.git/blob - t/v1reindex.t
t/v[12]reindex.t: Test that the resulting msgmap is as expected
[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 my $msgmap;
36 {
37         my %config = %$ibx_config;
38         my $ibx = PublicInbox::Inbox->new(\%config);
39         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
40         foreach my $i (1..10) {
41                 $mime->header_set('Message-Id', "<$i\@example.com>");
42                 ok($im->add($mime), "message $i added");
43                 if ($i == 4) {
44                         $im->remove($mime);
45                 }
46         }
47
48         if ('test remove later') {
49                 $mime->header_set('Message-Id', "<5\@example.com>");
50                 $im->remove($mime);
51         }
52
53         $im->done;
54         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
55         eval { $rw->index_sync() };
56         is($@, '', 'no error from indexing');
57
58         $minmax = [ $ibx->mm->minmax ];
59         ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
60         is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
61
62         my ($min, $max) = @$minmax;
63         $msgmap = $ibx->mm->msg_range(\$min, $max);
64         is_deeply($msgmap, [
65                           [1, '1@example.com' ],
66                           [2, '2@example.com' ],
67                           [3, '3@example.com' ],
68                           [6, '6@example.com' ],
69                           [7, '7@example.com' ],
70                           [8, '8@example.com' ],
71                           [9, '9@example.com' ],
72                           [10, '10@example.com' ],
73                   ], 'msgmap as expected');
74 }
75
76 {
77         my %config = %$ibx_config;
78         my $ibx = PublicInbox::Inbox->new(\%config);
79         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
80         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
81         eval { $rw->index_sync({reindex => 1}) };
82         is($@, '', 'no error from reindexing');
83         $im->done;
84
85         my ($min, $max) = $ibx->mm->minmax;
86         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
87 }
88
89 my $xap = "$mainrepo/public-inbox/xapian".PublicInbox::Search::SCHEMA_VERSION();
90 remove_tree($xap);
91 ok(!-d $xap, 'Xapian directories removed');
92 {
93         my %config = %$ibx_config;
94         my $ibx = PublicInbox::Inbox->new(\%config);
95         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
96         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
97
98         eval { $rw->index_sync({reindex => 1}) };
99         is($@, '', 'no error from reindexing');
100         $im->done;
101         ok(-d $xap, 'Xapian directories recreated');
102
103         delete $ibx->{mm};
104         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
105
106         my ($min, $max) = $ibx->mm->minmax;
107         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
108 }
109
110 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
111 remove_tree($xap);
112 ok(!-d $xap, 'Xapian directories removed again');
113 {
114         my @warn;
115         local $SIG{__WARN__} = sub { push @warn, @_ };
116         my %config = %$ibx_config;
117         my $ibx = PublicInbox::Inbox->new(\%config);
118         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
119         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
120         eval { $rw->index_sync({reindex => 1}) };
121         is($@, '', 'no error from reindexing without msgmap');
122         is(scalar(@warn), 0, 'no warnings from reindexing');
123         $im->done;
124         ok(-d $xap, 'Xapian directories recreated');
125         delete $ibx->{mm};
126         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
127
128         my ($min, $max) = $ibx->mm->minmax;
129         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
130 }
131
132 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
133 remove_tree($xap);
134 ok(!-d $xap, 'Xapian directories removed again');
135 {
136         my @warn;
137         local $SIG{__WARN__} = sub { push @warn, @_ };
138         my %config = %$ibx_config;
139         my $ibx = PublicInbox::Inbox->new(\%config);
140         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
141         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
142         eval { $rw->index_sync({reindex => 1}) };
143         is($@, '', 'no error from reindexing without msgmap');
144         is_deeply(\@warn, [], 'no warnings');
145         $im->done;
146         ok(-d $xap, 'Xapian directories recreated');
147         delete $ibx->{mm};
148         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
149
150         my ($min, $max) = @$minmax;
151         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
152 }
153
154 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
155 remove_tree($xap);
156 ok(!-d $xap, 'Xapian directories removed again');
157 {
158         my @warn;
159         local $SIG{__WARN__} = sub { push @warn, @_ };
160         my %config = %$ibx_config;
161         $config{indexlevel} = 'medium';
162         my $ibx = PublicInbox::Inbox->new(\%config);
163         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
164         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
165         eval { $rw->index_sync({reindex => 1}) };
166         is($@, '', 'no error from reindexing without msgmap');
167         is_deeply(\@warn, [], 'no warnings');
168         $im->done;
169         ok(-d $xap, 'Xapian directories recreated');
170         delete $ibx->{mm};
171         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
172         my $mset = $ibx->search->query('hello world', {mset=>1});
173         isnt($mset->size, 0, 'got Xapian search results');
174
175         my ($min, $max) = $ibx->mm->minmax;
176         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
177 }
178
179 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
180 remove_tree($xap);
181 ok(!-d $xap, 'Xapian directories removed again');
182 {
183         my @warn;
184         local $SIG{__WARN__} = sub { push @warn, @_ };
185         my %config = %$ibx_config;
186         $config{indexlevel} = 'basic';
187         my $ibx = PublicInbox::Inbox->new(\%config);
188         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
189         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
190         eval { $rw->index_sync({reindex => 1}) };
191         is($@, '', 'no error from reindexing without msgmap');
192         is_deeply(\@warn, [], 'no warnings');
193         $im->done;
194         ok(-d $xap, 'Xapian directories recreated');
195         delete $ibx->{mm};
196         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
197         my $mset = $ibx->search->reopen->query('hello world', {mset=>1});
198         is($mset->size, 0, "no Xapian search results");
199
200         my ($min, $max) = $ibx->mm->minmax;
201         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
202 }
203
204 # upgrade existing basic to medium
205 # note: changing indexlevels is not yet supported in v2,
206 # and may not be without more effort
207 # no removals
208 {
209         my @warn;
210         local $SIG{__WARN__} = sub { push @warn, @_ };
211         my %config = %$ibx_config;
212         $config{indexleve} = 'medium';
213         my $ibx = PublicInbox::Inbox->new(\%config);
214         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
215         eval { $rw->index_sync };
216         is($@, '', 'no error from indexing');
217         is_deeply(\@warn, [], 'no warnings');
218         my $mset = $ibx->search->reopen->query('hello world', {mset=>1});
219         isnt($mset->size, 0, 'search OK after basic -> medium');
220
221         my ($min, $max) = $ibx->mm->minmax;
222         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
223 }
224
225 done_testing();