X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Flib%2FDBICTest.pm;h=8f5dca31742289e9e1671299015f858be2ac92df;hb=c7aececc25235b590e39a63b7aa9ea98a3f4a10e;hp=413322369f8af38b35a2d29af492a363588697a5;hpb=c1a04675b324399531ada15951209b863429c92f;p=dbsrgits%2FDBIx-Class-Fixtures.git diff --git a/t/lib/DBICTest.pm b/t/lib/DBICTest.pm index 4133223..8f5dca3 100755 --- a/t/lib/DBICTest.pm +++ b/t/lib/DBICTest.pm @@ -45,15 +45,18 @@ default, unless the no_deploy or no_populate flags are set. sub init_schema { my $self = shift; my %args = @_; + my $db_file = "t/var/DBIxClass.db"; - unlink($db_file) if -e $db_file; - unlink($db_file . "-journal") if -e $db_file . "-journal"; mkdir("t/var") unless -d "t/var"; + if ( !$args{no_deploy} ) { + unlink($db_file) if -e $db_file; + unlink($db_file . "-journal") if -e $db_file . "-journal"; + } - my $dsn = $ENV{"FIXTURETEST_DSN"} || "dbi:SQLite:${db_file}"; - my $dbuser = $ENV{"FIXTURETEST_DBUSER"} || ''; - my $dbpass = $ENV{"FIXTURETEST_DBPASS"} || ''; + my $dsn = $args{"dsn"} || "dbi:SQLite:${db_file}"; + my $dbuser = $args{"user"} || ''; + my $dbpass = $args{"pass"} || ''; my $schema; @@ -67,7 +70,7 @@ sub init_schema { $schema = DBICTest::Schema->compose_namespace('DBICTest') ->connect(@connect_info); } - $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']); + if ( !$args{no_deploy} ) { __PACKAGE__->deploy_schema( $schema ); __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} ); @@ -75,6 +78,14 @@ sub init_schema { return $schema; } + +sub get_ddl_file { + my $self = shift; + my $schema = shift; + + return 't/lib/' . lc($schema->storage->dbh->{Driver}->{Name}) . '.sql'; +} + =head2 deploy_schema DBICTest->deploy_schema( $schema ); @@ -85,13 +96,40 @@ sub deploy_schema { my $self = shift; my $schema = shift; - open IN, "t/lib/sqlite.sql"; + my $file = shift || $self->get_ddl_file($schema); + open( my $fh, "<",$file ) or die "couldnt open $file, $!"; my $sql; - { local $/ = undef; $sql = ; } - close IN; - ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql); + { local $/ = undef; $sql = <$fh>; } + + foreach my $line (split(/;\n/, $sql)) { + print "$line\n"; + next if(!$line); + next if($line =~ /^--/); + next if($line =~ /^BEGIN TRANSACTION/m); + next if($line =~ /^COMMIT/m); + next if $line =~ /^\s+$/; # skip whitespace only + + $schema->storage->dbh->do($line) || print "Error on SQL: $line\n"; + } +} + + +=head2 clear_schema + + DBICTest->clear_schema( $schema ); + +=cut + +sub clear_schema { + my $self = shift; + my $schema = shift; + + foreach my $class ($schema->sources) { + $schema->resultset($class)->delete; + } } + =head2 populate_schema DBICTest->populate_schema( $schema ); @@ -110,6 +148,13 @@ sub populate_schema { [ 1, 'Caterwauler McCrae' ], [ 2, 'Random Boy Band' ], [ 3, 'We Are Goth' ], + [ 4, '' ], # Test overridden new will default name to "Test Name" using use_create => 1. + [ 32948, 'Big PK' ], + ]); + + $schema->populate('Artist::WashedUp', [ + [ qw/fk_artistid/ ], + [ 2 ], ]); $schema->populate('CD', [ @@ -118,7 +163,8 @@ sub populate_schema { [ 2, 1, "Forkful of bees", 2001 ], [ 3, 1, "Caterwaulin' Blues", 1997 ], [ 4, 2, "Generic Manufactured Singles", 2001 ], - [ 5, 3, "Come Be Depressed With Us", 1998 ], + [ 5, 2, "We like girls and stuff", 2003 ], + [ 6, 3, "Come Be Depressed With Us", 1998 ], ]); $schema->populate('Tag', [ @@ -146,16 +192,19 @@ sub populate_schema { [ 1, 1 ], [ 1, 2 ], [ 1, 3 ], + [ 2, 1 ], + [ 2, 2 ], + [ 3, 3 ], ]); $schema->populate('Track', [ - [ qw/trackid cd position title/ ], + [ qw/trackid cd position title last_updated_on/ ], [ 4, 2, 1, "Stung with Success"], [ 5, 2, 2, "Stripy"], [ 6, 2, 3, "Sticky Honey"], [ 7, 3, 1, "Yowlin"], [ 8, 3, 2, "Howlin"], - [ 9, 3, 3, "Fowlin"], + [ 9, 3, 3, "Fowlin", '2007-10-20 00:00:00'], [ 10, 4, 1, "Boring Name"], [ 11, 4, 2, "Boring Song"], [ 12, 4, 3, "No More Ideas"],