e99c82b532a13ba173936f793bbc619205300d2a
[scpubgit/stemmatology.git] / persistence / 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 $user = 'user@example.org';
23 my $t = Text::Tradition->new( 
24         'name'  => 'inline', 
25         'input' => 'Tabular',
26         'file'  => 't/data/simple.txt',
27         );
28 my $stemma_enabled = $t->can( 'add_stemma' );
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         # Add a test user
40         my $user = $d->add_user({ username => $user, password => 'UserPass' }); 
41         $user->add_tradition( $t );
42         $d->store( $user );
43         is( $t->user, $user, "Assigned tradition to test user" );
44         
45         SKIP: {
46                 skip "Analysis package not installed", 5 unless $stemma_enabled;
47                 my $s = $t->add_stemma( dotfile => 't/data/simple.dot' );
48                 ok( $d->save( $t ), "Updated tradition with stemma" );
49                 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
50                 is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" );
51                 try {
52                         $d->save( $s );
53                 } catch( Text::Tradition::Error $e ) {
54                         is( $e->ident, 'database error', "Got exception trying to save stemma directly" );
55                         like( $e->message, qr/Cannot directly save non-Tradition object/, 
56                                 "Exception has correct message" );
57                 }
58         }
59 }
60 my $nt = Text::Tradition->new(
61         'name' => 'CX',
62         'input' => 'CollateX',
63         'file' => 't/data/Collatex-16.xml',
64         );
65 ok( $nt->$_isa('Text::Tradition'), "Made new tradition" );
66
67 {
68         my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
69         my $scope = $f->new_scope;
70         is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
71         my $nuuid = $f->save( $nt );
72         ok( $nuuid, "Stored second tradition" );
73         my @tlist = $f->traditionlist;
74         is( scalar @tlist, 2, "Directory index has both traditions" );
75         my $tf = $f->tradition( $uuid );
76         my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
77         is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
78         is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
79         my $sid;
80         SKIP: {
81                 skip "Analysis package not installed", 4 unless $stemma_enabled;
82                 $sid = $f->object_to_id( $tf->stemma(0) );
83                 try {
84                         $f->tradition( $sid );
85                 } catch( Text::Tradition::Error $e ) {
86                         is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" );
87                         like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" );
88                 }
89                 if( $ENV{TEST_DELETION} ) {
90                         try {
91                                 $f->delete( $sid );
92                         } catch( Text::Tradition::Error $e ) {
93                                 is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
94                                 like( $e->message, qr/Cannot directly delete non-Tradition object/, 
95                                         "Exception has correct message" );
96                         }
97                 }
98         }
99         
100         SKIP: {
101                 skip "Set TEST_DELETION in env to test DB deletion functionality", 3
102                         unless $ENV{TEST_DELETION};
103                 $f->delete( $uuid );
104                 ok( !$f->exists( $uuid ), "Object is deleted from DB" );
105                 ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ) if $stemma_enabled;
106                 is( scalar $f->traditionlist, 1, "Object is deleted from index" );
107         }
108 }
109
110 {
111         my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
112         my $scope = $g->new_scope;
113         SKIP: {
114                 skip "Set TEST_DELETION in env to test DB deletion functionality", 1
115                         unless $ENV{TEST_DELETION};
116                 is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
117         }
118         my $ntobj = $g->tradition( 'CX' );
119         my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses;
120         my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses;
121         is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" );
122 }
123 }
124
125
126
127
128 1;