Merge branch 'master' of https://github.com/tla/stemmatology
[scpubgit/stemmatology.git] / persistence / t / text_tradition_directory.t
CommitLineData
12523041 1#!/usr/bin/perl -w
2
3use strict;
4use Test::More 'no_plan';
5$| = 1;
6
7
8
9# =begin testing
10{
861c3e27 11use TryCatch;
12523041 12use File::Temp;
951ddfe8 13use Safe::Isa;
12523041 14use Text::Tradition;
12523041 15use_ok 'Text::Tradition::Directory';
16
17my $fh = File::Temp->new();
18my $file = $fh->filename;
19$fh->close;
20my $dsn = "dbi:SQLite:dbname=$file";
861c3e27 21my $uuid;
12523041 22my $t = Text::Tradition->new(
56cf65bd 23 'name' => 'inline',
24 'input' => 'Tabular',
25 'file' => 't/data/simple.txt',
26 );
37bf09f4 27my $stemma_enabled = $t->can( 'add_stemma' );
861c3e27 28
29{
30 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
31 'extra_args' => { 'create' => 1 } );
951ddfe8 32 ok( $d->$_isa('Text::Tradition::Directory'), "Got directory object" );
861c3e27 33
34 my $scope = $d->new_scope;
35 $uuid = $d->save( $t );
36 ok( $uuid, "Saved test tradition" );
37
951ddfe8 38 SKIP: {
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" );
44 try {
45 $d->save( $s );
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" );
50 }
861c3e27 51 }
52}
53my $nt = Text::Tradition->new(
54 'name' => 'CX',
55 'input' => 'CollateX',
56 'file' => 't/data/Collatex-16.xml',
57 );
951ddfe8 58ok( $nt->$_isa('Text::Tradition'), "Made new tradition" );
861c3e27 59
60{
61 my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
62 my $scope = $f->new_scope;
98a6cab2 63 is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
861c3e27 64 my $nuuid = $f->save( $nt );
65 ok( $nuuid, "Stored second tradition" );
98a6cab2 66 my @tlist = $f->traditionlist;
67 is( scalar @tlist, 2, "Directory index has both traditions" );
861c3e27 68 my $tf = $f->tradition( $uuid );
98a6cab2 69 my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
70 is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
861c3e27 71 is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
951ddfe8 72 my $sid;
73 SKIP: {
74 skip "Analysis package not installed", 4 unless $stemma_enabled;
75 $sid = $f->object_to_id( $tf->stemma(0) );
76 try {
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" );
81 }
82 try {
83 $f->delete( $sid );
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" );
88 }
861c3e27 89 }
ad39942e 90
861c3e27 91 $f->delete( $uuid );
92 ok( !$f->exists( $uuid ), "Object is deleted from DB" );
951ddfe8 93 ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ) if $stemma_enabled;
98a6cab2 94 is( scalar $f->traditionlist, 1, "Object is deleted from index" );
861c3e27 95}
96
e3a52f81 97TODO: {
98 todo_skip "Deletion conflicts with Analysis package", 2
99 if $t->does('Text::Tradition::HasStemma');
861c3e27 100 my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
101 my $scope = $g->new_scope;
98a6cab2 102 is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
ad39942e 103 my $ntobj = $g->tradition( 'CX' );
09909f9d 104 my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses;
105 my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses;
ad39942e 106 is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" );
861c3e27 107}
12523041 108}
109
110
111
112
1131;