aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMatthias Baumgartner <dev@igsor.net>2022-12-18 13:42:34 +0100
committerMatthias Baumgartner <dev@igsor.net>2022-12-18 13:42:34 +0100
commit0e52514639b043454425a9cc2317d27e628a1027 (patch)
treee9630257a423521ec3aa6c83f6b56a0b2a581bc2 /test
parent21a02197d73f263ae222f2ccc49248d8617e2d7d (diff)
downloadbsfs-0e52514639b043454425a9cc2317d27e628a1027.tar.gz
bsfs-0e52514639b043454425a9cc2317d27e628a1027.tar.bz2
bsfs-0e52514639b043454425a9cc2317d27e628a1027.zip
namespace and uri extensions
Diffstat (limited to 'test')
-rw-r--r--test/namespace/test_namespace.py87
-rw-r--r--test/utils/test_uri.py18
2 files changed, 79 insertions, 26 deletions
diff --git a/test/namespace/test_namespace.py b/test/namespace/test_namespace.py
index 1ad53e3..f109653 100644
--- a/test/namespace/test_namespace.py
+++ b/test/namespace/test_namespace.py
@@ -5,8 +5,12 @@ A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# imports
+import operator
import unittest
+# bsfs imports
+from bsfs.utils import URI
+
# objects to test
from bsfs.namespace.namespace import Namespace, ClosedNamespace
@@ -16,73 +20,104 @@ from bsfs.namespace.namespace import Namespace, ClosedNamespace
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#)')
+ 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, #, /)')
+ self.assertEqual(repr(Namespace('http://example.org', fsep='.')), 'Namespace(http://example.org, ., /)')
+ self.assertEqual(repr(Namespace('http://example.org', psep='.')), 'Namespace(http://example.org, #, .)')
+ # repeated separators are truncated
+ 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, #, /)')
# comparison
class Foo(Namespace): pass
self.assertEqual(Namespace('http://example.org/'), Namespace('http://example.org/'))
+ self.assertEqual(Namespace('http://example.org/'), Namespace('http://example.org'))
+ self.assertEqual(Namespace('http://example.org/'), Namespace('http://example.org#'))
+ self.assertNotEqual(Namespace('http://example.org'), Namespace('http://example.org', fsep='.'))
+ self.assertNotEqual(Namespace('http://example.org'), Namespace('http://example.org', psep='.'))
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.assertEqual(hash(Namespace('http://example.org/')), hash(Namespace('http://example.org')))
+ self.assertEqual(hash(Namespace('http://example.org/')), hash(Namespace('http://example.org#')))
+ self.assertNotEqual(hash(Namespace('http://example.org')), hash(Namespace('http://example.com')))
+ self.assertNotEqual(hash(Namespace('http://example.org')), hash(Namespace('http://example.org', fsep='.')))
+ self.assertNotEqual(hash(Namespace('http://example.org')), hash(Namespace('http://example.org', psep='.')))
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/').foo, 'http://example.org#foo')
+ self.assertEqual(Namespace('http://example.org/').bar, 'http://example.org#bar')
+ self.assertEqual(Namespace('http://example.org/', fsep='/').foo, 'http://example.org/foo')
+ self.assertEqual(Namespace('http://example.org/', fsep='/').bar, 'http://example.org/bar')
+ self.assertEqual(Namespace('http://example.org', fsep='/').foo, 'http://example.org/foo')
+ self.assertEqual(Namespace('http://example.org', fsep='/').bar, 'http://example.org/bar')
+ self.assertEqual(Namespace('http://example.org#', fsep='/').foo, 'http://example.org#/foo')
+ self.assertEqual(Namespace('http://example.org#', fsep='/').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')['foo'], 'http://example.org#foo')
+ self.assertEqual(Namespace('http://example.org')['bar'], 'http://example.org#bar')
+ self.assertEqual(Namespace('http://example.org', fsep='/')['foo'], 'http://example.org/foo')
+ self.assertEqual(Namespace('http://example.org', fsep='/')['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_add(self):
+ self.assertEqual(Namespace('http://example.org') + 'foo', Namespace('http://example.org/foo'))
+ self.assertEqual(Namespace('http://example.org', psep='.') + 'foo', Namespace('http://example.org.foo', psep='.'))
+ self.assertEqual(Namespace('http://example.org') + 'foo' + 'bar', Namespace('http://example.org/foo/bar'))
+ # can add URIs
+ self.assertEqual(Namespace('http://example.org') + URI('foo'), Namespace('http://example.org/foo'))
+ # can only add strings
+ self.assertRaises(TypeError, operator.add, Namespace('http://example.org'), 1234)
+ self.assertRaises(TypeError, operator.add, Namespace('http://example.org'), Namespace('http://example.com'))
+
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#)')
+ 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, #, /)')
+ self.assertEqual(repr(ClosedNamespace('http://example.org', fsep='.')), 'ClosedNamespace(http://example.org, ., /)')
+ self.assertEqual(repr(ClosedNamespace('http://example.org', psep='.')), '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'), ClosedNamespace('http://example.org#'))
+ self.assertEqual(ClosedNamespace('http://example.org'), ClosedNamespace('http://example.org'))
+ 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')), hash(ClosedNamespace('http://example.org')))
+ self.assertEqual(hash(ClosedNamespace('http://example.org')), hash(ClosedNamespace('http://example.org/')))
+ 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/', '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/', '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')
diff --git a/test/utils/test_uri.py b/test/utils/test_uri.py
index 976e75d..770e65a 100644
--- a/test/utils/test_uri.py
+++ b/test/utils/test_uri.py
@@ -5,6 +5,7 @@ A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# imports
+import operator
import unittest
# objects to test
@@ -161,6 +162,23 @@ class TestURI(unittest.TestCase):
# empty URI
self.assertRaises(ValueError, getattr, URI(''), 'fragment')
+ def test_overloaded(self):
+ # composition
+ self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment') + 'hello', URI)
+ self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment') * 2, URI)
+ self.assertIsInstance(2 * URI('http://user@www.example.com:1234/{}/path1?{}#fragment'), URI) # rmul
+ self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment').join(['hello', 'world']) , URI)
+ # stripping
+ self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').strip(), URI)
+ self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').lstrip(), URI)
+ self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').rstrip(), URI)
+ # case fold
+ self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').lower(), URI)
+ self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').upper(), URI)
+ # formatting
+ self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment').format('hello', 'world'), URI)
+ self.assertIsInstance(URI('http://user@www.example.com:1234/%s/path1?%s#fragment') % ('hello', 'world'), URI)
+ self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').replace('path0', 'pathX'), URI)
## main ##