ensembl-hive  2.8.1
TestDB.pm
Go to the documentation of this file.
1 =head1 LICENSE
2 
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 =cut
19 
20 =head1 DESCRIPTION
21 
22 A database class that creates the DBIC schema in a test database, and then
23 cleans up after itself.
24 
25 Convenience methods prevent having to delve into DBIC guts for common activities
26 
27 =head1 SYNOPSIS
28 
29 my $testdb = Xref::Test::TestDB->new(
30  config_file => 'filename_if_not_default.conf',
31  reuse => 1 # This prevents the test DB cleanup, so you can debug database content
32 );
33 
34 =cut
35 
36 package Xref::Test::TestDB;
37 
38 use strict;
39 use warnings;
40 use Moose;
41 use namespace::autoclean;
42 
43 extends 'Xref::DB';
44 
45 has reuse => (
46  isa => 'Bool',
47  is => 'ro',
48  default => 0,
49 );
50 
51 sub _guess_config {
52  return 'testdb.conf';
53 }
54 
55 # On top of default config validator, inject a randomised test DB name
56 around '_init_config' => sub {
57  my ($sub, $self, @args) = @_;
58 
59  my $proto_config = $self->$sub();
60 
61  if (! exists $proto_config->{db}) {
62  $proto_config->{db} = sprintf '%s_xref_test_%s',$ENV{USER},int(rand(100000));
63  $proto_config->{create} = 1;
64  }
65  # return $proto_config;
66  $self->config($proto_config);
67  return;
68 };
69 
70 
71 =head2 DEMOLISH
72 
73 Description: It's a destructor. It cleans up databases left behind by the test
74  Behaviour is overridden with $self->reuse(1)
75 
76 =cut
77 sub DEMOLISH {
78  my $self = shift;
79  if ($self->reuse == 0 && defined $self->config) {
80  if ( $self->config->{driver} eq 'SQLite') {
81  unlink $self->config->{file};
82  } elsif ($self->config->{driver} eq 'mysql') {
83  $self->schema->storage->dbh->do('drop database '.$self->config->{db});
84  }
85  }
86  return;
87 }
88 
89 __PACKAGE__->meta->make_immutable;
90 
91 1;
cleanup
public cleanup()
Xref::DB
Definition: DB.pm:40
debug
public debug()
Xref::Test::TestDB
Definition: TestDB.pm:14