/[Search-Estraier]/trunk/t/5_Node.t
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/t/5_Node.t

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26