From 33b621a67e1dedd416929797f635d81f66169ff3 Mon Sep 17 00:00:00 2001 From: Magali Ruffier <mr6@ebi.ac.uk> Date: Mon, 5 Aug 2013 14:07:01 +0000 Subject: [PATCH] updated to use patch database rather than old core also some ergonomical changes: - get a count+ is test replaced by is_count method - variable names declared at the top of the modules, so can easily change gene/dbid/db used if the test database changes --- modules/t/attributeAdaptor.t | 170 +++++++++++++++++++---------------- 1 file changed, 92 insertions(+), 78 deletions(-) diff --git a/modules/t/attributeAdaptor.t b/modules/t/attributeAdaptor.t index cadf6148bd..84d9ef7994 100644 --- a/modules/t/attributeAdaptor.t +++ b/modules/t/attributeAdaptor.t @@ -11,9 +11,14 @@ our $verbose = 1; our $clean = 0; my $multi = Bio::EnsEMBL::Test::MultiTestDB->new; +my $dbtype = 'patch'; +my $dbid = 92975; +my $stable_id = "ENSG00000112761"; +my $stable_id2 = "ENSG00000112769"; +my $count; -# get a core DBAdaptor -my $db = $multi->get_DBAdaptor("core"); +# get a DBAdaptor +my $db = $multi->get_DBAdaptor($dbtype); my $slice_adaptor = $db->get_SliceAdaptor(); my $mfa = $db->get_MiscFeatureAdaptor(); @@ -24,61 +29,58 @@ my $ga = $db->get_GeneAdaptor(); # my $aa = $db->get_AttributeAdaptor(); -ok($aa && ref($aa) && $aa->isa('Bio::EnsEMBL::DBSQL::AttributeAdaptor')); +is(ref($aa), 'Bio::EnsEMBL::DBSQL::AttributeAdaptor', "We have an attribute adaptor"); # hide the contents of the attrib_type, misc_attrib, seq_region_attrib tables # so we can test storing etc. with a clean slate -$multi->hide('core', 'misc_attrib', 'seq_region_attrib', 'attrib_type', 'gene_attrib'); +$multi->hide($dbtype, 'misc_attrib', 'seq_region_attrib', 'attrib_type', 'gene_attrib'); ############## # MiscFeature functionality tests # my $attrib = Bio::EnsEMBL::Attribute->new(-NAME => 'test_name', - -CODE => 'test_code', - -DESCRIPTION => 'test_desc', - -VALUE => 'test_value'); + -CODE => 'test_code', + -DESCRIPTION => 'test_desc', + -VALUE => 'test_value'); -my $mf = $mfa->fetch_by_dbID(1); +my $mf = $mfa->fetch_by_dbID($dbid); $aa->store_on_MiscFeature($mf, [$attrib]); # # make sure the misc_attrib table was updated # -my $count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM misc_attrib " . "WHERE misc_feature_id = 1")->[0]->[0]; - -ok($count == 1); +is_rows(1, $db, "misc_attrib", "where misc_feature_id = ? ", [$dbid]); # # make sure the attrib_type table was updated # -$count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM attrib_type " . "WHERE code = 'test_code'")->[0]->[0]; -ok($count == 1); +is_rows(1, $db, "attrib_type", "where code = ? ", ["test_code"]); # # test that we can now retrieve this attribute # my @attribs = @{$aa->fetch_all_by_MiscFeature($mf)}; -ok(@attribs == 1); +is(@attribs, 1, "Fetched 1 features"); $attrib = $attribs[0]; -ok($attrib->name eq 'test_name'); -ok($attrib->code eq 'test_code'); -ok($attrib->description eq 'test_desc'); -ok($attrib->value eq 'test_value'); +is($attrib->name, 'test_name', "Attrib name is test_name"); +is($attrib->code, 'test_code', "Attrib code is test_code"); +is($attrib->description, 'test_desc', "Attrib description is test_desc"); +is($attrib->value, 'test_value', "Attrib value is test_value"); @attribs = @{$aa->fetch_all_by_MiscFeature()}; -ok(@attribs == 1); +is(@attribs, 1, "One attrib fetched"); $attrib = $attribs[0]; -ok($attrib->name eq 'test_name'); -ok($attrib->code eq 'test_code'); -ok($attrib->description eq 'test_desc'); -ok($attrib->value eq 'test_value'); +is($attrib->name, 'test_name', "Attrib name is test_name"); +is($attrib->code, 'test_code', "Attrib code is test_code"); +is($attrib->description, 'test_desc', "Attrib description is test_desc"); +is($attrib->value, 'test_value', "Attrib value is test_value"); # # test the removal of this attribute @@ -88,15 +90,14 @@ $aa->remove_from_MiscFeature($mf); # # make sure the misc_attrib table was updated # -$count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM misc_attrib " . "WHERE misc_feature_id = 1")->[0]->[0]; -ok($count == 0); +is_rows(0, $db, "misc_attrib", "where misc_feature_id = ? ", [$dbid]); # # make sure the attribute is no longer retrievable # @attribs = @{$aa->fetch_all_by_MiscFeature($mf)}; -ok(@attribs == 0); +is(@attribs, 0, "Attribute is no longer retrievable"); ################# # Slice functionality tests @@ -107,67 +108,64 @@ $attrib = Bio::EnsEMBL::Attribute->new(-NAME => 'test_name2', -DESCRIPTION => 'test_desc2', -VALUE => 'test_value2'); -my $slice = $slice_adaptor->fetch_by_region('chromosome', '20'); +my $slice = $slice_adaptor->fetch_by_region('chromosome', '6'); $aa->store_on_Slice($slice, [$attrib]); +my $seqid = $slice->get_seq_region_id(); # # make sure the seq_region_attrib table was updated # -$count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM seq_region_attrib " . "WHERE seq_region_id = " . $slice->get_seq_region_id())->[0]->[0]; -ok($count == 1); +is_rows(1, $db, "seq_region_attrib", "where seq_region_id = ? ", [$seqid]); # # make sure the attrib_type table was updated # -$count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM attrib_type " . "WHERE code = 'test_code2'")->[0]->[0]; -ok($count == 1); +is_rows(1, $db, "attrib_type", "where code = ? ", ["test_code2"]); # # test that we can now retrieve this attribute # @attribs = @{$aa->fetch_all_by_Slice($slice)}; -ok(@attribs == 1); +is(@attribs, 1, "Fetched attribute"); @attribs = @{$aa->fetch_all_by_Slice($slice, "rubbish")}; -ok(@attribs == 0); +is(@attribs, 0, "No attribute fetched for code rubbish"); @attribs = @{$aa->fetch_all_by_Slice($slice, "test_code2")}; -ok(@attribs == 1); +is(@attribs, 1, "One attribute fetched for test_code2"); @attribs = @{$aa->fetch_all_by_Slice(undef, "test_code2")}; -ok(@attribs == 1); +is(@attribs, 1, "One attribute fetched across all slices"); $attrib = $attribs[0]; -ok($attrib->name eq 'test_name2'); -ok($attrib->code eq 'test_code2'); -ok($attrib->description eq 'test_desc2'); -ok($attrib->value eq 'test_value2'); +is($attrib->name, 'test_name2', "Attrib name is test_name2"); +is($attrib->code, 'test_code2', "Attrib code is test_code2"); +is($attrib->description, 'test_desc2', "Attrib description is test_desc2"); +is($attrib->value, 'test_value2', "Attrib value is test_value2"); # -# test the removal of this attribute with atrrib code +# test the removal of this attribute with attrib code # $aa->remove_from_Slice($slice, "junk"); -$count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM seq_region_attrib " . "WHERE seq_region_id = " . $slice->get_seq_region_id())->[0]->[0]; -ok($count == 1); +is_rows(1, $db, "seq_region_attrib", "where seq_region_id = ? ", [$seqid]); # # test the removal of this attribute # $aa->remove_from_Slice($slice, "test_code2"); -$count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM seq_region_attrib " . "WHERE seq_region_id = " . $slice->get_seq_region_id())->[0]->[0]; -ok($count == 0); +is_rows(0, $db, "seq_region_attrib", "where seq_region_id = ? ", [$seqid]); # # make sure the attribute is no longer retrievable # @attribs = @{$aa->fetch_all_by_Slice($slice)}; -ok(@attribs == 0); +is(@attribs, 0, "No attribs left for slice"); # # try to add an attribute with an already existing code @@ -176,30 +174,26 @@ $aa->store_on_Slice($slice, [$attrib]); # # make sure the seq_region_attrib table was updated # -$count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM seq_region_attrib " . "WHERE seq_region_id = " . $slice->get_seq_region_id())->[0]->[0]; -ok($count == 1); +is_rows(1, $db, "seq_region_attrib", "where seq_region_id = ? ", [$seqid]); # # make sure the attrib_type table was updated # -$count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM attrib_type " . "WHERE code = 'test_code2'")->[0]->[0]; -ok($count == 1); +is_rows(1, $db, "attrib_type", "where code = ? ", ["test_code2"]); @attribs = @{$aa->fetch_all_by_Slice($slice)}; -note "attribs: " . scalar(@attribs); -ok(@attribs == 1); +is(@attribs, 1, "One attrib on slice"); @attribs = @{$aa->fetch_all_by_Slice(undef)}; -ok(@attribs == 1); +is(@attribs, 1, "One attrib for all slices"); # # test the removal of this attribute # $aa->remove_from_Slice($slice); -$count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM seq_region_attrib " . "WHERE seq_region_id = " . $slice->get_seq_region_id())->[0]->[0]; -ok($count == 0); +is_rows(0, $db, "seq_region_attrib", "where seq_region_id = ? " , [$seqid]); # # test the storage of empty attrib values @@ -212,6 +206,12 @@ ok($count == 0); $aa->store_on_Slice($slice, [Bio::EnsEMBL::Attribute->new(%args, -VALUE => 0)]); my $new_rows = count_rows($db, 'seq_region_attrib'); cmp_ok($new_rows, '>', $current_rows, 'Asserting the storage of undefined attributes will always store them'); + # now remove again + $aa->remove_from_Slice($slice); + $count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM seq_region_attrib " . "WHERE seq_region_id = " . $slice->get_seq_region_id())->[0]->[0]; + + is_rows(0, $db, "seq_region_attrib", "where seq_region_id = ? ", [$seqid]); + } ################# @@ -223,7 +223,7 @@ $attrib = Bio::EnsEMBL::Attribute->new(-NAME => 'test_name2', -DESCRIPTION => 'test_desc2', -VALUE => 'test_value2'); -my $gene = $ga->fetch_by_stable_id('ENSG00000171456'); +my $gene = $ga->fetch_by_stable_id($stable_id); $aa->store_on_Gene($gene, [$attrib]); @@ -232,35 +232,35 @@ $aa->store_on_Gene($gene, [$attrib]); # $count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM gene_attrib " . "WHERE gene_id = " . $gene->dbID())->[0]->[0]; -ok($count == 1); +is($count, 1, "One gene attrib fetched"); # # make sure the attrib_type table was updated # $count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM attrib_type " . "WHERE code = 'test_code2'")->[0]->[0]; -ok($count == 1); +is($count, 1, "One attrib_type available"); # # test that we can now retrieve this attribute # @attribs = @{$aa->fetch_all_by_Gene($gene)}; -ok(@attribs == 1); +is(@attribs, 1, "Fetched one attribute for gene"); @attribs = @{$aa->fetch_all_by_Gene($gene, "rubbish")}; -ok(@attribs == 0); +is(@attribs, 0, "Fetched no attribute for code rubbish"); @attribs = @{$aa->fetch_all_by_Gene($gene, "test_code2")}; -ok(@attribs == 1); +is(@attribs, 1, "Fetched one attribute for code test_code2"); @attribs = @{$aa->fetch_all_by_Gene(undef, "test_code2")}; -ok(@attribs == 1); +is(@attribs, 1, "Fetch one attribute for genes"); $attrib = $attribs[0]; -ok($attrib->name eq 'test_name2'); -ok($attrib->code eq 'test_code2'); -ok($attrib->description eq 'test_desc2'); -ok($attrib->value eq 'test_value2'); +is($attrib->name, 'test_name2', "Attrib name is test_name2"); +is($attrib->code, 'test_code2', "Attrib code is test_code2"); +is($attrib->description, 'test_desc2', "Attrib description is test_desc2"); +is($attrib->value, 'test_value2', "Attrib value is test_value2"); # # test the removal of this attribute with atrrib code @@ -268,7 +268,7 @@ ok($attrib->value eq 'test_value2'); $aa->remove_from_Gene($gene, "junk"); $count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM gene_attrib " . "WHERE gene_id = " . $gene->dbID())->[0]->[0]; -ok($count == 1); +is($count, 1, "One gene attrib available"); # # test the removal of this attribute @@ -277,13 +277,13 @@ ok($count == 1); $aa->remove_from_Gene($gene, "test_code2"); $count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM gene_attrib " . "WHERE gene_id = " . $gene->dbID())->[0]->[0]; -ok($count == 0); +is($count, 0, "Gene attrib has been removed"); # # make sure the attribute is no longer retrievable # @attribs = @{$aa->fetch_all_by_Gene($gene)}; -ok(@attribs == 0); +is(@attribs, 0, "No attribs available for gene"); # # try to add an attribute with an already existing code @@ -294,20 +294,20 @@ $aa->store_on_Gene($gene, [$attrib]); # $count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM gene_attrib " . "WHERE gene_id = " . $gene->dbID())->[0]->[0]; -ok($count == 1); +is($count, 1, "One attrib added for gene"); # # make sure the attrib_type table was updated # $count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM attrib_type " . "WHERE code = 'test_code2'")->[0]->[0]; -ok($count == 1); +is($count, 1, "One attrib stored for code test_code2"); @attribs = @{$aa->fetch_all_by_Gene($gene)}; note "attribs: " . scalar(@attribs); -ok(@attribs == 1); +is(@attribs, 1, "One attrib for gene"); @attribs = @{$aa->fetch_all_by_Gene(undef)}; -ok(@attribs == 1); +is(@attribs, 1, "One attrib for genes"); # # test the removal of this attribute @@ -315,7 +315,7 @@ ok(@attribs == 1); $aa->remove_from_Gene($gene); $count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM gene_attrib " . "WHERE gene_id = " . $gene->dbID())->[0]->[0]; -ok($count == 0); +is($count, 0, "Attrib has been removed from gene"); # # test the storage of empty attrib values @@ -332,7 +332,7 @@ ok($count == 0); $aa->remove_from_Gene($gene); $count = $db->dbc->db_handle->selectall_arrayref("SELECT count(*) FROM gene_attrib " . "WHERE gene_id = " . $gene->dbID())->[0]->[0]; - ok($count == 0); + is($count, 0, "Attrib has been removed from gene"); } @@ -340,7 +340,7 @@ ok($count == 0); # Test batch storage # -my $gene2 = $ga->fetch_by_stable_id('ENSG00000131044'); +my $gene2 = $ga->fetch_by_stable_id($stable_id2); my $batch = {$gene->dbID() => [Bio::EnsEMBL::Attribute->new(-NAME => 'test_name2', -CODE => 'test_code2', -DESCRIPTION => 'test_desc2', VALUE => 'val1'), Bio::EnsEMBL::Attribute->new(-NAME => 'test_name2', -CODE => 'test_code2', -DESCRIPTION => 'test_desc2', VALUE => 'val2')], $gene2->dbID() => [Bio::EnsEMBL::Attribute->new(-NAME => 'test_name2', -CODE => 'test_code2', -DESCRIPTION => 'test_desc2', VALUE => 'val3'),]}; my $current_rows = count_rows($db, 'gene_attrib'); @@ -349,11 +349,25 @@ my $new_rows = count_rows($db, 'gene_attrib'); cmp_ok($new_rows, '=', $current_rows + 3, 'Asserting the storage of multiple attributes will always store them'); @attribs = @{$aa->fetch_all_by_Gene($gene)}; -ok(@attribs == 2); +is(@attribs, 2, "Two attribs available for gene"); @attribs = @{$aa->fetch_all_by_Gene($gene2)}; -ok(@attribs == 1); +is(@attribs, 1, "One attrib stored for gene2"); + +my $slice2 = $slice_adaptor->fetch_by_region('chromosome', 'X'); +my $batch = {$slice->get_seq_region_id() => [Bio::EnsEMBL::Attribute->new(-NAME => 'test_name2', -CODE => 'test_code2', -DESCRIPTION => 'test_desc2', VALUE => 'val1'), Bio::EnsEMBL::Attribute->new(-NAME => 'test_name2', -CODE => 'test_code2', -DESCRIPTION => 'test_desc2', VALUE => 'val2')], + $slice2->get_seq_region_id() => [Bio::EnsEMBL::Attribute->new(-NAME => 'test_name2', -CODE => 'test_code2', -DESCRIPTION => 'test_desc2', VALUE => 'val3'),]}; +my $current_rows = count_rows($db, 'seq_region_attrib'); +$aa->store_batch_on_Slice($batch); +my $new_rows = count_rows($db, 'seq_region_attrib'); +cmp_ok($new_rows, '=', $current_rows + 3, 'Asserting the storage of multiple attributes will always store them'); + +@attribs = @{$aa->fetch_all_by_Slice($slice)}; +is(@attribs, 2, "Two attribs available for slice"); + +@attribs = @{$aa->fetch_all_by_Slice($slice2)}; +is(@attribs, 1, "One attrib stored for slice2"); -$multi->restore('core', 'misc_attrib', 'seq_region_attrib', 'attrib_type'); +$multi->restore($dbtype, 'misc_attrib', 'seq_region_attrib', 'attrib_type'); done_testing(); -- GitLab