diff options
author | Matthias Baumgartner <dev@igsor.net> | 2022-12-18 14:20:25 +0100 |
---|---|---|
committer | Matthias Baumgartner <dev@igsor.net> | 2022-12-18 14:20:25 +0100 |
commit | e94368c75468e3e94382b12705e55d396249eaca (patch) | |
tree | e9bfe27e5a641c040cfa8fe747a7cbb28091079c /test | |
parent | 12d95ed8bda18f2ef9d36190919cb838bfb5efcf (diff) | |
download | bsfs-e94368c75468e3e94382b12705e55d396249eaca.tar.gz bsfs-e94368c75468e3e94382b12705e55d396249eaca.tar.bz2 bsfs-e94368c75468e3e94382b12705e55d396249eaca.zip |
bsfs applications
Diffstat (limited to 'test')
-rw-r--r-- | test/apps/__init__.py | 0 | ||||
-rw-r--r-- | test/apps/config.json | 8 | ||||
-rw-r--r-- | test/apps/schema-1.nt | 19 | ||||
-rw-r--r-- | test/apps/schema-2.nt | 19 | ||||
-rw-r--r-- | test/apps/test_init.py | 91 | ||||
-rw-r--r-- | test/apps/test_migrate.py | 66 |
6 files changed, 203 insertions, 0 deletions
diff --git a/test/apps/__init__.py b/test/apps/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/apps/__init__.py diff --git a/test/apps/config.json b/test/apps/config.json new file mode 100644 index 0000000..ffc5ef7 --- /dev/null +++ b/test/apps/config.json @@ -0,0 +1,8 @@ +{ + "Graph": { + "user": "http://example.com/me", + "backend": { + "SparqlStore": {} + } + } +} diff --git a/test/apps/schema-1.nt b/test/apps/schema-1.nt new file mode 100644 index 0000000..e57146d --- /dev/null +++ b/test/apps/schema-1.nt @@ -0,0 +1,19 @@ + +prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> +prefix xsd: <http://www.w3.org/2001/XMLSchema#> + +# common bsfs prefixes +prefix bsfs: <http://bsfs.ai/schema/> +prefix bse: <http://bsfs.ai/schema/Entity#> + +# essential nodes +bsfs:Entity rdfs:subClassOf bsfs:Node . + +# common definitions +xsd:string rdfs:subClassOf bsfs:Literal . + +bse:filename rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:Entity ; + rdfs:range xsd:string ; + bsfs:unique "false"^^xsd:boolean . + diff --git a/test/apps/schema-2.nt b/test/apps/schema-2.nt new file mode 100644 index 0000000..525ac99 --- /dev/null +++ b/test/apps/schema-2.nt @@ -0,0 +1,19 @@ + +prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> +prefix xsd: <http://www.w3.org/2001/XMLSchema#> + +# common bsfs prefixes +prefix bsfs: <http://bsfs.ai/schema/> +prefix bse: <http://bsfs.ai/schema/Entity#> + +# essential nodes +bsfs:Entity rdfs:subClassOf bsfs:Node . + +# common definitions +xsd:integer rdfs:subClassOf bsfs:Literal . + +bse:filesize rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:Entity ; + rdfs:range xsd:integer ; + bsfs:unique "true"^^xsd:boolean . + 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 ## diff --git a/test/apps/test_migrate.py b/test/apps/test_migrate.py new file mode 100644 index 0000000..957509a --- /dev/null +++ b/test/apps/test_migrate.py @@ -0,0 +1,66 @@ +""" + +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 os +import sys +import unittest +import unittest.mock + +# bsie imports +from bsfs.schema import Schema + +# objects to test +from bsfs.apps.migrate import main + + +## code ## + +class TestMigrate(unittest.TestCase): + def test_main(self): + config = os.path.join(os.path.dirname(__file__), 'config.json') + schema_1 = os.path.join(os.path.dirname(__file__), 'schema-1.nt') + schema_2 = os.path.join(os.path.dirname(__file__), 'schema-2.nt') + + # provide no config + with contextlib.redirect_stderr(io.StringIO()): + self.assertRaises(SystemExit, main, []) + + # read schema from file + with open(schema_1) as ifile: + target = Schema.from_string(ifile.read()) + graph = main([config, schema_1]) + self.assertTrue(target <= graph.schema) + + # read schema from multiple files + with open(schema_1) as ifile: + target = Schema.from_string(ifile.read()) + with open(schema_2) as ifile: + target = target + Schema.from_string(ifile.read()) + graph = main([config, schema_1, schema_2]) + self.assertTrue(target <= graph.schema) + + # read schema from stdin + with open(schema_1, 'rt') as ifile: + target = Schema.from_string(ifile.read()) + with open(schema_1, 'rt') as ifile: + with unittest.mock.patch('sys.stdin', ifile): + graph = main([config]) + self.assertTrue(target <= graph.schema) + + # remove predicates + # NOTE: cannot currently test this since there's nothing to remove in the loaded (empty) schema. + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## + |