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