Insert 0 instead of empty in case of failure.
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Test / Record / Fetch.pm
1 # $Id: Fetch.pm,v 1.12 2006-11-01 09:56:50 sondberg 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     $conn->log("irspy_test", "Fetch test search succeeded");
32     my @syntax = (
33                    'canmarc',
34                    'danmarc',
35                    'grs-1',
36                    'ibermarc',
37                    'intermarc',
38                    'jpmarc',
39                    'librismarc',
40                    'mab',
41                    'normarc',
42                    'opac',
43                    'picamarc',
44                    'rusmarc',
45                    'summary',
46                    'sutrs',
47                    'swemarc',
48                    'ukmarc',
49                    'unimarc',
50                    'usmarc',
51                    'xml'
52                 );
53     #@syntax = qw(grs-1 sutrs usmarc xml); # simplify for debugging
54     foreach my $syntax (@syntax) {
55         $conn->irspy_rs_record($task->{rs}, 0,
56                                { syntax => $syntax },
57                                { start => 0, count => 1,
58                                  preferredRecordSyntax => $syntax },
59                                 ZOOM::Event::RECV_RECORD, \&record,
60                                 exception => \&error);
61     }
62
63     return ZOOM::IRSpy::Status::TASK_DONE;
64 }
65
66
67 sub record {
68     my($conn, $task, $test_args, $event) = @_;
69     my $syn = $test_args->{'syntax'};
70     my $rs = $task->{rs};
71
72     # Due to a bug in ZOOM-C (as of YAZ 2.1.38 of 31st October 2006),
73     # diagnostics in Present responses are not reported, so that we
74     # always end up in this callback rather than in error() where we
75     # should be.  Luckily, we can test whether the retrieval really
76     # did work by rendering the record, which will yield an undefined
77     # result if the fetch failed.
78     my $record = _fetch_record($rs, 0, $syn);
79     my $text = $record->render();
80     if (defined $text) {
81         $conn->log("irspy_test", "Successfully retrieved a $syn record");
82         if (0) {
83             print STDERR "Hits: ", $rs->size(), "\n";
84             print STDERR "Syntax: ", $syn, "\n";
85             print STDERR $text;
86         }
87     } else {
88         $conn->log("irspy_test", "Retrieval of $syn record failed: ",
89                    "exception unavailable");
90     }
91
92     $conn->record()->store_result('record_fetch',
93                                   'syntax'   => $syn,
94                                   'ok'       => defined $text ? 1 : 0);
95
96     return ZOOM::IRSpy::Status::TASK_DONE;
97 }
98
99
100 sub _fetch_record {
101     my($rs, $index0, $syntax) = @_;
102
103     my $oldSyntax = $rs->option(preferredRecordSyntax => $syntax);
104     my $record = $rs->record(0);
105     $oldSyntax = "" if !defined $oldSyntax;
106     $rs->option(preferredRecordSyntax => $oldSyntax);
107
108     return $record;
109 }
110
111
112 sub error {
113     my($conn, $task, $test_args, $exception) = @_;
114     my $syn = $test_args->{'syntax'};
115
116     $conn->log("irspy_test", "Retrieval of $syn record failed: ", $exception);
117     $conn->record()->store_result('record_fetch',
118                                   'syntax'       => $syn,
119                                   'ok'        => 0);
120     return ZOOM::IRSpy::Status::TASK_DONE;
121 }
122
123
124 1;