/[pgestraier]/trunk/t/pgest.t
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/t/pgest.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations)
Wed Jul 6 15:15:04 2005 UTC (18 years, 10 months ago) by dpavlin
File MIME type: application/x-troff
File size: 3330 byte(s)
test ordering

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26