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

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

merge from hackathon-mt

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 Plagger->set_context($context);
97 my $feed;
98 $context->run_hook('searcher.search', { query => "murakami" }, 0, sub { $feed = $_[0] });
99 ok $feed, 'I got feed';
100 is $feed->count, 1, 'murakami matches 1';
101
102 $context->run_hook('searcher.search', { query => "foobar" }, 0, sub { $feed = $_[0] });
103 ok $feed, 'I still got feed';
104 is $feed->count, 0, 'No match';
105
106 === Second run ... make sure it's not clobbered
107 --- input config
108 plugins:
109   - module: Subscription::Config
110     config:
111       feed:
112         - file://$t::TestPlagger::BaseDirURI/t/samples/rss-full.xml
113   - module: Search::Estraier
114     config:
115       url: http://localhost:$main::port/node/plagger
116 --- expected
117 Plagger->set_context($context);
118 ok -e $main::dir, 'invindex exists';
119
120 my $feed;
121 $context->run_hook('searcher.search', { query => "murakami" }, 0, sub { $feed = $_[0] });
122 ok $feed, 'I still got feed';
123 is $feed->count, 1, 'murakami matches 1';
124
125 use Encode;
126 $context->run_hook('searcher.search', { query => decode_utf8("フォルダ") }, 0, sub { $feed = $_[0] });
127 ok $feed, 'I still got feed';
128 is $feed->count, 1, 'Unicode search';
129
130
Note: See TracBrowser for help on using the browser.