Class-AutoDB
view release on metacpan or search on metacpan
t/autodb.000.reqs.t view on Meta::CPAN
# 1) create new database as current user, else try as root, else use 'test'
# 2) grant permissions on database as current user or root. okay if this fails
# 3) connect to test database as current user
# FAIL if this doesn't work
my($dbh,$errstr);
my $testdb="testdb$$";
my $user=$ENV{USER};
if (create_db($testdb,$user) || create_db($testdb,'root')) {
grant_db($testdb,$user,$user) or grant_db($testdb,$user,'root');
$dbh=connect_db($testdb,$user);
}
unless ($dbh) {
# try using 'test'
$testdb='test';
create_db($testdb,$user) or create_db($testdb,'root');
grant_db('test',$user,$user) or grant_db('test',$user,'root');
$dbh=connect_db('test',$user);
unless ($dbh) {
$errstr="Cannot connect to database $testdb or test as user $user. errstr=".DBI->errstr;
goto FAIL;
}}
# make sure we can do all necessary operations
# create, alter, drop tables. insert, select, replace, update, select, delete
# NG 10-11-19: ops on views needed for Babel, not AutoDB
# NG 10-11-19: DROP tables and views if they exist
$dbh->do(qq(DROP TABLE IF EXISTS test_table)) or goto FAIL;
$dbh->do(qq(DROP VIEW IF EXISTS test_table)) or goto FAIL;
$dbh->do(qq(DROP TABLE IF EXISTS test_view)) or goto FAIL;
$dbh->do(qq(DROP VIEW IF EXISTS test_view)) or goto FAIL;
$dbh->do(qq(CREATE TABLE test_table(xxx INT))) or goto FAIL;
$dbh->do(qq(ALTER TABLE test_table ADD COLUMN yyy INT)) or goto FAIL;
$dbh->do(qq(CREATE VIEW test_view AS SELECT * from test_table)) or goto FAIL;
# do drop at end, since we need table here
$dbh->do(qq(INSERT INTO test_table(xxx) VALUES(123))) or goto FAIL;
$dbh->do(qq(SELECT * FROM test_table)) or goto FAIL;
$dbh->do(qq(SELECT * FROM test_view)) or goto FAIL;
$dbh->do(qq(REPLACE INTO test_table(xxx) VALUES(456))) or goto FAIL;
$dbh->do(qq(UPDATE test_table SET yyy=789 WHERE xxx=123)) or goto FAIL;
$dbh->do(qq(DELETE FROM test_table WHERE xxx=123)) or goto FAIL;
$dbh->do(qq(DROP VIEW IF EXISTS test_view)) or goto FAIL;
$dbh->do(qq(DROP TABLE IF EXISTS test_table)) or goto FAIL;
# NG 13-09-15: print MySQL version to help track down subtle FAILs
my $version=$dbh->selectrow_arrayref(qq(SELECT version())) or fail('get MySQL version');
if ($version) {
if (scalar(@$version)==1) {
diag('Testing MySQL version '.$version->[0]." database $testdb");
} else {
fail('get MySQL version returned row with wrong number of columns. expected 1, got '.
scalar(@$version));
}
}
# since we made it here, we can do everything!
return $testdb;
FAIL:
$errstr or $errstr=DBI->errstr;
my $diag=<<DIAG
These tests require that MySQL be running on 'localhost', that the user
running the tests can access MySQL without a password, and with these
credentials, has sufficient privileges to (1) create a 'test' database,
(2) create, alter, and drop tables in the 'test' database, (3) create and
drop views, and (4) run queries and updates on the database.
When verifying these capabilities, the test driver got the following
error message:
$errstr
DIAG
;
diag($diag);
undef;
}
# create test database. errors handled by caller
sub create_db {
my($testdb,$user)=@_;
my $dbh;
eval
{$dbh=DBI->connect("dbi:mysql:",$user,undef,
{AutoCommit=>1, ChopBlanks=>1, PrintError=>0, PrintWarn=>0, Warn=>0,})};
return undef unless $dbh;
$dbh->do(qq(CREATE DATABASE IF NOT EXISTS $testdb));
}
# grant all to test database. errors don't matter
sub grant_db {
my($testdb,$grantee,$user)=@_;
my $dbh;
eval
{$dbh=DBI->connect("dbi:mysql:",$user,undef,
{AutoCommit=>1, ChopBlanks=>1, PrintError=>0, PrintWarn=>0, Warn=>0,})};
return undef unless $dbh;
$dbh->do(qq(GRANT ALL on $testdb.* TO $grantee\@localhost));
}
# connect to testdb as current user
sub connect_db {
my($testdb,$user)=@_;
my $dbh;
eval
{$dbh=DBI->connect("dbi:mysql:database=$testdb",$user,undef,
{AutoCommit=>1, ChopBlanks=>1, PrintError=>0, PrintWarn=>0, Warn=>0,})};
$dbh;
}
# SDBM files created in t/SDBM directory
sub check_sdbm {
my $errstr=_check_sdbm();
return 1 unless $errstr;
my $diag=<<DIAG
These tests require that the user running the tests can create and
access access SDBM files. When verifying these capabilities, the test
driver got the following error message:
$errstr
DIAG
;
diag($diag);
undef;
}
( run in 1.662 second using v1.01-cache-2.11-cpan-140bd7fdf52 )