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',
28 eval { $stemma_enabled = $t->enable_stemmata; };
31 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
32 'extra_args' => { 'create' => 1 } );
33 ok( $d->$_isa('Text::Tradition::Directory'), "Got directory object" );
35 my $scope = $d->new_scope;
36 $uuid = $d->save( $t );
37 ok( $uuid, "Saved test tradition" );
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" );
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" );
54 my $nt = Text::Tradition->new(
56 'input' => 'CollateX',
57 'file' => 't/data/Collatex-16.xml',
59 ok( $nt->$_isa('Text::Tradition'), "Made new tradition" );
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" );
75 skip "Analysis package not installed", 4 unless $stemma_enabled;
76 $sid = $f->object_to_id( $tf->stemma(0) );
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" );
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" );
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" );
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" );