sync with trunk
[bioperl-db.git] / t / 01dbadaptor.t
blob58777be779f4b7816829cc655faf756143abd206
1 # -*-Perl-*-
2 # $Id$
4 BEGIN {
5     use lib 't';
6     use Bio::Root::Test;
7     test_begin(-tests => 23);
8     use_ok('BioSQLBase');
9     use_ok('Bio::DB::BioSQL::DBAdaptor');
10     use_ok('Bio::BioEntry');
11     use_ok('Bio::DB::Persistent::BioNamespace');
14 my $biosql = DBTestHarness->new("biosql");
15 ok $biosql;
17 my $dbc = $biosql->get_DBContext();
18 ok $dbc;
20 my $db = $dbc->dbadaptor();
21 isa_ok $db,"Bio::DB::DBAdaptorI";
22 isa_ok $db,"Bio::DB::BioSQL::DBAdaptor";
24 # test connection
25 my $dbh = $dbc->dbi()->new_connection($dbc);
26 ok $dbh;
27 my $rc = $dbh->ping();
28 ok ($rc && ($rc ne '0 but true'));
30 # test that the -dsn option works as advertised
31 my $dsn = $dbc->dbi()->build_dsn($dbc); # that's what's used for connecting
32 my $db2 = Bio::DB::BioDB->new(-database => "biosql", -dsn => $dsn);
33 my $dbc2 = $db2->dbcontext;
34 is ($dbc2->dbi->build_dsn($dbc2), $dsn);
35 $dbc2->host("i.dont.exist.com");
36 $dbc2->port(9876);
37 is ($dbc2->dbi->build_dsn($dbc2), $dsn); # dsn is to be taken verbatim
38 $db2 = undef;
39 # test the dsn parsing results
40 $dbc2 = Bio::DB::SimpleDBContext->new(-dsn => $dsn);
41 is ($dbc2->driver, $dbc->driver);
42 is ($dbc2->dbname, $dbc->dbname);
43 is ($dbc2->host, $dbc->host);
44 is ($dbc2->port, $dbc->port);
46 # test that transaction control is active by trying to roll back
47 my $ns = Bio::BioEntry->new(-namespace => "__dummy__", -authority => "nobody");
48 my $adp = $db->get_object_adaptor("BioNamespace");
49 # we need to disable caching, or otherwise it will bite us
50 $adp->caching_mode(0);
51 my $pns = Bio::DB::Persistent::BioNamespace->new(-identifiable => $ns,
52                                                  -adaptor => $adp);
53 is ($pns->namespace, $ns->namespace);
54 is ($pns->authority, $ns->authority);
55 # try to find it - this should fail
56 my $dbns = $adp->find_by_unique_key($pns);
57 if ($dbns) {
58     warn("found __dummy__ namespace - leftover from previously aborted test?");
59     # remove it
60     $dbns->remove();
61     # we need to commit here or otherwise we can't safely test for rollback
62     $dbns->commit;
64 ok $pns->create();
65 # now we should find it - sanity check
66 $dbns = Bio::DB::Persistent::BioNamespace->new(-identifiable => $ns);
67 $dbns = $adp->find_by_unique_key($dbns);
68 ok ($dbns);
69 is ($dbns->primary_key, $pns->primary_key);
70 is ($dbns->namespace, $pns->namespace);
71 # now rollback
72 $adp->rollback();
73 $dbns = $adp->find_by_unique_key($pns);
74 is ($dbns, undef);
75 if ($dbns) {
76     warn("your RDBMS does not have transactions enabled - please fix this\n");
79 # and the namespace should be gone
80 $dbh->disconnect();