make test DB generation script work with new world order
[scpubgit/stemmatology.git] / base / t / text_tradition_directory.t
CommitLineData
12523041 1#!/usr/bin/perl -w
2
3use strict;
4use Test::More 'no_plan';
5$| = 1;
6
7
8
9# =begin testing
10{
861c3e27 11use TryCatch;
12523041 12use File::Temp;
951ddfe8 13use Safe::Isa;
12523041 14use Text::Tradition;
12523041 15use_ok 'Text::Tradition::Directory';
16
17my $fh = File::Temp->new();
18my $file = $fh->filename;
19$fh->close;
20my $dsn = "dbi:SQLite:dbname=$file";
861c3e27 21my $uuid;
12523041 22my $t = Text::Tradition->new(
56cf65bd 23 'name' => 'inline',
24 'input' => 'Tabular',
25 'file' => 't/data/simple.txt',
26 );
951ddfe8 27my $stemma_enabled;
28eval { $stemma_enabled = $t->enable_stemmata; };
861c3e27 29
30{
31 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
32 'extra_args' => { 'create' => 1 } );
951ddfe8 33 ok( $d->$_isa('Text::Tradition::Directory'), "Got directory object" );
861c3e27 34
35 my $scope = $d->new_scope;
36 $uuid = $d->save( $t );
37 ok( $uuid, "Saved test tradition" );
38
951ddfe8 39 SKIP: {
40 skip "Analysis package not installed", 5 unless $stemma_enabled;
41 my $s = $t->add_stemma( dotfile => 't/data/simple.dot' );
42 ok( $d->save( $t ), "Updated tradition with stemma" );
43 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
44 is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" );
45 try {
46 $d->save( $s );
47 } catch( Text::Tradition::Error $e ) {
48 is( $e->ident, 'database error', "Got exception trying to save stemma directly" );
49 like( $e->message, qr/Cannot directly save non-Tradition object/,
50 "Exception has correct message" );
51 }
861c3e27 52 }
53}
54my $nt = Text::Tradition->new(
55 'name' => 'CX',
56 'input' => 'CollateX',
57 'file' => 't/data/Collatex-16.xml',
58 );
951ddfe8 59ok( $nt->$_isa('Text::Tradition'), "Made new tradition" );
861c3e27 60
61{
62 my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
63 my $scope = $f->new_scope;
98a6cab2 64 is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
861c3e27 65 my $nuuid = $f->save( $nt );
66 ok( $nuuid, "Stored second tradition" );
98a6cab2 67 my @tlist = $f->traditionlist;
68 is( scalar @tlist, 2, "Directory index has both traditions" );
861c3e27 69 my $tf = $f->tradition( $uuid );
98a6cab2 70 my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
71 is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
861c3e27 72 is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
951ddfe8 73 my $sid;
74 SKIP: {
75 skip "Analysis package not installed", 4 unless $stemma_enabled;
76 $sid = $f->object_to_id( $tf->stemma(0) );
77 try {
78 $f->tradition( $sid );
79 } catch( Text::Tradition::Error $e ) {
80 is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" );
81 like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" );
82 }
83 try {
84 $f->delete( $sid );
85 } catch( Text::Tradition::Error $e ) {
86 is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
87 like( $e->message, qr/Cannot directly delete non-Tradition object/,
88 "Exception has correct message" );
89 }
861c3e27 90 }
ad39942e 91
861c3e27 92 $f->delete( $uuid );
93 ok( !$f->exists( $uuid ), "Object is deleted from DB" );
951ddfe8 94 ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ) if $stemma_enabled;
98a6cab2 95 is( scalar $f->traditionlist, 1, "Object is deleted from index" );
861c3e27 96}
97
d7ba60b4 98{
861c3e27 99 my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
100 my $scope = $g->new_scope;
98a6cab2 101 is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
ad39942e 102 my $ntobj = $g->tradition( 'CX' );
09909f9d 103 my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses;
104 my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses;
ad39942e 105 is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" );
861c3e27 106}
12523041 107}
108
109
110
111
1121;