4 use Test::More 'no_plan';
14 use_ok 'Text::Tradition::Directory';
16 my $fh = File::Temp->new();
17 my $file = $fh->filename;
19 my $dsn = "dbi:SQLite:dbname=$file";
21 my $t = Text::Tradition->new(
24 'file' => 't/data/simple.txt',
28 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
29 'extra_args' => { 'create' => 1 } );
30 is( ref $d, 'Text::Tradition::Directory', "Got directory object" );
32 my $scope = $d->new_scope;
33 $uuid = $d->save( $t );
34 ok( $uuid, "Saved test tradition" );
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" );
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" );
48 my $nt = Text::Tradition->new(
50 'input' => 'CollateX',
51 'file' => 't/data/Collatex-16.xml',
53 is( ref( $nt ), 'Text::Tradition', "Made new tradition" );
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) );
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" );
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" );
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" );
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" );