2 |
|
|
3 |
use strict; |
use strict; |
4 |
|
|
5 |
use Test::More tests => 8; |
use Test::More tests => 56; |
6 |
use Test::Exception; |
use Test::Exception; |
7 |
use blib; |
use blib; |
8 |
|
|
12 |
|
|
13 |
my $i; |
my $i; |
14 |
|
|
15 |
throws_ok { SWISH::PlusPlus->open() } qr/index_dir/, "need index_dir"; |
throws_ok { SWISH::PlusPlus->new() } qr/index_dir/, "need index_dir"; |
16 |
|
|
17 |
## FIXME |
## FIXME |
18 |
my $index = '/tmp/swish-pp'; |
my $index = '/tmp/swish-pp'; |
19 |
|
my $debug = 0; |
20 |
|
$debug = 4 if (@ARGV); |
21 |
|
|
22 |
$i = SWISH::PlusPlus->open( |
$i = SWISH::PlusPlus->new( |
23 |
index_dir => $index, |
index_dir => $index, |
24 |
|
meta_in_body => 1, |
25 |
|
debug => $debug, |
26 |
); |
); |
27 |
ok($i, "open index"); |
ok($i, "open index"); |
28 |
|
|
29 |
ok(-e $index, "index exist"); |
ok(-e $index, "index exist"); |
30 |
|
|
31 |
|
ok($i->{'cwd'}, "cwd ".$i->{'cwd'}); |
32 |
|
|
33 |
ok($i->check_bin, "swish++ check"); |
ok($i->check_bin, "swish++ check"); |
34 |
|
|
35 |
diag $i->{'version'} || die; |
ok($i->{'version'}, "version ".$i->{'version'}); |
36 |
|
|
37 |
ok($i->index_document( 42 => 'meaning of life' ), "index 42"); |
ok($i->index_document( 42 => 'meaning of life' ), "index 42"); |
38 |
|
|
39 |
|
my $data = { |
40 |
|
path => 'life', |
41 |
|
title => 'Ultimate question answer found here', |
42 |
|
body => '42 is answer to all questions', |
43 |
|
meta => { |
44 |
|
author => 'Dobrica Pavlinusic', |
45 |
|
date => '2004-12-05', |
46 |
|
comment => 'woow!', |
47 |
|
}, |
48 |
|
}; |
49 |
|
|
50 |
|
ok($i->add( %{$data} ), "index life"); |
51 |
|
|
52 |
# add one dummy document so that swish++ won't reject all words |
# add one dummy document so that swish++ won't reject all words |
53 |
# with index of just one document |
# with index of just one document |
54 |
ok($i->index_document( _dummy_ => '' ), "fillter"); |
ok($i->index_document( _dummy_ => '' ), "fillter"); |
55 |
|
|
56 |
cmp_ok($i->{'index_dir'}, 'eq', $index, "index_dir ok"); |
cmp_ok($i->{'index_dir'}, 'eq', $index, "index_dir ok"); |
57 |
|
|
58 |
|
# I would rather test search to close it itself! |
59 |
|
#$i->finish_update; |
60 |
|
|
61 |
|
foreach my $word (qw(meaning of life)) { |
62 |
|
my @r = $i->search($word); |
63 |
|
cmp_ok(scalar @r, '==', 1, "find $word"); |
64 |
|
} |
65 |
|
|
66 |
|
# test_find('words to test against meta','meta', nr_results) |
67 |
|
sub test_find { |
68 |
|
my $words = shift || die "no words?"; |
69 |
|
my $meta = shift; |
70 |
|
my $nr = shift; |
71 |
|
|
72 |
|
$nr = 1 unless defined($nr); |
73 |
|
|
74 |
|
foreach my $word (split(/\s+/,$words)) { |
75 |
|
my $path; |
76 |
|
my $q = $word; |
77 |
|
$q = "$meta=($word)" if ($meta); |
78 |
|
|
79 |
|
my @r = $i->search($q); |
80 |
|
cmp_ok(scalar @r, '==', $nr, "search $q - $nr results"); |
81 |
|
if ($meta) { |
82 |
|
like($i->property(shift @r, $meta), qr/$word/i, "find $word in $meta property"); |
83 |
|
cmp_ok(scalar $i->search("$word"), '==', $nr, "find $word anywhere -- $nr results"); |
84 |
|
} |
85 |
|
|
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
|
test_find($data->{'body'}); |
90 |
|
|
91 |
|
foreach (qw(title author date comment)) { |
92 |
|
test_find(( $data->{$_} || $data->{'meta'}->{$_} ), $_); |
93 |
|
} |
94 |
|
|
95 |
|
ok($i->delete("life"), "delete life"); |
96 |
|
ok($i->{'_deleted'}->{'life'}, "life marked as deleted"); |
97 |
|
|
98 |
|
ok($i->finish_update, "finish_update"); |
99 |
|
|
100 |
|
test_find($data->{'body'}, undef, '0'); |