#!/usr/bin/perl -w use strict; use Test::More tests => 53; BEGIN { use_ok('DBI'); use_ok('DBD::Pg'); }; # hum? my $connect = "DBI:Pg:dbname=test"; my $dbh = DBI->connect($connect,"","") || die $DBI::errstr; ok($dbh, "dbh"); my $pwd = $0; $pwd =~ s#/[^/]*$##; if ($pwd !~ m#^/#) { my $cwd = `pwd`; chomp($cwd); $pwd = $cwd . '/' . $pwd; } ok($pwd, "pwd: $pwd"); my $index = "$pwd/../data/casket/"; my $node = 'http://localhost:1978/node/trivia'; my $sql = "select id from pgest('$node','admin','admin',0,?,?,?,?,?,array['\@id']) as (id text)"; diag $sql; my $sth = $dbh->prepare($sql) || die $dbh->errstr(); ok($sth, "sth"); sub pgest { $sth->execute(@_) || die "FATAL ERROR: direct " . $sth->errstr(); { no warnings; ok($sth, "execute(".join(",",@_).")"); } my @arr; while (my ($id) = $sth->fetchrow_array() ) { push @arr, $id; } return @arr; } sub estcmd { my ($q,$attr, $order, $limit, $offset) = @_; my $cmd = "estcall search -vu -sf"; $cmd .= " -attr '$attr'" if ($attr); $cmd .= " -ord '$order'" if ($order); if ($limit) { $cmd .= " -max $limit"; } else { $cmd .= " -max 999999"; } $cmd .= " -sk $offset" if ($offset); $q ||= ''; $cmd .= " $node '$q'"; diag $cmd; open(my $fh, "$cmd |") || die "cmd: $!"; my $del = <$fh>; chomp($del); while(<$fh>) { last if (/^\Q$del\E/); } my @arr; while(<$fh>) { chomp; last if (/^\Q$del\E/); push @arr, $_; } return @arr; } # test simple query foreach my $q (qw(blade runner Philip k. dick)) { ok(my $hits = estcmd($q), "estcmd: $q"); diag "$hits hits"; cmp_ok(scalar pgest($q, '', undef, 0, 0), '==', $hits, "pgest: $q"); } # test attr query foreach my $q (('@title STRINC Blade Runner', '@title ISTRBW blade runner')) { ok(my $hits = estcmd('',$q), "estcmd: $q"); diag "$hits hits"; cmp_ok($hits, '==', scalar pgest(undef, $q, undef, 0, 0), 'blade runner'); } diag "Error handling test follows, ignore messages..."; # test NULL handling ok(! $dbh->do(qq`select * from pgest(null, '', '', null, 0, 0, array['\@id']) as (id text)`), "null index_path"); ok(my $hits = pgest('blade runner', '', undef, 0, 0), "test search"); cmp_ok($hits, '==', pgest('blade runner', undef, undef, 0, 0), "null attr"); cmp_ok($hits, '==', pgest('blade runner', '', undef, undef, 0), "null limit"); cmp_ok($hits, '==', pgest('blade runner', '', undef, 0, undef), "null offset"); cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef, undef), "null optional"); # test limit, offset and global mess my $d = int($hits / 3); cmp_ok($d, '==', pgest('blade runner',undef, undef, $d, undef), "limit $d"); cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef, undef), "check"); cmp_ok(($hits - $d), '==', pgest('blade runner',undef, undef, undef, $d), "offset $d"); cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef, undef), "check"); cmp_ok(($hits - $d), '==', pgest('blade runner',undef, undef, ($hits - $d), $d), "limit ".($hits - $d)." offset $d"); cmp_ok($hits, '==', pgest('blade runner', undef, undef, undef, undef), "check"); # test sort my @arr_asc = pgest('blade runner', undef, '@id NUMA', undef, undef); my @arr_desc = pgest('blade runner', undef, '@id NUMD', undef, undef); cmp_ok(@arr_asc, '==', @arr_desc, "same number of results"); my $errors = 0; foreach my $i (0 .. $#arr_asc) { my ($a, $b) = ($arr_asc[$i], $arr_desc[$#arr_desc - $i]); if ($a ne $b) { $errors++; diag "element $i: $a != $b"; } } cmp_ok($errors, '==', 0, "errors in ordering");