r1650@llin: dpavlin | 2007-11-20 11:07:57 +0100
[webpac2] / t / 5-output-kinosearch.t
index 64e4792..93754be 100755 (executable)
@@ -1,30 +1,25 @@
 #!/usr/bin/perl -w
 
-use Test::More tests => 15;
-use Test::Exception;
-use Cwd qw/abs_path/;
-use KinoSearch;
-use File::Slurp;
-use Data::Dump qw/dump/;
-use blib;
 use strict;
+use blib;
+
+use Test::More tests => 35;
+
+use KinoSearch;
+use Encode qw/decode/;
 
 BEGIN {
+use_ok( 'WebPAC::Test' );
 use_ok( 'WebPAC::Output::KinoSearch' );
 }
 
-my $debug = shift @ARGV;
-
-ok(my $abs_path = abs_path($0), "abs_path");
-$abs_path =~ s#/[^/]*$#/#; #
-diag "abs_path: $abs_path";
 my $path = "$abs_path/kino/";
 
 ok(my $out = new WebPAC::Output::KinoSearch({
        path => $path,
        database => 'test',
        clean => 1,
-       debug => $debug,
+       %LOG
 }), "new");
 
 ok( $out->init, 'init' );
@@ -32,7 +27,7 @@ ok( $out->init, 'init' );
 my $ds = {
        'Source' => {
                'name' => 'Izvor: ',
-               'search' => [ 'foo' ]
+               'search' => [ 'tko zna' ]
        },
        'ID' => {
                'search' => 'id',
@@ -40,6 +35,9 @@ my $ds = {
        'Array' => {
                'search' => [ qw/a1 a2 s3 a4 a5/ ],
        },
+       'foo' => {
+               'search' => [ 'foo' ],
+       },
 };
 
 throws_ok { $out->add( ) } qr/need id/, 'add without params';
@@ -47,7 +45,9 @@ throws_ok { $out->add( 42 ) } qr/need ds/, 'add without ds';
 
 ok( $out->add( 42, $ds ), 'add 42' );
 
-ok( $out->add( 99, { foo => { search => 'bar' } } ), 'add 99' );
+my @strange = ( qw/èajðinica odma¹æivanje ¾abokreèina ¹uma/ );
+
+ok( $out->add( 99, { foo => { search => [ @strange ] } } ), 'add 99' );
 
 ok( $out->add( 100, { foo => { search => [ qw/foo bar baz/ ] } } ), 'add 100' );
 
@@ -58,19 +58,32 @@ ok( my $index = $out->index, 'have index' );
 diag $out->path," eq ",$path;
 cmp_ok( $out->path, 'eq', $path, 'path' );
 
-my $query_string = 'foo';
-
-my $total_hits = $index->search(
-       query      => $query_string,
-       offset     => 0,
-       num_wanted => 10,
-);
-                                                                                                                  
-diag "Total hits: $total_hits\n";
-while ( my $hit = $index->fetch_hit_hashref ) {
-       ok( $hit, 'hit' );
-       diag dump($hit);
+sub test_search {
+       my ( $query_string, $expected_hits ) = @_;
+
+       my $total_hits = $index->search(
+               query      => $query_string,
+               offset     => 0,
+               num_wanted => 10,
+       );
+
+       ok( $total_hits, "search '$query_string'" );
+
+       diag "Total hits: $total_hits\n" if $debug;
+
+       cmp_ok( $total_hits, '==', $expected_hits, 'total_hits' );
+
+       while ( my $hit = $index->fetch_hit_hashref ) {
+               ok( $hit, 'hit' );
+               ok( $hit->{foo} =~ m/èajðinica/, 'utf-8 conversion' );
+               diag dump($hit) if $debug;
+       }
+
 }
 
+test_search( 'foo', 2 );
+
+test_search( $_, 1 ) foreach @strange;
+
 ok( $out->finish, 'finish' );