r1399@llin: dpavlin | 2007-10-31 11:19:39 +0100
[webpac2] / t / 5-output-kinosearch.t
1 #!/usr/bin/perl -w
2
3 use Test::More tests => 15;
4 use Test::Exception;
5 use Cwd qw/abs_path/;
6 use KinoSearch;
7 use File::Slurp;
8 use Data::Dump qw/dump/;
9 use blib;
10 use strict;
11
12 BEGIN {
13 use_ok( 'WebPAC::Output::KinoSearch' );
14 }
15
16 my $debug = shift @ARGV;
17
18 ok(my $abs_path = abs_path($0), "abs_path");
19 $abs_path =~ s#/[^/]*$#/#; #
20 diag "abs_path: $abs_path";
21 my $path = "$abs_path/kino/";
22
23 ok(my $out = new WebPAC::Output::KinoSearch({
24         path => $path,
25         database => 'test',
26         clean => 1,
27         debug => $debug,
28 }), "new");
29
30 ok( $out->init, 'init' );
31
32 my $ds = {
33         'Source' => {
34                 'name' => 'Izvor: ',
35                 'search' => [ 'foo' ]
36         },
37         'ID' => {
38                 'search' => 'id',
39         },
40         'Array' => {
41                 'search' => [ qw/a1 a2 s3 a4 a5/ ],
42         },
43 };
44
45 throws_ok { $out->add( ) } qr/need id/, 'add without params';
46 throws_ok { $out->add( 42 ) } qr/need ds/, 'add without ds';
47
48 ok( $out->add( 42, $ds ), 'add 42' );
49
50 ok( $out->add( 99, { foo => { search => 'bar' } } ), '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 my $query_string = 'foo';
62
63 my $total_hits = $index->search(
64         query      => $query_string,
65         offset     => 0,
66         num_wanted => 10,
67 );
68                                                                                                                    
69 diag "Total hits: $total_hits\n";
70 while ( my $hit = $index->fetch_hit_hashref ) {
71         ok( $hit, 'hit' );
72         diag dump($hit);
73 }
74
75 ok( $out->finish, 'finish' );
76