More robust test suite skip-checks, check ENV before loading modules
[dbsrgits/DBIx-Class.git] / t / cdbi / testlib / MyBase.pm
CommitLineData
c6d74d3e 1package # hide from PAUSE
2 MyBase;
ea2e61bf 3
4use strict;
d2cee1fa 5use base qw(DBIx::Class::CDBICompat);
6
7use DBI;
ea2e61bf 8
f9cc85ce 9our $dbh;
ea2e61bf 10
49b3a264 11my $err;
12if (! $ENV{DBICTEST_MYSQL_DSN} ) {
13 $err = 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test';
14}
15elsif ( ! DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mysql') ) {
16 $err = 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mysql')
17}
18
19if ($err) {
20 my $t = eval { Test::Builder->new };
21 if ($t and ! $t->current_test) {
22 $t->skip_all ($err);
23 }
24 else {
25 die "$err\n";
26 }
27}
28
9381840d 29my @connect = (@ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}, { PrintError => 0});
ea2e61bf 30$dbh = DBI->connect(@connect) or die DBI->errstr;
31my @table;
32
33END { $dbh->do("DROP TABLE $_") foreach @table }
34
35__PACKAGE__->connection(@connect);
36
37sub set_table {
6a3bf251 38 my $class = shift;
39 $class->table($class->create_test_table);
ea2e61bf 40}
41
42sub create_test_table {
6a3bf251 43 my $self = shift;
44 my $table = $self->next_available_table;
45 my $create = sprintf "CREATE TABLE $table ( %s )", $self->create_sql;
46 push @table, $table;
47 $dbh->do($create);
48 return $table;
ea2e61bf 49}
50
51sub next_available_table {
6a3bf251 52 my $self = shift;
53 my @tables = sort @{
54 $dbh->selectcol_arrayref(
55 qq{
ea2e61bf 56 SHOW TABLES
57 }
6a3bf251 58 )
59 };
60 my $table = $tables[-1] || "aaa";
61 return "z$table";
ea2e61bf 62}
63
641;