aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/namespace/__init__.py0
-rw-r--r--test/namespace/test_namespace.py97
2 files changed, 97 insertions, 0 deletions
diff --git a/test/namespace/__init__.py b/test/namespace/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/namespace/__init__.py
diff --git a/test/namespace/test_namespace.py b/test/namespace/test_namespace.py
new file mode 100644
index 0000000..1ad53e3
--- /dev/null
+++ b/test/namespace/test_namespace.py
@@ -0,0 +1,97 @@
+"""
+
+Part of the tagit test suite.
+A copy of the license is provided with the project.
+Author: Matthias Baumgartner, 2022
+"""
+# imports
+import unittest
+
+# objects to test
+from bsfs.namespace.namespace import Namespace, ClosedNamespace
+
+
+## code ##
+
+class TestNamespace(unittest.TestCase):
+ def test_essentials(self):
+ # string conversion
+ self.assertEqual(str(Namespace('http://example.org/')), 'Namespace(http://example.org/)')
+ self.assertEqual(str(Namespace('http://example.org#')), 'Namespace(http://example.org#)')
+ self.assertEqual(repr(Namespace('http://example.org/')), 'Namespace(http://example.org/)')
+ self.assertEqual(repr(Namespace('http://example.org#')), 'Namespace(http://example.org#)')
+ # comparison
+ class Foo(Namespace): pass
+ self.assertEqual(Namespace('http://example.org/'), Namespace('http://example.org/'))
+ self.assertNotEqual(Namespace('http://example.org/'), Foo('http://example.org/'))
+ self.assertNotEqual(Foo('http://example.org/'), Namespace('http://example.org/'))
+ self.assertNotEqual(Namespace('http://example.org/'), Namespace('http://example.org#'))
+ self.assertNotEqual(Namespace('http://example.org#'), Namespace('http://example.org/'))
+ # hashing
+ self.assertEqual(hash(Namespace('http://example.org/')), hash(Namespace('http://example.org/')))
+ self.assertNotEqual(hash(Namespace('http://example.org/')), hash(Foo('http://example.org/')))
+ self.assertNotEqual(hash(Foo('http://example.org/')), hash(Namespace('http://example.org/')))
+ self.assertNotEqual(hash(Namespace('http://example.org/')), hash(Namespace('http://example.org#')))
+ self.assertNotEqual(hash(Namespace('http://example.org#')), hash(Namespace('http://example.org/')))
+
+ def test_getattr(self):
+ self.assertEqual(Namespace('http://example.org/').foo, 'http://example.org/foo')
+ self.assertEqual(Namespace('http://example.org/').bar, 'http://example.org/bar')
+ self.assertEqual(Namespace('http://example.org/me#').foo, 'http://example.org/me#foo')
+ self.assertEqual(Namespace('http://example.org/me#').bar, 'http://example.org/me#bar')
+
+ def test_getitem(self):
+ self.assertEqual(Namespace('http://example.org/')['foo'], 'http://example.org/foo')
+ self.assertEqual(Namespace('http://example.org/')['bar'], 'http://example.org/bar')
+ self.assertEqual(Namespace('http://example.org/me#')['foo'], 'http://example.org/me#foo')
+ self.assertEqual(Namespace('http://example.org/me#')['bar'], 'http://example.org/me#bar')
+
+
+class TestClosedNamespace(unittest.TestCase):
+ def test_essentials(self):
+ # string conversion
+ self.assertEqual(str(ClosedNamespace('http://example.org/')), 'ClosedNamespace(http://example.org/)')
+ self.assertEqual(str(ClosedNamespace('http://example.org#')), 'ClosedNamespace(http://example.org#)')
+ self.assertEqual(repr(ClosedNamespace('http://example.org/')), 'ClosedNamespace(http://example.org/)')
+ self.assertEqual(repr(ClosedNamespace('http://example.org#')), 'ClosedNamespace(http://example.org#)')
+ # comparison
+ class Foo(ClosedNamespace): pass
+ self.assertEqual(ClosedNamespace('http://example.org/'), ClosedNamespace('http://example.org/'))
+ self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar'), ClosedNamespace('http://example.org/', 'foo', 'bar'))
+ self.assertNotEqual(ClosedNamespace('http://example.org/', 'foo'), ClosedNamespace('http://example.org/', 'bar'))
+ self.assertNotEqual(ClosedNamespace('http://example.org/'), Foo('http://example.org/'))
+ self.assertNotEqual(Foo('http://example.org/'), ClosedNamespace('http://example.org/'))
+ self.assertNotEqual(ClosedNamespace('http://example.org/'), ClosedNamespace('http://example.org#'))
+ self.assertNotEqual(ClosedNamespace('http://example.org#'), ClosedNamespace('http://example.org/'))
+ # hashing
+ self.assertEqual(hash(ClosedNamespace('http://example.org/')), hash(ClosedNamespace('http://example.org/')))
+ self.assertEqual(hash(ClosedNamespace('http://example.org/', 'foo', 'bar')), hash(ClosedNamespace('http://example.org/', 'foo', 'bar')))
+ self.assertNotEqual(hash(ClosedNamespace('http://example.org/', 'foo')), hash(ClosedNamespace('http://example.org/', 'bar')))
+ self.assertNotEqual(hash(ClosedNamespace('http://example.org/')), hash(Foo('http://example.org/')))
+ self.assertNotEqual(hash(Foo('http://example.org/')), hash(ClosedNamespace('http://example.org/')))
+ self.assertNotEqual(hash(ClosedNamespace('http://example.org/')), hash(ClosedNamespace('http://example.org#')))
+ self.assertNotEqual(hash(ClosedNamespace('http://example.org#')), hash(ClosedNamespace('http://example.org/')))
+
+ def test_getattr(self):
+ self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar').foo, 'http://example.org/foo')
+ self.assertEqual(ClosedNamespace('http://example.org/', 'bar', 'bar').bar, 'http://example.org/bar')
+ self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar').foo, 'http://example.org/me#foo')
+ self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar').bar, 'http://example.org/me#bar')
+ self.assertRaises(KeyError, getattr, ClosedNamespace('http://example.org/', 'bar', 'bar'), 'foobar')
+ self.assertRaises(KeyError, getattr, ClosedNamespace('http://example.org#', 'bar', 'bar'), 'foobar')
+
+ def test_getitem(self):
+ self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar')['foo'], 'http://example.org/foo')
+ self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar')['bar'], 'http://example.org/bar')
+ self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar')['foo'], 'http://example.org/me#foo')
+ self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar')['bar'], 'http://example.org/me#bar')
+ self.assertRaises(KeyError, ClosedNamespace('http://example.org/', 'bar', 'bar').__getitem__, 'foobar')
+ self.assertRaises(KeyError, ClosedNamespace('http://example.org#', 'bar', 'bar').__getitem__, 'foobar')
+
+
+## main ##
+
+if __name__ == '__main__':
+ unittest.main()
+
+## EOF ##