summaryrefslogtreecommitdiff
path: root/test/test_namespace.py
blob: 3c321f8cbace4445e62dddd2fa59773629a53b10 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import unittest

from rdflib.graph import Graph
from rdflib.namespace import Namespace, FOAF, RDF, RDFS, SH
from rdflib.term import URIRef


class NamespacePrefixTest(unittest.TestCase):
    def test_compute_qname(self):
        """Test sequential assignment of unknown prefixes"""
        g = Graph()
        self.assertEqual(
            g.compute_qname(URIRef("http://foo/bar/baz")),
            ("ns1", URIRef("http://foo/bar/"), "baz"),
        )

        self.assertEqual(
            g.compute_qname(URIRef("http://foo/bar#baz")),
            ("ns2", URIRef("http://foo/bar#"), "baz"),
        )

        # should skip to ns4 when ns3 is already assigned
        g.bind("ns3", URIRef("http://example.org/"))
        self.assertEqual(
            g.compute_qname(URIRef("http://blip/blop")),
            ("ns4", URIRef("http://blip/"), "blop"),
        )

        # should return empty qnames correctly
        self.assertEqual(
            g.compute_qname(URIRef("http://foo/bar/")),
            ("ns1", URIRef("http://foo/bar/"), ""),
        )

        # should compute qnames of URNs correctly as well
        self.assertEqual(
            g.compute_qname(URIRef("urn:ISSN:0167-6423")),
            ("ns5", URIRef("urn:ISSN:"), "0167-6423"),
        )

        self.assertEqual(
            g.compute_qname(URIRef("urn:ISSN:")),
            ("ns5", URIRef("urn:ISSN:"), ""),
        )

        # should compute qnames with parenthesis correctly
        self.assertEqual(
            g.compute_qname(URIRef("http://foo/bar/name_with_(parenthesis)")),
            ("ns1", URIRef("http://foo/bar/"), "name_with_(parenthesis)"),
        )

    def test_reset(self):
        data = (
            "@prefix a: <http://example.org/a> .\n"
            "a: <http://example.org/b> <http://example.org/c> ."
        )
        graph = Graph().parse(data=data, format="turtle")
        for p, n in tuple(graph.namespaces()):
            graph.store._Memory__namespace.pop(p)
            graph.store._Memory__prefix.pop(n)
        graph.namespace_manager.reset()
        self.assertFalse(tuple(graph.namespaces()))
        u = URIRef("http://example.org/a")
        prefix, namespace, name = graph.namespace_manager.compute_qname(
            u, generate=True
        )
        self.assertNotEqual(namespace, u)

    def test_reset_preserve_prefixes(self):
        data = (
            "@prefix a: <http://example.org/a> .\n"
            "a: <http://example.org/b> <http://example.org/c> ."
        )
        graph = Graph().parse(data=data, format="turtle")
        graph.namespace_manager.reset()
        self.assertTrue(tuple(graph.namespaces()))
        u = URIRef("http://example.org/a")
        prefix, namespace, name = graph.namespace_manager.compute_qname(
            u, generate=True
        )
        self.assertEqual(namespace, u)

    def test_n3(self):
        g = Graph()
        g.add(
            (
                URIRef("http://example.com/foo"),
                URIRef("http://example.com/bar"),
                URIRef("http://example.com/baz"),
            )
        )
        n3 = g.serialize(format="n3", encoding='latin-1')
        # Gunnar disagrees that this is right:
        # self.assertTrue("<http://example.com/foo> ns1:bar <http://example.com/baz> ." in n3)
        # as this is much prettier, and ns1 is already defined:
        self.assertTrue(b"ns1:foo ns1:bar ns1:baz ." in n3)

    def test_n32(self):
        # this test not generating prefixes for subjects/objects
        g = Graph()
        g.add(
            (
                URIRef("http://example1.com/foo"),
                URIRef("http://example2.com/bar"),
                URIRef("http://example3.com/baz"),
            )
        )
        n3 = g.serialize(format="n3", encoding="latin-1")

        self.assertTrue(
            b"<http://example1.com/foo> ns1:bar <http://example3.com/baz> ."
            in n3
        )

    def test_closed_namespace(self):
        """Tests terms both in an out of the ClosedNamespace FOAF"""

        def add_not_in_namespace(s):
            return FOAF[s]

        # a non-existent FOAF property
        self.assertRaises(KeyError, add_not_in_namespace, "blah")

        # a property name within the FOAF namespace
        self.assertEqual(
            add_not_in_namespace("givenName"),
            URIRef("http://xmlns.com/foaf/0.1/givenName"),
        )

    def test_contains_method(self):
        """Tests for Namespace.__contains__() methods."""

        ref = URIRef('http://www.w3.org/ns/shacl#example')
        self.assertTrue(type(SH) == Namespace, "SH no longer a Namespace, update test.")
        self.assertTrue(ref in SH, "sh:example not in SH")

        ref = URIRef('http://www.w3.org/2000/01/rdf-schema#label')
        self.assertTrue(ref in RDFS, "ClosedNamespace(RDFS) does not include rdfs:label")
        ref = URIRef('http://www.w3.org/2000/01/rdf-schema#example')
        self.assertFalse(ref in RDFS, "ClosedNamespace(RDFS) includes out-of-ns member rdfs:example")

        ref = URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
        self.assertTrue(ref in RDF, "_RDFNamespace does not include rdf:type")