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