diff options
Diffstat (limited to 'test/test_namespace.py')
| -rw-r--r-- | test/test_namespace.py | 76 |
1 files changed, 75 insertions, 1 deletions
diff --git a/test/test_namespace.py b/test/test_namespace.py index d8611d37..54cb3299 100644 --- a/test/test_namespace.py +++ b/test/test_namespace.py @@ -1,4 +1,5 @@ import unittest +from unittest.case import expectedFailure from contextlib import redirect_stderr from io import StringIO from warnings import warn @@ -8,6 +9,76 @@ from rdflib.namespace import FOAF, RDF, RDFS, SH, DefinedNamespaceMeta from rdflib.term import URIRef +class NamespaceTest(unittest.TestCase): + def setUp(self): + self.ns_str = "http://example.com/name/space/" + self.ns = Namespace(self.ns_str) + + def test_repr(self): + # NOTE: this assumes ns_str has no characthers that need escaping + self.assertIn(self.ns_str, f"{self.ns!r}") + self.assertEqual(self.ns, eval(f"{self.ns!r}")) + + def test_str(self): + self.assertEqual(self.ns_str, f"{self.ns}") + + def test_member(self): + self.assertEqual(f"{self.ns_str}a", f"{self.ns.a}") + + 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 ClosedNamespaceTest(unittest.TestCase): + def setUp(self): + self.ns_str = "http://example.com/name/space/" + self.ns = ClosedNamespace(self.ns_str, ["a", "b", "c"]) + + def test_repr(self): + # NOTE: this assumes ns_str has no characthers that need escaping + self.assertIn(self.ns_str, f"{self.ns!r}") + + @expectedFailure + def test_repr_ef(self): + """ + This fails because ClosedNamespace repr does not represent the second argument + """ + self.assertEqual(self.ns, eval(f"{self.ns!r}")) + + def test_str(self): + self.assertEqual(self.ns_str, f"{self.ns}") + + def test_member(self): + self.assertEqual(f"{self.ns_str}a", f"{self.ns.a}") + + def test_missing_member(self): + with self.assertRaises(AttributeError) as context: + f"{self.ns.missing}" + self.assertIn("missing", f"{context.exception}") + + +class URIPatternTest(unittest.TestCase): + def setUp(self): + self.pattern_str = "http://example.org/%s/%d/resource" + self.pattern = URIPattern(self.pattern_str) + + def test_repr(self): + # NOTE: this assumes pattern_str has no characthers that need escaping + self.assertIn(self.pattern_str, f"{self.pattern!r}") + self.assertEqual(self.pattern, eval(f"{self.pattern!r}")) + + def test_format(self): + self.assertEqual( + "http://example.org/foo/100/resource", str(self.pattern % ("foo", 100)) + ) + + class NamespacePrefixTest(unittest.TestCase): def test_compute_qname(self): """Test sequential assignment of unknown prefixes""" @@ -135,10 +206,13 @@ class NamespacePrefixTest(unittest.TestCase): # a property name within the FOAF namespace self.assertEqual( - add_not_in_namespace("givenName"), + FOAF.givenName, URIRef("http://xmlns.com/foaf/0.1/givenName"), ) + # namescape can be used as str + self.assertTrue(FOAF.givenName.startswith(FOAF)) + def test_contains_method(self): """Tests for Namespace.__contains__() methods.""" |
