split off stemma analysis modules from base Tradition layer
[scpubgit/stemmatology.git] / base / t / text_tradition_directory.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5 $| = 1;
6
7
8
9 # =begin testing
10 {
11 use TryCatch;
12 use File::Temp;
13 use Safe::Isa;
14 use Text::Tradition;
15 use_ok 'Text::Tradition::Directory';
16
17 my $fh = File::Temp->new();
18 my $file = $fh->filename;
19 $fh->close;
20 my $dsn = "dbi:SQLite:dbname=$file";
21 my $uuid;
22 my $t = Text::Tradition->new( 
23         'name'  => 'inline', 
24         'input' => 'Tabular',
25         'file'  => 't/data/simple.txt',
26         );
27 my $stemma_enabled;
28 eval { $stemma_enabled = $t->enable_stemmata; };
29
30 {
31         my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
32                 'extra_args' => { 'create' => 1 } );
33         ok( $d->$_isa('Text::Tradition::Directory'), "Got directory object" );
34         
35         my $scope = $d->new_scope;
36         $uuid = $d->save( $t );
37         ok( $uuid, "Saved test tradition" );
38         
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                 }
52         }
53 }
54 my $nt = Text::Tradition->new(
55         'name' => 'CX',
56         'input' => 'CollateX',
57         'file' => 't/data/Collatex-16.xml',
58         );
59 ok( $nt->$_isa('Text::Tradition'), "Made new tradition" );
60
61 {
62         my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
63         my $scope = $f->new_scope;
64         is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
65         my $nuuid = $f->save( $nt );
66         ok( $nuuid, "Stored second tradition" );
67         my @tlist = $f->traditionlist;
68         is( scalar @tlist, 2, "Directory index has both traditions" );
69         my $tf = $f->tradition( $uuid );
70         my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
71         is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
72         is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
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                 }
90         }
91         
92         $f->delete( $uuid );
93         ok( !$f->exists( $uuid ), "Object is deleted from DB" );
94         ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ) if $stemma_enabled;
95         is( scalar $f->traditionlist, 1, "Object is deleted from index" );
96 }
97
98 {
99         my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
100         my $scope = $g->new_scope;
101         is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
102         my $ntobj = $g->tradition( 'CX' );
103         my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses;
104         my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses;
105         is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" );
106 }
107 }
108
109
110
111
112 1;