From: tla Date: Sun, 27 Oct 2013 22:04:33 +0000 (+0100) Subject: finish and test record_stemweb_result for Tradition (#12) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a47b32d9615d1037bc0f3dccbb733daddcfa63a3;p=scpubgit%2Fstemmatology.git finish and test record_stemweb_result for Tradition (#12) --- diff --git a/analysis/lib/Text/Tradition/HasStemma.pm b/analysis/lib/Text/Tradition/HasStemma.pm index 6e6ccd8..68e04f5 100644 --- a/analysis/lib/Text/Tradition/HasStemma.pm +++ b/analysis/lib/Text/Tradition/HasStemma.pm @@ -3,6 +3,7 @@ package Text::Tradition::HasStemma; use strict; use warnings; use Moose::Role; +use Date::Parse; use Text::Tradition::Stemma; use Text::Tradition::StemmaUtil qw/ parse_newick /; @@ -131,23 +132,50 @@ sub add_stemma { Records the result returned by a Stemweb calculation, and clears any existing job ID. -TODO Test! +=begin testing + +use Text::Tradition; +use JSON qw/ from_json /; + +my $t = Text::Tradition->new( + 'name' => 'Stemweb test', + 'input' => 'Self', + 'file' => 't/data/besoin.xml', + 'stemweb_jobid' => '4', + ); + +is( $t->stemma_count, 0, "No stemmas added yet" ); + +my $answer = from_json( '{"status": 0, "job_id": "4", "algorithm": "RHM", "format": "newick", "start_time": "2013-10-26 10:44:14.050263", "result": "((((((((((((F,U),V),S),T1),T2),A),J),B),L),D),M),C);\n", "end_time": "2013-10-26 10:45:55.398944"}' ); +$t->record_stemweb_result( $answer ); +ok( !$t->has_stemweb_jobid, "Job ID was removed from tradition" ); +is( $t->stemma_count, 1, "Tradition has new stemma" ); +ok( $t->stemma(0)->is_undirected, "New stemma is undirected as it should be" ); +is( $t->stemma(0)->identifier, "RHM 1382777054_0", "Stemma has correct identifier" ); + + +=end testing =cut sub record_stemweb_result { - my( $self, $format, $data ) = @_; - if( $format eq 'dot' ) { - $self->add_stemma( dot => $data ); - } elsif( $format eq 'newick' ) { - my $stemmata = parse_newick( $data ); + my( $self, $answer ) = @_; + if( $answer->{format} eq 'dot' ) { + $self->add_stemma( dot => $answer->{result} ); + } elsif( $answer->{format} eq 'newick' ) { + my $stemmata = parse_newick( $answer->{result} ); + my $title = sprintf( "%s %d", $answer->{algorithm}, + str2time( $answer->{start_time} ) ); + my $i = 0; foreach my $stemma ( @$stemmata ) { + my $ititle = $title . "_$i"; $i++; + $stemma->set_identifier( $ititle ); $self->_add_stemma( $stemma ); } - $self->_clear_stemweb_jobid(); } else { - $self->throw( "Cannot parse tree results with format $format" ); + $self->throw( "Cannot parse tree results with format " . $answer->{format} ); } + $self->_clear_stemweb_jobid(); } 1; diff --git a/analysis/t/text_tradition_hasstemma.t b/analysis/t/text_tradition_hasstemma.t index 6f16b5e..66fc675 100644 --- a/analysis/t/text_tradition_hasstemma.t +++ b/analysis/t/text_tradition_hasstemma.t @@ -25,5 +25,29 @@ is( $t->stemma(0), $s, "Tradition hands back the right stemma" ); +# =begin testing +{ +use Text::Tradition; +use JSON qw/ from_json /; + +my $t = Text::Tradition->new( + 'name' => 'Stemweb test', + 'input' => 'Self', + 'file' => 't/data/besoin.xml', + 'stemweb_jobid' => '4', + ); + +is( $t->stemma_count, 0, "No stemmas added yet" ); + +my $answer = from_json( '{"status": 0, "job_id": "4", "algorithm": "RHM", "format": "newick", "start_time": "2013-10-26 10:44:14.050263", "result": "((((((((((((F,U),V),S),T1),T2),A),J),B),L),D),M),C);\n", "end_time": "2013-10-26 10:45:55.398944"}' ); +$t->record_stemweb_result( $answer ); +ok( !$t->has_stemweb_jobid, "Job ID was removed from tradition" ); +is( $t->stemma_count, 1, "Tradition has new stemma" ); +ok( $t->stemma(0)->is_undirected, "New stemma is undirected as it should be" ); +is( $t->stemma(0)->identifier, "RHM 1382777054_0", "Stemma has correct identifier" ); +} + + + 1; diff --git a/analysis/t/text_tradition_stemma.t b/analysis/t/text_tradition_stemma.t index 784f11f..3650076 100644 --- a/analysis/t/text_tradition_stemma.t +++ b/analysis/t/text_tradition_stemma.t @@ -47,9 +47,6 @@ is( ref( $udstemma ), 'Text::Tradition::Stemma', "Created stemma from undirected is( scalar $udstemma->witnesses, 13, "Found correct number of extant witnesses" ); is( scalar $udstemma->hypotheticals, 12, "Found correct number of hypotheticals" ); ok( $udstemma->is_undirected, "Stemma was recorded as undirected" ); - -# TODO Create stemma from graph, create stemma from undirected graph, -# create stemma from incompletely-specified graph }