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