1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
|
5 |
use Test::More tests => 94; |
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 id from pgest('$index',?,?,?,?,?,array['\@id']) as (id text)"; |
29 |
diag "$sql"; |
30 |
|
31 |
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
32 |
ok($sth, "sth"); |
33 |
|
34 |
my $sql_node = "select id from pgest('http://localhost:1978/node/trivia','admin','admin',0,?,?,?,?,?,array['\@id']) as (id text)"; |
35 |
my $sth_node = $dbh->prepare($sql_node) || die $dbh->errstr(); |
36 |
ok($sth_node, "sth_node"); |
37 |
|
38 |
sub pgest { |
39 |
$sth->execute(@_) || die "FATAL ERROR: direct " . $sth->errstr(); |
40 |
$sth_node->execute(@_) || die "FATAL ERROR: node ". $sth_node->errstr(); |
41 |
{ |
42 |
no warnings; |
43 |
ok($sth, "execute(".join(",",@_).")"); |
44 |
ok($sth_node, "execute on node"); |
45 |
} |
46 |
|
47 |
my @arr; |
48 |
while (my ($id) = $sth->fetchrow_array() ) { |
49 |
push @arr, $id; |
50 |
} |
51 |
my @arr_node; |
52 |
while (my ($id) = $sth_node->fetchrow_array() ) { |
53 |
push @arr_node, $id; |
54 |
} |
55 |
cmp_ok($#arr, '==', $#arr_node, "pgest direct and node results"); |
56 |
|
57 |
return @arr; |
58 |
} |
59 |
|
60 |
sub estcmd { |
61 |
my $q = shift; |
62 |
my $attr = shift; |
63 |
|
64 |
my $cmd = "estcmd search "; |
65 |
$cmd .= " -attr '$attr' " if ($attr); |
66 |
$cmd .= " -max 999999 "; # why do I need this? |
67 |
$q ||= ''; |
68 |
$cmd .= "$index '$q'"; |
69 |
diag $cmd; |
70 |
|
71 |
open(my $fh, "$cmd |") || die "cmd: $!"; |
72 |
my $del = <$fh>; |
73 |
chomp($del); |
74 |
while(<$fh>) { |
75 |
last if (/^\Q$del\E/); |
76 |
} |
77 |
my @arr; |
78 |
while(<$fh>) { |
79 |
chomp; |
80 |
last if (/^\Q$del\E/); |
81 |
push @arr, $_; |
82 |
} |
83 |
|
84 |
return @arr; |
85 |
} |
86 |
|
87 |
# test simple query |
88 |
foreach my $q (qw(blade runner Philip k. dick)) { |
89 |
|
90 |
ok(my $hits = estcmd($q), "estcmd: $q"); |
91 |
|
92 |
diag "$hits hits"; |
93 |
|
94 |
cmp_ok(scalar pgest($q, '', undef, 0, 0), '==', $hits, 'blade runner'); |
95 |
} |
96 |
|
97 |
# test attr query |
98 |
foreach my $q (('@title STRINC Blade Runner', '@title ISTRBW blade runner')) { |
99 |
|
100 |
ok(my $hits = estcmd('',$q), "estcmd: $q"); |
101 |
|
102 |
diag "$hits hits"; |
103 |
|
104 |
cmp_ok(scalar pgest(undef, $q, undef, 0, 0), '==', $hits, 'blade runner'); |
105 |
} |
106 |
|
107 |
diag "Error handling test follows, ignore messages..."; |
108 |
# test NULL handling |
109 |
ok(! $dbh->do(qq`select * from pgest(null, '', '', null, 0, 0, array['\@id']) as (id text)`), "null index_path"); |
110 |
ok(my $hits = pgest('blade runner', '', undef, 0, 0), "test search"); |
111 |
cmp_ok($hits, '==', pgest('blade runner', undef, undef, 0, 0), "null attr"); |
112 |
cmp_ok($hits, '==', pgest('blade runner', '', undef, undef, 0), "null limit"); |
113 |
cmp_ok($hits, '==', pgest('blade runner', '', undef, 0, undef), "null offset"); |
114 |
cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef, undef), "null optional"); |
115 |
|
116 |
# test limit, offset and global mess |
117 |
my $d = int($hits / 3); |
118 |
cmp_ok($d, '==', pgest('blade runner',undef, undef, $d, undef), "limit $d"); |
119 |
cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef, undef), "check"); |
120 |
|
121 |
cmp_ok(($hits - $d), '==', pgest('blade runner',undef, undef, undef, $d), "offset $d"); |
122 |
cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef, undef), "check"); |
123 |
|
124 |
cmp_ok(($hits - $d), '==', pgest('blade runner',undef, undef, ($hits - $d), $d), "limit ".($hits - $d)." offset $d"); |
125 |
cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef, undef), "check"); |
126 |
|
127 |
# test sort |
128 |
my @arr_asc = pgest('blade runner', undef, '@id NUMA', undef, undef); |
129 |
my @arr_desc = pgest('blade runner', undef, '@id NUMD', undef, undef); |
130 |
|
131 |
cmp_ok(@arr_asc, '==', @arr_desc, "same number of results"); |
132 |
my $errors = 0; |
133 |
foreach my $i (0 .. $#arr_asc) { |
134 |
my ($a, $b) = ($arr_asc[$i], $arr_desc[$#arr_desc - $i]); |
135 |
if ($a ne $b) { |
136 |
$errors++; |
137 |
diag "element $i: $a != $b"; |
138 |
} |
139 |
} |
140 |
|
141 |
cmp_ok($errors, '==', 0, "errors in ordering"); |