Number of search-hits included in log message.
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Test / Record / Fetch.pm
1 # $Id: Fetch.pm,v 1.13 2006-11-02 15:02:09 mike Exp $
2
3 # See the "Main" test package for documentation
4
5 package ZOOM::IRSpy::Test::Record::Fetch;
6
7 use 5.008;
8 use strict;
9 use warnings;
10
11 use ZOOM::IRSpy::Test;
12 our @ISA = qw(ZOOM::IRSpy::Test);
13
14
15 sub start {
16     my $class = shift();
17     my($conn) = @_;
18
19     # Here I want to get a use attribute from the session, which we've
20     # managed to search for in the Search/Bib1 or Search/Dan1 tests.
21     # But how?  So far we search for title: 1=4
22     $conn->irspy_search_pqf("\@attr 1=4 mineral", {}, {},       
23                             ZOOM::Event::RECV_SEARCH, \&completed_search,
24                             exception => \&error);
25 }
26
27
28 sub completed_search {
29     my($conn, $task, $udata, $event) = @_;
30
31     my $n = $task->{rs}->size();
32     $conn->log("irspy_test", "Fetch test search found $n records");
33     my @syntax = (
34                    'canmarc',
35                    'danmarc',
36                    'grs-1',
37                    'ibermarc',
38                    'intermarc',
39                    'jpmarc',
40                    'librismarc',
41                    'mab',
42                    'normarc',
43                    'opac',
44                    'picamarc',
45                    'rusmarc',
46                    'summary',
47                    'sutrs',
48                    'swemarc',
49                    'ukmarc',
50                    'unimarc',
51                    'usmarc',
52                    'xml'
53                 );
54     #@syntax = qw(grs-1 sutrs usmarc xml); # simplify for debugging
55     foreach my $syntax (@syntax) {
56         $conn->irspy_rs_record($task->{rs}, 0,
57                                { syntax => $syntax },
58                                { start => 0, count => 1,
59                                  preferredRecordSyntax => $syntax },
60                                 ZOOM::Event::RECV_RECORD, \&record,
61                                 exception => \&error);
62     }
63
64     return ZOOM::IRSpy::Status::TASK_DONE;
65 }
66
67
68 sub record {
69     my($conn, $task, $test_args, $event) = @_;
70     my $syn = $test_args->{'syntax'};
71     my $rs = $task->{rs};
72
73     # Due to a bug in ZOOM-C (as of YAZ 2.1.38 of 31st October 2006),
74     # diagnostics in Present responses are not reported, so that we
75     # always end up in this callback rather than in error() where we
76     # should be.  Luckily, we can test whether the retrieval really
77     # did work by rendering the record, which will yield an undefined
78     # result if the fetch failed.
79     my $record = _fetch_record($rs, 0, $syn);
80     my $text = $record->render();
81     if (defined $text) {
82         $conn->log("irspy_test", "Successfully retrieved a $syn record");
83         if (0) {
84             print STDERR "Hits: ", $rs->size(), "\n";
85             print STDERR "Syntax: ", $syn, "\n";
86             print STDERR $text;
87         }
88     } else {
89         $conn->log("irspy_test", "Retrieval of $syn record failed: ",
90                    "exception unavailable");
91     }
92
93     $conn->record()->store_result('record_fetch',
94                                   'syntax'   => $syn,
95                                   'ok'       => defined $text ? 1 : 0);
96
97     return ZOOM::IRSpy::Status::TASK_DONE;
98 }
99
100
101 sub _fetch_record {
102     my($rs, $index0, $syntax) = @_;
103
104     my $oldSyntax = $rs->option(preferredRecordSyntax => $syntax);
105     my $record = $rs->record(0);
106     $oldSyntax = "" if !defined $oldSyntax;
107     $rs->option(preferredRecordSyntax => $oldSyntax);
108
109     return $record;
110 }
111
112
113 sub error {
114     my($conn, $task, $test_args, $exception) = @_;
115     my $syn = $test_args->{'syntax'};
116
117     $conn->log("irspy_test", "Retrieval of $syn record failed: ", $exception);
118     $conn->record()->store_result('record_fetch',
119                                   'syntax'       => $syn,
120                                   'ok'        => 0);
121     return ZOOM::IRSpy::Status::TASK_DONE;
122 }
123
124
125 1;