--- trunk/t/swish.t 2004/12/03 23:30:22 7 +++ trunk/t/01swish.t 2004/12/07 16:05:43 22 @@ -2,7 +2,7 @@ use strict; -use Test::More tests => 8; +use Test::More tests => 56; use Test::Exception; use blib; @@ -12,26 +12,89 @@ my $i; -throws_ok { SWISH::PlusPlus->open() } qr/index_dir/, "need index_dir"; +throws_ok { SWISH::PlusPlus->new() } qr/index_dir/, "need index_dir"; ## FIXME my $index = '/tmp/swish-pp'; +my $debug = 0; +$debug = 4 if (@ARGV); -$i = SWISH::PlusPlus->open( +$i = SWISH::PlusPlus->new( index_dir => $index, + meta_in_body => 1, + debug => $debug, ); ok($i, "open index"); ok(-e $index, "index exist"); +ok($i->{'cwd'}, "cwd ".$i->{'cwd'}); + ok($i->check_bin, "swish++ check"); -diag $i->{'version'} || die; +ok($i->{'version'}, "version ".$i->{'version'}); ok($i->index_document( 42 => 'meaning of life' ), "index 42"); +my $data = { + path => 'life', + title => 'Ultimate question answer found here', + body => '42 is answer to all questions', + meta => { + author => 'Dobrica Pavlinusic', + date => '2004-12-05', + comment => 'woow!', + }, +}; + +ok($i->add( %{$data} ), "index life"); + # add one dummy document so that swish++ won't reject all words # with index of just one document ok($i->index_document( _dummy_ => '' ), "fillter"); cmp_ok($i->{'index_dir'}, 'eq', $index, "index_dir ok"); + +# I would rather test search to close it itself! +#$i->finish_update; + +foreach my $word (qw(meaning of life)) { + my @r = $i->search($word); + cmp_ok(scalar @r, '==', 1, "find $word"); +} + +# test_find('words to test against meta','meta', nr_results) +sub test_find { + my $words = shift || die "no words?"; + my $meta = shift; + my $nr = shift; + + $nr = 1 unless defined($nr); + + foreach my $word (split(/\s+/,$words)) { + my $path; + my $q = $word; + $q = "$meta=($word)" if ($meta); + + my @r = $i->search($q); + cmp_ok(scalar @r, '==', $nr, "search $q - $nr results"); + if ($meta) { + like($i->property(shift @r, $meta), qr/$word/i, "find $word in $meta property"); + cmp_ok(scalar $i->search("$word"), '==', $nr, "find $word anywhere -- $nr results"); + } + + } +} + +test_find($data->{'body'}); + +foreach (qw(title author date comment)) { + test_find(( $data->{$_} || $data->{'meta'}->{$_} ), $_); +} + +ok($i->delete("life"), "delete life"); +ok($i->{'_deleted'}->{'life'}, "life marked as deleted"); + +ok($i->finish_update, "finish_update"); + +test_find($data->{'body'}, undef, '0');