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; |
58 | is( scalar $f->tradition_ids, 1, "Directory index has our tradition" ); |
59 | my $nuuid = $f->save( $nt ); |
60 | ok( $nuuid, "Stored second tradition" ); |
61 | is( scalar $f->tradition_ids, 2, "Directory index has both traditions" ); |
62 | my $tf = $f->tradition( $uuid ); |
63 | is( $tf->name, $t->name, "Retrieved the tradition from a new directory" ); |
e0d617e6 |
64 | my $sid = $f->object_to_id( $tf->stemma(0) ); |
861c3e27 |
65 | try { |
66 | $f->tradition( $sid ); |
67 | } catch( Text::Tradition::Error $e ) { |
68 | is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" ); |
69 | like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" ); |
70 | } |
71 | try { |
72 | $f->delete( $sid ); |
73 | } catch( Text::Tradition::Error $e ) { |
74 | is( $e->ident, 'database error', "Got exception trying to delete stemma directly" ); |
75 | like( $e->message, qr/Cannot directly delete non-Tradition object/, |
76 | "Exception has correct message" ); |
77 | } |
78 | $f->delete( $uuid ); |
79 | ok( !$f->exists( $uuid ), "Object is deleted from DB" ); |
80 | ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ); |
81 | is( scalar $f->tradition_ids, 1, "Object is deleted from index" ); |
82 | } |
83 | |
84 | SKIP: { |
85 | skip 'Have yet to figure out garbage collection', 1; |
86 | my $g = Text::Tradition::Directory->new( 'dsn' => $dsn ); |
87 | my $scope = $g->new_scope; |
88 | is( scalar $g->tradition_ids, 1, "Now one object in new directory index" ); |
89 | } |
12523041 |
90 | } |
91 | |
92 | |
93 | |
94 | |
95 | 1; |