aboutsummaryrefslogtreecommitdiffstats
path: root/test/front/test_builder.py
blob: 0328a0ad3ebc652c8cb489d2bc596c4e65e885e0 (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
"""

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.graph import Graph
from bsfs.graph.ac import NullAC
from bsfs.triple_store import SparqlStore
from bsfs.utils import errors, URI

# objects to test
from bsfs.front.builder import build_backend, build_graph


## code ##

class TestBuilder(unittest.TestCase):
    def test_build_backend(self):
        # valid config produces a valid store
        store = build_backend({'SparqlStore': {}})
        self.assertIsInstance(store, SparqlStore)
        self.assertIsNone(store.uri)
        # cannot create an invalid store
        self.assertRaises(errors.ConfigError, build_backend, {'MyStore': {}})
        # must pass a dict
        self.assertRaises(TypeError, build_backend, 1234)
        self.assertRaises(TypeError, build_backend, 'hello world')
        self.assertRaises(TypeError, build_backend, [1,2,3])
        # cannot create a store from an invalid config
        self.assertRaises(errors.ConfigError, build_backend, {})
        self.assertRaises(errors.ConfigError, build_backend, {'SparqlStore': {}, 'OtherStore': {}})
        self.assertRaises(TypeError, build_backend, {'SparqlStore': {'hello': 'world'}})

    def test_build_graph(self):
        # valid config produces a valid graph
        graph = build_graph({'Graph': {'backend': {'SparqlStore': {}}, 'user': 'http://example.com/me'}})
        self.assertIsInstance(graph, Graph)
        self.assertIsInstance(graph._backend, SparqlStore)
        self.assertEqual(graph._ac, NullAC(graph._backend, URI('http://example.com/me')))
        # cannot create an invalid graph
        self.assertRaises(errors.ConfigError, build_graph, {'MyGraph': {}})
        # must pass a dict
        self.assertRaises(TypeError, build_graph, 1234)
        self.assertRaises(TypeError, build_graph, 'hello world')
        self.assertRaises(TypeError, build_graph, [1,2,3])
        # cannot create a graph from an invalid config
        self.assertRaises(errors.ConfigError, build_graph, {})
        self.assertRaises(errors.ConfigError, build_graph, {'Graph': {}, 'Graph2': {}})
        self.assertRaises(errors.ConfigError, build_graph, {'Graph': {}})
        self.assertRaises(errors.ConfigError, build_graph, {'Graph': {'user': 'http://example.com/me'}})
        self.assertRaises(errors.ConfigError, build_graph, {'Graph': {'backend': 'Hello world'}})
        self.assertRaises(TypeError, build_graph, {'Graph': {'user': 'http://example.com/me', 'backend': 'Hello world'}})


## main ##

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

## EOF ##