Markdown
[ZOOM-Perl-moved-to-github.git] / t / 14-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 14-sorting.t'
3
4 use strict;
5 use warnings;
6 use Test::More tests => 29;
7 use MARC::Record;
8
9 BEGIN { use_ok('Net::Z3950::ZOOM') };
10
11 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
12
13 my $host = "z3950.indexdata.com/gils";
14 my $conn = Net::Z3950::ZOOM::connection_new($host, 0);
15 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
16 ok($errcode == 0, "connection to '$host'");
17
18 my $qstr = '@attr 1=4 map';
19 my $query = Net::Z3950::ZOOM::query_create();
20 Net::Z3950::ZOOM::query_prefix($query, $qstr);
21 my $res = Net::Z3950::ZOOM::query_sortby($query, "1=4 <i");
22 ok($res == 0, "sort specification accepted");
23 my $rs = Net::Z3950::ZOOM::connection_search($conn, $query);
24 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
25 ok($errcode == 0, "search for '$qstr'");
26 my $n = Net::Z3950::ZOOM::resultset_size($rs);
27 ok($n == 5, "found $n records (expected 5)");
28
29 Net::Z3950::ZOOM::resultset_option_set($rs, preferredRecordSyntax => "usmarc");
30 my $previous = "";              # Sorts before all legitimate titles
31 foreach my $i (1 .. $n) {
32     my $rec = Net::Z3950::ZOOM::resultset_record($rs, $i-1);
33     ok(defined $rec, "got record $i of $n");
34     my $raw = Net::Z3950::ZOOM::record_get($rec, "raw");
35     my $marc = new_from_usmarc MARC::Record($raw);
36     my $title = $marc->title();
37     ok($title ge $previous, "title '$title' ge previous '$previous'");
38     $previous = $title;
39 }
40
41 # Now reverse the order of sorting.  We never use resultset_sort(),
42 # which is identical to sort1() except that it returns nothing.
43 my $status = Net::Z3950::ZOOM::resultset_sort1($rs, "yaz", "1=4>i");
44 ok($status < 0, "malformed sort criterion rejected");
45 $status = Net::Z3950::ZOOM::resultset_sort1($rs, "yaz", "1=4 >i");
46 ok($status == 0, "sort criterion accepted");
47
48 $previous = "z";                # Sorts after all legitimate titles
49 foreach my $i (1 .. $n) {
50     my $rec = Net::Z3950::ZOOM::resultset_record($rs, $i-1);
51     ok(defined $rec, "got record $i of $n");
52     my $raw = Net::Z3950::ZOOM::record_get($rec, "raw");
53     my $marc = new_from_usmarc MARC::Record($raw);
54     my $title = $marc->title();
55     ok($title le $previous, "title '$title' le previous '$previous'");
56     $previous = $title;
57 }
58
59 Net::Z3950::ZOOM::resultset_destroy($rs);
60 ok(1, "destroyed result-set");
61 Net::Z3950::ZOOM::connection_destroy($conn);
62 ok(1, "destroyed connection");