/[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

Annotation of /trunk/t/5_Node.t

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26