Sync'ed RichSeqI with the implementation. RichSeq provides backward
[bioperl-live.git] / t / Biblio.t
blobac1be8ae60fd9d29afa615a4e61e16adc3ebbc74
1 # This is -*-Perl-*- code
2 ## Bioperl Test Harness Script for Modules
3 ##
4 # $Id$
6 # Before `make install' is performed this script should be runnable with
7 # `make test'. After `make install' it should work as `perl Biblio.t'
9 use strict;
10 use vars qw($NUMTESTS);
12 my $error;
14 BEGIN { 
15     # to handle systems with no installed Test module
16     # we include the t dir (where a copy of Test.pm is located)
17     # as a fallback
18     eval { require Test; };
19     $error = 0;
20     if( $@ ) {
21         use lib 't';
22     }
23     use Test;
24     plan tests => 24;
27 my $testnum;
28 my $verbose = 0;
30 ## End of black magic.
32 ## Insert additional test code below but remember to change
33 ## the print "1..x\n" in the BEGIN block to reflect the
34 ## total number of tests that will be run. 
36 my $serror = 0; my $serror2 = 0;
37 my $ferror = 0; my $ferror2 = 0;
38 my $xerror = 0;
40 my $format = ($ENV{'TEST_DETAILS'} ? '%-25s' : '');
42 unless (eval "require SOAP::Lite; 1;") {
43     print STDERR "SOAP::Lite not installed. Skipping some tests.\n";
44     $serror = 1;
47 unless (eval "require IO::String; 1;") {
48     print STDERR "IO::String not installed. Skipping some tests.\n";
49     $serror2 = 1;
52 unless (eval "require XML::Parser; 1;") {
53     print STDERR "XML::Parser not installed. Skipping some tests.\n";
54     $xerror = 1;
57 use Bio::Root::IO;
58 my $testfile = Bio::Root::IO->catfile ('t','data','stress_test_medline.xml');
59 unless (-e $testfile) {
60     print STDERR "Cannot find testing data '$testfile'. Skipping some tests.\n";
61     $ferror = 1;
63 my $testfile2 = Bio::Root::IO->catfile ('t','data','stress_test_pubmed.xml');
64 unless (-e $testfile2) {
65     print STDERR "Cannot find testing data '$testfile2'. Skipping some tests.\n";
66     $ferror2 = 1;
70 # check 'use ...'
71 eval { require Bio::Biblio };
72 print sprintf ($format, 'use Bio::Biblio'); ok (%Bio::Biblio::);
73 print $@ if $@;
75 # check 'new...'
76 my $biblio;
77 eval { $biblio = new Bio::Biblio (-location => 'http://localhost:4567'); };
78 print sprintf ($format, "new Bio::Biblio "); skip ($serror, defined $biblio);
79 #print $@ if $@;
82 # check 'use ...IO...'
83 eval { require Bio::Biblio::IO };
84 print sprintf ($format, "use Bio::Biblio::IO "); ok (%Bio::Biblio::IO::);
86 my $io;
88 # check MEDLINE XML parser
89 print sprintf ($format, "new Bio::Biblio::IO (1)");
90 skip ($ferror || $xerror,
91       defined (eval { $io = new Bio::Biblio::IO ('-format' => 'medlinexml',
92                                                  '-file'   => $testfile,
93                                                  '-result' => 'raw') }));
94 print $@ if $@;
96 print "Reading and parsing MEDLINE XML file...\n";
97 print sprintf ($format, "    citation 1 "); skip ($ferror || $xerror, eval { $io->next_bibref->{'medlineID'} }, 'Text1');
98 print sprintf ($format, "    citation 2 "); skip ($ferror || $xerror, eval { $io->next_bibref->{'medlineID'} }, 'Text248');
99 print sprintf ($format, "    citation 3 "); skip ($ferror || $xerror, eval { $io->next_bibref->{'medlineID'} }, 'Text495');
101 print "Getting citations using callback...\n";
102 my (@ids) = ('Text1', 'Text248', 'Text495');
103 my $callback_used = 'no';
104 if ($ferror || $xerror) {
105     foreach my $i (1..3) {
106         print sprintf ($format, "    citation $i "); skip (1,1);
107     }
108 } else {
109     $io = new Bio::Biblio::IO ('-format'   => 'medlinexml',
110                                '-file'     => $testfile,
111 #                              '-result'   => 'medline2ref',  # this is default
112                                '-callback' => \&callback);
114 print sprintf ($format, "    calling callback "); skip ($ferror || $xerror, $callback_used, 'yes');
116 sub callback {
117     my $citation = shift;
118     $callback_used = 'yes';
119     print sprintf ($format, '    citation ' . (@ids+0) . ' '); skip ($ferror, $citation->{'_identifier'}, shift @ids);
122 print "Reading and parsing XML string...\n";
123 if ($xerror) {
124     print sprintf ($format, "    citation 1 "); skip (1, 1);
125     print sprintf ($format, "    citation 2 "); skip (1, 1);
126 } else {
127     $io = new Bio::Biblio::IO ('-format'   => 'medlinexml',
128                                '-data'     => <<XMLDATA,
129 <MedlineCitationSet>
130 <MedlineCitation>
131 <MedlineID>12345678</MedlineID>
132 <Article><Journal></Journal></Article>
133 </MedlineCitation>
134 <MedlineCitation>
135 <MedlineID>abcdefgh</MedlineID>
136 <Article><Journal></Journal></Article>
137 </MedlineCitation>
138 </MedlineCitationSet>
139 XMLDATA
140                                '-result'   => 'medline2ref',
141                                );
142     print sprintf ($format, "    citation 1 "); ok ($io->next_bibref->{'_identifier'}, '12345678');
143     print sprintf ($format, "    citation 2 "); ok ($io->next_bibref->{'_identifier'}, 'abcdefgh');
146 print "Reading and parsing XML string handle...\n";
147 #use IO::String;
148 if ($xerror || $serror2) {
149     print sprintf ($format, "    citation 1 "); skip (1,1);
150     print sprintf ($format, "    citation 2 "); skip (1,1);
151 } else {
152     my $data = <<XMLDATA;
153 <MedlineCitationSet>
154 <MedlineCitation>
155 <MedlineID>87654321</MedlineID>
156 <Article><Journal></Journal></Article>
157 </MedlineCitation>
158 <MedlineCitation>
159 <MedlineID>hgfedcba</MedlineID>
160 <Article><Journal></Journal></Article>
161 </MedlineCitation>
162 </MedlineCitationSet>
163 XMLDATA
165     $io = new Bio::Biblio::IO ('-format' => 'medlinexml',
166                                '-fh'     => IO::String->new ($data),
167                                );
168     print sprintf ($format, "    citation 1 "); ok (eval { $io->next_bibref->identifier }, '87654321');
169     print sprintf ($format, "    citation 2 "); ok (eval { $io->next_bibref->identifier }, 'hgfedcba');
172 # check PUBMED XML parser
173 print sprintf ($format, "new Bio::Biblio::IO (2)");
174 skip ($ferror2 || $xerror,
175       defined (eval { $io = new Bio::Biblio::IO ('-format' => 'pubmedxml',
176                                                  '-file'   => $testfile2,
177                                                  '-result' => 'pubmed2ref') }));
178 print "Reading and parsing PUBMED XML file...\n";
179 if ($xerror) {
180     foreach my $i (1..4) {
181         print sprintf ($format, "    citation $i "); skip (1,"Can't read citation from PUBMED XML");
182     }
183 } else {
184     print sprintf ($format, "    citation 1 "); skip ($ferror2, eval { $io->next_bibref->identifier }, '11223344');
185     print sprintf ($format, "    citation 2 "); skip ($ferror2, eval { $io->next_bibref->identifier }, '21583752');
186     print sprintf ($format, "    citation 3 "); skip ($ferror2, eval { $io->next_bibref->identifier }, '21465135');
187     print sprintf ($format, "    citation 4 "); skip ($ferror2, eval { $io->next_bibref->identifier }, '21138228');
190 # test for FH
191 my $fh;
192 my @expvals = qw(11223344 21583752 21465135 21138228);
193 print "Testing FH\n";
194 eval { 
195     $fh = Bio::Biblio::IO->newFh('-format' => 'pubmedxml',
196                                   '-file'   => $testfile2,
197                                   '-result' => 'pubmed2ref');
198     while(<$fh>) {
199         ok($_->identifier,shift @expvals);
200     }
202 if( $@) {
203     foreach ( 1..4 ) { skip(1,"unable to use pubmedxml"); }
205 __END__