Skip to content

Commit

Permalink
Add tests from PR RDFLib#597
Browse files Browse the repository at this point in the history
  • Loading branch information
edmondchuc committed Jul 7, 2021
1 parent becf5a5 commit cb0c346
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 14 additions & 0 deletions rdflib/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ def __repr__(self):
return f"Namespace({self!r})"

def __contains__(self, ref):
"""Allows to check if a URI is within (starts with) this Namespace.
>>> from rdflib import URIRef
>>> namespace = Namespace('http://example.org/')
>>> uri = URIRef('http://example.org/foo')
>>> uri in namespace
True
>>> person_class = namespace['Person']
>>> person_class in namespace
True
>>> obj = URIRef('http://not.example.org/bar')
>>> obj in namespace
False
"""
return ref.startswith(self) # test namespace membership with "ref in ns" syntax


Expand Down
13 changes: 12 additions & 1 deletion test/test_namespace.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import unittest

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


class NamespaceTest(unittest.TestCase):
def test_dcterms_title(self):
self.assertEqual(DCTERMS.title, URIRef(DCTERMS + 'title'))

def test_iri(self):
prefix = u'http://jörn.loves.encoding.problems/'
ns = Namespace(prefix)
self.assertEqual(ns, str(prefix))
self.assert_(ns[u'jörn'].startswith(ns))


class NamespacePrefixTest(unittest.TestCase):
def test_compute_qname(self):
"""Test sequential assignment of unknown prefixes"""
Expand Down

0 comments on commit cb0c346

Please sign in to comment.