Skip to content
Snippets Groups Projects
Commit 5623d794 authored by Arne Stabenau's avatar Arne Stabenau
Browse files

added test_db and remove_test_db. Will generate the database from binary dump

in test-db subdirectory in t. Database handle for this db is static in EnsTestDB so
not regenerated for each subtest.
parent e9c22306
No related branches found
No related tags found
No related merge requests found
......@@ -61,6 +61,9 @@ use Carp;
#Package variable for unique database name
my $counter=0;
my $static_test_dbh = undef;
my $static_test_dbname = "";
{
# This is a list of possible entries in the config
# file "EnsTestDB.conf" or in the hash being used.
......@@ -214,6 +217,9 @@ sub create_db {
### FIXME: not portable between different drivers
my $locator = 'DBI:'. $self->driver .':host='.$self->host;
if( defined $self->port() ) {
$locator .= ";port=".$self->port();
}
my $db = DBI->connect(
$locator, $self->user, $self->pass, {RaiseError => 1}
) or confess "Can't connect to server";
......@@ -349,6 +355,94 @@ sub DESTROY {
}
}
=head2 test_db
Arg 1 : txt $database_name
is not autogenerated, must be removed explicitly
Function : returns the test-db filled with approx 2MB of data in the
current schema. Done once on first call. Uploads test-db
subdir into normal test database.
Returntype: Bio::EnsEMBL::DBSQL::DBAdaptor
Exceptions: none
Caller : test modules
=cut
sub test_db {
my $self = shift;
my $dbname = shift;
if( ! defined $static_test_dbh ) {
# dbhandle generation ...
my $locator = 'DBI:'. $self->driver .':host='.$self->host;
if( defined $self->port() ) {
$locator .= ";port=".$self->port();
}
my $db = DBI->connect(
$locator, $self->user, $self->pass, {RaiseError => 1}
) or confess "Can't connect to server";
my $sth = $db->prepare( "show databases" );
my $dbexists = 0;
$sth->execute();
while( my $aref = $sth->fetchrow_arrayref ) {
if( $aref->[0] eq $dbname ) {
# db exists
$dbexists = 1;
last;
}
}
# check if db exists, then dont upload
if( $dbexists ) {
$db->do( "use $dbname" );
$static_test_dbh = $db;
$static_test_dbname = $dbname;
} else {
# create and upload database
$db->do( "create database $dbname" );
$db->do( "use $dbname" );
my $dir = __FILE__;
$dir =~ s/[^\/]*$//g;
my @files = glob( $dir."test-db/*.sql" );
my $tmp_db = $self->db_handle();
# create the schema
$self->{_db_handle} = $db ;
$self->do_sql_file( @files );
$self->{_db_handle}= $tmp_db;
# import the data
for my $file ( @files ) {
my $datafile = $file;
$datafile =~ s/\.sql$/.txt/g;
my ( $tablename ) = $file =~ /([^\/]*)\.sql/;
$db->do( "load data local infile '$datafile' into table $tablename" );
print STDERR "imported $datafile\n";
}
$static_test_dbname = $dbname;
$static_test_dbh = $db;
}
}
return $static_test_dbh;
}
sub remove_test_db {
$static_test_dbh->do( "drop database $static_test_dbname" );
$static_test_dbh->disconnect();
$static_test_dbh = undef;
$static_test_dbname = undef;
}
1;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment