1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
|
5 |
use Test::More tests => 47; |
6 |
use Test::Exception; |
7 |
use lib '.'; |
8 |
use blib; |
9 |
use YAML; |
10 |
|
11 |
BEGIN { |
12 |
use_ok('SWISH::PlusPlus') |
13 |
}; |
14 |
|
15 |
my $i; |
16 |
|
17 |
throws_ok { SWISH::PlusPlus->new() } qr/index_dir/, "need index_dir"; |
18 |
|
19 |
## FIXME |
20 |
my $index = '/tmp/swish-pp'; |
21 |
|
22 |
$i = SWISH::PlusPlus->new( |
23 |
index_dir => $index, |
24 |
meta_in_body => 1, |
25 |
# debug => 1, |
26 |
); |
27 |
ok($i, "open index"); |
28 |
|
29 |
ok(-e $index, "index exist"); |
30 |
|
31 |
ok($i->{'cwd'}, "cwd ".$i->{'cwd'}); |
32 |
|
33 |
ok($i->check_bin, "swish++ check"); |
34 |
|
35 |
ok($i->{'version'}, "version ".$i->{'version'}); |
36 |
|
37 |
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 |
print Dump( $data ); |
51 |
|
52 |
ok($i->add( %{$data} ), "index life"); |
53 |
|
54 |
# add one dummy document so that swish++ won't reject all words |
55 |
# with index of just one document |
56 |
ok($i->index_document( _dummy_ => '' ), "fillter"); |
57 |
|
58 |
cmp_ok($i->{'index_dir'}, 'eq', $index, "index_dir ok"); |
59 |
|
60 |
# I would rather test search to close it itself! |
61 |
#$i->finish_update; |
62 |
|
63 |
foreach my $word (qw(meaning of life)) { |
64 |
my @r = $i->search($word); |
65 |
cmp_ok(scalar @r, '==', 1, "find $word"); |
66 |
} |
67 |
|
68 |
# test_find('words to test against meta','meta') |
69 |
sub test_find { |
70 |
my $words = shift || die "no words?"; |
71 |
my $meta = shift; |
72 |
|
73 |
foreach my $word (split(/\s+/,$words)) { |
74 |
my $path; |
75 |
my $q = $word; |
76 |
$q = "$meta=($word)" if ($meta); |
77 |
|
78 |
my @r = $i->search($q); |
79 |
cmp_ok(scalar @r, '==', 1, "search $q"); |
80 |
if ($meta) { |
81 |
like($i->property(shift @r, $meta), qr/$word/i, "find $word in $meta property"); |
82 |
cmp_ok(scalar $i->search("$word"), '==', 1, "find $word anywhere"); |
83 |
} |
84 |
|
85 |
} |
86 |
} |
87 |
|
88 |
test_find($data->{'body'}); |
89 |
|
90 |
foreach (qw(title author date comment)) { |
91 |
test_find(( $data->{$_} || $data->{'meta'}->{$_} ), $_); |
92 |
} |
93 |
|