diff options
author | Matthias Baumgartner <dev@igsor.net> | 2022-12-18 14:21:11 +0100 |
---|---|---|
committer | Matthias Baumgartner <dev@igsor.net> | 2022-12-18 14:21:11 +0100 |
commit | 91437ba89d35bf482f3d9671bb99ef2fc69f5985 (patch) | |
tree | e9bfe27e5a641c040cfa8fe747a7cbb28091079c /test/apps/test_init.py | |
parent | 87e4cd5a4581094f490f79d4f1cf91f51897660f (diff) | |
parent | e94368c75468e3e94382b12705e55d396249eaca (diff) | |
download | bsfs-91437ba89d35bf482f3d9671bb99ef2fc69f5985.tar.gz bsfs-91437ba89d35bf482f3d9671bb99ef2fc69f5985.tar.bz2 bsfs-91437ba89d35bf482f3d9671bb99ef2fc69f5985.zip |
Merge branch 'develop' into main
Diffstat (limited to 'test/apps/test_init.py')
-rw-r--r-- | test/apps/test_init.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/test/apps/test_init.py b/test/apps/test_init.py new file mode 100644 index 0000000..bae6a68 --- /dev/null +++ b/test/apps/test_init.py @@ -0,0 +1,91 @@ +""" + +Part of the bsfs test suite. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import contextlib +import io +import json +import os +import tempfile +import unittest + +# bsie imports +from bsfs.front import build_graph +from bsfs.graph import Graph + +# objects to test +from bsfs.apps.init import main, init_sparql_store + + +## code ## + +class TestInit(unittest.TestCase): + def test_main(self): + + # cannot pass an invalid store + with contextlib.redirect_stderr(io.StringIO()): + self.assertRaises(SystemExit, main, ['--user', 'http://example.com/me', 'foobar']) + + # produces a config structure + outbuf = io.StringIO() + with contextlib.redirect_stdout(outbuf): + main(['--user', 'http://example.com/me', 'sparql']) + self.assertEqual(json.loads(outbuf.getvalue()), { + 'Graph': { + 'user': 'http://example.com/me', + 'backend': { + 'SparqlStore': {}}}}) + # config is valid + self.assertIsInstance(build_graph(json.loads(outbuf.getvalue())), Graph) + + # respects user flag + outbuf = io.StringIO() + with contextlib.redirect_stdout(outbuf): + main(['--user', 'http://example.com/you', 'sparql']) + self.assertEqual(json.loads(outbuf.getvalue()), { + 'Graph': { + 'user': 'http://example.com/you', + 'backend': { + 'SparqlStore': {}}}}) + + # respects output flag + _, path = tempfile.mkstemp(prefix='bsfs-test-', text=True) + outbuf = io.StringIO() + with contextlib.redirect_stdout(outbuf): + main(['--user', 'http://example.com/me', '--output', path, 'sparql']) + with open(path, 'rt') as ifile: + config = ifile.read() + os.unlink(path) + self.assertEqual(outbuf.getvalue(), '') + self.assertEqual(json.loads(config), { + 'Graph': { + 'user': 'http://example.com/me', + 'backend': { + 'SparqlStore': {}}}}) + + def test_init_sparql_store(self): + # returns a config structure + self.assertEqual(init_sparql_store('http://example.com/me'), { + 'Graph': { + 'user': 'http://example.com/me', + 'backend': { + 'SparqlStore': {}}}}) + # respects user + self.assertEqual(init_sparql_store('http://example.com/you'), { + 'Graph': { + 'user': 'http://example.com/you', + 'backend': { + 'SparqlStore': {}}}}) + # the config is valid + self.assertIsInstance(build_graph(init_sparql_store('http://example.com/me')), Graph) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## |