Source

p5-moosex-semantic / t / rdf_export / basic.t

use Test::More tests=>10;
use Test::Moose;
use RDF::Trine qw(blank);
use Data::Dumper;
use MooseX::Semantic::Test::Person;
use File::Temp qw( tempfile );

sub basic_export {
    my $toby = MooseX::Semantic::Test::Person->new(
        rdf_about => 'http://tobyinkster.co.uk/#i',
        name => 'Toby Inkster',
        country => 'UK',
        subjects => ['perl'],
    );
    my $greg = MooseX::Semantic::Test::Person->new(
        rdf_about => 'http://kasei.us/about/foaf.xrdf#greg',
        name => 'Gregory Williams',
        country => 'USA',
        favorite_numer => 3,
        friends => [ $toby ],
    );
    my $model = $greg->export_to_model;
    isa_ok($model, 'RDF::Trine::Model');
    ok($model->size == 9, 'generated 9 statments');
    # warn Dumper $model->size;
    # warn Dumper $model;
    ok(my $model_as_string = $greg->export_to_string(format=>'ntriples'), 'export_to_string works');
    # warn Dumper $model_as_string;

    my ( $fh, $fname ) = tempfile;
    ok( $greg->export_to_file( $fh, format=>'ntriples' ), 'export_to_file works');
    close $fh;
    # open $fh, "<", $fname;
    my $fh_contents = do { local $/; open $fh, "<$fname"; <$fh> };
    # close $fh;
    is( $fh_contents, $model_as_string, 'File export and String export match');
}

sub basic_blank_node {
    my $bob = MooseX::Semantic::Test::Person->new(
        rdf_about => blank,
        name => 'Bob',
    );
    my $alice = MooseX::Semantic::Test::Person->new(
        rdf_about => blank,
        name => 'Alice',
    );
    $bob->add_friend( $alice );
    ok($bob->rdf_about->is_blank, 'Bob is blank');
    ok($alice->rdf_about->is_blank, 'Alice is blank');
    is($bob->get_friend(0), $alice, "Alice is Bob's friend");
    ok(my $model = $bob->export_to_model, 'Can export Bob to model');
    is($model->size, 5, 'Correct number of statements exported');
}

&basic_export;
&basic_blank_node;
# done_testing;