1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
|
use Test::More tests => 20; |
|
|
use Test::Exception; |
|
|
use Cwd qw/abs_path/; |
|
|
use KinoSearch; |
|
|
use File::Slurp; |
|
|
use Data::Dump qw/dump/; |
|
|
use blib; |
|
3 |
use strict; |
use strict; |
4 |
|
use blib; |
5 |
|
|
6 |
|
use Test::More tests => 35; |
7 |
|
|
8 |
|
use KinoSearch; |
9 |
|
use Encode qw/decode/; |
10 |
|
|
11 |
BEGIN { |
BEGIN { |
12 |
|
use_ok( 'WebPAC::Test' ); |
13 |
use_ok( 'WebPAC::Output::KinoSearch' ); |
use_ok( 'WebPAC::Output::KinoSearch' ); |
14 |
} |
} |
15 |
|
|
|
my $debug = shift @ARGV; |
|
|
|
|
|
ok(my $abs_path = abs_path($0), "abs_path"); |
|
|
$abs_path =~ s#/[^/]*$#/#; # |
|
|
diag "abs_path: $abs_path"; |
|
16 |
my $path = "$abs_path/kino/"; |
my $path = "$abs_path/kino/"; |
17 |
|
|
18 |
ok(my $out = new WebPAC::Output::KinoSearch({ |
ok(my $out = new WebPAC::Output::KinoSearch({ |
19 |
path => $path, |
path => $path, |
20 |
database => 'test', |
database => 'test', |
21 |
clean => 1, |
clean => 1, |
22 |
debug => $debug, |
%LOG |
23 |
}), "new"); |
}), "new"); |
24 |
|
|
25 |
ok( $out->init, 'init' ); |
ok( $out->init, 'init' ); |
27 |
my $ds = { |
my $ds = { |
28 |
'Source' => { |
'Source' => { |
29 |
'name' => 'Izvor: ', |
'name' => 'Izvor: ', |
30 |
'search' => [ 'foo' ] |
'search' => [ 'tko zna' ] |
31 |
}, |
}, |
32 |
'ID' => { |
'ID' => { |
33 |
'search' => 'id', |
'search' => 'id', |
35 |
'Array' => { |
'Array' => { |
36 |
'search' => [ qw/a1 a2 s3 a4 a5/ ], |
'search' => [ qw/a1 a2 s3 a4 a5/ ], |
37 |
}, |
}, |
38 |
|
'foo' => { |
39 |
|
'search' => [ 'foo' ], |
40 |
|
}, |
41 |
}; |
}; |
42 |
|
|
43 |
throws_ok { $out->add( ) } qr/need id/, 'add without params'; |
throws_ok { $out->add( ) } qr/need id/, 'add without params'; |
45 |
|
|
46 |
ok( $out->add( 42, $ds ), 'add 42' ); |
ok( $out->add( 42, $ds ), 'add 42' ); |
47 |
|
|
48 |
ok( $out->add( 99, { foo => { search => 'bar' } } ), 'add 99' ); |
my @strange = ( qw/čajšinica odma¹ęivanje ¾abokrečina ¹uma/ ); |
49 |
|
|
50 |
|
ok( $out->add( 99, { foo => { search => [ @strange ] } } ), 'add 99' ); |
51 |
|
|
52 |
ok( $out->add( 100, { foo => { search => [ qw/foo bar baz/ ] } } ), 'add 100' ); |
ok( $out->add( 100, { foo => { search => [ qw/foo bar baz/ ] } } ), 'add 100' ); |
53 |
|
|
58 |
diag $out->path," eq ",$path; |
diag $out->path," eq ",$path; |
59 |
cmp_ok( $out->path, 'eq', $path, 'path' ); |
cmp_ok( $out->path, 'eq', $path, 'path' ); |
60 |
|
|
61 |
my $query_string = 'foo'; |
sub test_search { |
62 |
|
my ( $query_string, $expected_hits ) = @_; |
63 |
|
|
64 |
|
my $total_hits = $index->search( |
65 |
|
query => $query_string, |
66 |
|
offset => 0, |
67 |
|
num_wanted => 10, |
68 |
|
); |
69 |
|
|
70 |
|
ok( $total_hits, "search '$query_string'" ); |
71 |
|
|
72 |
|
diag "Total hits: $total_hits\n" if $debug; |
73 |
|
|
74 |
|
cmp_ok( $total_hits, '==', $expected_hits, 'total_hits' ); |
75 |
|
|
76 |
|
while ( my $hit = $index->fetch_hit_hashref ) { |
77 |
|
ok( $hit, 'hit' ); |
78 |
|
ok( $hit->{foo} =~ m/čajšinica/, 'utf-8 conversion' ); |
79 |
|
diag dump($hit) if $debug; |
80 |
|
} |
81 |
|
|
|
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); |
|
82 |
} |
} |
83 |
|
|
84 |
|
test_search( 'foo', 2 ); |
85 |
|
|
86 |
|
test_search( $_, 1 ) foreach @strange; |
87 |
|
|
88 |
ok( $out->finish, 'finish' ); |
ok( $out->finish, 'finish' ); |
89 |
|
|