File: trine.t

package info (click to toggle)
librdf-aref-perl 0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 444 kB
  • ctags: 52
  • sloc: perl: 606; makefile: 11
file content (93 lines) | stat: -rw-r--r-- 2,303 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use strict;
use Test::More;
use RDF::aREF;
use RDF::aREF::Encoder;

BEGIN {
    eval { 
        require RDF::Trine;
        RDF::Trine->import(qw(statement iri literal));
        1; 
    } or do {
        plan skip_all => "RDF::Trine required";
    };
}

my $model = RDF::Trine::Model->new;

decode_aref( {
        _id => 'http://example.org/alice',
        a => 'foaf_Person',
        foaf_knowns => 'http://example.org/bob'
    }, 
    callback => $model,
);
is $model->size, 2, 'added two statements';

my $aref = encode_aref $model;
is_deeply $aref, {
       'http://example.org/alice' => {
         'a' => 'foaf_Person',
         'foaf_knowns' => '<http://example.org/bob>'
       }
    }, 'encode_aref from RDF::Trine::Model';

decode_aref( {
        _id => 'http://example.org/alice',
        a => 'foaf_Person',
        foaf_knowns => 'http://example.org/claire'
    },
    callback => $model,
);
is $model->size, 3, 'added another statement';

# bnodes
$model = RDF::Trine::Model->new;
my $decoder = RDF::aREF::Decoder->new( callback => $model );
my $aref = { _id => '<x:subject>', foaf_knows => { foaf_name => 'alice' } }; 
$decoder->decode( $aref );
$decoder->decode( $aref );
is $model->size, 4, 'no bnode collision';
is $decoder->bnode_count, 2;
$decoder->bnode_count(1);
$decoder->decode( $aref );
is $model->size, 4, 'bnode collision';

# errors
my $warning;
local $SIG{__WARN__} = sub { $warning = shift };
decode_aref( {
        _id => 'isbn:123', 
        rdfs_seeAlso => [
            'isbn:456 x',  # looks like an IRI to aREF but rejected by Trine
            'isbn:789'
        ]
    }, 
    callback => $model,
);
ok $warning, "bad IRI";
is $model->size, 5, 'ignored illformed URI';

my $encoder = RDF::aREF::Encoder->new(ns => '20140910');
my $aref = { };
$encoder->add_triple( $aref, statement( 
    iri('http://example.org/'),
    iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
    iri('http://xmlns.com/foaf/0.1/Agent'),
) );
$encoder->add_triple( $aref, statement( 
    iri('http://example.org/'),
    iri('http://xmlns.com/foaf/0.1/name'),
    literal('Anne','de'),
) );

is_deeply $aref, {
    'http://example.org/' => {
        a => 'foaf_Agent',
        foaf_name => 'Anne@de'
    }
}, 'encoder add_triple';

# TODO encode_aref $model / $iterator

done_testing;