/[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 31 by dpavlin, Thu Jan 5 15:36:25 2006 UTC revision 151 by dpavlin, Tue May 16 11:39:53 2006 UTC
# Line 3  Line 3 
3  use strict;  use strict;
4  use blib;  use blib;
5    
6  use Test::More tests => 9;  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  ok(my $node = new Search::Estraier::Node, 'new');  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');  isa_ok($node, 'Search::Estraier::Node');
20    
21  ok($node->set_url('http://localhost:1978/'), 'set_url');  ok($node->set_url("http://localhost:1978/node/$test1_node"), "set_url $test1_node");
22    
23  ok($node->set_proxy('proxy.example.com', 8080), 'set_proxy');  ok($node->set_proxy('', 8080), 'set_proxy');
24  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';
25    
26  ok($node->set_timeout(42), 'set_timeout');  ok($node->set_timeout(42), 'set_timeout');
27  throws_ok {$node->set_timeout('foo') } qr/timeout/, 'set_timeout NaN';  throws_ok {$node->set_timeout('foo') } qr/timeout/, 'set_timeout NaN';
28    
29  ok($node->set_auth('foo','bar'), 'set_auth');  ok($node->set_auth('admin','admin'), 'set_auth');
30    
31    cmp_ok($node->status, '==', -1, 'status');
32    
33    # test master functionality
34    
35    diag("node->master shutdown not tested");
36    
37    diag("node->master backup not tested");
38    
39    ok(my @users = $node->master( action => 'userlist' ), 'userlist');
40    
41    #diag "users: ", Dumper( \@users );
42    diag "found ", $#users + 1, " users";
43    
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_';
118    @uri=data0
119    @title=Material Girl
120    
121    Living in a material world
122    And I am a material girl
123    You know that we are living in a material world
124    And I am a material girl
125    _END_OF_DRAFT_
126    
127    #diag "draft:\n$draft";
128    ok(my $doc = new Search::Estraier::Document($draft), 'new doc from draft');
129    
130    ok( $node->put_doc($doc), "put_doc data001");
131    
132    for ( 1 .. 17 ) {
133            $doc->add_attr('@uri', 'test' . $_);
134            ok( $node->put_doc($doc), "put_doc test$_");
135            #diag $doc->dump_draft;
136            cmp_ok( $node->doc_num, '==', ($_ + 1), "node->doc_num " . ($_ + 1));
137    }
138    
139    ok(! $node->uri_to_id( 'does-not-exists' ), "non-existant uri_to_id");
140    
141    my $id;
142    ok($id = $node->uri_to_id( 'data0' ), "uri_to_id(data0)");
143    
144    ok($doc = $node->get_doc( $id ), "get_doc($id) for edit");
145    $doc->add_attr('foo', 'bar');
146    #diag Dumper($doc);
147    ok( $node->edit_doc( $doc ), 'edit_doc');
148    
149    my $doc_num;
150    ok( $doc_num = $node->doc_num, "node->doc_num $doc_num");
151    
152    ok( $node->out_doc( $id ), "out_doc($id)");
153    
154    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');
171    ok($cond->set_phrase('girl'), 'cond set_phrase');
172    ok($cond->set_max($max), "cond set_max($max)");
173    ok($cond->set_order('@uri ASCD'), 'cond set_order');
174    ok($cond->add_attr('@title STRINC Material'), 'cond add_attr');
175    
176    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');
177    
178    ok( my $nres = $node->search( $cond, 0 ), 'search');
179    
180    isa_ok( $nres, 'Search::Estraier::NodeResult' );
181    
182    cmp_ok($nres->doc_num, '==', $max, "nres->doc_num $max");
183    
184    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) ) {
189            my $num = $i + $delete_num + 1;
190            my $uri = 'test' . $num;
191    
192            if ($i < $nres->doc_num) {
193                    ok( my $rdoc = $nres->get_doc( $i ), "nres->get_doc $i");
194    
195                    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");
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)");
204            ok( $node->get_doc( $id ), "get_doc($id)");
205            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");
207            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");
209            ok( my $k2 = $node->etch_doc_by_uri( $uri ), "etch_doc_by_uri $uri");
210            #diag Dumper($k, $k2);
211            ok( eq_hash( $k1, $k2 ), "keywords");
212    }
213    
214    ok(my $hints = $nres->hints, 'hints');
215    diag Dumper($hints);
216    
217    ok($node->_set_info, "refresh _set_info");
218    
219    my $v;
220    ok($v = $node->name, "name: $v");
221    ok($v = $node->label, "label: $v");
222    ok($v = $node->doc_num, "doc_num: $v");
223    ok(defined($v = $node->word_num), "word_num: $v");
224    ok($v = $node->size, "size: $v");
225    
226    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
249    ok(! $node->set_user('foobar', 1), 'set_user');
250    
251    ok(my $node2 = new Search::Estraier::Node( "http://localhost:1978/node/$test2_node" ), "new $test2_node");
252    ok($node2->set_auth('admin','admin'), "set_auth $test2_node");
253    
254    # croak_on_error
255    
256    ok($node = new Search::Estraier::Node( url => "http://localhost:1978/non-existant", croak_on_error => 1 ), "new non-existant");
257    throws_ok { $node->name } qr/404/, 'croak on error';
258    
259    # croak_on_error
260    ok($node = new Search::Estraier::Node( url => "http://localhost:1978/node/$test1_node", croak_on_error => 1 ), "new $test1_node");
261    
262    ok(! $node->uri_to_id('foobar'), 'uri_to_id without croak');
263    
264    
265    # test users
266    ok(! $node->admins, 'no admins');
267    ok(! $node->guests, 'no guests');
268    
269    
270    # test search without results
271    ok($cond = new Search::Estraier::Condition, 'new cond');
272    ok($cond->set_phrase('this_is_phrase_which_does_not_exits'), 'cond set_phrase');
273    
274    ok($nres = $node->search( $cond, 0 ), 'search');
275    
276    # now, test links
277    my $test2_label = "$test2_node label";
278    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    # test create
301    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    ok($msg = $node->master( action => 'sync' ), "sync: $msg");
352    
353    diag "over";

Legend:
Removed from v.31  
changed lines
  Added in v.151

  ViewVC Help
Powered by ViewVC 1.1.26