1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
|
5 |
use Test::More tests => 33; |
6 |
|
7 |
BEGIN { |
8 |
use_ok('DBI'); |
9 |
use_ok('DBD::Pg'); |
10 |
}; |
11 |
|
12 |
# hum? |
13 |
my $connect = "DBI:Pg:dbname=test"; |
14 |
|
15 |
my $dbh = DBI->connect($connect,"","") || die $DBI::errstr; |
16 |
ok($dbh, "dbh"); |
17 |
|
18 |
my $pwd = $0; |
19 |
$pwd =~ s#/[^/]*$##; |
20 |
if ($pwd !~ m#^/#) { |
21 |
my $cwd = `pwd`; |
22 |
chomp($cwd); |
23 |
$pwd = $cwd . '/' . $pwd; |
24 |
} |
25 |
ok($pwd, "pwd: $pwd"); |
26 |
my $index = "$pwd/../data/casket/"; |
27 |
|
28 |
my $sql = "select * from pgest('$index',?,?,?,?)"; |
29 |
diag "$sql"; |
30 |
|
31 |
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
32 |
ok($sth, "sth"); |
33 |
|
34 |
sub pgest { |
35 |
$sth->execute(@_) || die $sth->errstr(); |
36 |
ok($sth, "execute(".join(",",@_).")"); |
37 |
|
38 |
my @arr; |
39 |
while (my $row = $sth->fetchrow_hashref() ) { |
40 |
push @arr, $row; |
41 |
} |
42 |
ok(@arr, "results ".($#arr + 1)); |
43 |
|
44 |
return @arr; |
45 |
} |
46 |
|
47 |
sub estcmd { |
48 |
my $q = shift; |
49 |
my $attr = shift; |
50 |
|
51 |
my $cmd = "estcmd search "; |
52 |
$cmd .= " -attr '$attr' " if ($attr); |
53 |
$q ||= ''; |
54 |
$cmd .= "$index '$q'"; |
55 |
diag $cmd; |
56 |
|
57 |
open(my $fh, "$cmd |") || die "cmd: $!"; |
58 |
while(<$fh>) { |
59 |
if (/^HIT=(\d+)/) { |
60 |
return $1; |
61 |
} |
62 |
} |
63 |
return undef; |
64 |
} |
65 |
|
66 |
# test simple query |
67 |
foreach my $q (qw(blade runner Philip k. dick)) { |
68 |
|
69 |
ok(my $hits = estcmd($q), "estcmd: $q"); |
70 |
|
71 |
diag "$hits hits"; |
72 |
|
73 |
cmp_ok(scalar pgest($q, '', 0, 0), '==', $hits, 'blade runner'); |
74 |
} |
75 |
|
76 |
# test attr query |
77 |
foreach my $q (('@title STRINC Blade Runner', '@title ISTRBW blade runner')) { |
78 |
|
79 |
ok(my $hits = estcmd('',$q), "estcmd: $q"); |
80 |
|
81 |
diag "$hits hits"; |
82 |
|
83 |
cmp_ok(scalar pgest('', $q, 0, 0), '==', $hits, 'blade runner'); |
84 |
} |
85 |
|