split off stemma analysis modules from base Tradition layer
[scpubgit/stemmatology.git] / base / t / text_tradition_parser_self.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 File::Temp;
12 use Safe::Isa;
13 use Test::Warn;
14 use Text::Tradition;
15 use TryCatch;
16 binmode STDOUT, ":utf8";
17 binmode STDERR, ":utf8";
18 eval { no warnings; binmode $DB::OUT, ":utf8"; };
19
20 my $tradition = 't/data/florilegium_graphml.xml';
21 my $t = Text::Tradition->new( 
22     'name'  => 'inline', 
23     'input' => 'Self',
24     'file'  => $tradition,
25     );
26
27 ok( $t->$_isa('Text::Tradition'), "Parsed GraphML version 2" );
28 if( $t ) {
29     is( scalar $t->collation->readings, 319, "Collation has all readings" );
30     is( scalar $t->collation->paths, 376, "Collation has all paths" );
31     is( scalar $t->witnesses, 13, "Collation has all witnesses" );
32 }
33
34 # TODO add a relationship, add a stemma, write graphml, reparse it, check that 
35 # the new data is there
36 $t->language('Greek');
37 my $stemma_enabled;
38 try {
39         $stemma_enabled = $t->enable_stemmata;
40 } catch {
41         ok( 1, "Skipping stemma tests without Analysis module" );
42 }
43 if( $stemma_enabled ) {
44         $t->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
45 }
46 $t->collation->add_relationship( 'w12', 'w13', 
47         { 'type' => 'grammatical', 'scope' => 'global', 
48           'annotation' => 'This is some note' } );
49 ok( $t->collation->get_relationship( 'w12', 'w13' ), "Relationship set" );
50 my $graphml_str = $t->collation->as_graphml;
51
52 my $newt = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml_str );
53 ok( $newt->$_isa('Text::Tradition'), "Parsed current GraphML version" );
54 if( $newt ) {
55     is( scalar $newt->collation->readings, 319, "Collation has all readings" );
56     is( scalar $newt->collation->paths, 376, "Collation has all paths" );
57     is( scalar $newt->witnesses, 13, "Collation has all witnesses" );
58     is( scalar $newt->collation->relationships, 1, "Collation has added relationship" );
59     is( $newt->language, 'Greek', "Tradition has correct language setting" );
60     my $rel = $newt->collation->get_relationship( 'w12', 'w13' );
61     ok( $rel, "Found set relationship" );
62     is( $rel->annotation, 'This is some note', "Relationship has its properties" );
63     if( $stemma_enabled ) {
64             is( scalar $newt->stemmata, 1, "Tradition has its stemma" );
65         is( $newt->stemma(0)->witnesses, $t->stemma(0)->witnesses, "Stemma has correct length witness list" );
66     }
67 }
68
69 # Test user save / restore
70 my $fh = File::Temp->new();
71 my $file = $fh->filename;
72 $fh->close;
73 my $dsn = "dbi:SQLite:dbname=$file";
74 my $userstore = Text::Tradition::Directory->new( { dsn => $dsn,
75         extra_args => { create => 1 } } );
76 my $scope = $userstore->new_scope();
77 my $testuser = $userstore->create_user( { url => 'http://example.com' } );
78 ok( $testuser->$_isa('Text::Tradition::User'), "Created test user via userstore" );
79 $testuser->add_tradition( $newt );
80 is( $newt->user->id, $testuser->id, "Assigned tradition to test user" );
81 $graphml_str = $newt->collation->as_graphml;
82 my $usert;
83 warning_is {
84         $usert = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml_str );
85 } 'DROPPING user assignment without a specified userstore',
86         "Got expected user drop warning on parse";
87 $usert = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml_str,
88         'userstore' => $userstore );
89 is( $usert->user->id, $testuser->id, "Parsed tradition with userstore points to correct user" );
90
91 # Test warning if we can
92 unless( $stemma_enabled ) {
93         my $nst;
94         warnings_exist {
95                 $nst = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/lexformat.xml' );
96         } [qr/DROPPING stemmata/],
97                 "Got expected stemma drop warning on parse";
98 }
99 }
100
101
102
103
104 1;