1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Plucene::Simple;
7
8use File::Temp qw/ tempdir /;
9my $dir = tempdir(CLEANUP => 1);
10
11use Test::More tests => 2;
12
13#------------------------------------------------------------------------------
14# Helper stuff
15#------------------------------------------------------------------------------
16
17sub data {
18	return ({
19			title    => 'The Strayest of Toasters',
20			starring => 'me',
21			about    => 'My life in the suicide ranks',
22		},
23		{
24			title    => 'The Plasticest of Forks',
25			starring => 'you',
26			about    => "Gravity's Angel",
27		},
28		{
29			title    => 'Poker Nights',
30			starring => 'us',
31			about    => 'How to lose money the easy way',
32		},
33		{
34			title    => 'Fun in the sun',
35			starring => 'me',
36			about    => 'Do we ever really see the sun here?',
37		},
38	);
39}
40
41sub build_index {
42	my $to_delete  = shift;
43	my $pos        = 0;
44	my @programmes = data();
45	my $index      = Plucene::Simple->open($dir);
46	foreach my $ref (@programmes) {
47		$index->delete_document($pos) if $to_delete;
48		$index->add($pos => $ref);
49		$pos++;
50	}
51	$index->optimize;
52}
53
54#------------------------------------------------------------------------------
55# Tests
56#------------------------------------------------------------------------------
57
58build_index();
59my $index = Plucene::Simple->open($dir);
60my @ids   = $index->search("me");
61is_deeply \@ids, [ 0, 3 ], "Correct ids returned";
62
63build_index(1);
64$index = Plucene::Simple->open($dir);
65@ids   = $index->search("me");
66is_deeply \@ids, [ 0, 3 ],
67	"Correct ids returned (after deleting them and reindexing)";
68