Skip to content

Commit

Permalink
make tests pass with modern mysql; modernise tests to fail more helpf…
Browse files Browse the repository at this point in the history
…ully
  • Loading branch information
DrHyde committed Aug 2, 2021
1 parent c495f46 commit 23fa5dc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
12 changes: 7 additions & 5 deletions t/mysql.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use strict;
use warnings;
use lib '.';

use Test::Differences;

eval "use DBD::mysql";
my $dbname;
if($@) {
Expand All @@ -30,27 +32,27 @@ ok($dbh, "using database $dbname");
my $dir = File::Temp->newdir();
ok(-d $dir, "temp dir $dir exists");

is_deeply(
eq_or_diff(
[qw(address person)],
[sort { $a cmp $b } $db_driver->_get_tables($dbh)],
"Got list of tables from DB"
);
is_deeply(
eq_or_diff(
do {
my %r = $db_driver->_get_columns($dbh, 'person');
$r{id}->{default} = ''; # some versions of the DB return undef here
\%r;
},
{
id => { type => 'int(11)', pk => 1, null => !!0, default => '' },
id => { type => 'int', pk => 1, null => !!0, default => '' },
known_as => { type => 'varchar(128)', pk => !!0, null => !!1, default => undef },
formal_name => { type => 'varchar(128)', pk => !!0, null => !!1, default => undef },
dob => { type => 'datetime', pk => !!0, null => !!1, default => undef }
},
"Got list of columns from a table"
);

is_deeply(
eq_or_diff(
[sort { $a cmp $b } Class::DBI::ClassGenerator::create(
directory => $dir,
connect_info => $dsn,
Expand All @@ -69,7 +71,7 @@ ok(-f $_, "... and $_ exists") foreach(
File::Spec->catfile($dir, qw(A Class Person.pm))
);

is_deeply(
eq_or_diff(
[sort { $a cmp $b } Class::DBI::ClassGenerator::create(
directory => $dir,
connect_info => $dsn,
Expand Down
64 changes: 34 additions & 30 deletions t/mysql_create_db.pl
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,40 @@

use DBI;

my $drh = DBI->install_driver("mysql");
if(
my $db = DBI->connect('dbi:mysql:', 'root', '')
) {
my $dbname = 'DRCcdbicgentest01';
while(!$db->do("CREATE DATABASE $dbname")) {
$dbname++;
last if($dbname eq 'DRCcdbicgentest10');
}
END {
$db->do("DROP DATABASE $dbname") if($dbname);
}

my $dbname = 'DRCcdbicgentest01';
while(!$drh->func('createdb', $dbname, 'localhost', 'root', '', 'admin')) {
$dbname++;
last if($dbname eq 'DRCcdbicgentest10');
}
END {
$drh->func('dropdb', $dbname, 'localhost', 'root', '', 'admin')
}

if($dbname ne 'DRCcdbicgentest10') {
my $dbh = DBI->connect("dbi:mysql:database=$dbname", 'root', '');
if($dbname ne 'DRCcdbicgentest10') {
my $dbh = DBI->connect("dbi:mysql:database=$dbname", 'root', '');

$dbh->do(q{
CREATE TABLE person (
id INT PRIMARY KEY NOT NULL,
known_as VARCHAR(128),
formal_name VARCHAR(128),
dob DATETIME
);
});
$dbh->do(q{
CREATE TABLE address (
id INT PRIMARY KEY,
person_id INT,
address_text VARCHAR(255),
postcode_area CHAR(4),
postcode_street CHAR(3)
);
});
$dbh->do(q{
CREATE TABLE person (
id INT PRIMARY KEY NOT NULL,
known_as VARCHAR(128),
formal_name VARCHAR(128),
dob DATETIME
);
});
$dbh->do(q{
CREATE TABLE address (
id INT PRIMARY KEY,
person_id INT,
address_text VARCHAR(255),
postcode_area CHAR(4),
postcode_street CHAR(3)
);
});
}
$dbname;
} else {
'DRCcdbicgentest10';
}
$dbname;

0 comments on commit 23fa5dc

Please sign in to comment.