Fixed recursively loading re-rooted stemma
[scpubgit/stemmaweb.git] / script / maketestdb.pl
CommitLineData
9a3fb143 1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5use feature 'say';
6use Text::Tradition;
7use Text::Tradition::Directory;
8
9my $DBDIR = 'db';
10my $DBNAME = 'traditions.db';
11my $DBEXT = 'test';
bfaf51f3 12# Make the directory on the filesystem if necessary
13unless( -d $DBDIR ) {
14 mkdir $DBDIR
15 or die "Could not make database director $DBDIR";
16 say "Created directory for test database";
17}
18# Delete the old db if it exists
9a3fb143 19if( -f "$DBDIR/$DBNAME.$DBEXT" ) {
20 unlink( "$DBDIR/$DBNAME.$DBEXT" );
21}
22if( -l "$DBDIR/$DBNAME" ) {
23 unlink( "$DBDIR/$DBNAME" );
24} elsif( -e "$DBDIR/$DBNAME" ) {
25 unlink( "$DBDIR/$DBNAME.bak" ) if -f "$DBDIR/$DBNAME.bak";
26 rename( "$DBDIR/$DBNAME", "$DBDIR/$DBNAME.bak" )
27 or die "Could not rename existing $DBNAME";
28}
29# Set up the test directory
30symlink( "$DBNAME.$DBEXT", "$DBDIR/$DBNAME" ) or die "Could not set up testing db symlink";
31
32my $dir = Text::Tradition::Directory->new(
33 dsn => "dbi:SQLite:dbname=$DBDIR/$DBNAME",
34 extra_args => { create => 1 }
35 );
36my $scope = $dir->new_scope();
bfaf51f3 37say "Created test database";
9a3fb143 38
39# Create users
40my $user = $dir->add_user({ username => 'user@example.org', password => 'UserPass' });
41my $admin = $dir->add_user({ username => 'admin@example.org',
42 password => 'AdminPass', role => 'admin' });
43die "Failed to create test users" unless $user && $admin;
44say "Created users";
45
46my $t1 = Text::Tradition->new( input => 'Self', file => 't/data/besoin.xml' );
47die "Failed to create test tradition #1" unless $t1;
532cc23b 48$t1->add_stemma( dotfile => 't/data/besoin_stemweb.dot' );
9a3fb143 49$user->add_tradition( $t1 );
50$dir->store( $user );
51say "Created test user tradition";
52
53my $t2 = Text::Tradition->new( input => 'Tabular', sep_char => ',',
54 file => 't/data/florilegium.csv' );
55$t2->add_stemma( dotfile => 't/data/florilegium.dot' );
56die "Failed to create test tradition #2" unless $t2;
57$t2->public( 1 );
58$dir->store( $t2 );
56e3972e 59my $t3 = Text::Tradition->new( input => 'Self', file => 't/data/john.xml' );
60$t3->public( 1 );
61$t3->name( 'John verse' );
62$dir->store( $t3 );
2d6431a4 63my $t4 = Text::Tradition->new( input => 'Self', file => 't/data/collatecorr.xml' );
64$t4->public( 1 );
65$user->add_tradition( $t4 );
66$dir->store( $t4 );
67$dir->store( $user );
68
56e3972e 69say "Created test public traditions";
9a3fb143 70