aboutsummaryrefslogtreecommitdiffstats
path: root/test/namespace/test_namespace.py
blob: 25362039908b1ae403055145b540e532056d5ad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""

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/')), 'http://example.org')
        self.assertEqual(str(Namespace('http://example.org#')), '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////')), 'http://example.org')
        self.assertEqual(str(Namespace('http://example.org####')), '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/')), 'http://example.org')
        self.assertEqual(str(ClosedNamespace('http://example.org#')), '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 ##