New
[ZOOM-Perl-moved-to-github.git] / t / 14-sorting.t
1 # $Id: 14-sorting.t,v 1.6 2005-11-18 17:53:53 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 = "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 $len = 0;
37     my $raw = Net::Z3950::ZOOM::record_get($rec, "raw", $len);
38     my $marc = new_from_usmarc MARC::Record($raw);
39     my $title = $marc->title();
40     ok($title ge $previous, "title '$title' ge previous '$previous'");
41     $previous = $title;
42 }
43
44 # Now reverse the order of sorting.  We never use resultset_sort(),
45 # which is identical to sort1() except that it returns nothing.
46 my $status = Net::Z3950::ZOOM::resultset_sort1($rs, "yaz", "1=4>i");
47 ok($status < 0, "malformed sort criterion rejected");
48 $status = Net::Z3950::ZOOM::resultset_sort1($rs, "yaz", "1=4 >i");
49 ok($status == 0, "sort criterion accepted");
50
51 $previous = "z";                # Sorts after all legitimate titles
52 foreach my $i (1 .. $n) {
53     my $rec = Net::Z3950::ZOOM::resultset_record($rs, $i-1);
54     ok(defined $rec, "got record $i of $n");
55     my $len = 0;
56     my $raw = Net::Z3950::ZOOM::record_get($rec, "raw", $len);
57     my $marc = new_from_usmarc MARC::Record($raw);
58     my $title = $marc->title();
59     ok($title le $previous, "title '$title' le previous '$previous'");
60     $previous = $title;
61 }
62
63 Net::Z3950::ZOOM::resultset_destroy($rs);
64 ok(1, "destroyed result-set");
65 Net::Z3950::ZOOM::connection_destroy($conn);
66 ok(1, "destroyed connection");