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

Diff of /trunk/t/5_Node.t

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 106 by dpavlin, Sun Feb 19 14:26:21 2006 UTC revision 198 by dpavlin, Sun Jan 20 16:50:59 2008 UTC
# Line 3  Line 3 
3  use strict;  use strict;
4  use blib;  use blib;
5    
6  use Test::More tests => 101;  use Test::More;
7  use Test::Exception;  use Test::Exception;
8  use Data::Dumper;  use Data::Dumper;
9    
10  BEGIN { use_ok('Search::Estraier') };  BEGIN {
11            plan tests => 313;
12            use_ok('Search::Estraier');
13    };
14    
15  my $debug = 0;  my $debug = shift @ARGV;
16    
17  # name of node for test  # name of node for test
18  my $test1_node = 'test1';  my $test1_node = '_test1_' . $$;
19  my $test2_node = 'test2';  my $test2_node = '_test2_' . $$;
20    
21    my $estmaster_uri = $ENV{'ESTMASTER_URI'} || 'http://localhost:1978';
22    
23  ok(my $node = new Search::Estraier::Node( debug => $debug ), 'new');  ok(my $node = new Search::Estraier::Node( debug => $debug ), 'new');
24  isa_ok($node, 'Search::Estraier::Node');  isa_ok($node, 'Search::Estraier::Node');
25    
26  ok($node->set_url("http://localhost:1978/node/$test1_node"), "set_url $test1_node");  ok($node->set_url("$estmaster_uri/node/$test1_node"), "set_url $test1_node");
27    
28  ok($node->set_proxy('', 8080), 'set_proxy');  ok($node->set_proxy('', 8080), 'set_proxy');
29  throws_ok {$node->set_proxy('proxy.example.com', 'foo') } qr/port/, 'set_proxy port NaN';  throws_ok {$node->set_proxy('proxy.example.com', 'foo') } qr/port/, 'set_proxy port NaN';
# Line 26  throws_ok {$node->set_proxy('proxy.examp Line 31  throws_ok {$node->set_proxy('proxy.examp
31  ok($node->set_timeout(42), 'set_timeout');  ok($node->set_timeout(42), 'set_timeout');
32  throws_ok {$node->set_timeout('foo') } qr/timeout/, 'set_timeout NaN';  throws_ok {$node->set_timeout('foo') } qr/timeout/, 'set_timeout NaN';
33    
34  ok($node->set_auth('admin','admin'), 'set_auth');  my ($user, $passwd) = (
35            $ENV{EST_USER} || 'admin',
36            $ENV{EST_PASSWD} || 'admin'
37    );
38    
39    ok($node->set_auth($user, $passwd), 'set_auth');
40    
41  cmp_ok($node->status, '==', -1, 'status');  cmp_ok($node->status, '==', -1, 'status');
42    
43    # test master functionality
44    
45  SKIP: {  SKIP: {
46    
47  skip "no $test1_node node in Hyper Estraier", 91, unless($node->name);  skip "can't find estmaster at $estmaster_uri", ( 313 - 10 ) if (! eval { $node->master( action => 'nodelist' ) } );
48    
49    diag "using $estmaster_uri";
50    diag("node->master shutdown not tested");
51    
52    diag("node->master backup not tested");
53    
54    ok(my @users = $node->master( action => 'userlist' ), 'userlist');
55    
56    #diag "users: ", Dumper( \@users );
57    diag "found ", $#users + 1, " users";
58    
59    my $user = {
60            name => '_test_' . $$,
61            flags => 'b',
62            fname => 'Search::Estraier',
63            misc => 'test user',
64    };
65    
66    my $msg;
67    ok($msg = $node->master(
68            action => 'useradd',
69            %{ $user },
70            passwd => 'test1234',
71    ), "useradd: $msg");
72    
73    ok(my @users2 = $node->master( action => 'userlist' ), 'userlist');
74    cmp_ok($#users2, '==', $#users + 1, 'added user');
75    
76    while (my $row = shift @users2) {
77            next unless ($row->{name} eq $user);
78            map {
79                    cmp_ok($user->{$_}, 'eq', $row->{$_}, "$_");
80            } keys %{ $user };
81    }
82    
83    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  my @res = ( -1, 200 );  This tests create three nodes in your Hyper Estraier.
99    
100  my $nodelist;  Since you have more than 43 modes, and Hyper Estraier needs more than
101  foreach my $url (qw{?action=nodelist http://localhost:1978/master?action=nodelist}) {  1024 file descriptors for more than 46 nodes, expect tests to fail.
102          cmp_ok(  
103                  $node->shuttle_url( $url, 'text/plain', undef, \$nodelist)  If tests do fail, you can try to add
104          ,'==', shift @res, 'nodelist');  
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    _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  my $draft = <<'_END_OF_DRAFT_';  my $draft = <<'_END_OF_DRAFT_';
133  @uri=data001  @uri=data0
134  @title=Material Girl  @title=Material Girl
135    
136  Living in a material world  Living in a material world
# Line 58  ok(my $doc = new Search::Estraier::Docum Line 144  ok(my $doc = new Search::Estraier::Docum
144    
145  ok( $node->put_doc($doc), "put_doc data001");  ok( $node->put_doc($doc), "put_doc data001");
146    
147  for ( 1 .. 10 ) {  for ( 1 .. 17 ) {
148          $doc->add_attr('@uri', 'test' . $_);          $doc->add_attr('@uri', 'test' . $_);
149            $doc->set_score( $_ * 10000 );
150          ok( $node->put_doc($doc), "put_doc test$_");          ok( $node->put_doc($doc), "put_doc test$_");
151          #diag $doc->dump_draft;          #diag $doc->dump_draft;
152            cmp_ok( $node->doc_num, '==', ($_ + 1), "node->doc_num " . ($_ + 1));
153  }  }
154    
155    ok(! $node->uri_to_id( 'does-not-exists' ), "non-existant uri_to_id");
156    
157  my $id;  my $id;
158  ok($id = $node->uri_to_id( 'data001' ), "uri_to_id = $id");  ok($id = $node->uri_to_id( 'data0' ), "uri_to_id(data0)");
159    
160  my $data_max = 5;  throws_ok { $node->get_doc( 'foo') } qr/id must be number/, 'croak on non-number';
161    
162  for ( 1 .. $data_max ) {  ok($doc = $node->get_doc( $id ), "get_doc($id) for edit");
         ok( $node->out_doc_by_uri( 'test' . $_ ), "out_doc_by_uri test$_");  
 }  
   
 ok($doc = $node->get_doc( $id ), 'get_doc for edit');  
163  $doc->add_attr('foo', 'bar');  $doc->add_attr('foo', 'bar');
164  #diag Dumper($doc);  #diag Dumper($doc);
165  ok( $node->edit_doc( $doc ), 'edit_doc');  ok( $node->edit_doc( $doc ), 'edit_doc');
166    
167  ok( $node->out_doc( $id ), "out_doc $id");  my $doc_num;
168    ok( $doc_num = $node->doc_num, "node->doc_num $doc_num");
169    
170    ok( $node->out_doc( $id ), "out_doc($id)");
171    
172    cmp_ok( $node->doc_num, '==', --$doc_num, "node->doc_num " . $doc_num);
173    
174    ok( ! $node->edit_doc( $doc ), "edit_doc of removed doc");
175    
176    my $cache;
177    ok($cache = $node->cacheusage, "cacheusage: $cache");
178    
179  ok( ! $node->edit_doc( $doc ), "edit removed");  my $delete_num = 5;
180    
181  my $max = 3;  for ( 1 .. $delete_num ) {
182            ok( $node->out_doc_by_uri( 'test' . $_ ), "out_doc_by_uri test$_");
183            cmp_ok( $node->doc_num, '==', $doc_num - $_, "node->doc_num " . ($doc_num - $_));
184    }
185    
186    my $doc_num2 = $doc_num - $delete_num;
187    cmp_ok($node->doc_num, '==', $doc_num2, "node->doc_num $doc_num2");
188    
189    my $max = int($doc_num2 / 2);
190    
191  ok(my $cond = new Search::Estraier::Condition, 'new cond');  ok(my $cond = new Search::Estraier::Condition, 'new cond');
192  ok($cond->set_phrase('girl'), 'cond set_phrase');  ok($cond->set_phrase('girl'), 'cond set_phrase');
193  ok($cond->set_max($max), "cond set_max $max");  ok($cond->set_max($max), "cond set_max($max)");
194  ok($cond->set_order('@uri ASCD'), 'cond set_order');  ok($cond->set_order('@uri ASCD'), 'cond set_order');
195  ok($cond->add_attr('@title STRINC Material'), 'cond add_attr');  ok($cond->add_attr('@title STRINC Material'), 'cond add_attr');
196    ok($cond->set_mask(qw/1 2/), 'cond set_mask');
197    
198  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');  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    
200  ok( my $nres = $node->search( $cond, 0 ), 'search');  ok( my $nres = $node->search( $cond, 0 ), 'search');
201    
202  isa_ok( $nres, 'Search::Estraier::NodeResult' );  isa_ok( $nres, 'Search::Estraier::NodeResult' );
203    
204  cmp_ok($nres->doc_num, '==', $max, "doc_num = $max");  cmp_ok($nres->doc_num, '==', $max, "nres->doc_num $max");
205    
206  cmp_ok($nres->hits, '==', $data_max, "hits");  cmp_ok($nres->hits, '==', $doc_num2, "nres->hits $doc_num2");
207    
208  for ( 6 .. 10 ) {  # upper limit is $nres->hits and not $nres->doc_num because we
209          my $uri = 'test' . $_;  # check all documents, not just results!
210          ok( my $id = $node->uri_to_id( $uri ), "uri_to_id($uri) = $id");  for my $i ( 0 .. ($nres->hits - 1) ) {
211          ok( $node->get_doc( $id ), "get_doc $id");          my $num = $i + $delete_num + 1;
212          ok( $node->get_doc_by_uri( $uri ), "get_doc_by_uri $uri");          my $uri = 'test' . $num;
213    
214            if ($i < $nres->doc_num) {
215                    ok( my $rdoc = $nres->get_doc( $i ), "nres->get_doc $i");
216    
217                    cmp_ok( $rdoc->attr('@uri'), 'eq', $uri, "\@uri = $uri");
218                    cmp_ok( $node->uri_to_id( $uri ), '==', $num + 1, "uri_to_id($uri)");
219    
220                    ok( my $k = $rdoc->keywords( $id ), "rdoc keywords");
221            } else {
222                    ok( ! $nres->get_doc( $i ), "nres->get_doc doesn't exist");
223            }
224    
225            ok( my $id = $node->uri_to_id( $uri ), "uri_to_id($uri)");
226            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          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");
233          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");
234          ok( my $k = $node->etch_doc( $id ), "etch_doc_by_uri $uri");          ok( my $k1 = $node->etch_doc( $id ), "etch_doc_by_uri $uri");
235          ok( my $k2 = $node->etch_doc_by_uri( $uri ), "etch_doc_by_uri $uri");          ok( my $k2 = $node->etch_doc_by_uri( $uri ), "etch_doc_by_uri $uri");
236          #diag Dumper($k, $k2);          #diag Dumper($k, $k2);
237          ok( eq_hash( $k, $k2 ), "keywords");          ok( eq_hash( $k1, $k2 ), "keywords");
238  }  }
239    
240  ok(my $hints = $nres->hints, 'hints');  ok(my $hints = $nres->hints, 'hints');
241  diag Dumper($hints);  diag Dumper($hints) if ($debug);
242    foreach my $h (qw/TIME DOCNUM VERSION NODE HIT WORDNUM/) {
243            ok(defined( $nres->hint($h) ), "have hint $h");
244    }
245    
246  ok($node->_set_info, "refresh _set_info");  ok($node->_set_info, "refresh _set_info");
247    
# Line 122  my $v; Line 249  my $v;
249  ok($v = $node->name, "name: $v");  ok($v = $node->name, "name: $v");
250  ok($v = $node->label, "label: $v");  ok($v = $node->label, "label: $v");
251  ok($v = $node->doc_num, "doc_num: $v");  ok($v = $node->doc_num, "doc_num: $v");
252  ok($v = $node->word_num, "word_num: $v");  ok(defined($v = $node->word_num), "word_num: $v");
253  ok($v = $node->size, "size: $v");  ok($v = $node->size, "size: $v");
254    
255  ok($node->set_snippet_width( 100, 10, 10 ), "set_snippet_width");  ok($node->set_snippet_width( 100, 10, 10 ), "set_snippet_width");
256    
257    # 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    cmp_ok($cond->skip, '==', $skip, "skip is $skip");
261    
262    like($node->cond_to_query( $cond ), qr/skip=$skip/, 'cond_to_query have skip');
263    
264    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            my $uri = 'test' . ($i + $delete_num + $skip + 1);
271            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    # test distinct
280    ok($cond = new Search::Estraier::Condition, 'new cond');
281    ok($cond->set_phrase('girl'), 'cond set_phrase');
282    my $distinct = '@title';
283    ok($cond->set_distinct( $distinct ), "cond set_distinct($distinct)");
284    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    
291  # user doesn't exist  # user doesn't exist
292  ok(! $node->set_user('foobar', 1), 'set_user');  ok($node->set_user('foobar', 1), 'set_user');
293    
294  ok(my $node2 = new Search::Estraier::Node( "http://localhost:1978/node/$test2_node" ), "new $test2_node");  ok(my $node2 = new Search::Estraier::Node( "$estmaster_uri/node/$test2_node" ), "new $test2_node");
295  ok($node2->set_auth('admin','admin'), "set_auth $test2_node");  ok($node2->set_auth('admin','admin'), "set_auth $test2_node");
296    
297  # croak_on_error  # croak_on_error
298    
299  ok($node = new Search::Estraier::Node( url => "http://localhost:1978/non-existant", croak_on_error => 1 ), "new non-existant");  ok($node = new Search::Estraier::Node( url => "$estmaster_uri/non-existant", croak_on_error => 1 ), "new non-existant");
300  throws_ok { $node->name } qr/404/, 'croak on error';  throws_ok { $node->name } qr/404/, 'croak on error';
301    
302  # croak_on_error  # croak_on_error
303  ok($node = new Search::Estraier::Node( url => "http://localhost:1978/node/$test1_node", croak_on_error => 1 ), "new $test1_node");  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    
305  ok(! $node->uri_to_id('foobar'), 'uri_to_id without croak');  ok(! $node->uri_to_id('foobar'), 'uri_to_id without croak');
306    
 SKIP: {  
         skip "no $test2_node in Hyper Estraier, skipping set_link", 2 unless (my $test2_label = $node2->label);  
307    
308          ok($node->set_link("http://localhost:1978/node/$test2_node", $test2_label, 42), "set_link $test2_node ($test2_label) 42");  # test users
309          ok($node->set_link("http://localhost:1978/node/$test2_node", $test2_label, 0), "set_link $test2_node ($test2_label) delete");  ok($node->admins, 'have admins');
310  }       # SKIP 2  ok(! $node->guests, 'no guests');
311    
312    
313    # 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    # now, test links
320    my $test2_label = "$test2_node label";
321    my $link_url = "$estmaster_uri/node/$test2_node";
322    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    ok($node->set_link("$estmaster_uri/node/$test2_node", $test2_label, 0), "set_link $test2_node ($test2_label) delete");
327    
328    ok($msg = $node->master(
329            action => 'nodeclr',
330            name => $node->name,
331    ), "nodeclr " . $node->name . ": $msg");
332    
333    cmp_ok($node->doc_num, '==', 0, 'no documents');
334    
335    # 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    
343    # test create
344    my $node_name = '_test_create_' . $$;
345    my $node_label = "test $$ label";
346    
347    ok($node = new Search::Estraier::Node(
348            url => "$estmaster_uri/node/$node_name",
349            create => 1,
350            label => $node_label,
351            croak_on_error => 1
352    ), "new create+croak");
353    
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    ok($node = new Search::Estraier::Node(
358            url => "$estmaster_uri/node/$node_name",
359            create => 1,
360            label => $node_label,
361            croak_on_error => 0
362    ), "new create existing");
363    
364    ok($node = new Search::Estraier::Node(
365            url => "$estmaster_uri/node/$node_name",
366            create => 1,
367            label => $node_label,
368            croak_on_error => 1
369    ), "new create+croak existing");
370    
371    # cleanup
372    ok($msg = $node->master(
373            action => 'nodedel',
374            name => $node_name,
375    ), "nodedel $node_name: $msg");
376    
377    # and again, this time without node
378    ok($node = new Search::Estraier::Node(
379            url => "$estmaster_uri/node/$node_name",
380            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    ok($msg = $node->master( action => 'sync' ), "sync: $msg");
395    
396  }       # SKIP 1  } # SKIP
397    
398  diag "over";  diag "over";

Legend:
Removed from v.106  
changed lines
  Added in v.198

  ViewVC Help
Powered by ViewVC 1.1.26