]> Sergey Matveev's repositories - public-inbox.git/blob - t/over.t
734fdaa3604a55dd1da7df9ddb67ad48bd52c345
[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->connect;
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->disconnect;
27
28 $over = PublicInbox::Over->new("$tmpdir/over.sqlite3");
29 $over->connect;
30 ok($over->{dbh}->{ReadOnly}, 'Over is ReadOnly');
31
32 $over = PublicInbox::OverIdx->new("$tmpdir/over.sqlite3");
33 $over->connect;
34 is($over->sid('hello-world'), $x, 'idempotent across reopen');
35 $over->each_by_mid('never', sub { fail('should not be called') });
36
37 $x = $over->create_ghost('never');
38 is(int($x), $x, 'integer tid for ghost');
39 $y = $over->create_ghost('NEVAR');
40 is($y, $x + 1, 'integer tid for ghost increases');
41
42 my $ddd = compress('');
43 foreach my $s ('', undef) {
44         $over->add_over([0, 0, 98, [ 'a' ], [], $s, $ddd]);
45         $over->add_over([0, 0, 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([0, 0, 98, [ 'a' ], [], 's', $ddd]);
52 $over->add_over([0, 0, 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([0, 0, 98, [ 'a' ], [], 's', $ddd]);
58 $over->add_over([0, 0, 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 done_testing();