71de6fe577d064dab4faf144dc0ed586410e3fe3
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Test / Record / Fetch.pm
1 # $Id: Fetch.pm,v 1.27 2007-03-15 11:38:14 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         $task->{rs}->destroy();
46         my $qindex = $udata->{queryindex}+1;
47         my $q = $queries[$qindex];
48         return ZOOM::IRSpy::Status::TEST_SKIPPED
49             if !defined $q;
50
51         $conn->log("irspy_test", "Trying another search ...");
52         $conn->irspy_search_pqf($queries[$qindex], { queryindex => $qindex }, {},
53                                 ZOOM::Event::ZEND, \&completed_search,
54                                 exception => \&completed_search);
55         return ZOOM::IRSpy::Status::TASK_DONE;
56     }
57
58     my @syntax = (
59                    'canmarc',
60                    'danmarc',
61                    'grs-1',
62                    'ibermarc',
63                    'intermarc',
64                    'jpmarc',
65                    'librismarc',
66                    'mab',
67                    'normarc',
68 #                   'opac',
69                    'picamarc',
70                    'rusmarc',
71                    'summary',
72                    'sutrs',
73                    'swemarc',
74                    'ukmarc',
75                    'unimarc',
76                    'usmarc',
77                    'xml'
78                 );
79     #@syntax = qw(grs-1 sutrs usmarc xml); # simplify for debugging
80     foreach my $i (0 ..$#syntax) {
81         my $syntax = $syntax[$i];
82         $conn->irspy_rs_record($task->{rs}, 0,
83                                { syntax => $syntax,
84                                  last => ($i == $#syntax) },
85                                { start => 0, count => 1,
86                                  preferredRecordSyntax => $syntax },
87                                 ZOOM::Event::ZEND, \&record,
88                                 exception => \&fetch_error);
89     }
90
91     return ZOOM::IRSpy::Status::TASK_DONE;
92 }
93
94
95 sub record {
96     my($conn, $task, $udata, $event) = @_;
97     my $syn = $udata->{'syntax'};
98     my $rs = $task->{rs};
99
100     my $record = _fetch_record($rs, 0, $syn);
101     my $ok = 0;
102     if (!$record || $record->error()) {
103         $conn->log("irspy_test", "retrieval of $syn record failed: ",
104                    defined $record ? $record->exception() : "???");
105     } else {
106         $ok = 1;
107         my $text = $record->render();
108         $conn->log("irspy_test", "Successfully retrieved a $syn record");
109         if (0) {
110             print STDERR "Hits: ", $rs->size(), "\n";
111             print STDERR "Syntax: ", $syn, "\n";
112             print STDERR $text;
113         }
114     }
115
116     $conn->record()->store_result('record_fetch',
117                                   'syntax'   => $syn,
118                                   'ok'       => $ok);
119
120     $rs->destroy() if $udata->{last};
121     return ($udata->{last} ?
122             ZOOM::IRSpy::Status::TEST_GOOD :
123             ZOOM::IRSpy::Status::TASK_DONE);
124 }
125
126
127 sub _fetch_record {
128     my($rs, $index0, $syntax) = @_;
129
130     my $oldSyntax = $rs->option(preferredRecordSyntax => $syntax);
131     my $record = $rs->record(0);
132     $oldSyntax = "" if !defined $oldSyntax;
133     $rs->option(preferredRecordSyntax => $oldSyntax);
134
135     return $record;
136 }
137
138
139 sub __UNUSED_search_error {
140     my($conn, $task, $test_args, $exception) = @_;
141
142     $conn->log("irspy_test", "Initial search failed: ", $exception);
143     return ZOOM::IRSpy::Status::TEST_SKIPPED;
144 }
145
146
147 sub fetch_error {
148     my($conn, $task, $udata, $exception) = @_;
149     my $syn = $udata->{'syntax'};
150
151     $conn->log("irspy_test", "Retrieval of $syn record failed: ", $exception);
152     $conn->record()->store_result('record_fetch',
153                                   'syntax'       => $syn,
154                                   'ok'        => 0);
155     $task->{rs}->destroy() if $udata->{last};
156     return ZOOM::IRSpy::Status::TASK_DONE;
157 }
158
159
160 1;