set up proper garbage-collecting deletion of traditions from the directory
[scpubgit/stemmatology.git] / t / text_tradition_directory.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5 $| = 1;
6
7
8
9 # =begin testing
10 {
11 use Test::Warn;
12 use File::Temp;
13 use Text::Tradition;
14 use_ok 'Text::Tradition::Directory';
15
16 my $fh = File::Temp->new();
17 my $file = $fh->filename;
18 $fh->close;
19 my $dsn = "dbi:SQLite:dbname=$file";
20
21 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
22         'extra_args' => { 'create' => 1 } );
23 is( ref $d, 'Text::Tradition::Directory', "Got directory object" );
24
25 my $scope = $d->new_scope;
26 my $t = Text::Tradition->new( 
27         'name'  => 'inline', 
28         'input' => 'Tabular',
29         'file'  => 't/data/simple.txt',
30         );
31 my $uuid = $d->save( $t );
32 ok( $uuid, "Saved test tradition" );
33
34 my $s = $t->add_stemma( 't/data/simple.dot' );
35 ok( $d->save( $t ), "Updated tradition with stemma" );
36 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
37 is( $d->tradition( $uuid )->stemma, $s, "...and it has the correct stemma" );
38 warning_like { $d->save( $s ) } qr/not a Text::Tradition/, "Correctly failed to save stemma directly";
39
40 my $e = Text::Tradition::Directory->new( 'dsn' => $dsn );
41 $scope = $e->new_scope;
42 is( scalar $e->tradition_ids, 1, "Directory index has our tradition" );
43 my $te = $e->tradition( $uuid );
44 is( $te->name, $t->name, "Retrieved the tradition from a new directory" );
45 my $sid = $e->object_to_id( $te->stemma );
46 warning_like { $e->tradition( $sid ) } qr/not a Text::Tradition/, "Did not retrieve stemma via tradition call";
47 warning_like { $e->delete( $sid ) } qr/Cannot directly delete non-Tradition object/, "Stemma object not deleted from DB";
48 $e->delete( $uuid );
49 ok( !$e->exists( $uuid ), "Object is deleted from DB" );
50 ok( !$e->exists( $sid ), "Object stemma also deleted from DB" );
51 is( scalar $e->tradition_ids, 0, "Object is deleted from index" );
52 }
53
54
55
56
57 1;