require methods we assume existence of; add Module::Load to Language
[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 = $t->can( 'add_stemma' );
28
29 {
30         my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
31                 'extra_args' => { 'create' => 1 } );
32         ok( $d->$_isa('Text::Tradition::Directory'), "Got directory object" );
33         
34         my $scope = $d->new_scope;
35         $uuid = $d->save( $t );
36         ok( $uuid, "Saved test tradition" );
37         
38         SKIP: {
39                 skip "Analysis package not installed", 5 unless $stemma_enabled;
40                 my $s = $t->add_stemma( dotfile => 't/data/simple.dot' );
41                 ok( $d->save( $t ), "Updated tradition with stemma" );
42                 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
43                 is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" );
44                 try {
45                         $d->save( $s );
46                 } catch( Text::Tradition::Error $e ) {
47                         is( $e->ident, 'database error', "Got exception trying to save stemma directly" );
48                         like( $e->message, qr/Cannot directly save non-Tradition object/, 
49                                 "Exception has correct message" );
50                 }
51         }
52 }
53 my $nt = Text::Tradition->new(
54         'name' => 'CX',
55         'input' => 'CollateX',
56         'file' => 't/data/Collatex-16.xml',
57         );
58 ok( $nt->$_isa('Text::Tradition'), "Made new tradition" );
59
60 {
61         my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
62         my $scope = $f->new_scope;
63         is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
64         my $nuuid = $f->save( $nt );
65         ok( $nuuid, "Stored second tradition" );
66         my @tlist = $f->traditionlist;
67         is( scalar @tlist, 2, "Directory index has both traditions" );
68         my $tf = $f->tradition( $uuid );
69         my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
70         is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
71         is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
72         my $sid;
73         SKIP: {
74                 skip "Analysis package not installed", 4 unless $stemma_enabled;
75                 $sid = $f->object_to_id( $tf->stemma(0) );
76                 try {
77                         $f->tradition( $sid );
78                 } catch( Text::Tradition::Error $e ) {
79                         is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" );
80                         like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" );
81                 }
82                 try {
83                         $f->delete( $sid );
84                 } catch( Text::Tradition::Error $e ) {
85                         is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
86                         like( $e->message, qr/Cannot directly delete non-Tradition object/, 
87                                 "Exception has correct message" );
88                 }
89         }
90         
91         $f->delete( $uuid );
92         ok( !$f->exists( $uuid ), "Object is deleted from DB" );
93         ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ) if $stemma_enabled;
94         is( scalar $f->traditionlist, 1, "Object is deleted from index" );
95 }
96
97 {
98         my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
99         my $scope = $g->new_scope;
100         is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
101         my $ntobj = $g->tradition( 'CX' );
102         my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses;
103         my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses;
104         is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" );
105 }
106 }
107
108
109
110
111 1;