aboutsummaryrefslogtreecommitdiffstats
path: root/test/graph/test_graph.py
blob: d89d34605f354e766b564194d8ef5a684c016e6a (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""

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 import schema
from bsfs.graph.ac import NullAC
from bsfs.graph.nodes import Nodes
from bsfs.namespace import ns
from bsfs.query import ast
from bsfs.triple_store import SparqlStore
from bsfs.utils import URI, errors

# objects to test
from bsfs.graph.graph import Graph


## code ##

class TestGraph(unittest.TestCase):
    def setUp(self):
        self.backend = SparqlStore.Open()
        self.backend.schema = schema.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            bsfs:Entity rdfs:subClassOf bsfs:Node .
            ''')
        self.user = URI('http://example.com/me')
        self.ac = NullAC(self.backend, self.user)

    def test_str(self):
        self.assertEqual(str(Graph(self.backend, self.ac)),
            'Graph(SparqlStore(uri=None))')
        self.assertEqual(repr(Graph(self.backend, self.ac)),
            'Graph(SparqlStore(uri=None), NullAC(http://example.com/me))')
        # str respects backend
        class Foo(SparqlStore): pass
        self.assertEqual(str(Graph(Foo.Open(), self.ac)),
            'Graph(Foo(uri=None))')
        self.assertEqual(repr(Graph(Foo.Open(), self.ac)),
            'Graph(Foo(uri=None), NullAC(http://example.com/me))')
        # str respect user
        self.assertEqual(str(Graph(self.backend, NullAC(self.backend, URI('http://example.com/you')))),
            'Graph(SparqlStore(uri=None))')
        self.assertEqual(repr(Graph(self.backend, NullAC(self.backend, URI('http://example.com/you')))),
            'Graph(SparqlStore(uri=None), NullAC(http://example.com/you))')
        # str respects type
        class Bar(Graph): pass
        self.assertEqual(str(Bar(self.backend, self.ac)),
            'Bar(SparqlStore(uri=None))')
        self.assertEqual(repr(Bar(self.backend, self.ac)),
            'Bar(SparqlStore(uri=None), NullAC(http://example.com/me))')

    def test_equality(self):
        graph = Graph(self.backend, self.ac)
        # instance is equal to itself
        self.assertEqual(graph, graph)
        self.assertEqual(hash(graph), hash(graph))
        # instance is equal to a clone
        self.assertEqual(graph, Graph(self.backend, self.ac))
        self.assertEqual(hash(graph), hash(Graph(self.backend, self.ac)))
        # equality respects backend
        self.assertNotEqual(graph, Graph(SparqlStore.Open(), self.ac))
        self.assertNotEqual(hash(graph), hash(Graph(SparqlStore.Open(), self.ac)))
        # equality respects user
        self.assertNotEqual(graph, Graph(self.backend, URI('http://example.com/you')))
        self.assertNotEqual(hash(graph), hash(Graph(self.backend, URI('http://example.com/you'))))

    def test_essentials(self):
        graph = Graph(self.backend, self.ac)
        # schema
        self.assertEqual(graph.schema, self.backend.schema)
        self.assertRaises(AttributeError, setattr, graph, 'schema', None)

    def test_node(self):
        graph = Graph(self.backend, self.ac)
        guid = URI('http://example.com/me/entity#1234')
        # returns a Nodes instance
        self.assertEqual(
            graph.node(ns.bsfs.Entity, guid),
            Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), {guid}))
        # node_type must be in the schema
        self.assertRaises(KeyError, graph.node, ns.bsfs.Invalid, guid)

    def test_nodes(self):
        graph = Graph(self.backend, self.ac)
        guids = {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}
        # returns a Nodes instance
        self.assertEqual(
            graph.nodes(ns.bsfs.Entity, guids),
            Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), guids))
        # node_type must be in the schema
        self.assertRaises(KeyError, graph.nodes, ns.bsfs.Invalid, guids)

    def test_all(self):
        graph = Graph(self.backend, self.ac)
        # resulting nodes can be empty
        self.assertEqual(graph.all(ns.bsfs.Entity),
            Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), set()))
        # resulting nodes contains all nodes of the respective type
        guids = {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}
        self.backend.create(graph.schema.node(ns.bsfs.Entity), guids)
        self.assertEqual(graph.all(ns.bsfs.Entity),
            Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), guids))
        # node_type must be in the schema
        self.assertRaises(KeyError, graph.all, ns.bsfs.Invalid)

    def test_migrate(self):
        # setup
        graph = Graph(self.backend, self.ac)

        # argument must be a schema
        class Foo(): pass
        self.assertRaises(TypeError, graph.migrate, 'hello world')
        self.assertRaises(TypeError, graph.migrate, 1234)
        self.assertRaises(TypeError, graph.migrate, Foo())

        # cannot append inconsistent schema
        self.assertRaises(errors.ConsistencyError, graph.migrate, schema.Schema({}, {
            schema.Node(ns.bsfs.Entity,
                schema.Node(ns.bsfs.Intermediate,
                    schema.Node(ns.bsfs.Node, None)))}), append=True)

        # cannot migrate to inconsistent schema
        self.assertRaises(errors.ConsistencyError, graph.migrate, schema.Schema({}, {
            schema.Node(ns.bsfs.Entity,
                schema.Node(ns.bsfs.Intermediate,
                    schema.Node(ns.bsfs.Node, None)))}), append=False)

        # can migrate to compatible schema
        target_1 = schema.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bse: <http://bsfs.ai/schema/Entity#>
            bsfs:Entity rdfs:subClassOf bsfs:Node .
            xsd:string rdfs:subClassOf bsfs:Literal .
            bsfs:Number rdfs:subClassOf bsfs:Literal .
            xsd:integer rdfs:subClassOf bsfs:Number .

            bse:filename rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:string ;
                bsfs:unique "false"^^xsd:boolean .

            bse:filesize rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:integer;
                bsfs:unique "false"^^xsd:boolean .

            ''')
        graph.migrate(target_1)
        # new schema is applied
        self.assertLess(target_1, graph.schema)
        # graph appends its predicates
        self.assertEqual(graph.schema, target_1 + schema.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bsm: <http://bsfs.ai/schema/Meta#>
            bsfs:Number rdfs:subClassOf bsfs:Literal .
            xsd:float rdfs:subClassOf bsfs:Number .
            bsm:t_created rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Node ;
                rdfs:range xsd:float ;
                bsfs:unique "true"^^xsd:boolean .
            '''))

        # can overwrite the current schema
        target_2 = schema.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bse: <http://bsfs.ai/schema/Entity#>
            bsfs:Entity rdfs:subClassOf bsfs:Node .
            xsd:string rdfs:subClassOf bsfs:Literal .
            bsfs:Number rdfs:subClassOf bsfs:Literal .
            xsd:integer rdfs:subClassOf bsfs:Number .

            bse:filename rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:string ;
                bsfs:unique "false"^^xsd:boolean .

            bse:author rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:string ;
                bsfs:unique "true"^^xsd:boolean .

            ''')
        graph.migrate(target_2, append=False)
        # append overwrites existing predicates
        self.assertFalse(target_1 <= graph.schema)
        # new schema is applied
        self.assertLess(target_2, graph.schema)
        # graph appends its predicates
        self.assertEqual(graph.schema, target_2 + schema.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bsm: <http://bsfs.ai/schema/Meta#>
            bsfs:Number rdfs:subClassOf bsfs:Literal .
            xsd:float rdfs:subClassOf bsfs:Number .
            bsm:t_created rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Node ;
                rdfs:range xsd:float ;
                bsfs:unique "true"^^xsd:boolean .
            '''))

    def test_get(self):
        # setup
        graph = Graph(self.backend, self.ac)
        graph.migrate(schema.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bse: <http://bsfs.ai/schema/Entity#>

            bsfs:Entity rdfs:subClassOf bsfs:Node .
            bsfs:Tag rdfs:subClassOf bsfs:Node .
            xsd:string rdfs:subClassOf bsfs:Literal .

            bse:tag rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range bsfs:Tag ;
                bsfs:unique "false"^^xsd:boolean .

            bse:comment rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Node ;
                rdfs:range xsd:string ;
                bsfs:unique "false"^^xsd:boolean .

            '''))
        # add some instances
        ents = graph.nodes(ns.bsfs.Entity, {URI('http://example.com/entity#1234'), URI('http://example.com/entity#4321')})
        tags = graph.nodes(ns.bsfs.Tag, {URI('http://example.com/tag#1234'), URI('http://example.com/tag#4321')})
        # add some node links
        ents.set(ns.bse.tag, tags)
        # add some literals
        graph.node(ns.bsfs.Entity, URI('http://example.com/entity#1234')).set(ns.bse.comment, 'hello world')
        graph.node(ns.bsfs.Entity, URI('http://example.com/entity#1234')).set(ns.bse.comment, 'foo')
        graph.node(ns.bsfs.Entity, URI('http://example.com/entity#1234')).set(ns.bse.comment, 'foobar')
        graph.node(ns.bsfs.Tag, URI('http://example.com/tag#1234')).set(ns.bse.comment, 'foo')
        graph.node(ns.bsfs.Tag, URI('http://example.com/tag#4321')).set(ns.bse.comment, 'bar')

        # get exception for invalid query
        self.assertRaises(errors.ConsistencyError, graph.get, ns.bsfs.Entity, ast.filter.Any(ns.bse.tag, ast.filter.Equals('hello world')))

        # query returns nodes
        self.assertEqual(graph.get(ns.bsfs.Entity, ast.filter.Any(ns.bse.tag, ast.filter.Is(tags))), ents)
        self.assertEqual(graph.get(ns.bsfs.Entity, ast.filter.Any(ns.bse.comment, ast.filter.StartsWith('foo'))),
            graph.node(ns.bsfs.Entity, URI('http://example.com/entity#1234')))
        self.assertEqual(graph.get(ns.bsfs.Node, ast.filter.Any(ns.bse.comment, ast.filter.StartsWith('foo'))),
            graph.nodes(ns.bsfs.Node, {URI('http://example.com/entity#1234'), URI('http://example.com/tag#1234')}))
        self.assertEqual(graph.get(ns.bsfs.Entity, ast.filter.Or(
            ast.filter.Any(ns.bse.comment, ast.filter.EndsWith('bar')),
            ast.filter.Any(ns.bse.tag, ast.filter.All(ns.bse.comment, ast.filter.Equals('bar'))))),
            ents)




## main ##

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

## EOF ##