Revision 8 (by dpavlin, 2004/12/19 03:06:01) new api:
- renamed open to open_index
- removed dependency on IPC::Run
- tests which all pass
#!/usr/bin/perl -w

use strict;

use Test::More tests => 93;
use Test::Exception;
use File::Temp qw/ :mktemp /;
use blib;

BEGIN {
	use_ok('SWISH::Split');
	use_ok('SWISH::API');
};

# FIXME debug
system "rm -Rf /tmp/swish?????";
system "rm -Rf /tmp/swish?????.prop";
system "rm -Rf /tmp/swish?????.temp";

my %param;

throws_ok { SWISH::Split->open_index(%param) } qr/slice_name/, "slice_name";

sub slice_hash {
	return $_[0];
};

$param{'slice_name'} = \&slice_hash;
throws_ok { SWISH::Split->open_index(%param) } qr/slices/, "need slices";

$param{'slices'} = 3;
throws_ok { SWISH::Split->open_index(%param) } qr/index/, "need index";

ok($param{'index'} = mktemp("/tmp/swishXXXXX"), "index name");

diag "index path: $param{'index'}\n";

ok(open(F, "> $param{'index'}"), "touch");
close(F);

throws_ok { SWISH::Split->open_index(%param) } qr/dir/, "need dir";

ok(unlink($param{'index'}), "rm");

ok(mkdir($param{'index'}), "mkdir");

$param{'swish_config'} = qq{
PropertyNames foo
};

$param{'debug'} = 1 if (@ARGV);

ok(my $i=SWISH::Split->open_index(%param), "open_index");

cmp_ok(my $s = $i->in_slice("swishpath"), '==', 1, "open_index");

ok(my $config = $i->make_config($s), "make_config");
diag "swish config: $config";

# make temporary index and data names

ok(my $test_index = mktemp("/tmp/swishXXXXX"), "test index name");
diag "test index: $test_index";
ok(my $test_data = mktemp("/tmp/swishXXXXX"), "test data name");
diag "test data: $test_data";

ok(my $xml = $i->to_xml({ foo => 'bar' }), "to_xml");

sub write_test_data($$) {
	my ($path,$xml) = @_;

	use bytes;
	my $l = length($xml);

	diag "xml: $xml [$l bytes]";
	ok(open(DATA, "> $test_data"), "write to test data");
	print DATA "Path-name: $path\nContent-length: $l\n\n$xml";
	ok(close(DATA), "close");
}

write_test_data('testpath',$xml);

# test swish-e binary
ok(my $out =`cat $test_data | swish-e -S prog -f $test_index -c $config 2>&1`, "test config");

like($out, qr/foo/, "found foo");
like($out, qr/testpath/, "found testpath");

diag "swish-e binary o.k.";

# test compatiblity of produced index with SWISH::API

sub swish_search {
	my ($index, $query, $hits, $path, $size, $prop, $val) = @_;

	my $swish = SWISH::API->new($index);
	ok(! $swish->Error, "SWISH::API->new $index");

	ok(my $results = $swish->Query($query), "SWISH::API->Query $query");
	ok(! $swish->Error, "no error");

	cmp_ok($results->Hits, '==', $hits, "got $hits hits");

	ok(my $result = $results->NextResult, "get result");

	SKIP: {
		skip "no results found, skipping property test", 3 unless ($result);

		cmp_ok($result->Property('swishdocpath'), '==', $path, "correct swishdocpath") if ($path);
		cmp_ok($result->Property('swishdocsize'), '==', $size, "correct swishdocsize") if (defined($size));
		cmp_ok($result->Property($prop), '==', $val, "correct data") if (defined($prop) && defined($val));
	}
}

swish_search($test_index, "foo=(bar)", 1, "testpath", length($xml), "foo", "bar");

diag "SWISH::API o.k.";

# now, test slice handling

ok($s = $i->create_slice('testpath'), "create_slice $s");

ok($s = $i->put_slice('testpath', $xml), "put_slice $s");

ok($i->close_slice($s), "close_slice $s");

swish_search($param{'index'}."/$s", "foo=(bar)", 1, "testpath", length($xml)+1, "foo", "bar");

diag "slice $s handling o.k.";

my %slice_files;
ok($s = $i->add('testpath',{ foo => 'bar' }),"add foo [slice $s]");
$slice_files{$s}++;

foreach (1..$param{'slices'} * 10) {
	ok($s = $i->add('testpath'.$_,{ 'foo' => sprintf("bar%04d", $_) }), "add $_ [slice $s]");
	$slice_files{$s}++;
}

cmp_ok($i->done, '==', 3, "finish");

foreach (1..$param{'slices'}) {
	swish_search( $param{'index'}."/$_", "foo=(bar*)", $slice_files{$_}, "testpath", undef, "foo", "bar");
}

#diag "$out";