* convert over to Module::Build
[bioperl-db.git] / t / 13remove.t
blobe5d88c82ec59e5acfdde4381daa012538b74acfc
1 # -*-Perl-*-
2 # $Id$
4 BEGIN {
5     use lib 't';
6     use Bio::Root::Test;
7     test_begin(-tests => 61);
9     use_ok('DBTestHarness');
10     use_ok('Bio::SeqIO');
13 $biosql = DBTestHarness->new("biosql");
14 $db = $biosql->get_DBAdaptor();
15 ok $db;
17 my $seqio = Bio::SeqIO->new('-format' => 'genbank',
18                             '-file' => test_input_file('test.genbank'));
20 my ($seq, $pseq);
21 my @seqs = ();
22 my @arr = ();
24 eval {
25     my $pk = -1;
26     while($seq = $seqio->next_seq()) {
27         $pseq = $db->create_persistent($seq);
28         $pseq->namespace("mytestnamespace");
29         $pseq->create();
30         ok $pseq->primary_key();
31         cmp_ok $pseq->primary_key(), '!=', $pk;
32         $pk = $pseq->primary_key();
33         push(@seqs, $pseq);
34     }
35     is (scalar(@seqs), 4);
36     $pseq = $seqs[@seqs-1];
38     $seqadp = $db->get_object_adaptor("Bio::SeqI");
39     ok $seqadp;
41     # re-fetch from database
42     $pseq = $seqadp->find_by_primary_key($pseq->primary_key());
43     
44     # features
45     @arr = $pseq->top_SeqFeatures();
46     is (scalar(@arr), 26);
48     # references
49     @arr = $pseq->annotation()->get_Annotations("reference");
50     is (scalar(@arr), 1);
52     # all feature qualifier/value pairs
53     @arr = ();
54     foreach my $feat ($pseq->top_SeqFeatures()) {
55         foreach ($feat->get_all_tags()) {
56             push(@arr, $feat->each_tag_value($_));
57         }
58     }
59     is (scalar(@arr), 38);
61     # delete all features
62     foreach my $feat ($pseq->top_SeqFeatures()) {
63         is ($feat->remove(), 1);
64     }
66     # delete all references
67     foreach my $ref ($pseq->annotation()->get_Annotations("reference")) {
68         is ($ref->remove(), 1);
69     }
71     # re-fetch sequence and retest
72     $pseq = $seqadp->find_by_primary_key($pseq->primary_key());
73     
74     # features
75     @arr = $pseq->top_SeqFeatures();
76     is (scalar(@arr), 0);
78     # references
79     @arr = $pseq->annotation()->get_Annotations("reference");
80     is (scalar(@arr), 0);
82     # test removing associations:
84     # add the same comment to both seq0 and seq1
85     my $cmt = Bio::Annotation::Comment->new(
86                                         -tagname => "comment",
87                                         -text => "this is a simple comment");
88     # add the same simple value to both seq0 and seq1
89     my $sv = Bio::Annotation::SimpleValue->new(-tagname => "Fancy",
90                                                -value => "a simple value");
91     $seqs[0]->annotation->add_Annotation($cmt);
92     $seqs[0]->annotation->add_Annotation($sv);
93     $seqs[1]->annotation->add_Annotation($cmt);
94     $seqs[1]->annotation->add_Annotation($sv);
95     ok $seqs[0]->store();
96     ok $seqs[1]->store();
97     # delete all annotation from seq0 (also shares a reference with seq1)
98     ok $seqs[0]->annotation->remove(-fkobjs => [$seqs[0]]);
100     # now re-fetch seq0 and seq1 by primary key
101     $pseq = $seqadp->find_by_primary_key($seqs[0]->primary_key);
102     my $pseq1 = $seqadp->find_by_primary_key($seqs[1]->primary_key);
103     # test annotation counts and whether seq1 was unaffected
104     is (scalar($pseq->annotation->get_Annotations()), 0);
105     is (scalar($pseq1->annotation->get_Annotations("reference")), 3);
106     is (scalar($pseq1->annotation->get_Annotations("comment")), 1);
107     my ($cmt1) = $pseq1->annotation->get_Annotations("comment");
108     is ($cmt1->text, $cmt->text);
109     is (scalar($pseq1->annotation->get_Annotations("Fancy")), 1);
110     my ($sv1) = $pseq1->annotation->get_Annotations("Fancy");
111     is ($sv1->value, $sv->value);
114 print STDERR $@ if $@;
116 # delete seq
117 foreach $pseq (@seqs) {
118     is ($pseq->remove(), 1);
120 my $ns = Bio::DB::Persistent::BioNamespace->new(-identifiable => $pseq);
121 ok $ns = $db->get_object_adaptor($ns)->find_by_unique_key($ns);
122 ok $ns->primary_key();
123 is ($ns->remove(), 1);