2 # =============================================================================
3 # $Id: 07_sort.t,v 1.4 2004-09-16 15:07:55 heikki Exp $
6 # =============================================================================
11 push (@INC,'demo','blib/lib','blib/arch');
17 use Test::More tests => 32;
19 # ----------------------------------------------------------------------------
20 # Session opening and closing
24 IDZebra::logFile("test07.log");
25 # IDZebra::logLevel(0x4F);
26 # IDZebra::logLevel(15);
27 use_ok('IDZebra::Session');
32 # ----------------------------------------------------------------------------
33 # Session opening and closing
34 my $sess = IDZebra::Session->open(configFile => 'demo/zebra.cfg',
35 groupName => 'demo2');
36 # ----------------------------------------------------------------------------
37 # Insert some test data
44 $sess->databases('demo1', 'demo2');
45 $ret=$sess->end_trans;
48 for $F (<lib/IDZebra/*.pm>)
50 ($ret,$sysno)=$sess->insert_record (file=>$F, recordType => 'grs.perl.pod');
51 ok( $ret==0, "inserted $F");
52 #print STDERR "Inserted $F ok. ret=$ret sys=$sysno\n";
55 $ret=$sess->end_trans;
56 ok($filecount>0,"Inserted files");
57 is($ret->{inserted},$filecount, "Inserted all");
60 # -----------------------------------------------------------------------------
61 # Search 1 database, retrieve records, sort "titles" locally (dangerous!)
63 my $rs1 = $sess->search(cqlmap => 'demo/cql.map',
65 databases => [qw(demo1)]);
67 my (@unsorted, @sorted, @sortedi);
71 foreach my $rec ($rs1->records()) {
72 if ($rec->{errCode}) {
75 my ($title) = ($rec->buf =~ /\n\s*package\s+([a-zA-Z0-9:]+)\s*\;\s*\n/);
76 push (@unsorted, $title);
78 ok (($wasError == 0), "retrieval");
80 @sorted = sort (@unsorted);
82 @sortedi = sort ({my $a1=$a; $a1 =~ y/[A-Z]/[a-z]/;
83 my $b1=$b; $b1 =~ y/[A-Z]/[a-z]/;
84 ($a1 cmp $b1);} @unsorted);
87 # -----------------------------------------------------------------------------
88 # Sort rs itself ascending
90 isa_ok ($rs1, 'IDZebra::Resultset');
94 isa_ok ($rs1, 'IDZebra::Resultset');
98 foreach my $rec ($rs1->records()) {
99 if ($rec->{errCode}) { $wasError++; }
100 my ($title) = ($rec->buf =~ /\n\s*package\s+([a-zA-Z0-9:]+)\s*\;\s*\n/);
101 if ($sortedi[$rec->position - 1] ne $title) { $sortError++; }
104 ok (($wasError == 0), "retrieval");
105 ok (($sortError == 0), "sorting ascending");
107 # -----------------------------------------------------------------------------
108 # Sort descending, new rs
110 # todo_skip "Sort into different rset crashes", 3;
112 my $rs2 = $rs1->sort('1=4 id');
114 isa_ok ($rs2, 'IDZebra::Resultset');
118 foreach my $rec ($rs2->records()) {
119 if ($rec->{errCode}) { $wasError++; }
120 my ($title) = ($rec->buf =~ /\n\s*package\s+([a-zA-Z0-9:]+)\s*\;\s*\n/);
121 if ($sorted[$rs2->count - $rec->position] ne $title) { $sortError++; }
122 is ($title, $sorted[$rs2->count - $rec->position], "sort order $title");
126 is ($wasError,0 , "retrieval");
127 is ($sortError, 0, "sorting descending");
131 # -----------------------------------------------------------------------------
132 # Search + sort ascending
133 my $rs3 = $sess->search(cql => 'IDZebra',
134 databases => [qw(demo1)],
136 isa_ok ($rs3, 'IDZebra::Resultset');
140 foreach my $rec ($rs3->records()) {
141 if ($rec->{errCode}) { $wasError++; }
142 my ($title) = ($rec->buf =~ /\n\s*package\s+([a-zA-Z0-9:]+)\s*\;\s*\n/);
143 if ($sortedi[$rec->position - 1] ne $title) { $sortError++; }
146 ok (($wasError == 0), "saerch+sort, retrieval");
147 ok (($sortError == 0), "search+sort descending");
149 # ----------------------------------------------------------------------------
153 $rs4 = $rs3->sort("ostrich");
154 ok (($rs4->errCode != 0),"Wrong sort: ".$rs4->errCode."(".$rs4->errString.")");
155 # ----------------------------------------------------------------------------