Commit | Line | Data |
12523041 |
1 | #!/usr/bin/perl -w |
2 | |
3 | use strict; |
4 | use Test::More 'no_plan'; |
5 | $| = 1; |
6 | |
7 | |
8 | |
9 | # =begin testing |
10 | { |
861c3e27 |
11 | use TryCatch; |
12523041 |
12 | use File::Temp; |
13 | use Text::Tradition; |
12523041 |
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"; |
861c3e27 |
20 | my $uuid; |
12523041 |
21 | my $t = Text::Tradition->new( |
56cf65bd |
22 | 'name' => 'inline', |
23 | 'input' => 'Tabular', |
24 | 'file' => 't/data/simple.txt', |
25 | ); |
861c3e27 |
26 | |
27 | { |
28 | my $d = Text::Tradition::Directory->new( 'dsn' => $dsn, |
29 | 'extra_args' => { 'create' => 1 } ); |
30 | is( ref $d, 'Text::Tradition::Directory', "Got directory object" ); |
31 | |
32 | my $scope = $d->new_scope; |
33 | $uuid = $d->save( $t ); |
34 | ok( $uuid, "Saved test tradition" ); |
35 | |
9ba651b9 |
36 | my $s = $t->add_stemma( dotfile => 't/data/simple.dot' ); |
861c3e27 |
37 | ok( $d->save( $t ), "Updated tradition with stemma" ); |
38 | is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" ); |
e0d617e6 |
39 | is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" ); |
861c3e27 |
40 | try { |
41 | $d->save( $s ); |
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" ); |
46 | } |
47 | } |
48 | my $nt = Text::Tradition->new( |
49 | 'name' => 'CX', |
50 | 'input' => 'CollateX', |
51 | 'file' => 't/data/Collatex-16.xml', |
52 | ); |
53 | is( ref( $nt ), 'Text::Tradition', "Made new tradition" ); |
54 | |
55 | { |
56 | my $f = Text::Tradition::Directory->new( 'dsn' => $dsn ); |
57 | my $scope = $f->new_scope; |
98a6cab2 |
58 | is( scalar $f->traditionlist, 1, "Directory index has our tradition" ); |
861c3e27 |
59 | my $nuuid = $f->save( $nt ); |
60 | ok( $nuuid, "Stored second tradition" ); |
98a6cab2 |
61 | my @tlist = $f->traditionlist; |
62 | is( scalar @tlist, 2, "Directory index has both traditions" ); |
861c3e27 |
63 | my $tf = $f->tradition( $uuid ); |
98a6cab2 |
64 | my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist; |
65 | is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" ); |
861c3e27 |
66 | is( $tf->name, $t->name, "Retrieved the tradition from a new directory" ); |
e0d617e6 |
67 | my $sid = $f->object_to_id( $tf->stemma(0) ); |
861c3e27 |
68 | try { |
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" ); |
73 | } |
74 | try { |
75 | $f->delete( $sid ); |
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" ); |
80 | } |
ad39942e |
81 | |
861c3e27 |
82 | $f->delete( $uuid ); |
83 | ok( !$f->exists( $uuid ), "Object is deleted from DB" ); |
84 | ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ); |
98a6cab2 |
85 | is( scalar $f->traditionlist, 1, "Object is deleted from index" ); |
861c3e27 |
86 | } |
87 | |
d7ba60b4 |
88 | { |
861c3e27 |
89 | my $g = Text::Tradition::Directory->new( 'dsn' => $dsn ); |
90 | my $scope = $g->new_scope; |
98a6cab2 |
91 | is( scalar $g->traditionlist, 1, "Now one object in new directory index" ); |
ad39942e |
92 | my $ntobj = $g->tradition( 'CX' ); |
09909f9d |
93 | my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses; |
94 | my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses; |
ad39942e |
95 | is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" ); |
861c3e27 |
96 | } |
12523041 |
97 | } |
98 | |
99 | |
100 | |
101 | |
102 | 1; |