aboutsummaryrefslogtreecommitdiffstats
path: root/test/triple_store/test_base.py
blob: a4b05596214aaa0dc04645282abd39622fcb1f78 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""

Part of the bsfs test suite.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# imports
import unittest

# bsie imports
from bsfs.utils import URI

# objects to test
from bsfs.triple_store.base import TripleStoreBase


## code ##

class DummyBase(TripleStoreBase):
    @classmethod
    def Open(cls, uri, **kwargs):
        return cls(uri)

    def commit(self):
        pass

    def rollback(self):
        pass

    @property
    def schema(self):
        pass

    @schema.setter
    def schema(self, schema):
        pass

    def exists(self, node_type, guids):
        pass

    def create(self, node_type, guids):
        pass

    def set(self, node_type, guids, predicate, values):
        pass

class DummyStore(DummyBase):
    pass

class DummyAlternative(DummyBase):
    pass


class TestTripleStoreBase(unittest.TestCase):

    def test_equality(self):
        # identical instances are equal
        store = DummyStore.Open(None)
        self.assertEqual(store, store)
        self.assertEqual(hash(store), hash(store))
        store = DummyStore.Open(URI('http://example.com/store'))
        self.assertEqual(store, store)
        self.assertEqual(hash(store), hash(store))
        # in-memory storages are not equal
        # NOTE: Don't use
        # >>> self.assertNotEqual(hash(DummyStore(None)), hash(DummyStore(None)))
        # The two stores are created subsequently since each of them is deleted
        # right after hashing. Because the two instances never exist at the same
        # time, their id may (and typically will) be identical.
        # This only matters when the `id` function is used, i.e. when uri=None.
        a, b = DummyStore.Open(None), DummyStore.Open(None)
        self.assertNotEqual(a, b)
        self.assertNotEqual(hash(a), hash(b))
        a, b = DummyStore.Open(None), DummyStore.Open(URI('http://example.com/store'))
        self.assertNotEqual(a, b)
        self.assertNotEqual(hash(a), hash(b))
        a, b = DummyStore.Open(URI('http://example.com/store')), DummyStore.Open(None)
        self.assertNotEqual(a, b)
        self.assertNotEqual(hash(a), hash(b))
        a, b = DummyStore.Open(None), DummyStore.Open(URI('http://example.com/alternative'))
        self.assertNotEqual(a, b)
        self.assertNotEqual(hash(a), hash(b))
        # equality respects uri
        self.assertEqual(
            DummyStore.Open(URI('http://example.com/store')),
            DummyStore.Open(URI('http://example.com/store')))
        self.assertEqual(
            hash(DummyStore.Open(URI('http://example.com/alternative'))),
            hash(DummyStore.Open(URI('http://example.com/alternative'))))
        self.assertNotEqual(
            DummyStore.Open(URI('http://example.com/store')),
            DummyStore.Open(URI('http://example.com/alternative')))
        self.assertNotEqual(
            DummyStore.Open(URI('http://example.com/store')),
            hash(DummyStore.Open(URI('http://example.com/alternative'))))
        # equality respects type
        self.assertNotEqual(DummyStore.Open(None), None)
        self.assertNotEqual(hash(DummyStore.Open(None)), hash(None))
        self.assertNotEqual(DummyStore.Open(None), 'hello world')
        self.assertNotEqual(hash(DummyStore.Open(None)), hash('hello world'))
        self.assertNotEqual(DummyStore.Open(None), 1234)
        self.assertNotEqual(hash(DummyStore.Open(None)), hash(1234))
        class Foo(): pass
        f = Foo()
        self.assertNotEqual(DummyStore.Open(None), f)
        self.assertNotEqual(hash(DummyStore.Open(None)), hash(f))
        self.assertNotEqual(
            DummyStore.Open(None),
            DummyAlternative.Open(None))
        self.assertNotEqual(
            hash(DummyStore.Open(None)),
            hash(DummyAlternative.Open(None)))

    def test_string_conversion(self):
        # string conversion respects uri
        self.assertEqual('DummyStore(uri=http://example.com/store)',
            str(DummyStore.Open(URI('http://example.com/store'))))
        self.assertEqual('DummyStore(uri=http://example.com/store)',
            repr(DummyStore.Open(URI('http://example.com/store'))))
        self.assertEqual('DummyStore(uri=http://example.com/alternative)',
            str(DummyStore.Open(URI('http://example.com/alternative'))))
        self.assertEqual('DummyStore(uri=http://example.com/alternative)',
            repr(DummyStore.Open(URI('http://example.com/alternative'))))
        self.assertEqual('DummyStore(uri=None)',
            str(DummyStore.Open(None)))
        self.assertEqual('DummyStore(uri=None)',
            repr(DummyStore.Open(None)))
        # string conversion respects type
        self.assertEqual('DummyAlternative(uri=http://example.com/store)',
            str(DummyAlternative.Open(URI('http://example.com/store'))))

    def test_uri(self):
        # uri returns correct value
        self.assertEqual(None,
            DummyStore.Open(None).uri)
        self.assertEqual(URI('http://example.com/store'),
            DummyStore.Open(URI('http://example.com/store')).uri)
        self.assertEqual(URI('http://example.com/alternative'),
            DummyStore.Open(URI('http://example.com/alternative')).uri)
        # persistence respects uri
        self.assertFalse(DummyStore.Open(None).is_persistent())
        self.assertTrue(DummyStore.Open(URI('http://example.com/store')).is_persistent())
        self.assertTrue(DummyStore.Open(URI('http://example.com/alternative')).is_persistent())

## main ##

if __name__ == '__main__':
    unittest.main()

## EOF ##