aboutsummaryrefslogtreecommitdiffstats
path: root/test/apps/test_init.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/apps/test_init.py')
-rw-r--r--test/apps/test_init.py91
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 ##