root/trunk/plagger/t/plugins/Search-Estraier/search.t

Revision 1388 (checked in by miyagawa, 14 years ago)

check if estconfig is installed

Line 
1 use strict;
2 use FindBin;
3 use File::Spec;
4 use File::Path;
5 use IO::Socket::INET;
6 use List::Util qw(first);
7 use LWP::UserAgent;
8
9 use t::TestPlagger;
10
11 test_plugin_deps;
12 test_requires('Tie::File');
13 test_requires_command('estmaster');
14 test_requires_command('estconfig');
15
16 my $ver = `estconfig --version`;
17 if ($ver =~ /^1\.3/ && $^O eq 'freebsd') {
18     plan skip_all => "This test doesn't work with 1.3.x on FreeBSD: $ver";
19 }
20
21 our $dir = File::Spec->catfile($FindBin::Bin, 'estdb');
22 our $port;
23
24 eval { init_estmaster($dir) };
25 if ($@) {
26     plan skip_all => $@;
27 }
28
29 plan 'no_plan';
30 run_eval_expected;
31
32 sub init_estmaster {
33     my($dir) = @_;
34
35     # find random open port
36     $port = first { !IO::Socket::INET->new("localhost:$_") }
37             map { 1000 + int rand(10000) } 1..10;
38
39     diag("Use port $port for testing");
40
41     # init estdb
42     system('estmaster', 'init', $dir);
43
44     # inline edit via Tie::File
45     tie my @conf, 'Tie::File', File::Spec->catfile($dir, '_conf') or die "_conf: $!";
46     my $done;
47     for my $line (@conf) {
48         $line =~ s/portnum: .*/portnum: $port/ and $done = 1;
49     }
50     untie @conf;
51
52     unless ($done) {
53         die "_conf file doesn't have portnum:";
54     }
55
56     # start estmaster in background
57     system('estmaster', 'start', '-bg', $dir);
58
59     # sleep for a little bit for estmaster to startup
60     sleep 1;
61
62     # send HTTP POST to create a new node
63     my $ua = LWP::UserAgent->new;
64     $ua->credentials("localhost:$port", 'Super User', 'admin', 'admin');
65     my $res = $ua->post("http://localhost:$port/master_ui", [
66         name => 'plagger',
67         label => 'plagger',
68         action => 8,
69     ]);
70
71     $res->content =~ /successfully/
72         or die "Node creation failed: " . $res->content;
73 }
74
75 END {
76     if ($dir && -e $dir) {
77         diag("shutting down estmaster");
78         system('estmaster', 'stop', $dir);
79         rmtree $dir;
80     }
81 }
82
83 __END__
84
85 === Search
86 --- input config
87 plugins:
88   - module: Subscription::Config
89     config:
90       feed:
91         - file://$t::TestPlagger::BaseDirURI/t/samples/vox.xml
92   - module: Search::Estraier
93     config:
94       url: http://localhost:$main::port/node/plagger
95 --- expected
96 # xxx this is clumsy
97 no warnings 'redefine';
98 *Plagger::context = sub { $context };
99
100 my $feed;
101 $context->run_hook('searcher.search', { query => "murakami" }, 0, sub { $feed = $_[0] });
102 ok $feed, 'I got feed';
103 is $feed->count, 1, 'murakami matches 1';
104
105 $context->run_hook('searcher.search', { query => "foobar" }, 0, sub { $feed = $_[0] });
106 ok $feed, 'I still got feed';
107 is $feed->count, 0, 'No match';
108
109 === Second run ... make sure it's not clobbered
110 --- input config
111 plugins:
112   - module: Subscription::Config
113     config:
114       feed:
115         - file://$t::TestPlagger::BaseDirURI/t/samples/rss-full.xml
116   - module: Search::Estraier
117     config:
118       url: http://localhost:$main::port/node/plagger
119 --- expected
120 ok -e $main::dir, 'invindex exists';
121
122 # xxx this is clumsy
123 no warnings 'redefine';
124 *Plagger::context = sub { $context };
125
126 my $feed;
127 $context->run_hook('searcher.search', { query => "murakami" }, 0, sub { $feed = $_[0] });
128 ok $feed, 'I still got feed';
129 is $feed->count, 1, 'murakami matches 1';
130
131 use Encode;
132 $context->run_hook('searcher.search', { query => decode_utf8("フォルダ") }, 0, sub { $feed = $_[0] });
133 ok $feed, 'I still got feed';
134 is $feed->count, 1, 'Unicode search';
135
136
Note: See TracBrowser for help on using the browser.