[prev] [thread] [next] [Date index for 2005/02/13]
I adapted my test cases for use with Class::DBI::Loader::mysql -- use strict; my $numtests; BEGIN { $numtests =12; } use Test::More tests => $numtests; use Class::DBI::Loader qw/debug/; use DBI; my $dbh; my $database = $ENV{MYSQL_NAME}; my $user = $ENV{MYSQL_USER}; my $password = $ENV{MYSQL_PASS}; SKIP: { eval { require Class::DBI::mysql; }; skip "Class::DBI::mysql is not installed", $numtests if $@; skip 'You need to set the MYSQL_NAME, MYSQL_USER and MYSQL_PASS environment variables', $numtests unless ( $database && $user ); my $dsn = "dbi:mysql:$database"; $dbh = DBI->connect( $dsn, $user, $password, { RaiseError => 1, PrintError => 1, } ); #setup the Class:DBI example $dbh->do(<<'SQL'); CREATE TABLE Artist ( artistid INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(64) ) type=InnoDB SQL $dbh->do(<<'SQL'); CREATE TABLE CD ( cdid INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, artist INTEGER NOT NULL references Artist (artistid) ON DELETE CASCADE, title VARCHAR(64), year INTEGER ) type=InnoDB SQL $dbh->do(<<'SQL'); CREATE TABLE Track ( trackid INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, cd INTEGER REFERENCES CD(cdid) ON DELETE CASCADE, position INTEGER, TITLE VARCHAR(64) ) type=InnoDB SQL my $loader = Class::DBI::Loader->new( dsn => $dsn, user => $user, password => $password, namespace => 'Music', relationships => 1 ); my @classes = $loader->classes; my @tables = $loader->tables; use Data::Dumper; diag( Dumper(@classes)); diag( Dumper(@tables)); is( $loader->find_class("Artist"),"Music::Artist","table to class checking"); is( $loader->find_class("CD"),"Music::CD","table to class checking"); is( $loader->find_class("Track"),"Music::Track","table to class checking"); isnt ( $loader->find_class("track"),"Music::Track","table to class checking"); my $artist = Music::Artist->create( { #artistid => 1, name => 'U2' }); ok(defined $artist,"Successful artist insertion"); is($artist->name,"U2","Check artist name"); is($artist->artistid,1,"Check artist ID number"); my $cd = $artist->can("add_to_cds"); ok(defined $cd,"CD relationship found"); SKIP: { skip "CD relationship not found",4 unless defined $cd; eval { $cd = $artist->add_to_cds({ # cdid => 1, title => 'October', year => 1980, }); }; ok(defined $cd,"CD insert"); # Oops, got it wrong. $cd->year(1981); $cd->update; is($cd->year,1981); ok($cd->can("add_to_tracks")); my $track = $cd->add_to_tracks( { position =>1, title =>"Life is Random" }); is($track->position,1); ok($cd->delete); } } END { if ($dbh) { $dbh->do("DROP TABLE Track CASCADE;"); $dbh->do("DROP TABLE CD CASCADE;"); $dbh->do("DROP TABLE Artist CASCADE;"); $dbh->disconnect; } }
Relationship testing for MySQL
|
Generated at 17:31 on 15 Feb 2005 by mariachi v0.52