added test file
[webpac2] / t / 5-output-kinosearch.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib 'lib';
5
6 use Test::More tests => 35;
7
8 use KinoSearch;
9 use Encode qw/decode/;
10
11 BEGIN {
12 use_ok( 'WebPAC::Test' );
13 use_ok( 'WebPAC::Output::KinoSearch' );
14 }
15
16 my $path = "$abs_path/kino/";
17
18 ok(my $out = new WebPAC::Output::KinoSearch({
19         path => $path,
20         database => 'test',
21         clean => 1,
22         %LOG
23 }), "new");
24
25 ok( $out->init, 'init' );
26
27 my $ds = {
28         'Source' => {
29                 'name' => 'Izvor: ',
30                 'search' => [ 'tko zna' ]
31         },
32         'ID' => {
33                 'search' => 'id',
34         },
35         'Array' => {
36                 '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';
44 throws_ok { $out->add( 42 ) } qr/need ds/, 'add without ds';
45
46 ok( $out->add( 42, $ds ), 'add 42' );
47
48 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' );
53
54 ok( -e $out->path, "created $path" );
55
56 ok( my $index = $out->index, 'have index' );
57
58 diag $out->path," eq ",$path;
59 cmp_ok( $out->path, 'eq', $path, 'path' );
60
61 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
82 }
83
84 test_search( 'foo', 2 );
85
86 test_search( $_, 1 ) foreach @strange;
87
88 ok( $out->finish, 'finish' );
89