2 |
|
|
3 |
use strict; |
use strict; |
4 |
|
|
5 |
use Test::More tests => 25; |
use Test::More tests => 67; |
6 |
|
|
7 |
BEGIN { |
BEGIN { |
8 |
use_ok('DBI'); |
use_ok('DBI'); |
25 |
ok($pwd, "pwd: $pwd"); |
ok($pwd, "pwd: $pwd"); |
26 |
my $index = "$pwd/../data/casket/"; |
my $index = "$pwd/../data/casket/"; |
27 |
|
|
28 |
my $sql = "select * from pgest('$index',?,?,?,?)"; |
my $sql = "select * from pgest('$index',?,?,?,?,?) as (id text)"; |
29 |
diag "$sql"; |
diag "$sql"; |
30 |
|
|
31 |
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
32 |
ok($sth, "sth"); |
ok($sth, "sth"); |
33 |
|
|
34 |
sub pgest { |
sub pgest { |
35 |
$sth->execute(@_) || die $sth->errstr(); |
$sth->execute(@_, "{'\@id'}" ) || die $sth->errstr(); |
36 |
ok($sth, "execute(".join(",",@_).")"); |
{ |
37 |
|
no warnings; |
38 |
|
ok($sth, "execute(".join(",",@_).")"); |
39 |
|
} |
40 |
|
|
41 |
my @arr; |
my @arr; |
42 |
while (my $row = $sth->fetchrow_hashref() ) { |
while (my $row = $sth->fetchrow_hashref() ) { |
49 |
|
|
50 |
sub estcmd { |
sub estcmd { |
51 |
my $q = shift; |
my $q = shift; |
52 |
|
my $attr = shift; |
53 |
|
|
54 |
my $cmd = "estcmd search $index '$q'"; |
my $cmd = "estcmd search "; |
55 |
|
$cmd .= " -attr '$attr' " if ($attr); |
56 |
|
$q ||= ''; |
57 |
|
$cmd .= "$index '$q'"; |
58 |
diag $cmd; |
diag $cmd; |
59 |
|
|
60 |
open(my $fh, "$cmd |") || die "cmd: $!"; |
open(my $fh, "$cmd |") || die "cmd: $!"; |
66 |
return undef; |
return undef; |
67 |
} |
} |
68 |
|
|
69 |
foreach my $q (qw(blade runner Philip filip dick)) { |
# test simple query |
70 |
|
foreach my $q (qw(blade runner Philip k. dick)) { |
71 |
|
|
72 |
ok(my $hits = estcmd($q), "estcmd: $q"); |
ok(my $hits = estcmd($q), "estcmd: $q"); |
73 |
|
|
75 |
|
|
76 |
cmp_ok(scalar pgest($q, '', 0, 0), '==', $hits, 'blade runner'); |
cmp_ok(scalar pgest($q, '', 0, 0), '==', $hits, 'blade runner'); |
77 |
} |
} |
78 |
|
|
79 |
|
# test attr query |
80 |
|
foreach my $q (('@title STRINC Blade Runner', '@title ISTRBW blade runner')) { |
81 |
|
|
82 |
|
ok(my $hits = estcmd('',$q), "estcmd: $q"); |
83 |
|
|
84 |
|
diag "$hits hits"; |
85 |
|
|
86 |
|
cmp_ok(scalar pgest('', $q, 0, 0), '==', $hits, 'blade runner'); |
87 |
|
} |
88 |
|
|
89 |
|
diag "Error handling test follows, ignore messages..."; |
90 |
|
# test NULL handling |
91 |
|
ok(! $dbh->do(qq`select * from pgest(null, '', '', 0, 0, array['\@id']) as (id text)`), "null index_path"); |
92 |
|
ok(my $hits = pgest('blade runner', '', 0, 0), "test search"); |
93 |
|
cmp_ok($hits, '==', pgest('blade runner', undef, 0, 0), "null attr"); |
94 |
|
cmp_ok($hits, '==', pgest('blade runner', '', undef, 0), "null limit"); |
95 |
|
cmp_ok($hits, '==', pgest('blade runner', '', 0, undef), "null offset"); |
96 |
|
cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef), "null optional"); |
97 |
|
|
98 |
|
# test limit, offset and global mess |
99 |
|
my $d = int($hits / 3); |
100 |
|
cmp_ok($d, '==', pgest('blade runner',undef, $d, undef), "limit $d"); |
101 |
|
cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef), "check"); |
102 |
|
|
103 |
|
cmp_ok(($hits - $d), '==', pgest('blade runner',undef, undef, $d), "offset $d"); |
104 |
|
cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef), "check"); |
105 |
|
|
106 |
|
cmp_ok(($hits - $d - $d), '==', pgest('blade runner',undef, ($hits - $d), $d), "limit $d offset $d"); |
107 |
|
cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef), "check"); |