Added more missing 'Data::Stag' and 'LWP::UserAgent' requirements
[bioperl-live.git] / t / RemoteDB / HIV / HIVQuery.t
blobde27e52f3011ab0b7f8d5ba91d3a9a2b6b99ca5f
1 # testing Bio::DB::Query::HIVQuery
2 # $Id: HIVQuery.t 232 2008-12-11 14:51:51Z maj $
3 use strict;
4 use warnings;
6 BEGIN {
7     use Bio::Root::Test;
8     test_begin(
9         -tests => 41,
10         -requires_modules => [qw(LWP::UserAgent
11                                  XML::Simple )]
12     );
13     use_ok('Bio::DB::Query::HIVQuery');
14     use_ok('Bio::DB::HIV');
15     use_ok('Bio::Annotation::Collection');
16     use_ok('Bio::Annotation::Comment');
17     use_ok('Bio::Annotation::Reference');
18     use_ok('Bio::DB::HIV::HIVQueryHelper');
22 my $tobj= new Bio::DB::Query::HIVQuery(-RUN_OPTION=>0);
24 #object tests
25 isa_ok($tobj, 'Bio::DB::Query::HIVQuery');
27 #compliance tests
28 isa_ok($tobj, 'Bio::Root::Root');
29 can_ok($tobj, qw( count ids query ));
31 #methods
32 can_ok($tobj, qw( 
33                   get_annotations_by_ids 
34                   get_annotations_by_id 
35                   add_annotations_for_id 
36                   remove_annotations_for_ids
37                   remove_annotations_for_id
38                   remove_annotations
39                   get_accessions
40                   get_accessions_by_ids
41                   get_accessions_by_id
42                   )
43     );
44 #internals
45 can_ok($tobj, qw(
46                   _do_query
47                   _reset
48                   _session_id
49                   _run_option
50                   add_id
51                   lanl_base
52                   map_db
53                   make_search_if
54                   search_
55                   _map_db_uri
56                   _make_search_if_uri
57                   _search_uri
58                   _schema_file
59                   _schema
60                   _lanl_query
61                   _lanl_response
62                   _create_lanl_query
63                   _do_lanl_request
64                   _parse_lanl_response
65                   _parse_query_string
66                   _sorry 
67                  )
68     );
70 #defaults tests
71 ok($tobj->_map_db_uri, "_map_db_uri set in default object");
72 ok($tobj->_make_search_if_uri, "_make_search_if_uri set in default object");
73 ok($tobj->_search_uri, "_search_uri set in default object");
74 ok($tobj->_schema_file, "_schema_file set in default object");
75 ok(defined $tobj->_run_option, "_run_option set in default object");
76 ok($tobj->{_annotations}, "annotations container available");
78 # query syntax (no run)
79 $tobj->_reset;
80 $tobj->query(['coreceptor'=>['CCR5 CXCR4','CXCR4'], subtype=>['D', 'F'], country=>'Any']);
81 is($tobj->_do_query(0), 0, 'query syntax check 1');
82 $tobj->_reset;
83 $tobj->query({'coreceptor'=>['CCR5 CXCR4','CXCR4'], subtype=>['D', 'F'], country=>'Any'});
84 is($tobj->_do_query(0), 0, 'query syntax check 2');
85 $tobj->_reset;
86 $tobj->query({'query'=>{'coreceptor'=>['CCR5 CXCR4','CXCR4'], subtype=>['D', 'F']}, 'annot'=> ['country']});
87 is($tobj->_do_query(0),0, 'query syntax check 3');
88 $tobj->_reset;
89 $tobj->query("('CCR5 CXCR4', 'CXCR4')[coreceptor] (D F)[subtype] {[country]}");
90 is($tobj->_do_query(0),0, 'query parser check');
92 #multiquery parse
93 $tobj->_reset;
94 $tobj->query( "(SI[phenotype] ('CCR5 CXCR4')[coreceptor] C[subtype] OR NSI[phenotype] D[subtype]) AND ZA[country]");
95 is($tobj->_do_query(0),0, 'multiquery parse check');
97 SKIP: {
98         test_skip(-requires_module => 'HTML::Parser', -tests => 3);
99         no warnings; # HTML::Parser has an uninited value issue
100         use_ok('HTML::Parser');
101         use warnings;
102         # help test; just tests that file can be written and that tags are in matching 
103         # pairs, with reasonable placement of <html> and </html>
104         my $hlpf = test_output_file();
105         my $a = 0;
106         my $html = 0;
107         my $h = HTML::Parser->new(empty_element_tags => 1,
108                                   start_h => [sub {$a++; (shift eq 'html') && ($a==1) && $html++;}, "tagname"],
109                                   end_h   => [sub {$a--; (shift eq 'html') && ($a==0) && $html++;}, "tagname"] );
110         ok($tobj->help($hlpf), "help html to file");
111         {
112                 local $/;
113                 undef $/;
114                 open my $HP, '<', $hlpf or die "Could not read file '$hlpf': $!\n";
115                 $h->parse(<$HP>);
116                 is_deeply([$a, $html], [0, 2], "help html parsed");
117                 close $HP; # Always explicitly close filehandles
118                 1;
119         }
123 #exceptions tests
124 #pre-run query exceptions
125 $tobj->verbose(2);
126 $tobj->_reset;
127 $tobj->query( "narb[scroob]" );
128 throws_ok {$tobj->_do_query} qr/BadParameter/, "bad field exception check";
129 $tobj->_reset;
130 $tobj->query( "narb[phenotype]" );
131 throws_ok {$tobj->_do_query} qr/BadParameter/, "bad match data exception check";
132 $tobj->_reset;
133 $tobj->query( [ 'cd4_count'  => "" ] );
134 throws_ok {$tobj->_do_query} qr/BadParameter/, "empty field not ok exception check";
135 $tobj->_reset;
136 $tobj->{_schema} = undef;
137 throws_ok {$tobj->_do_query} qr/SchemaNotInit/, "uninitialized schema exception check";
138 throws_ok {$tobj->count} qr/Query not yet run/, "query not run (level 1) warning check";
139 throws_ok {$tobj->get_annotations_by_id($tobj->ids)} qr/Requires query run/, "query not run (level 2) warning check";
143 # network tests
144 SKIP : {
145     test_skip(-tests => 10,
146           -requires_networking => 1);
147     eval {$tobj  = Bio::DB::Query::HIVQuery->new(-QUERY=>"(SI[phenotype] ('CCR5 CXCR4')[coreceptor] C[subtype] OR NSI[phenotype] D[subtype]) AND ZA[country]",-RUN_OPTION=>2)};
148     if ($@) {
149         diag($@);
150         skip("Network problems, skipping all", 10);
151     }
152     ok($tobj,"live query");
153     cmp_ok( $tobj->count, ">=", 12, "enough sequences returned");
154 # test query object handling of Bio::DB::HIV   
155     my ($tdb, $seqio, $seq);
156     ok( $tdb = new Bio::DB::HIV, "create Bio::DB::HIV object");
157         eval {$seqio = $tdb->get_Stream_by_query($tobj)};
158     if ($@) {
159         diag($@);
160         skip("Network problems, skipping all", 7);
161     }
162     ok($seqio, "get SeqIO stream from query");
163 # test HIVAnnotProcessor indirectly
164     ok($seq = $seqio->next_seq, "access sequence stream");
165     ok($seq->annotation->get_value('Virus'), "'Virus' annotation present");
166     like ($seq->annotation->get_value('Virus','phenotype'), qr/SI/, "'Virus' phenotype annotation present");
167     like ($seq->annotation->get_value('Virus', 'subtype'), qr/[CD]/, "'Virus' subtype annotation present");
168     ok($seq->accession_number, "GenBank accession available");
170 # exception checks
171     $tobj->_reset;
172     $tobj->verbose(2);
173     $tobj->query( "2BR02B[accession]" );
174     throws_ok {$tobj->_do_query(2)} qr/no sequences/i, "no sequences warning check";