3 |
use strict; |
use strict; |
4 |
use blib; |
use blib; |
5 |
|
|
6 |
use Test::More tests => 11; |
use Test::More tests => 120; |
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 = 1; |
13 |
|
|
14 |
# name of node for test |
# name of node for test |
15 |
my $test_node = 'Search-Estraier'; |
my $test1_node = 'test1'; |
16 |
|
my $test2_node = 'test2'; |
17 |
|
|
18 |
ok(my $node = new Search::Estraier::Node, 'new'); |
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'); |
cmp_ok($node->status, '==', -1, 'status'); |
32 |
|
|
33 |
|
# test master functionality |
34 |
|
|
35 |
|
diag "not testing shutdown\n"; |
36 |
|
|
37 |
|
ok(my $foo = $node->master( action => 'sync' ), 'sync'); |
38 |
|
|
39 |
|
diag "not testing backup\n"; |
40 |
|
|
41 |
|
ok(my @users = $node->master( action => 'userlist' ), 'userlist'); |
42 |
|
|
43 |
|
diag "users: ", Dumper( \@users ); |
44 |
|
|
45 |
|
SKIP: { |
46 |
|
|
47 |
|
skip "no $test1_node node in Hyper Estraier", 110, unless($node->name); |
48 |
|
|
49 |
|
my @res = ( -1, 200 ); |
50 |
|
|
51 |
my $nodelist; |
my $nodelist; |
52 |
cmp_ok($node->shuttle_url( '?action=nodelist', 'text/plain', undef, \$nodelist), |
foreach my $url (qw{?action=nodelist http://localhost:1978/master?action=nodelist}) { |
53 |
'==', 200, 'nodelist'); |
cmp_ok( |
54 |
|
$node->shuttle_url( $url, 'text/plain', undef, \$nodelist) |
55 |
|
,'==', shift @res, 'nodelist'); |
56 |
|
} |
57 |
|
|
58 |
my $draft = <<'_END_OF_DRAFT_'; |
my $draft = <<'_END_OF_DRAFT_'; |
59 |
@uri=data001 |
@uri=data001 |
65 |
And I am a material girl |
And I am a material girl |
66 |
_END_OF_DRAFT_ |
_END_OF_DRAFT_ |
67 |
|
|
68 |
diag "draft:\n$draft"; |
#diag "draft:\n$draft"; |
69 |
|
ok(my $doc = new Search::Estraier::Document($draft), 'new doc from draft'); |
70 |
|
|
71 |
|
ok( $node->put_doc($doc), "put_doc data001"); |
72 |
|
|
73 |
|
for ( 1 .. 10 ) { |
74 |
|
$doc->add_attr('@uri', 'test' . $_); |
75 |
|
ok( $node->put_doc($doc), "put_doc test$_"); |
76 |
|
#diag $doc->dump_draft; |
77 |
|
} |
78 |
|
|
79 |
|
my $id; |
80 |
|
ok($id = $node->uri_to_id( 'data001' ), "uri_to_id = $id"); |
81 |
|
|
82 |
|
my $data_max = 5; |
83 |
|
|
84 |
|
for ( 1 .. $data_max ) { |
85 |
|
ok( $node->out_doc_by_uri( 'test' . $_ ), "out_doc_by_uri test$_"); |
86 |
|
} |
87 |
|
|
88 |
|
ok($doc = $node->get_doc( $id ), 'get_doc for edit'); |
89 |
|
$doc->add_attr('foo', 'bar'); |
90 |
|
#diag Dumper($doc); |
91 |
|
ok( $node->edit_doc( $doc ), 'edit_doc'); |
92 |
|
|
93 |
|
ok( $node->out_doc( $id ), "out_doc $id"); |
94 |
|
|
95 |
|
ok( ! $node->edit_doc( $doc ), "edit removed"); |
96 |
|
|
97 |
|
my $max = 3; |
98 |
|
|
99 |
|
ok(my $cond = new Search::Estraier::Condition, 'new cond'); |
100 |
|
ok($cond->set_phrase('girl'), 'cond set_phrase'); |
101 |
|
ok($cond->set_max($max), "cond set_max $max"); |
102 |
|
ok($cond->set_order('@uri ASCD'), 'cond set_order'); |
103 |
|
ok($cond->add_attr('@title STRINC Material'), 'cond add_attr'); |
104 |
|
|
105 |
|
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'); |
106 |
|
|
107 |
|
ok( my $nres = $node->search( $cond, 0 ), 'search'); |
108 |
|
|
109 |
|
isa_ok( $nres, 'Search::Estraier::NodeResult' ); |
110 |
|
|
111 |
|
cmp_ok($nres->doc_num, '==', $max, "doc_num = $max"); |
112 |
|
|
113 |
|
cmp_ok($nres->hits, '==', $data_max, "hits"); |
114 |
|
|
115 |
|
for my $i ( 0 .. ($nres->hits - 1) ) { |
116 |
|
my $uri = 'test' . ($i + $data_max + 1); |
117 |
|
|
118 |
|
if ($i < $nres->doc_num) { |
119 |
|
ok( my $rdoc = $nres->get_doc( $i ), "get_doc $i"); |
120 |
|
|
121 |
|
cmp_ok( $rdoc->attr('@uri'), 'eq', $uri, "\@uri = $uri"); |
122 |
|
ok( my $k = $rdoc->keywords( $id ), "rdoc keywords"); |
123 |
|
} |
124 |
|
|
125 |
|
ok( my $id = $node->uri_to_id( $uri ), "uri_to_id($uri) = $id"); |
126 |
|
ok( $node->get_doc( $id ), "get_doc $id"); |
127 |
|
ok( $node->get_doc_by_uri( $uri ), "get_doc_by_uri $uri"); |
128 |
|
cmp_ok( $node->get_doc_attr( $id, '@uri' ), 'eq', $uri, "get_doc_attr $id"); |
129 |
|
cmp_ok( $node->get_doc_attr_by_uri( $uri, '@uri' ), 'eq', $uri, "get_doc_attr $id"); |
130 |
|
ok( my $k1 = $node->etch_doc( $id ), "etch_doc_by_uri $uri"); |
131 |
|
ok( my $k2 = $node->etch_doc_by_uri( $uri ), "etch_doc_by_uri $uri"); |
132 |
|
#diag Dumper($k, $k2); |
133 |
|
ok( eq_hash( $k1, $k2 ), "keywords"); |
134 |
|
} |
135 |
|
|
136 |
|
ok(my $hints = $nres->hints, 'hints'); |
137 |
|
diag Dumper($hints); |
138 |
|
|
139 |
|
ok($node->_set_info, "refresh _set_info"); |
140 |
|
|
141 |
|
my $v; |
142 |
|
ok($v = $node->name, "name: $v"); |
143 |
|
ok($v = $node->label, "label: $v"); |
144 |
|
ok($v = $node->doc_num, "doc_num: $v"); |
145 |
|
ok(defined($v = $node->word_num), "word_num: $v"); |
146 |
|
ok($v = $node->size, "size: $v"); |
147 |
|
|
148 |
|
ok($node->set_snippet_width( 100, 10, 10 ), "set_snippet_width"); |
149 |
|
|
150 |
|
# user doesn't exist |
151 |
|
ok(! $node->set_user('foobar', 1), 'set_user'); |
152 |
|
|
153 |
|
ok(my $node2 = new Search::Estraier::Node( "http://localhost:1978/node/$test2_node" ), "new $test2_node"); |
154 |
|
ok($node2->set_auth('admin','admin'), "set_auth $test2_node"); |
155 |
|
|
156 |
|
# croak_on_error |
157 |
|
|
158 |
|
ok($node = new Search::Estraier::Node( url => "http://localhost:1978/non-existant", croak_on_error => 1 ), "new non-existant"); |
159 |
|
throws_ok { $node->name } qr/404/, 'croak on error'; |
160 |
|
|
161 |
|
# croak_on_error |
162 |
|
ok($node = new Search::Estraier::Node( url => "http://localhost:1978/node/$test1_node", croak_on_error => 1 ), "new $test1_node"); |
163 |
|
|
164 |
|
ok(! $node->uri_to_id('foobar'), 'uri_to_id without croak'); |
165 |
|
|
166 |
|
# test users |
167 |
|
|
168 |
|
ok(! $node->admins, 'no admins'); |
169 |
|
ok(! $node->guests, 'no guests'); |
170 |
|
|
171 |
|
# test search without results |
172 |
|
|
173 |
|
ok($cond = new Search::Estraier::Condition, 'new cond'); |
174 |
|
ok($cond->set_phrase('this_is_phrase_which_does_not_exits'), 'cond set_phrase'); |
175 |
|
|
176 |
|
ok($nres = $node->search( $cond, 0 ), 'search'); |
177 |
|
|
178 |
|
SKIP: { |
179 |
|
skip "no $test2_node in Hyper Estraier, skipping set_link", 5 unless (my $test2_label = $node2->label); |
180 |
|
|
181 |
|
my $link_url = "http://localhost:1978/node/$test2_node"; |
182 |
|
|
183 |
|
ok($node->set_link( $link_url, $test2_label, 42), "set_link $test2_node ($test2_label) 42"); |
184 |
|
|
185 |
|
ok(my $links = $node->links, 'links'); |
186 |
|
|
187 |
|
cmp_ok($#{$links}, '==', 0, 'one link'); |
188 |
|
|
189 |
|
like($links->[0], qr/^$link_url/, 'link correct'); |
190 |
|
|
191 |
|
ok($node->set_link("http://localhost:1978/node/$test2_node", $test2_label, 0), "set_link $test2_node ($test2_label) delete"); |
192 |
|
} # SKIP 2 |
193 |
|
|
194 |
|
} # SKIP 1 |
195 |
|
|
196 |
|
diag "over"; |