1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
use strict;
use Test::More tests => 25;
BEGIN {
use_ok('DBD::Mock');
use_ok('DBI');
}
{ # aliasing is off
my $dbh;
eval {
$dbh = DBI->connect('dbi:Mock:mysql', '', '');
};
ok(!$@, '... got our non-mock DB successfully');
isa_ok($dbh, 'DBI::db');
ok(!defined($dbh->{mock_attribute_aliases}), '... nothing here');
ok(!$dbh->{mock_database_name}, '... nothing here');
}
# now turn it on
$DBD::Mock::AttributeAliasing++;
{ # but without a dbname it does nothing
my $dbh;
eval {
$dbh = DBI->connect('dbi:Mock:', '', '');
};
ok(!$@, '... got our non-mock DB successfully');
isa_ok($dbh, 'DBI::db');
ok(!defined($dbh->{mock_attribute_aliases}), '... nothing here');
ok(!$dbh->{mock_database_name}, '... nothing here');
}
# now test the error
eval {
DBI->connect('dbi:Mock:Fail', '', '');
};
like($@, qr/Attribute aliases not available for \'Fail\'/, '... got the error we expected');
# test the MySQL mock db
{
my $dbh;
eval {
$dbh = DBI->connect('dbi:Mock:mysql', '', '');
};
ok(!$@, '... got our mock DB successfully');
isa_ok($dbh, 'DBI::db');
is($dbh->{mock_database_name}, 'mysql', '... and its the name we expected');
ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');
my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
isa_ok($sth, 'DBI::st');
$sth->execute();
is($dbh->{mysql_insertid}, 1, '... our alias works');
}
# and test it with the lowercasing
{
my $dbh;
eval {
$dbh = DBI->connect('dbi:Mock:MySQL', '', '');
};
ok(!$@, '... got our mock DB successfully');
isa_ok($dbh, 'DBI::db');
is($dbh->{mock_database_name}, 'MySQL', '... and its the name we expected');
ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');
my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
isa_ok($sth, 'DBI::st');
$sth->execute();
is($dbh->{mysql_insertid}, 1, '... our alias works');
}
|