4 use Test::More 'no_plan';
15 use_ok 'Text::Tradition::Directory';
17 my $fh = File::Temp->new();
18 my $file = $fh->filename;
20 my $dsn = "dbi:SQLite:dbname=$file";
22 my $t = Text::Tradition->new(
25 'file' => 't/data/simple.txt',
27 my $stemma_enabled = $t->can( 'add_stemma' );
30 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
31 'extra_args' => { 'create' => 1 } );
32 ok( $d->$_isa('Text::Tradition::Directory'), "Got directory object" );
34 my $scope = $d->new_scope;
35 $uuid = $d->save( $t );
36 ok( $uuid, "Saved test tradition" );
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" );
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" );
53 my $nt = Text::Tradition->new(
55 'input' => 'CollateX',
56 'file' => 't/data/Collatex-16.xml',
58 ok( $nt->$_isa('Text::Tradition'), "Made new tradition" );
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" );
74 skip "Analysis package not installed", 4 unless $stemma_enabled;
75 $sid = $f->object_to_id( $tf->stemma(0) );
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" );
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" );
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" );
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" );