remove some debugging statements
[scpubgit/stemmatology.git] / 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 Text::Tradition;
14 use_ok 'Text::Tradition::Directory';
15
16 my $fh = File::Temp->new();
17 my $file = $fh->filename;
18 $fh->close;
19 my $dsn = "dbi:SQLite:dbname=$file";
20 my $uuid;
21 my $t = Text::Tradition->new( 
22         'name'  => 'inline', 
23         'input' => 'Tabular',
24         'file'  => 't/data/simple.txt',
25         );
26
27 {
28         my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
29                 'extra_args' => { 'create' => 1 } );
30         is( ref $d, 'Text::Tradition::Directory', "Got directory object" );
31         
32         my $scope = $d->new_scope;
33         $uuid = $d->save( $t );
34         ok( $uuid, "Saved test tradition" );
35         
36         my $s = $t->add_stemma( dotfile => 't/data/simple.dot' );
37         ok( $d->save( $t ), "Updated tradition with stemma" );
38         is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
39         is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" );
40         try {
41                 $d->save( $s );
42         } catch( Text::Tradition::Error $e ) {
43                 is( $e->ident, 'database error', "Got exception trying to save stemma directly" );
44                 like( $e->message, qr/Cannot directly save non-Tradition object/, 
45                         "Exception has correct message" );
46         }
47 }
48 my $nt = Text::Tradition->new(
49         'name' => 'CX',
50         'input' => 'CollateX',
51         'file' => 't/data/Collatex-16.xml',
52         );
53 is( ref( $nt ), 'Text::Tradition', "Made new tradition" );
54
55 {
56         my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
57         my $scope = $f->new_scope;
58         is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
59         my $nuuid = $f->save( $nt );
60         ok( $nuuid, "Stored second tradition" );
61         my @tlist = $f->traditionlist;
62         is( scalar @tlist, 2, "Directory index has both traditions" );
63         my $tf = $f->tradition( $uuid );
64         my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
65         is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
66         is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
67         my $sid = $f->object_to_id( $tf->stemma(0) );
68         try {
69                 $f->tradition( $sid );
70         } catch( Text::Tradition::Error $e ) {
71                 is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" );
72                 like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" );
73         }
74         try {
75                 $f->delete( $sid );
76         } catch( Text::Tradition::Error $e ) {
77                 is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
78                 like( $e->message, qr/Cannot directly delete non-Tradition object/, 
79                         "Exception has correct message" );
80         }
81         
82         $f->delete( $uuid );
83         ok( !$f->exists( $uuid ), "Object is deleted from DB" );
84         ok( !$f->exists( $sid ), "Object stemma also deleted from DB" );
85         is( scalar $f->traditionlist, 1, "Object is deleted from index" );
86 }
87
88 {
89         my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
90         my $scope = $g->new_scope;
91         is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
92         my $ntobj = $g->tradition( 'CX' );
93         my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses;
94         my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses;
95         is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" );
96 }
97 }
98
99
100
101
102 1;