1#!/usr/bin/perl -w 2 3=head1 NAME 4 5t/search1.t - test Plucene::Simple 6 7=head1 DESCRIPTION 8 9Test indexing, searching and deleting from an index. 10 11=cut 12 13use strict; 14use warnings; 15 16use Plucene::Simple; 17 18use File::Path; 19use Test::More tests => 13; 20 21use constant DIRECTORY => "/tmp/testindex/$$"; 22 23END { rmtree DIRECTORY } 24 25#------------------------------------------------------------------------------ 26# Helper stuff 27#------------------------------------------------------------------------------ 28 29sub data { 30 return [ 31 wsc => { name => "Writing Solid Code" }, 32 rap => { name => "Rapid Development" }, 33 gui => { name => "GUI Bloopers" }, 34 ora => { name => "Using Oracle 8i" }, 35 app => { name => "Advanced Perl Programming" }, 36 xpe => { name => "Extreme Programming Explained" }, 37 boo => { name => "Boo-Hoo" }, 38 dbs => { name => "Designing From Both Sides of the Screen" }, 39 dbi => { name => "Programming the Perl DBI" }, 40 ]; 41} 42 43#------------------------------------------------------------------------------ 44# Tests 45#------------------------------------------------------------------------------ 46 47{ # Add some stuff into the index 48 my @data = @{ data() }; 49 isa_ok my $plucy = Plucene::Simple->open(DIRECTORY) => 'Plucene::Simple'; 50 $plucy->add(@data); 51} 52 53{ # search the index 54 my $plucy = Plucene::Simple->open(DIRECTORY); 55 my @docs = $plucy->search("name:perl"); 56 is @docs, 2, "2 results for Perl"; 57 is_deeply \@docs, [ "app", "dbi" ], "The correct ones"; 58 @docs = $plucy->search("name:illusions"); 59 is @docs, 0, "No results for 'illusions'"; 60} 61 62{ # index another document 63 my $plucy = Plucene::Simple->open(DIRECTORY); 64 $plucy->index_document('boi', 'The Book of Illusions'); 65 my @docs = $plucy->search("illusions"); 66 is @docs, 1, "One result for illusions"; 67 is_deeply \@docs, ["boi"], "...the correct one"; 68} 69 70{ # delete a document 71 my $plucy = Plucene::Simple->open(DIRECTORY); 72 my @docs = $plucy->search("name:oracle"); 73 is @docs, 1, "One result for oracle"; 74 is_deeply \@docs, ["ora"], "...the correct one"; 75 $plucy->delete_document('ora'); 76 @docs = $plucy->search("name:oracle"); 77 is @docs, 0, "No results for oracle (after deletion)"; 78} 79 80{ # bogus searches 81 my $plucy = Plucene::Simple->open(DIRECTORY); 82 my @docs = $plucy->search; 83 is scalar @docs, 0, "No results for no search string"; 84 @docs = $plucy->search("foo:bar"); 85 is scalar @docs, 0, "No results for foo:bar"; 86 87} 88 89{ # Terms not in default text field 90 my $plucy = Plucene::Simple->open(DIRECTORY); 91 $plucy->add( 92 foo => { 93 name => "The art of Unix programming", 94 author => "Eric Raymond" 95 }); 96 my @docs = $plucy->search("raymond"); 97 is @docs, 1, "One result for raymond"; 98 is_deeply \@docs, ["foo"], "...the correct one"; 99} 100