Use ZEND only for callbacks, rather than RECV_*
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Test / Record / Fetch.pm
1 # $Id: Fetch.pm,v 1.25 2007-02-23 15:03:44 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 # These queries 
16 my @queries = (
17                "\@attr 1=4 mineral",
18                "\@attr 1=4 computer",
19                "\@attr 1=44 mineral", # Smithsonian doesn't support AP 4!
20                ### We can add more queries here
21                );
22
23
24 sub start {
25     my $class = shift();
26     my($conn) = @_;
27
28     # Here I want to get a use attribute from the session, which we've
29     # managed to search for in the Search/Bib1 or Search/Dan1 tests.
30     # But how?  So far we search for title: 1=4
31     $conn->irspy_search_pqf($queries[0], { queryindex => 0 }, {},
32                             ZOOM::Event::ZEND, \&completed_search,
33                             exception => \&completed_search);
34 }
35
36
37 sub completed_search {
38     my($conn, $task, $udata, $event) = @_;
39
40     my $n = $task->{rs}->size();
41     $conn->log("irspy_test", "Fetch test search (", $task->{query}, ") ",
42                ref $event && $event->isa("ZOOM::Exception") ?
43                "failed: $event" : "found $n records (event=$event)");
44     if ($n == 0) {
45         my $qindex = $udata->{queryindex}+1;
46         my $q = $queries[$qindex];
47         return ZOOM::IRSpy::Status::TEST_SKIPPED
48             if !defined $q;
49
50         $conn->log("irspy_test", "Trying another search ...");
51         $conn->irspy_search_pqf($queries[$qindex], { queryindex => $qindex }, {},
52                                 ZOOM::Event::ZEND, \&completed_search,
53                                 exception => \&completed_search);
54         return ZOOM::IRSpy::Status::TASK_DONE;
55     }
56
57     my @syntax = (
58                    'canmarc',
59                    'danmarc',
60                    'grs-1',
61                    'ibermarc',
62                    'intermarc',
63                    'jpmarc',
64                    'librismarc',
65                    'mab',
66                    'normarc',
67 #                   'opac',
68                    'picamarc',
69                    'rusmarc',
70                    'summary',
71                    'sutrs',
72                    'swemarc',
73                    'ukmarc',
74                    'unimarc',
75                    'usmarc',
76                    'xml'
77                 );
78     #@syntax = qw(grs-1 sutrs usmarc xml); # simplify for debugging
79     foreach my $i (0 ..$#syntax) {
80         my $syntax = $syntax[$i];
81         $conn->irspy_rs_record($task->{rs}, 0,
82                                { syntax => $syntax,
83                                  last => ($i == $#syntax) },
84                                { start => 0, count => 1,
85                                  preferredRecordSyntax => $syntax },
86                                 ZOOM::Event::ZEND, \&record,
87                                 exception => \&fetch_error);
88     }
89
90     return ZOOM::IRSpy::Status::TASK_DONE;
91 }
92
93
94 sub record {
95     my($conn, $task, $udata, $event) = @_;
96     my $syn = $udata->{'syntax'};
97     my $rs = $task->{rs};
98
99     my $record = _fetch_record($rs, 0, $syn);
100     my $ok = 0;
101     if ($record->error()) {
102         $conn->log("irspy_test", "retrieval of $syn record failed: ",
103                    $record->exception());
104     } else {
105         $ok = 1;
106         my $text = $record->render();
107         $conn->log("irspy_test", "Successfully retrieved a $syn record");
108         if (0) {
109             print STDERR "Hits: ", $rs->size(), "\n";
110             print STDERR "Syntax: ", $syn, "\n";
111             print STDERR $text;
112         }
113     }
114
115     $conn->record()->store_result('record_fetch',
116                                   'syntax'   => $syn,
117                                   'ok'       => $ok);
118
119     return ($udata->{last} ?
120             ZOOM::IRSpy::Status::TEST_GOOD :
121             ZOOM::IRSpy::Status::TASK_DONE);
122 }
123
124
125 sub _fetch_record {
126     my($rs, $index0, $syntax) = @_;
127
128     my $oldSyntax = $rs->option(preferredRecordSyntax => $syntax);
129     my $record = $rs->record(0);
130     $oldSyntax = "" if !defined $oldSyntax;
131     $rs->option(preferredRecordSyntax => $oldSyntax);
132
133     return $record;
134 }
135
136
137 sub __UNUSED_search_error {
138     my($conn, $task, $test_args, $exception) = @_;
139
140     $conn->log("irspy_test", "Initial search failed: ", $exception);
141     return ZOOM::IRSpy::Status::TEST_SKIPPED;
142 }
143
144
145 sub fetch_error {
146     my($conn, $task, $test_args, $exception) = @_;
147     my $syn = $test_args->{'syntax'};
148
149     $conn->log("irspy_test", "Retrieval of $syn record failed: ", $exception);
150     $conn->record()->store_result('record_fetch',
151                                   'syntax'       => $syn,
152                                   'ok'        => 0);
153     return ZOOM::IRSpy::Status::TASK_DONE;
154 }
155
156
157 1;