# imports import operator import unittest # bsfs imports from bsfs.utils import URI # objects to test from bsfs.namespace.namespace import Namespace, FinalNamespace ## code ## class TestNamespace(unittest.TestCase): def test_essentials(self): # string conversion self.assertEqual(str(Namespace('http://example.org')), 'http://example.org') self.assertEqual(repr(Namespace('http://example.org')), "'http://example.org'") # comparison self.assertEqual(Namespace('http://example.org'), Namespace('http://example.org')) self.assertEqual(hash(Namespace('http://example.org')), hash(Namespace('http://example.org'))) # Namespace compares to string self.assertEqual(Namespace('http://example.org'), 'http://example.org') self.assertEqual(hash(Namespace('http://example.org')), hash('http://example.org')) # URI must match self.assertNotEqual(Namespace('http://example.org'), Namespace('http://example.com')) self.assertNotEqual(hash(Namespace('http://example.org')), hash(Namespace('http://example.com'))) def test_getattr(self): self.assertEqual(Namespace('http://example.org').foo, Namespace('http://example.org/foo')) self.assertEqual(Namespace('http://example.org').bar, Namespace('http://example.org/bar')) def test_call(self): self.assertEqual(Namespace('http://example.org')(), FinalNamespace('http://example.org', sep='#')) self.assertEqual(Namespace('http://example.org').foo(), FinalNamespace('http://example.org/foo', sep='#')) class TestFinalNamespace(unittest.TestCase): def test_essentials(self): # string conversion self.assertEqual(str(FinalNamespace('http://example.org')), 'http://example.org') self.assertEqual(repr(FinalNamespace('http://example.org')), "'http://example.org'") # comparison self.assertEqual(FinalNamespace('http://example.org'), FinalNamespace('http://example.org')) self.assertEqual(hash(FinalNamespace('http://example.org')), hash(FinalNamespace('http://example.org'))) # FinalNamespace compares to string self.assertEqual(FinalNamespace('http://example.org'), 'http://example.org') self.assertEqual(hash(FinalNamespace('http://example.org')), hash('http://example.org')) # URI must match self.assertNotEqual(FinalNamespace('http://example.org'), FinalNamespace('http://example.com')) self.assertNotEqual(hash(FinalNamespace('http://example.org')), hash(FinalNamespace('http://example.com'))) # separator is ignored self.assertEqual(FinalNamespace('http://example.org'), FinalNamespace('http://example.org', sep='/')) self.assertEqual(hash(FinalNamespace('http://example.org')), hash(FinalNamespace('http://example.org', sep='/'))) def test_getattr(self): self.assertEqual(FinalNamespace('http://example.org').foo, FinalNamespace('http://example.org#foo')) self.assertEqual(FinalNamespace('http://example.org').bar, FinalNamespace('http://example.org#bar')) self.assertEqual(FinalNamespace('http://example.org', sep='/').bar, FinalNamespace('http://example.org/bar')) ## main ## if __name__ == '__main__': unittest.main() ## EOF ##