Code optimization. Can force a skip ISA checking for small speed boost
[bioperl-live.git] / t / Biblio.t
blob27e5f5a2156e890ad12b24146212f22b6399ec6e
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN { 
7     use lib 't/lib';
8     use BioperlTest;
9     
10     test_begin(-tests => 24);
11         
12         use_ok('Bio::Biblio');
13         use_ok('Bio::Biblio::IO');
16 my $testnum;
17 my $verbose = test_debug();
19 my $testfile = test_input_file('stress_test_medline.xml');
20 my $testfile2 = test_input_file('stress_test_pubmed.xml');
22 # check 'new...'
23 SKIP: {
24     test_skip(-tests => 1, -requires_module => 'SOAP::Lite');
25         ok my $biblio = Bio::Biblio->new(-location => 'http://localhost:4567');
28 # check MEDLINE XML parser
29 my $io;
30 SKIP: {
31         test_skip(-tests => 4, -requires_module => 'XML::Parser');
32     
33     ok defined ($io = Bio::Biblio::IO->new('-format' => 'medlinexml',
34                                                  '-file'   => $testfile,
35                                                  '-result' => 'raw'));
36         
37     print "Reading and parsing MEDLINE XML file...\n" if $verbose;
38     is ($io->next_bibref->{'medlineID'}, 'Text1', 'citation 1');
39     is ($io->next_bibref->{'medlineID'}, 'Text248', 'citation 2');
40     is ($io->next_bibref->{'medlineID'}, 'Text495', 'citation 3');
44 print "Getting citations using callback...\n" if $verbose;
45 my (@ids) = ('Text1', 'Text248', 'Text495');
46 my $callback_used = 'no';
47 $io = Bio::Biblio::IO->new('-format'   => 'medlinexml',
48                            '-file'     => $testfile,
49                           #'-result'   => 'medline2ref',  # this is default
50                            '-callback' => \&callback);
52 is ( $callback_used, 'yes', 'calling callback');
54 sub callback {
55         my $citation = shift;
56         $callback_used = 'yes';
57         is ($citation->{'_identifier'}, shift @ids);
60 SKIP: {
61         test_skip(-tests => 2, -requires_module => 'XML::Parser');
62         
63     $io = Bio::Biblio::IO->new('-format'   => 'medlinexml',
64                                                            '-data'     => <<XMLDATA,
65 <MedlineCitationSet>
66 <MedlineCitation>
67 <MedlineID>12345678</MedlineID>
68 <Article><Journal></Journal></Article>
69 </MedlineCitation>
70 <MedlineCitation>
71 <MedlineID>abcdefgh</MedlineID>
72 <Article><Journal></Journal></Article>
73 </MedlineCitation>
74 </MedlineCitationSet>
75 XMLDATA
76                                                            '-result'   => 'medline2ref');
77         
78         is ($io->next_bibref->{'_identifier'}, '12345678', 'citation 1');
79         is ($io->next_bibref->{'_identifier'}, 'abcdefgh', 'citation 2');
82 SKIP: {
83         test_skip(-tests => 2, -requires_modules => [qw(XML::Parser IO::String)]);
84         
85     print "Reading and parsing XML string handle...\n" if $verbose;
86     my $data = <<XMLDATA;
87 <MedlineCitationSet>
88 <MedlineCitation>
89 <MedlineID>87654321</MedlineID>
90 <Article><Journal></Journal></Article>
91 </MedlineCitation>
92 <MedlineCitation>
93 <MedlineID>hgfedcba</MedlineID>
94 <Article><Journal></Journal></Article>
95 </MedlineCitation>
96 </MedlineCitationSet>
97 XMLDATA
98     
99         $io = Bio::Biblio::IO->new('-format' => 'medlinexml',
100                                                            '-fh'     => IO::String->new ($data));
101         is (eval { $io->next_bibref->identifier }, '87654321', 'citation 1');
102         is (eval { $io->next_bibref->identifier }, 'hgfedcba', 'citation 2');
105 SKIP: {
106         test_skip(-tests => 5, -requires_module => 'XML::Parser');
107         
108     # check PUBMED XML parser
109     ok defined (eval { $io = Bio::Biblio::IO->new('-format' => 'pubmedxml',
110                              '-file'   => $testfile2,
111                              '-result' => 'pubmed2ref') });
112         
113     print "Reading and parsing PUBMED XML file...\n" if $verbose;
114     
115         is ($io->next_bibref->identifier, '11223344', 'citation 1');
116         is ($io->next_bibref->identifier, '21583752', 'citation 2');
117         is ($io->next_bibref->identifier, '21465135', 'citation 3');
118         is ($io->next_bibref->identifier, '21138228', 'citation 4');
121 SKIP: {
122     # test for FH
123     my $fh;
124     my @expvals = qw(11223344 21583752 21465135 21138228);
125     print "Testing FH\n" if $verbose;
126     eval { 
127         $fh = Bio::Biblio::IO->newFh('-format' => 'pubmedxml',
128                       '-file'   => $testfile2,
129                       '-result' => 'pubmed2ref');
130         while(<$fh>) {
131             is($_->identifier,shift @expvals);
132         }
133     };
134     if( $@) {
135         skip("unable to use pubmedxml",4);
136     }