3 |
use strict; |
use strict; |
4 |
use blib; |
use blib; |
5 |
|
|
6 |
use Test::More tests => 115; |
use Test::More tests => 270; |
7 |
use Test::Exception; |
use Test::Exception; |
8 |
use Data::Dumper; |
use Data::Dumper; |
9 |
|
|
10 |
BEGIN { use_ok('Search::Estraier') }; |
BEGIN { use_ok('Search::Estraier') }; |
11 |
|
|
12 |
my $debug = 0; |
my $debug = shift @ARGV; |
13 |
|
|
14 |
# name of node for test |
# name of node for test |
15 |
my $test1_node = 'test1'; |
my $test1_node = '_test1_' . $$; |
16 |
my $test2_node = 'test2'; |
my $test2_node = '_test2_' . $$; |
17 |
|
|
18 |
ok(my $node = new Search::Estraier::Node( debug => $debug ), 'new'); |
ok(my $node = new Search::Estraier::Node( debug => $debug ), 'new'); |
19 |
isa_ok($node, 'Search::Estraier::Node'); |
isa_ok($node, 'Search::Estraier::Node'); |
30 |
|
|
31 |
cmp_ok($node->status, '==', -1, 'status'); |
cmp_ok($node->status, '==', -1, 'status'); |
32 |
|
|
33 |
SKIP: { |
# test master functionality |
34 |
|
|
35 |
skip "no $test1_node node in Hyper Estraier", 105, unless($node->name); |
diag("node->master shutdown not tested"); |
36 |
|
|
37 |
my @res = ( -1, 200 ); |
diag("node->master backup not tested"); |
38 |
|
|
39 |
my $nodelist; |
ok(my @users = $node->master( action => 'userlist' ), 'userlist'); |
40 |
foreach my $url (qw{?action=nodelist http://localhost:1978/master?action=nodelist}) { |
|
41 |
cmp_ok( |
#diag "users: ", Dumper( \@users ); |
42 |
$node->shuttle_url( $url, 'text/plain', undef, \$nodelist) |
diag "found ", $#users + 1, " users"; |
43 |
,'==', shift @res, 'nodelist'); |
|
44 |
|
my $user = { |
45 |
|
name => '_test_' . $$, |
46 |
|
flags => 'b', |
47 |
|
fname => 'Search::Estraier', |
48 |
|
misc => 'test user', |
49 |
|
}; |
50 |
|
|
51 |
|
my $msg; |
52 |
|
ok($msg = $node->master( |
53 |
|
action => 'useradd', |
54 |
|
%{ $user }, |
55 |
|
passwd => 'test1234', |
56 |
|
), "useradd: $msg"); |
57 |
|
|
58 |
|
ok(my @users2 = $node->master( action => 'userlist' ), 'userlist'); |
59 |
|
cmp_ok($#users2, '==', $#users + 1, 'added user'); |
60 |
|
|
61 |
|
while (my $row = shift @users2) { |
62 |
|
next unless ($row->{name} eq $user); |
63 |
|
map { |
64 |
|
cmp_ok($user->{$_}, 'eq', $row->{$_}, "$_"); |
65 |
|
} keys %{ $user }; |
66 |
|
} |
67 |
|
|
68 |
|
ok($msg = $node->master( |
69 |
|
action => 'userdel', |
70 |
|
name => $user->{name}, |
71 |
|
), "userdel: $msg"); |
72 |
|
|
73 |
|
ok(@users2 = $node->master( action => 'userlist' ), 'userlist'); |
74 |
|
cmp_ok($#users2, '==', $#users, 'removed user'); |
75 |
|
|
76 |
|
ok(my @nodes = $node->master( action => 'nodelist' ), 'nodelist' ); |
77 |
|
#diag "nodelist: ", Dumper( \@nodes ); |
78 |
|
diag "found ", $#nodes + 1, " nodes"; |
79 |
|
|
80 |
|
if ($#nodes > 42) { |
81 |
|
diag <<'_END_OF_WARNING_'; |
82 |
|
|
83 |
|
This tests create three nodes in your Hyper Estraier. |
84 |
|
|
85 |
|
Since you have more than 43 modes, and Hyper Estraier needs more than |
86 |
|
1024 file descriptors for more than 46 nodes, expect tests to fail. |
87 |
|
|
88 |
|
If tests do fail, you can try to add |
89 |
|
|
90 |
|
ulimit -n 2048 |
91 |
|
|
92 |
|
before staring estmaster, which will increase number of available nodes |
93 |
|
to 96 before estmaster runs out of file descriptors. |
94 |
|
|
95 |
|
_END_OF_WARNING_ |
96 |
} |
} |
97 |
|
|
98 |
|
my $temp_node = "_test_temp_$$"; |
99 |
|
|
100 |
|
foreach my $node_name ( $test1_node , $test2_node, $temp_node ) { |
101 |
|
ok($msg = $node->master( |
102 |
|
action => 'nodeadd', |
103 |
|
name => $node_name, |
104 |
|
label => "$node_name label", |
105 |
|
), "nodeadd $node_name: $msg"); |
106 |
|
} |
107 |
|
|
108 |
|
ok($msg = $node->master( |
109 |
|
action => 'nodedel', |
110 |
|
name => $temp_node, |
111 |
|
), "nodedel $temp_node: $msg"); |
112 |
|
|
113 |
|
#diag "not testing logrtt\n"; |
114 |
|
|
115 |
|
# test document creation |
116 |
|
|
117 |
my $draft = <<'_END_OF_DRAFT_'; |
my $draft = <<'_END_OF_DRAFT_'; |
118 |
@uri=data001 |
@uri=data0 |
119 |
@title=Material Girl |
@title=Material Girl |
120 |
|
|
121 |
Living in a material world |
Living in a material world |
129 |
|
|
130 |
ok( $node->put_doc($doc), "put_doc data001"); |
ok( $node->put_doc($doc), "put_doc data001"); |
131 |
|
|
132 |
for ( 1 .. 10 ) { |
for ( 1 .. 17 ) { |
133 |
$doc->add_attr('@uri', 'test' . $_); |
$doc->add_attr('@uri', 'test' . $_); |
134 |
ok( $node->put_doc($doc), "put_doc test$_"); |
ok( $node->put_doc($doc), "put_doc test$_"); |
135 |
#diag $doc->dump_draft; |
#diag $doc->dump_draft; |
136 |
|
cmp_ok( $node->doc_num, '==', ($_ + 1), "node->doc_num " . ($_ + 1)); |
137 |
} |
} |
138 |
|
|
139 |
my $id; |
ok(! $node->uri_to_id( 'does-not-exists' ), "non-existant uri_to_id"); |
|
ok($id = $node->uri_to_id( 'data001' ), "uri_to_id = $id"); |
|
|
|
|
|
my $data_max = 5; |
|
140 |
|
|
141 |
for ( 1 .. $data_max ) { |
my $id; |
142 |
ok( $node->out_doc_by_uri( 'test' . $_ ), "out_doc_by_uri test$_"); |
ok($id = $node->uri_to_id( 'data0' ), "uri_to_id(data0)"); |
|
} |
|
143 |
|
|
144 |
ok($doc = $node->get_doc( $id ), 'get_doc for edit'); |
ok($doc = $node->get_doc( $id ), "get_doc($id) for edit"); |
145 |
$doc->add_attr('foo', 'bar'); |
$doc->add_attr('foo', 'bar'); |
146 |
#diag Dumper($doc); |
#diag Dumper($doc); |
147 |
ok( $node->edit_doc( $doc ), 'edit_doc'); |
ok( $node->edit_doc( $doc ), 'edit_doc'); |
148 |
|
|
149 |
ok( $node->out_doc( $id ), "out_doc $id"); |
my $doc_num; |
150 |
|
ok( $doc_num = $node->doc_num, "node->doc_num $doc_num"); |
151 |
|
|
152 |
ok( ! $node->edit_doc( $doc ), "edit removed"); |
ok( $node->out_doc( $id ), "out_doc($id)"); |
153 |
|
|
154 |
my $max = 3; |
cmp_ok( $node->doc_num, '==', --$doc_num, "node->doc_num " . $doc_num); |
155 |
|
|
156 |
|
ok( ! $node->edit_doc( $doc ), "edit_doc of removed doc"); |
157 |
|
|
158 |
|
my $delete_num = 5; |
159 |
|
|
160 |
|
for ( 1 .. $delete_num ) { |
161 |
|
ok( $node->out_doc_by_uri( 'test' . $_ ), "out_doc_by_uri test$_"); |
162 |
|
cmp_ok( $node->doc_num, '==', $doc_num - $_, "node->doc_num " . ($doc_num - $_)); |
163 |
|
} |
164 |
|
|
165 |
|
my $doc_num2 = $doc_num - $delete_num; |
166 |
|
cmp_ok($node->doc_num, '==', $doc_num2, "node->doc_num $doc_num2"); |
167 |
|
|
168 |
|
my $max = int($doc_num2 / 2); |
169 |
|
|
170 |
ok(my $cond = new Search::Estraier::Condition, 'new cond'); |
ok(my $cond = new Search::Estraier::Condition, 'new cond'); |
171 |
ok($cond->set_phrase('girl'), 'cond set_phrase'); |
ok($cond->set_phrase('girl'), 'cond set_phrase'); |
172 |
ok($cond->set_max($max), "cond set_max $max"); |
ok($cond->set_max($max), "cond set_max($max)"); |
173 |
ok($cond->set_order('@uri ASCD'), 'cond set_order'); |
ok($cond->set_order('@uri ASCD'), 'cond set_order'); |
174 |
ok($cond->add_attr('@title STRINC Material'), 'cond add_attr'); |
ok($cond->add_attr('@title STRINC Material'), 'cond add_attr'); |
175 |
|
|
179 |
|
|
180 |
isa_ok( $nres, 'Search::Estraier::NodeResult' ); |
isa_ok( $nres, 'Search::Estraier::NodeResult' ); |
181 |
|
|
182 |
cmp_ok($nres->doc_num, '==', $max, "doc_num = $max"); |
cmp_ok($nres->doc_num, '==', $max, "nres->doc_num $max"); |
183 |
|
|
184 |
cmp_ok($nres->hits, '==', $data_max, "hits"); |
cmp_ok($nres->hits, '==', $doc_num2, "nres->hits $doc_num2"); |
185 |
|
|
186 |
|
# upper limit is $nres->hits and not $nres->doc_num because we |
187 |
|
# check all documents, not just results! |
188 |
for my $i ( 0 .. ($nres->hits - 1) ) { |
for my $i ( 0 .. ($nres->hits - 1) ) { |
189 |
my $uri = 'test' . ($i + $data_max + 1); |
my $num = $i + $delete_num + 1; |
190 |
|
my $uri = 'test' . $num; |
191 |
|
|
192 |
if ($i < $nres->doc_num) { |
if ($i < $nres->doc_num) { |
193 |
ok( my $rdoc = $nres->get_doc( $i ), "get_doc $i"); |
ok( my $rdoc = $nres->get_doc( $i ), "nres->get_doc $i"); |
194 |
|
|
195 |
cmp_ok( $rdoc->attr('@uri'), 'eq', $uri, "\@uri = $uri"); |
cmp_ok( $rdoc->attr('@uri'), 'eq', $uri, "\@uri = $uri"); |
196 |
|
cmp_ok( $node->uri_to_id( $uri ), '==', $num + 1, "uri_to_id($uri)"); |
197 |
|
|
198 |
ok( my $k = $rdoc->keywords( $id ), "rdoc keywords"); |
ok( my $k = $rdoc->keywords( $id ), "rdoc keywords"); |
199 |
|
} else { |
200 |
|
ok( ! $nres->get_doc( $i ), "nres->get_doc doesn't exist"); |
201 |
} |
} |
202 |
|
|
203 |
ok( my $id = $node->uri_to_id( $uri ), "uri_to_id($uri) = $id"); |
ok( my $id = $node->uri_to_id( $uri ), "uri_to_id($uri)"); |
204 |
ok( $node->get_doc( $id ), "get_doc $id"); |
ok( $node->get_doc( $id ), "get_doc($id)"); |
205 |
ok( $node->get_doc_by_uri( $uri ), "get_doc_by_uri $uri"); |
ok( $node->get_doc_by_uri( $uri ), "get_doc_by_uri($uri)"); |
206 |
cmp_ok( $node->get_doc_attr( $id, '@uri' ), 'eq', $uri, "get_doc_attr $id"); |
cmp_ok( $node->get_doc_attr( $id, '@uri' ), 'eq', $uri, "get_doc_attr $id"); |
207 |
cmp_ok( $node->get_doc_attr_by_uri( $uri, '@uri' ), 'eq', $uri, "get_doc_attr $id"); |
cmp_ok( $node->get_doc_attr_by_uri( $uri, '@uri' ), 'eq', $uri, "get_doc_attr $id"); |
208 |
ok( my $k1 = $node->etch_doc( $id ), "etch_doc_by_uri $uri"); |
ok( my $k1 = $node->etch_doc( $id ), "etch_doc_by_uri $uri"); |
225 |
|
|
226 |
ok($node->set_snippet_width( 100, 10, 10 ), "set_snippet_width"); |
ok($node->set_snippet_width( 100, 10, 10 ), "set_snippet_width"); |
227 |
|
|
228 |
|
# test skip |
229 |
|
my $skip = int($max / 2) || die "skip is zero, can't test"; |
230 |
|
ok($cond->set_skip( $skip ), "cond set_skip($skip)"); |
231 |
|
|
232 |
|
ok( $nres = $node->search( $cond, 0 ), 'search'); |
233 |
|
isa_ok( $nres, 'Search::Estraier::NodeResult' ); |
234 |
|
cmp_ok($nres->doc_num, '==', $max, "nres->doc_num " . ($max - $skip)); |
235 |
|
cmp_ok($nres->hits, '==', $doc_num2, "nres->hits $doc_num2"); |
236 |
|
|
237 |
|
for my $i ( 0 .. ($nres->doc_num - 1) ) { |
238 |
|
my $uri = 'test' . ($i + $delete_num + 1); |
239 |
|
ok( my $rdoc = $nres->get_doc( $i ), "nres->get_doc $i"); |
240 |
|
if ($rdoc) { |
241 |
|
cmp_ok( $rdoc->attr('@uri'), 'eq', $uri, "\@uri = $uri"); |
242 |
|
} else { |
243 |
|
fail('no rdoc'); |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
# user doesn't exist |
# user doesn't exist |
249 |
ok(! $node->set_user('foobar', 1), 'set_user'); |
ok(! $node->set_user('foobar', 1), 'set_user'); |
250 |
|
|
261 |
|
|
262 |
ok(! $node->uri_to_id('foobar'), 'uri_to_id without croak'); |
ok(! $node->uri_to_id('foobar'), 'uri_to_id without croak'); |
263 |
|
|
|
# test users |
|
264 |
|
|
265 |
|
# test users |
266 |
ok(! $node->admins, 'no admins'); |
ok(! $node->admins, 'no admins'); |
267 |
ok(! $node->guests, 'no guests'); |
ok(! $node->guests, 'no guests'); |
268 |
|
|
|
SKIP: { |
|
|
skip "no $test2_node in Hyper Estraier, skipping set_link", 5 unless (my $test2_label = $node2->label); |
|
269 |
|
|
270 |
my $link_url = "http://localhost:1978/node/$test2_node"; |
# test search without results |
271 |
|
ok($cond = new Search::Estraier::Condition, 'new cond'); |
272 |
ok($node->set_link( $link_url, $test2_label, 42), "set_link $test2_node ($test2_label) 42"); |
ok($cond->set_phrase('this_is_phrase_which_does_not_exits'), 'cond set_phrase'); |
273 |
|
|
274 |
ok(my $links = $node->links, 'links'); |
ok($nres = $node->search( $cond, 0 ), 'search'); |
275 |
|
|
276 |
cmp_ok($#{$links}, '==', 0, 'one link'); |
# now, test links |
277 |
|
my $test2_label = "$test2_node label"; |
278 |
like($links->[0], qr/^$link_url/, 'link correct'); |
my $link_url = "http://localhost:1978/node/$test2_node"; |
279 |
|
ok($node->set_link( $link_url, $test2_label, 42), "set_link $test2_node ($test2_label) 42"); |
280 |
|
ok(my $links = $node->links, 'links'); |
281 |
|
cmp_ok($#{$links}, '==', 0, 'one link'); |
282 |
|
like($links->[0], qr/^$link_url/, 'link correct'); |
283 |
|
ok($node->set_link("http://localhost:1978/node/$test2_node", $test2_label, 0), "set_link $test2_node ($test2_label) delete"); |
284 |
|
|
285 |
|
ok($msg = $node->master( |
286 |
|
action => 'nodeclr', |
287 |
|
name => $node->name, |
288 |
|
), "nodeclr $node->name: $msg"); |
289 |
|
|
290 |
|
cmp_ok($node->doc_num, '==', 0, 'no documents'); |
291 |
|
|
292 |
|
# cleanup test nodes |
293 |
|
foreach my $node_name ( $test1_node , $test2_node ) { |
294 |
|
ok($msg = $node->master( |
295 |
|
action => 'nodedel', |
296 |
|
name => $node_name, |
297 |
|
), "nodedel $node_name: $msg"); |
298 |
|
} |
299 |
|
|
300 |
ok($node->set_link("http://localhost:1978/node/$test2_node", $test2_label, 0), "set_link $test2_node ($test2_label) delete"); |
# test create |
301 |
} # SKIP 2 |
my $node_name = '_test_create_' . $$; |
302 |
|
my $node_label = "test $$ label"; |
303 |
|
|
304 |
|
ok($node = new Search::Estraier::Node( |
305 |
|
url => "http://localhost:1978/node/$node_name", |
306 |
|
create => 1, |
307 |
|
label => $node_label, |
308 |
|
croak_on_error => 1 |
309 |
|
), "new create+croak"); |
310 |
|
|
311 |
|
cmp_ok($node->name, 'eq', $node_name, "node $node_name exists"); |
312 |
|
cmp_ok($node->label, 'eq', $node_label, "node label: $node_label"); |
313 |
|
|
314 |
|
ok($node = new Search::Estraier::Node( |
315 |
|
url => "http://localhost:1978/node/$node_name", |
316 |
|
create => 1, |
317 |
|
label => $node_label, |
318 |
|
croak_on_error => 0 |
319 |
|
), "new create existing"); |
320 |
|
|
321 |
|
ok($node = new Search::Estraier::Node( |
322 |
|
url => "http://localhost:1978/node/$node_name", |
323 |
|
create => 1, |
324 |
|
label => $node_label, |
325 |
|
croak_on_error => 1 |
326 |
|
), "new create+croak existing"); |
327 |
|
|
328 |
|
# cleanup |
329 |
|
ok($msg = $node->master( |
330 |
|
action => 'nodedel', |
331 |
|
name => $node_name, |
332 |
|
), "nodedel $node_name: $msg"); |
333 |
|
|
334 |
|
# and again, this time without node |
335 |
|
ok($node = new Search::Estraier::Node( |
336 |
|
url => "http://localhost:1978/node/$node_name", |
337 |
|
create => 1, |
338 |
|
label => $node_label, |
339 |
|
croak_on_error => 0 |
340 |
|
), "new create non-existing"); |
341 |
|
|
342 |
|
cmp_ok($node->name, 'eq', $node_name, "node $node_name exists"); |
343 |
|
cmp_ok($node->label, 'eq', $node_label, "node label: $node_label"); |
344 |
|
|
345 |
|
# cleanup |
346 |
|
ok($msg = $node->master( |
347 |
|
action => 'nodedel', |
348 |
|
name => $node_name, |
349 |
|
), "nodedel $node_name: $msg"); |
350 |
|
|
351 |
} # SKIP 1 |
ok($msg = $node->master( action => 'sync' ), "sync: $msg"); |
352 |
|
|
353 |
diag "over"; |
diag "over"; |