c3e7cf3e5d5a8ed1464c51343be243acc80b1824
[ZOOM-Perl-moved-to-github.git] / t / 24-sorting.t
1 # $Id: 24-sorting.t,v 1.7 2006-11-02 17:48:26 mike Exp $
2
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl 24-sorting.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 29;
9 use MARC::Record;
10
11 BEGIN { use_ok('ZOOM') };
12
13 my $host = "z3950.indexdata.com/gils";
14 my $conn;
15 eval { $conn = new ZOOM::Connection($host, 0) };
16 ok(!$@, "connection to '$host'");
17
18 my $qstr = '@attr 1=4 map';
19 my $query = new ZOOM::Query::PQF($qstr);
20 eval { $query->sortby("1=4 <i") };
21 ok(!$@, "sort specification accepted");
22 my $rs;
23 eval { $rs = $conn->search($query) };
24 ok(!$@, "search for '$qstr'");
25 my $n = $rs->size();
26 ok($n == 5, "found $n records (expected 5)");
27
28 $rs->option(preferredRecordSyntax => "usmarc");
29 my $previous = "";              # Sorts before all legitimate titles
30 foreach my $i (1 .. $n) {
31     my $rec = $rs->record($i-1);
32     ok(defined $rec, "got record $i of $n");
33     my $raw = $rec->raw();
34     my $marc = new_from_usmarc MARC::Record($raw);
35     my $title = $marc->title();
36     ok($title ge $previous, "title '$title' ge previous '$previous'");
37     $previous = $title;
38 }
39
40 # Now reverse the order of sorting
41 my $status = $rs->sort("yaz", "1=4>i");
42 ok($status < 0, "malformed sort criterion rejected");
43 $status = $rs->sort("yaz", "1=4 >i");
44 ok($status == 0, "sort criterion accepted");
45
46 $previous = "z";                # Sorts after all legitimate titles
47 foreach my $i (1 .. $n) {
48     my $rec = $rs->record($i-1);
49     ok(defined $rec, "got record $i of $n");
50     my $raw = $rec->raw();
51     my $marc = new_from_usmarc MARC::Record($raw);
52     my $title = $marc->title();
53     ok($title le $previous, "title '$title' le previous '$previous'");
54     $previous = $title;
55 }
56
57 $rs->destroy();
58 ok(1, "destroyed result-set");
59 $conn->destroy();
60 ok(1, "destroyed connection");