release current version of persistence module
[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;
4ac3ff0b 22my $user = 'user@example.org';
12523041 23my $t = Text::Tradition->new(
56cf65bd 24 'name' => 'inline',
25 'input' => 'Tabular',
26 'file' => 't/data/simple.txt',
27 );
37bf09f4 28my $stemma_enabled = $t->can( 'add_stemma' );
861c3e27 29
30{
31 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
32 'extra_args' => { 'create' => 1 } );
951ddfe8 33 ok( $d->$_isa('Text::Tradition::Directory'), "Got directory object" );
861c3e27 34
35 my $scope = $d->new_scope;
36 $uuid = $d->save( $t );
37 ok( $uuid, "Saved test tradition" );
38
4ac3ff0b 39 # Add a test user
40 my $user = $d->add_user({ username => $user, password => 'UserPass' });
41 $user->add_tradition( $t );
42 $d->store( $user );
43 is( $t->user, $user, "Assigned tradition to test user" );
44
951ddfe8 45 SKIP: {
46 skip "Analysis package not installed", 5 unless $stemma_enabled;
47 my $s = $t->add_stemma( dotfile => 't/data/simple.dot' );
48 ok( $d->save( $t ), "Updated tradition with stemma" );
49 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
50 is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" );
51 try {
52 $d->save( $s );
53 } catch( Text::Tradition::Error $e ) {
54 is( $e->ident, 'database error', "Got exception trying to save stemma directly" );
55 like( $e->message, qr/Cannot directly save non-Tradition object/,
56 "Exception has correct message" );
57 }
861c3e27 58 }
59}
60my $nt = Text::Tradition->new(
61 'name' => 'CX',
62 'input' => 'CollateX',
63 'file' => 't/data/Collatex-16.xml',
64 );
951ddfe8 65ok( $nt->$_isa('Text::Tradition'), "Made new tradition" );
861c3e27 66
67{
68 my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
69 my $scope = $f->new_scope;
98a6cab2 70 is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
861c3e27 71 my $nuuid = $f->save( $nt );
72 ok( $nuuid, "Stored second tradition" );
98a6cab2 73 my @tlist = $f->traditionlist;
74 is( scalar @tlist, 2, "Directory index has both traditions" );
861c3e27 75 my $tf = $f->tradition( $uuid );
98a6cab2 76 my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
77 is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
861c3e27 78 is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
951ddfe8 79 my $sid;
80 SKIP: {
81 skip "Analysis package not installed", 4 unless $stemma_enabled;
82 $sid = $f->object_to_id( $tf->stemma(0) );
83 try {
84 $f->tradition( $sid );
85 } catch( Text::Tradition::Error $e ) {
86 is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" );
87 like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" );
88 }
148c2eb1 89 if( $ENV{TEST_DELETION} ) {
90 try {
91 $f->delete( $sid );
92 } catch( Text::Tradition::Error $e ) {
93 is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
94 like( $e->message, qr/Cannot directly delete non-Tradition object/,
95 "Exception has correct message" );
96 }
951ddfe8 97 }
861c3e27 98 }
ad39942e 99
148c2eb1 100 SKIP: {
101 skip "Set TEST_DELETION in env to test DB deletion functionality", 3
102 unless $ENV{TEST_DELETION};
103 $f->delete( $uuid );
104 ok( !$f->exists( $uuid ), "Object is deleted from DB" );
105 ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ) if $stemma_enabled;
106 is( scalar $f->traditionlist, 1, "Object is deleted from index" );
107 }
861c3e27 108}
109
4ac3ff0b 110{
861c3e27 111 my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
112 my $scope = $g->new_scope;
148c2eb1 113 SKIP: {
114 skip "Set TEST_DELETION in env to test DB deletion functionality", 1
115 unless $ENV{TEST_DELETION};
116 is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
117 }
ad39942e 118 my $ntobj = $g->tradition( 'CX' );
09909f9d 119 my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses;
120 my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses;
ad39942e 121 is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" );
861c3e27 122}
12523041 123}
124
125
126
127
1281;