summaryrefslogtreecommitdiff
path: root/test/test_turtle_serialize.py
blob: 9e6f0b632c0839efcf2c7e90fb3b7b18f0da983a (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from rdflib import Graph, URIRef, BNode, RDF, RDFS, Literal, Namespace
from rdflib.collection import Collection
from rdflib.plugins.serializers.turtle import TurtleSerializer


def testTurtleFinalDot():
    """
    https://github.com/RDFLib/rdflib/issues/282
    """

    g = Graph()
    u = URIRef("http://ex.org/bob.")
    g.bind("ns", "http://ex.org/")
    g.add((u, u, u))
    s = g.serialize(format="turtle", encoding="latin-1")
    assert b"ns:bob." not in s


def testTurtleBoolList():
    subject = URIRef("http://localhost/user")
    predicate = URIRef("http://localhost/vocab#hasList")
    g1 = Graph()
    list_item1 = BNode()
    list_item2 = BNode()
    list_item3 = BNode()
    g1.add((subject, predicate, list_item1))
    g1.add((list_item1, RDF.first, Literal(True)))
    g1.add((list_item1, RDF.rest, list_item2))
    g1.add((list_item2, RDF.first, Literal(False)))
    g1.add((list_item2, RDF.rest, list_item3))
    g1.add((list_item3, RDF.first, Literal(True)))
    g1.add((list_item3, RDF.rest, RDF.nil))

    ttl_dump = g1.serialize(format="turtle")
    g2 = Graph()
    g2.parse(data=ttl_dump, format="turtle")

    list_id = g2.value(subject, predicate)
    bool_list = [i.toPython() for i in Collection(g2, list_id)]
    assert bool_list == [True, False, True]


def testUnicodeEscaping():
    turtle_string = " <http://example.com/A> <http://example.com/B> <http://example.com/aaa\\u00F3bbbb> . <http://example.com/A> <http://example.com/C> <http://example.com/zzz\\U00100000zzz> . <http://example.com/A> <http://example.com/D> <http://example.com/aaa\\u00f3bbb> ."
    g = Graph()

    # shouldn't get an exception
    g.parse(data=turtle_string, format="turtle")
    triples = sorted(list(g))
    assert len(triples) == 3
    print(triples)
    # Now check that was decoded into python values properly
    assert triples[0][2] == URIRef("http://example.com/aaa\xf3bbbb")
    assert triples[1][2] == URIRef("http://example.com/zzz\U00100000zzz")
    assert triples[2][2] == URIRef("http://example.com/aaa\xf3bbb")


def test_turtle_valid_list():
    NS = Namespace("http://example.org/ns/")
    g = Graph()
    g.parse(
        data="""
            @prefix : <{0}> .
            :s :p (""), (0), (false) .
            """.format(
            NS
        ),
        format="turtle",
    )

    turtle_serializer = TurtleSerializer(g)

    for o in g.objects(NS.s, NS.p):
        assert turtle_serializer.isValidList(o)


def test_turtle_namespace():
    graph = Graph()
    graph.bind("OBO", "http://purl.obolibrary.org/obo/")
    graph.bind("GENO", "http://purl.obolibrary.org/obo/GENO_")
    graph.bind("RO", "http://purl.obolibrary.org/obo/RO_")
    graph.bind("RO_has_phenotype", "http://purl.obolibrary.org/obo/RO_0002200")
    graph.bind("SERIAL", "urn:ISSN:")
    graph.bind("EX", "http://example.org/")
    graph.add(
        (
            URIRef("http://example.org"),
            URIRef("http://purl.obolibrary.org/obo/RO_0002200"),
            URIRef("http://purl.obolibrary.org/obo/GENO_0000385"),
        )
    )
    graph.add(
        (
            URIRef("urn:ISSN:0167-6423"),
            RDFS.label,
            Literal("Science of Computer Programming"),
        )
    )
    graph.add(
        (
            URIRef("http://example.org/name_with_(parenthesis)"),
            RDFS.label,
            Literal("URI with parenthesis"),
        )
    )
    output = [
        val
        for val in graph.serialize(format="turtle").splitlines()
        if not val.startswith("@prefix")
    ]
    output = " ".join(output)
    assert "RO_has_phenotype:" in output
    assert "GENO:0000385" in output
    assert "SERIAL:0167-6423" in output
    assert "EX:name_with_(parenthesis)" in output


if __name__ == "__main__":
    import nose
    import sys

    nose.main(defaultTest=sys.argv[0])