3 |
use strict; |
use strict; |
4 |
use blib; |
use blib; |
5 |
|
|
6 |
use Test::More tests => 25; |
use Test::More tests => 55; |
7 |
|
use Test::Exception; |
8 |
|
use Data::Dumper; |
9 |
|
|
10 |
BEGIN { use_ok('Search::Estraier') }; |
BEGIN { use_ok('Search::Estraier') }; |
11 |
|
|
12 |
|
#print Search::Estraier::Document::_s('foo'); |
13 |
|
|
14 |
|
#cmp_ok(Search::Estraier::Document::_s(" this is a text "), 'eq', 'this is a text', '_s - strip spaces'); |
15 |
|
|
16 |
|
my $debug = shift @ARGV; |
17 |
|
|
18 |
my $attr_data = { |
my $attr_data = { |
19 |
'@uri' => 'http://localhost/Search-Estraier/', |
'@uri' => 'http://localhost/Search-Estraier/', |
20 |
'size' => 42, |
'size' => 42, |
21 |
|
'zero' => 0, |
22 |
|
'foo' => 'bar', |
23 |
|
'empty' => '', |
24 |
}; |
}; |
25 |
|
|
26 |
ok(my $estdoc = new Search::Estraier::Document, 'new'); |
my @test_texts = ( |
27 |
|
'This is a test', |
28 |
|
'of pure-perl bindings', |
29 |
|
'for HyperEstraier' |
30 |
|
); |
31 |
|
|
32 |
|
my $vectors = { |
33 |
|
'foo' => 42, |
34 |
|
'bar' => 100, |
35 |
|
'baz' => 0, |
36 |
|
}; |
37 |
|
|
38 |
ok($estdoc->delete, "delete"); |
ok(my $doc = new Search::Estraier::Document, 'new'); |
39 |
|
|
40 |
ok($estdoc = new Search::Estraier::Document, 'new'); |
isa_ok($doc, 'Search::Estraier::Document'); |
41 |
|
|
42 |
foreach my $a (keys %{$attr_data}) { |
cmp_ok($doc->id, '==', -1, 'id'); |
|
ok($estdoc->add_attr($a, $attr_data->{$a}), "add_attr $a"); |
|
|
} |
|
43 |
|
|
44 |
ok($estdoc->add_text('This is a demo'), 'add_text'); |
ok($doc->delete, "delete"); |
|
ok($estdoc->add_hidden_text('This is hidden text'), 'add_hidden_text'); |
|
45 |
|
|
46 |
ok(my $draft = $estdoc->dump_draft, 'dump_draft'); |
ok($doc = new Search::Estraier::Document, 'new'); |
47 |
|
|
48 |
diag "dump_draft:\n$draft"; |
foreach my $a (keys %{$attr_data}) { |
49 |
|
my $d = $attr_data->{$a}; |
50 |
|
ok($doc->add_attr($a, $d), "add_attr $a = $d"); |
51 |
|
#diag "draft:\n",$doc->dump_draft,Dumper($doc->{attrs}); |
52 |
|
cmp_ok($doc->attr($a), 'eq', $d, "attr $a = $d"); |
53 |
|
} |
54 |
|
|
55 |
|
foreach my $t (@test_texts) { |
56 |
|
ok($doc->add_text($t), "add_text: $t"); |
57 |
|
} |
58 |
|
|
59 |
ok(my $estdoc2 = new_from_draft Search::Estraier::Document($draft), 'new_from_draft'); |
ok($doc->add_hidden_text('This is hidden text'), 'add_hidden_text'); |
|
cmp_ok($estdoc2->dump_draft, 'eq', $draft, 'drafts same'); |
|
60 |
|
|
61 |
cmp_ok($estdoc->id, '==', -1, 'id'); |
ok($doc->add_vectors( %{ $vectors } ), 'add_vectors'); |
|
cmp_ok($estdoc2->id, '==', -1, 'id'); |
|
62 |
|
|
63 |
my @attr = $estdoc->attr_names; |
diag "current doc: ", Dumper($doc) if ($debug); |
|
diag "attr_names: ", join(',',@attr), "\n"; |
|
64 |
|
|
65 |
cmp_ok(scalar @attr, '==', 2, 'attr_names'); |
ok(my @texts = $doc->texts, 'texts'); |
66 |
|
|
67 |
ok(! $estdoc->attr('foobar'), "non-existant attr"); |
ok(my $draft = $doc->dump_draft, 'dump_draft'); |
68 |
|
|
69 |
foreach my $a (keys %{$attr_data}) { |
foreach my $a (keys %{$attr_data}) { |
70 |
cmp_ok($attr_data->{$a}, 'eq', $estdoc->attr($a), "attr $a = ".$attr_data->{$a}); |
my $regex = $a . '=' . $attr_data->{$a}; |
71 |
ok($estdoc->add_attr($a, ''), "delete attribute"); |
like($draft, qr/$regex/, "draft has $regex"); |
72 |
} |
} |
73 |
|
|
74 |
#diag "attr_names left: ", join(',',$estdoc->attr_names), "\n"; |
diag "dump_draft:\n$draft" if ($debug); |
|
cmp_ok($estdoc->attr_names, '==' , 0, "attributes removed"); |
|
75 |
|
|
76 |
my @texts = $estdoc->texts; |
ok(my $doc2 = new Search::Estraier::Document($draft), 'new from draft'); |
77 |
diag "texts: ", join(',',@texts), "\n"; |
diag "doc from draft: ", Dumper($doc2) if ($debug); |
78 |
|
cmp_ok($doc2->dump_draft, 'eq', $draft, 'drafts same'); |
79 |
|
|
80 |
cmp_ok(scalar @texts, '==', 1, 'texts'); |
cmp_ok($doc->id, '==', -1, 'id'); |
81 |
|
cmp_ok($doc2->id, '==', -1, 'id'); |
82 |
|
|
83 |
SKIP: { |
ok(my @attr = $doc->attr_names, 'attr_names'); |
84 |
skip "est_keywords not implemented", 2; |
diag "attr_names: ", join(',',@attr), "\n" if ($debug); |
85 |
|
|
86 |
my @keywords = $estdoc->keywords; |
cmp_ok(scalar @attr, '==', keys %{$attr_data}, 'attr_names'); |
|
diag "keywords: ", join(',',@keywords), "\n"; |
|
87 |
|
|
88 |
cmp_ok(scalar @keywords, '==', 1, 'keywords'); |
ok(! $doc->attr('foobar'), "non-existant attr"); |
|
} |
|
89 |
|
|
90 |
|
foreach my $a (keys %{$attr_data}) { |
91 |
|
cmp_ok($doc->attr($a), 'eq', $attr_data->{$a}, "attr $a = ".$attr_data->{$a}); |
92 |
|
ok($doc->add_attr($a, undef), "delete attribute"); |
93 |
|
} |
94 |
|
|
95 |
ok(my $snippet = $estdoc->make_snippet(480, 96, 96, qw(demo is)), "make_snippet"); |
@attr = $doc->attr_names; |
96 |
diag "make_snippet:\n$snippet"; |
diag "attr_names left: ", join(',',$doc->attr_names), "\n" if ($debug); |
97 |
|
cmp_ok(@attr, '==' , 0, "attributes removed"); |
98 |
|
|
99 |
|
diag "texts: ", join(',',@texts), "\n" if ($debug); |
100 |
|
ok(eq_array(\@test_texts, \@texts), 'texts'); |
101 |
|
|
102 |
|
ok(my $cat_text = $doc->cat_texts, 'cat_text'); |
103 |
|
diag "cat_texts: $cat_text" if ($debug); |
104 |
|
|
105 |
|
ok($doc = new Search::Estraier::Document, 'new empty'); |
106 |
|
ok(! $doc->texts, 'texts'); |
107 |
|
cmp_ok($doc->dump_draft, 'eq', "\n", 'dump_draft'); |
108 |
|
cmp_ok($doc->id, '==', -1, 'id'); |
109 |
|
ok(! $doc->attr_names, 'attr_names'); |
110 |
|
ok(! $doc->attr(undef), 'attr'); |
111 |
|
ok(! $doc->cat_texts, 'cat_texts'); |
112 |
|
|
|
ok($estdoc->scan_words(qw(this is demo)), "scan_words"); |
|