c6b489fd9aecd30be61a23b23b0a54b6265d866d
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Test / Record / Fetch.pm
1 # $Id: Fetch.pm,v 1.16 2006-11-29 11:06:29 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     return ZOOM::IRSpy::Status::TEST_SKIPPED if $n == 0;
34
35     my @syntax = (
36                    'canmarc',
37                    'danmarc',
38                    'grs-1',
39                    'ibermarc',
40                    'intermarc',
41                    'jpmarc',
42                    'librismarc',
43                    'mab',
44                    'normarc',
45                    'opac',
46                    'picamarc',
47                    'rusmarc',
48                    'summary',
49                    'sutrs',
50                    'swemarc',
51                    'ukmarc',
52                    'unimarc',
53                    'usmarc',
54                    'xml'
55                 );
56     #@syntax = qw(grs-1 sutrs usmarc xml); # simplify for debugging
57     foreach my $syntax (@syntax) {
58         $conn->irspy_rs_record($task->{rs}, 0,
59                                { syntax => $syntax },
60                                { start => 0, count => 1,
61                                  preferredRecordSyntax => $syntax },
62                                 ZOOM::Event::RECV_RECORD, \&record,
63                                 exception => \&error);
64     }
65
66     return ZOOM::IRSpy::Status::TASK_DONE;
67 }
68
69
70 sub record {
71     my($conn, $task, $test_args, $event) = @_;
72     my $syn = $test_args->{'syntax'};
73     my $rs = $task->{rs};
74
75     my $record = _fetch_record($rs, 0, $syn);
76     my $ok = 0;
77     if ($record->error()) {
78         $conn->log("irspy_test", "retrieval of $syn record failed: ",
79                    $record->exception());
80     } else {
81         $ok = 1;
82         my $text = $record->render();
83         $conn->log("irspy_test", "Successfully retrieved a $syn record");
84         if (0) {
85             print STDERR "Hits: ", $rs->size(), "\n";
86             print STDERR "Syntax: ", $syn, "\n";
87             print STDERR $text;
88         }
89     }
90
91     $conn->record()->store_result('record_fetch',
92                                   'syntax'   => $syn,
93                                   'ok'       => $ok);
94
95     return ZOOM::IRSpy::Status::TASK_DONE;
96 }
97
98
99 sub _fetch_record {
100     my($rs, $index0, $syntax) = @_;
101
102     my $oldSyntax = $rs->option(preferredRecordSyntax => $syntax);
103     my $record = $rs->record(0);
104     $oldSyntax = "" if !defined $oldSyntax;
105     $rs->option(preferredRecordSyntax => $oldSyntax);
106
107     return $record;
108 }
109
110
111 sub error {
112     my($conn, $task, $test_args, $exception) = @_;
113     my $syn = $test_args->{'syntax'};
114
115     $conn->log("irspy_test", "Retrieval of $syn record failed: ", $exception);
116     $conn->record()->store_result('record_fetch',
117                                   'syntax'       => $syn,
118                                   'ok'        => 0);
119     return ZOOM::IRSpy::Status::TASK_DONE;
120 }
121
122
123 1;