Test for success of sortby() call.
[ZOOM-Perl-moved-to-github.git] / t / 14-sorting.t
1 # $Id: 14-sorting.t,v 1.3 2005-11-04 16:59: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 #   ### At present, this test fails -- so far as I can see, because
7 #       the underlying ZOOM-C sorting functionality is broken, as
8 #       verified using "zoomsh" with the commands:
9 #
10 #               ZOOM>open indexdata.dk/gils
11 #               ZOOM>find @attr 1=4 map
12 #               ZOOM>sort 1=4
13 #               ZOOM>show 0 5
14 #
15 #       Hopefully Adam will fix the underlying code, and then this
16 #       will Just Work.
17
18 use strict;
19 use warnings;
20 use Test::More tests => 27;
21 use MARC::Record;
22
23 BEGIN { use_ok('Net::Z3950::ZOOM') };
24
25 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
26
27 my $host = "indexdata.com/gils";
28 my $conn = Net::Z3950::ZOOM::connection_new($host, 0);
29 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
30 ok($errcode == 0, "connection to '$host'");
31
32 my $qstr = '@attr 1=4 map';
33 my $query = Net::Z3950::ZOOM::query_create();
34 Net::Z3950::ZOOM::query_prefix($query, $qstr);
35 my $res = Net::Z3950::ZOOM::query_sortby($query, "1=4 <i");
36 ok($res == 0, "sort specification accepted");
37 my $rs = Net::Z3950::ZOOM::connection_search($conn, $query);
38 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
39 ok($errcode == 0, "search for '$qstr'");
40 my $n = Net::Z3950::ZOOM::resultset_size($rs);
41 ok($n == 5, "found $n records (expected 5)");
42
43 Net::Z3950::ZOOM::resultset_option_set($rs, preferredRecordSyntax => "usmarc");
44 my $previous = "";              # Sorts before all legitimate titles
45 foreach my $i (1 .. $n) {
46     my $rec = Net::Z3950::ZOOM::resultset_record($rs, $i-1);
47     ok(defined $rec, "got record $i of $n");
48     my $len = 0;
49     my $raw = Net::Z3950::ZOOM::record_get($rec, "raw", $len);
50     my $marc = new_from_usmarc MARC::Record($raw);
51     my $title = $marc->title();
52     ok($title ge $previous, "title '$title' ge previous '$previous'");
53     $previous = $title;
54 }
55
56 # Now reverse the order of sorting
57 Net::Z3950::ZOOM::resultset_sort($rs, "dummy", "1=4 >i");
58 ### There's no way to check for success, as this is a void function
59
60 $previous = "z";                # Sorts after all legitimate titles
61 foreach my $i (1 .. $n) {
62     my $rec = Net::Z3950::ZOOM::resultset_record($rs, $i-1);
63     ok(defined $rec, "got record $i of $n");
64     my $len = 0;
65     my $raw = Net::Z3950::ZOOM::record_get($rec, "raw", $len);
66     my $marc = new_from_usmarc MARC::Record($raw);
67     my $title = $marc->title();
68     ok($title le $previous, "title '$title' le previous '$previous'");
69     $previous = $title;
70 }
71
72 Net::Z3950::ZOOM::resultset_destroy($rs);
73 ok(1, "destroyed result-set");
74 Net::Z3950::ZOOM::connection_destroy($conn);
75 ok(1, "destroyed connection");