New
[ZOOM-Perl-moved-to-github.git] / t / 24-sorting.t
1 # $Id: 24-sorting.t,v 1.1 2005-11-04 17:08:20 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 24-sorting.t'
5 #
6 #   ### At present, this test fails: see comment to "14-sorting.t"
7
8 use strict;
9 use warnings;
10 use Test::More tests => 27;
11 use MARC::Record;
12
13 BEGIN { use_ok('ZOOM') };
14
15 my $host = "indexdata.com/gils";
16 my $conn;
17 eval { $conn = new ZOOM::Connection($host, 0) };
18 ok(!$@, "connection to '$host'");
19
20 my $qstr = '@attr 1=4 map';
21 my $query = new ZOOM::Query::PQF($qstr);
22 eval { $query->sortby("1=4 <i") };
23 ok(!$@, "sort specification accepted");
24 my $rs;
25 eval { $rs = $conn->search($query) };
26 ok(!$@, "search for '$qstr'");
27 my $n = $rs->size();
28 ok($n == 5, "found $n records (expected 5)");
29
30 $rs->option(preferredRecordSyntax => "usmarc");
31 my $previous = "";              # Sorts before all legitimate titles
32 foreach my $i (1 .. $n) {
33     my $rec = $rs->record($i-1);
34     ok(defined $rec, "got record $i of $n");
35     my $raw = $rec->raw();
36     my $marc = new_from_usmarc MARC::Record($raw);
37     my $title = $marc->title();
38     ok($title ge $previous, "title '$title' ge previous '$previous'");
39     $previous = $title;
40 }
41
42 # Now reverse the order of sorting
43 $rs->sort("dummy", "1=4 >i");
44 ### There's no way to check for success, as this is a void function
45
46 $previous = "z";                # Sorts after all legitimate titles
47 foreach my $i (1 .. $n) {
48     my $rec = $rs->record($i-1);
49     ok(defined $rec, "got record $i of $n");
50     my $raw = $rec->raw();
51     my $marc = new_from_usmarc MARC::Record($raw);
52     my $title = $marc->title();
53     ok($title le $previous, "title '$title' le previous '$previous'");
54     $previous = $title;
55 }
56
57 $rs->destroy();
58 ok(1, "destroyed result-set");
59 $conn->destroy();
60 ok(1, "destroyed connection");