""" Part of the tagit test suite. 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 ## 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, #, /)') 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/')) # 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/'))) 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/', 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', 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(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/', '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/')) # 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/', '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/'))) 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 ##