]> Sergey Matveev's repositories - public-inbox.git/blob - t/over.t
No ext_urls
[public-inbox.git] / t / over.t
1 # Copyright (C) 2018-2021 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 # ext index additions
78 $over->eidx_prep;
79 {
80         my @arg = qw(1349 2019 adeadba7cafe example.key);
81         ok($over->add_xref3(@arg), 'first add');
82         ok($over->add_xref3(@arg), 'add idempotent');
83         my $xref3 = $over->get_xref3(1349);
84         is_deeply($xref3, [ 'example.key:2019:adeadba7cafe' ], 'xref3 works');
85
86         @arg = qw(1349 2018 deadbeefcafe example.kee);
87         ok($over->add_xref3(@arg), 'add another xref3');
88         $xref3 = $over->get_xref3(1349);
89         is_deeply($xref3, [ 'example.key:2019:adeadba7cafe',
90                         'example.kee:2018:deadbeefcafe' ],
91                         'xref3 works forw two');
92
93         is($over->dbh->do(<<''), 1, 'remove first');
94 DELETE FROM xref3 WHERE xnum = 2019 AND docid = 1349
95
96         $xref3 = $over->get_xref3(1349);
97         is_deeply($xref3, [ 'example.kee:2018:deadbeefcafe' ],
98                 'confirm removal successful');
99         $over->rollback_lazy;
100 }
101
102 done_testing();