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