File: 11-get_pages_in_category.t

package info (click to toggle)
libmediawiki-bot-perl 5.007000-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 656 kB
  • sloc: perl: 1,992; makefile: 5
file content (50 lines) | stat: -rw-r--r-- 1,593 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use strict;
use warnings;
use Test::RequiresInternet 'test.wikipedia.org' => 80;
use Test::More 0.96 tests => 3;

use MediaWiki::Bot;
my $t = __FILE__;

my $bot = MediaWiki::Bot->new({
    agent   => "MediaWiki::Bot tests (https://metacpan.org/MediaWiki::Bot; $t)",
    host    => 'test.wikipedia.org',
});

subtest 'category loop' => sub {
    plan tests => 1;
    my @pages = $bot->get_all_pages_in_category('Category:Category loop', { max => 5 });
    is(scalar @pages, 1, 'Category loop protection works');
};

subtest 'big' => sub {
    plan tests => 2;
    my @pages = $bot->get_all_pages_in_category('Category:Really big category', { max => 51 });
    cmp_ok( scalar(@pages), '>', 500, 'Get big category, enough elements');
    ok(defined $pages[0], 'Get big category');
};

subtest 'callback' => sub {
    plan tests => 6;
    my $title;
    my $ns;
    my $pageid;

    $bot->get_all_pages_in_category('Category:Wikipedia', {
        hook => sub {
            my ($res) = @_;
            $title  = $res->[0]->{title};
            $ns     = $res->[0]->{ns};
            $pageid = $res->[0]->{pageid};
        }
    });

    ok(     defined($title),                    'Title returned via callback');
    like(   $title,             qr/\w+/,        'Title looks valid');

    ok(     defined($ns),                       'Namespace returned via callback');
    like(   $ns,                qr/\d/,         'Namespace is a number');

    ok(     defined($pageid),                   'Pageid returned via callback');
    like(   $pageid,            qr/\d/,         'Pageid is a number');
};