From: Errietta Kostala Date: Thu, 22 Jan 2015 15:26:13 +0000 (+0000) Subject: better db code X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=325d9f4176bba1c6811c031a75be3ef5eced7c94;p=scpubgit%2Fstemmaweb.git better db code --- diff --git a/t/04login.t b/t/04login.t index 59043c6..2b9849f 100644 --- a/t/04login.t +++ b/t/04login.t @@ -10,9 +10,11 @@ use HTML::TreeBuilder; use Data::Dumper; use FindBin; +use lib ("$FindBin::Bin/lib"); -use IPC::System::Simple qw(system); -system("$FindBin::Bin/../script/maketestdb.pl"); +use stemmaweb::Test::DB; + +stemmaweb::Test::DB->new_db; LWP::Protocol::PSGI->register(stemmaweb->psgi_app); diff --git a/t/lib/stemmaweb/Test/DB.pm b/t/lib/stemmaweb/Test/DB.pm new file mode 100644 index 0000000..6bad659 --- /dev/null +++ b/t/lib/stemmaweb/Test/DB.pm @@ -0,0 +1,50 @@ +package stemmaweb::Test::DB; + +use warnings; +use strict; + +use FindBin; + +sub new_db { + my $DBDIR = "$FindBin::Bin/db"; + my $DBNAME = 'traditions.db'; + my $DBEXT = 'test'; + + # Make the directory on the filesystem if necessary + unless( -d $DBDIR ) { + mkdir $DBDIR + or die "Could not make database director $DBDIR"; + } + # Delete the old db if it exists + if( -f "$DBDIR/$DBNAME.$DBEXT" ) { + unlink( "$DBDIR/$DBNAME.$DBEXT" ); + } + if( -l "$DBDIR/$DBNAME" ) { + unlink( "$DBDIR/$DBNAME" ); + } elsif( -e "$DBDIR/$DBNAME" ) { + unlink( "$DBDIR/$DBNAME.bak" ) if -f "$DBDIR/$DBNAME.bak"; + rename( "$DBDIR/$DBNAME", "$DBDIR/$DBNAME.bak" ) + or die "Could not rename existing $DBNAME"; + } + + # Set up the test directory + symlink( "$DBNAME.$DBEXT", "$DBDIR/$DBNAME" ) or die "Could not set up testing db symlink"; + + my $dir = Text::Tradition::Directory->new( + dsn => "dbi:SQLite:dbname=$DBDIR/$DBNAME", + extra_args => { create => 1 } + ); + my $scope = $dir->new_scope(); + + # Create users + my $user = $dir->add_user({ username => 'user@example.org', password => 'UserPass' }); + + + my $t2 = Text::Tradition->new( input => 'Tabular', sep_char => ',', + file => 't/data/florilegium.csv' ); + $t2->add_stemma( dotfile => 't/data/florilegium.dot' ); + + $user->add_tradition($t2); +} + +1;