]> Sergey Matveev's repositories - public-inbox.git/blob - t/over.t
overidx: inline create_ghost sub
[public-inbox.git] / t / over.t
1 # Copyright (C) 2018-2020 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 Compress::Zlib qw(compress);
7 use PublicInbox::TestCommon;
8 require_mods('DBD::SQLite');
9 use_ok 'PublicInbox::OverIdx';
10 my ($tmpdir, $for_destroy) = tmpdir();
11 my $over = PublicInbox::OverIdx->new("$tmpdir/over.sqlite3");
12 $over->dbh; # open file
13 is($over->max, 0, 'max is zero on new DB (scalar context)');
14 is_deeply([$over->max], [0], 'max is zero on new DB (list context)');
15 my $x = $over->next_tid;
16 is(int($x), $x, 'integer tid');
17 my $y = $over->next_tid;
18 is($y, $x+1, 'tid increases');
19
20 $x = $over->sid('hello-world');
21 is(int($x), $x, 'integer sid');
22 $y = $over->sid('hello-WORLD');
23 is($y, $x+1, 'sid increases');
24 is($over->sid('hello-world'), $x, 'idempotent');
25 ok(!$over->{dbh}->{ReadOnly}, 'OverIdx is not ReadOnly');
26 $over->dbh_close;
27
28 $over = PublicInbox::Over->new("$tmpdir/over.sqlite3");
29 ok($over->dbh->{ReadOnly}, 'Over is ReadOnly');
30
31 $over = PublicInbox::OverIdx->new("$tmpdir/over.sqlite3");
32 $over->dbh;
33 is($over->sid('hello-world'), $x, 'idempotent across reopen');
34 $over->each_by_mid('never', sub { fail('should not be called') });
35
36 $x = $over->resolve_mid_to_tid('never');
37 is(int($x), $x, 'integer tid for ghost');
38 $y = $over->resolve_mid_to_tid('NEVAR');
39 is($y, $x + 1, 'integer tid for ghost increases');
40
41 my $ddd = compress('');
42 my $msg = sub { { ts => 0, ds => 0, num => $_[0] } };
43 foreach my $s ('', undef) {
44         $over->add_over($msg->(98), [ 'a' ], [], $s, $ddd);
45         $over->add_over($msg->(99), [ 'b' ], [], $s, $ddd);
46         my $msgs = [ map { $_->{num} } @{$over->get_thread('a')} ];
47         is_deeply([98], $msgs,
48                 'messages not linked by empty subject');
49 }
50
51 $over->add_over($msg->(98), [ 'a' ], [], 's', $ddd);
52 $over->add_over($msg->(99), [ 'b' ], [], 's', $ddd);
53 foreach my $mid (qw(a b)) {
54         my $msgs = [ map { $_->{num} } @{$over->get_thread('a')} ];
55         is_deeply([98, 99], $msgs, 'linked messages by subject');
56 }
57 $over->add_over($msg->(98), [ 'a' ], [], 's', $ddd);
58 $over->add_over($msg->(99), [ 'b' ], ['a'], 'diff', $ddd);
59 foreach my $mid (qw(a b)) {
60         my $msgs = [ map { $_->{num} } @{$over->get_thread($mid)} ];
61         is_deeply([98, 99], $msgs, "linked messages by Message-ID: <$mid>");
62 }
63 isnt($over->max, 0, 'max is non-zero');
64
65 $over->rollback_lazy;
66
67 # L<perldata/"Version Strings">
68 my $v = eval 'v'.$over->{dbh}->{sqlite_version};
69 SKIP: {
70         skip("no WAL in SQLite version $v < 3.7.0", 1) if $v lt v3.7.0;
71         $over->{dbh}->do('PRAGMA journal_mode = WAL');
72         $over = PublicInbox::OverIdx->new("$tmpdir/over.sqlite3");
73         is($over->dbh->selectrow_array('PRAGMA journal_mode'), 'wal',
74                 'WAL journal_mode not clobbered if manually set');
75 }
76
77 done_testing();